Created
May 15, 2015 19:17
-
-
Save jmorton/84bd034fb57cc1343519 to your computer and use it in GitHub Desktop.
Get extents from a scene (padded my 900m)
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
""" Moo. | |
""" | |
import gdal | |
import osr | |
import math | |
import subprocess | |
import time | |
def nearest(n, mul=30, off = 15): | |
"""Find nearest multiple of n (with a given offset). | |
This is useful for getting consistent coordinates for | |
scenes that exhibit wiggle. | |
""" | |
return round((n-off)/mul)*mul+off | |
def extents(path, epsg=5070): | |
""" | |
Calculate "framing" extents for a scene. This will give you the | |
x-min, y-min, x-max, y-max values in meters relative to the origin | |
of the projection (EPSG-5070 / Albers-Conus). | |
""" | |
coord_ls = [] | |
t_ds = gdal.Open(path) | |
t_band = t_ds.GetRasterBand(1) | |
t_rows = t_band.YSize | |
t_cols = t_band.XSize | |
t_proj = t_ds.GetProjectionRef() | |
t_geo = t_ds.GetGeoTransform() | |
albers_proj = osr.SpatialReference() | |
albers_proj.ImportFromEPSG(epsg) | |
from_proj = osr.SpatialReference() | |
from_proj.ImportFromWkt(t_proj) | |
coord_trans = osr.CoordinateTransformation(from_proj, albers_proj) | |
ll = coord_trans.TransformPoint(t_geo[0], (t_geo[3] + t_geo[5] * t_rows)) | |
ul = coord_trans.TransformPoint(t_geo[0], t_geo[3]) | |
ur = coord_trans.TransformPoint((t_geo[0] + t_geo[1] * t_cols), t_geo[3]) | |
lr = coord_trans.TransformPoint((t_geo[0] + t_geo[1] * t_cols), (t_geo[3] + t_geo[5] * t_rows)) | |
x_min = min(ll[0], ul[0]) | |
y_min = min(ll[1], lr[1]) | |
x_max = max(lr[0], ur[0]) | |
y_max = max(ul[1], ur[1]) | |
# Determine the extents to keep consistent (xmin ymin xmax ymax) | |
ex = {} | |
ex['x_min'] = nearest(x_min) - 900 | |
ex['y_min'] = nearest(y_min) - 900 | |
ex['x_max'] = nearest(x_max) + 900 | |
ex['y_max'] = nearest(y_max) + 900 | |
return ex | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('src', help='source input (a geotiff)') | |
args = vars(parser.parse_args()) | |
extent = extents(args['src']) | |
for (name,value) in extent.items().sort(): | |
print("{:<6} {:>10}".format(name, value)) | |
# python convert.py LC80440332015128LGN00/LC80440332015128LGN00_B1.TIF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment