forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbox-wkt.py
51 lines (39 loc) · 1.35 KB
/
bbox-wkt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : Bounding box from WKT
Description : Get region of scene
Arguments : WKT of regin polygon(EPSG 4326)
-------------------
begin : 2016-08-28
copyright : (C) 2016 by Luiz Motta
email : motta dot luiz at gmail.com
***************************************************************************/
"""
import sys, argparse
from osgeo import gdal, ogr
gdal.UseExceptions()
gdal.PushErrorHandler('CPLQuietErrorHandler')
def run(wkt):
geom = ogr.CreateGeometryFromWkt( wkt )
if geom is None:
msg = '"%s"' % gdal.GetLastErrorMsg()
print '{ "isOk": 0, "msg": %s }' % msg
return 1
( minX, maxX, minY, maxY )= geom.GetEnvelope()
geom.Destroy()
#-projwin ulx uly lrx lry
data = ( minX, maxY, maxX, minY )
bbox = '"%f %f %f %f"' % data
print '{ "isOk": 1, "bbox": %s }' % bbox
return 0
def main():
d = 'Print bounding box of WKT({ "isOk": 1, "bbox": "ulx uly lrx lry"}).'
parser = argparse.ArgumentParser(description=d )
d = "WKT of geometry"
parser.add_argument('wkt', metavar='wkt', type=str, help=d )
args = parser.parse_args()
return run( args.wkt )
if __name__ == "__main__":
sys.exit( main() )