Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script to get the native zoom of a raster
#!/bin/bash
WIDTH=`gdalinfo -json $FILE | jq .size[0]`
MINX=`gdalinfo -json $FILE | jq .wgs84Extent.coordinates[0][0][0]`
MAXX=`gdalinfo -json $FILE | jq .wgs84Extent.coordinates[0][2][0]`
MAXZOOM=`echo "l( ( 360 * $WIDTH ) / ( 256 * ( $MAXX - $MINX ) ) ) / l(2)" | bc -l | perl -nl -MPOSIX -e 'print ceil($_);'`
# 2^n tiles at zoom n
# therefore 2^n * 256 pixels of width for the world at zoom n
# therefore 360 / (2^n * 256) degrees per pixel at zoom n
#
# meanwhile (max lng - min lng) / image width is the degrees per pixel of the image
#
# therefore zoom n = log_2((360 * width) / (256 * (max - min))))
# and then take the ceil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment