-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborder_envelope.py
More file actions
66 lines (49 loc) · 1.9 KB
/
border_envelope.py
File metadata and controls
66 lines (49 loc) · 1.9 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
from osgeo import ogr
from osgeo import osr
import os
import shutil
os.chdir("C:\Users\gramener\Desktop\sushmit\geospatial\shapefiles")
srcFile = ogr.Open("TM_WORLD_BORDERS\TM_WORLD_BORDERS-0.3.shp")
srclayer = srcFile.GetLayer(0)
if os.path.exists("bounding_boxes"):
shutil.rmtree("bounding_boxes")
os.mkdir("bounding_boxes")
spatialReference = osr.SpatialReference()
spatialReference.SetWellKnownGeogCS('WGS84')
driver = ogr.GetDriverByName("ESRI Shapefile")
dstPath = os.path.join("bounding_boxes","boundingBoxes.shp")
dstFile = driver.CreateDataSource(dstPath)
dstLayer = dstFile.CreateLayer("Layer",spatialReference)
fieldDef = ogr.FieldDefn("COUNTRY",ogr.OFTString)
fieldDef.SetWidth(3)
dstLayer.CreateField(fieldDef)
fieldDef = ogr.FieldDefn("CODE", ogr.OFTString)
fieldDef.SetWidth(3)
dstLayer.CreateField(fieldDef)
countries = []
for i in range(srclayer.GetFeatureCount()):
feature = srclayer.GetFeature(i)
countryCode = feature.GetField("ISO3")
countryName = feature.GetField("NAME")
geometry = feature.GetGeometryRef()
minLong,maxLong,minLat,maxLat = geometry.GetEnvelope()
countries.append((countryName,countryCode,minLat,minLong,maxLat,maxLong))
linearRing = ogr.Geometry(ogr.wkbLinearRing)
linearRing.AddPoint(minLong, minLat)
linearRing.AddPoint(maxLong, minLat)
linearRing.AddPoint(maxLong, maxLat)
linearRing.AddPoint(minLong, maxLat)
linearRing.AddPoint(minLong, minLat)
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(linearRing)
feature = ogr.Feature(dstLayer.GetLayerDefn())
feature.SetGeometry(polygon)
feature.SetField("COUNTRY", countryName)
feature.SetField("CODE", countryCode)
dstLayer.CreateFeature(feature)
feature.Destroy()
srcFile.Destroy()
dstFile.Destroy()
countries.sort()
for countryName,countryCode,minLat,minLong,maxLat,maxLong in countries:
print "%s..(%s),Lat=(%.04f)..(%.04f),Long=(%.04f)..(%.04f)"%(countryName,countryCode,minLat,minLong,maxLat,maxLong)