You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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 globstar lets **/*.tif match all TIFF files recursively (way more reliable than parsing ls output)
  • grep -o 'PROJCS\["[^"]*"\]' isolates the human-readable projection name string
  • sed cleans up the output to remove the PROJCS[" and trailing " clutter
  • sort | uniq ensures 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 | uniq reduces 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 (gdalinfo needs to be available in your system PATH)
  • shopt -s globstar requires Bash 4.0 or newer (standard on most modern Linux/macOS systems)
  • We replaced your original dir_list approach with Bash recursive globbing to avoid common pitfalls of parsing ls output

内容的提问来源于stack exchange,提问作者Rose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:19:31