-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproj_select_vrt.py
executable file
·54 lines (45 loc) · 1.63 KB
/
proj_select_vrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/env python
# -- Update to handle VRT
#David Shean
#dshean@gmail.com
#Script that automatically selects projection for input geometry
#Goes through user-defined bounding boxes w/ projections (defined in geolib)
#Default output proj is UTM zone containing centroid
#Should allow user to specify input coord srs
import os
import sys
import math
from osgeo import gdal, ogr, osr
from pygeotools.lib import geolib
def main(argv=None):
#Input is a filename
if len(sys.argv[1:]) == 1:
fn = sys.argv[1]
ext = os.path.splitext(fn)[1].lower()
#Accept DigitalGlobe image metadata in xml
if ext == '.xml':
from dgtools.lib import dglib
geom = dglib.xml2geom(fn)
#Want to be better about handling arbitrary gdal formats here
elif ext == '.tif' or ext == '.ras' or ext == '.vrt': # UPDATE PMM
ds = gdal.Open(fn)
geom = geolib.ds_geom(ds, t_srs=geolib.wgs_srs)
#geom = geolib.get_outline(ds, t_srs=geolib.wgs_srs)
#Input is lat/lon
elif len(sys.argv[1:]) == 2:
y = float(sys.argv[1])
x = float(sys.argv[2])
#Force longitude -180 to 180
x = (x+180) - math.floor((x+180)/360)*360 - 180
#Check that latitude is valid
if y > 90 or y < -90:
sys.exit('Invalid latitude: %f' % y)
geom = geolib.xy2geom(x, y)
else:
sys.exit("Usage: %s [lat lon]|[raster.tif]" % os.path.basename(sys.argv[0]))
#Now determine the srs from geom
srs = geolib.get_proj(geom)
#And print to stdout
print(srs.ExportToProj4().strip())
if __name__ == '__main__':
main()