forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bbox-geojson.py
51 lines (39 loc) · 1.38 KB
/
bbox-geojson.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 GeoJson
Description : Get region of scene
Arguments : GeoJson 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(geojson):
geom = ogr.CreateGeometryFromJson( geojson )
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 Geojson({ "isOk": 1, "bbox": "ulx uly lrx lry"}).'
parser = argparse.ArgumentParser(description=d )
d = "Geojson of geometry"
parser.add_argument('geojson', metavar='geojson', type=str, help=d )
args = parser.parse_args()
return run( args.geojson )
if __name__ == "__main__":
sys.exit( main() )