基于Bash与gdalinfo的TIFF栅格投影列示及NoData值一致性检查
Bash Scripts for TIFF Projection Listing & NoData Consistency Check
Hey Rose, I've revised and expanded your original Bash script to cover both of your technical requirements. Let's break it down:
1. List Unique Projections Across TIFF Files
To extract and list distinct projections from all TIFFs in your directory tree, we can parse the PROJCS line from gdalinfo output. Here's a clean, reliable script for this:
#!/bin/bash basepath=/home/rose/Desktop/GreatExtentDSMnodatacleaned # Update your root directory shopt -s globstar # Enable recursive directory matching echo "=== Unique Projections in Directory Tree ===" cd "$basepath" # Recursively find all TIFF files and extract projections for f in **/*.tif; do # Extract projection name (fallback to "Unknown" if none found) gdalinfo "$f" | grep -o 'PROJCS\["[^"]*"\]' | sed 's/PROJCS\[//; s/"//g' || echo "Unknown" done | sort | uniq
How it works:
shopt -s globstarlets**/*.tifmatch all TIFF files recursively (way more reliable than parsinglsoutput)grep -o 'PROJCS\["[^"]*"\]'isolates the human-readable projection name stringsedcleans up the output to remove thePROJCS["and trailing"cluttersort | uniqensures we only show distinct, non-duplicate projections
2. Verify Consistent NoData Values Across All TIFF Files
To check if every TIFF shares the same NoData value, we'll collect all NoData values, deduplicate them, and validate the count. Here's the script:
#!/bin/bash basepath=/home/rose/Desktop/GreatExtentDSMnodatacleaned # Update your root directory shopt -s globstar echo "=== Checking NoData Value Consistency ===" cd "$basepath" # Collect all NoData values (handling files with no NoData defined) nodata_values=$(for f in **/*.tif; do gdalinfo "$f" | grep -o 'NoData Value\=[-0-9]*' | sed 's/NoData Value\=//' || echo "None" done | sort | uniq) # Count unique NoData values unique_count=$(echo "$nodata_values" | wc -l) if [ "$unique_count" -eq 1 ]; then echo "✅ All TIFF files have the same NoData value: $(echo "$nodata_values" | head -n1)" else echo "❌ Found $unique_count distinct NoData values:" echo "$nodata_values" fi
How it works:
- We first gather all NoData values (or mark files without one as "None")
sort | uniqreduces the list to only unique entries- We count the unique entries: if only one exists, all files are consistent; otherwise, we list the differences clearly
Combined Script (Do Both Tasks at Once)
If you want to run both checks in a single go, use this combined script:
#!/bin/bash basepath=/home/rose/Desktop/GreatExtentDSMnodatacleaned # Update your root directory shopt -s globstar cd "$basepath" echo "=== Unique Projections ===" for f in **/*.tif; do gdalinfo "$f" | grep -o 'PROJCS\["[^"]*"\]' | sed 's/PROJCS\[//; s/"//g' || echo "Unknown" done | sort | uniq echo -e "\n=== NoData Value Consistency ===" nodata_values=$(for f in **/*.tif; do gdalinfo "$f" | grep -o 'NoData Value\=[-0-9]*' | sed 's/NoData Value\=//' || echo "None" done | sort | uniq) unique_count=$(echo "$nodata_values" | wc -l) if [ "$unique_count" -eq 1 ]; then echo "✅ All TIFF files have the same NoData value: $(echo "$nodata_values" | head -n1)" else echo "❌ Found $unique_count distinct NoData values:" echo "$nodata_values" fi
Quick Notes:
- Ensure GDAL is installed (
gdalinfoneeds to be available in your system PATH) shopt -s globstarrequires Bash 4.0 or newer (standard on most modern Linux/macOS systems)- We replaced your original
dir_listapproach with Bash recursive globbing to avoid common pitfalls of parsinglsoutput
内容的提问来源于stack exchange,提问作者Rose
相关产品推荐
相关产品推荐

