Created
May 2, 2020 07:13
-
-
Save andrewharvey/d689e2fab7325ab988a62473bc6dafac to your computer and use it in GitHub Desktop.
Shell script to get the native zoom of a raster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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