Skip to content

Instantly share code, notes, and snippets.

@andrewharvey
Created May 2, 2020 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewharvey/d689e2fab7325ab988a62473bc6dafac to your computer and use it in GitHub Desktop.
Save andrewharvey/d689e2fab7325ab988a62473bc6dafac to your computer and use it in GitHub Desktop.
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