Sunday 10 February 2019

python - Create polygon from OGREnvelope


I have a folder full of about 2000 files and I would like to generate a kind of key map for each file so people don't need to just randomly load each one in order to find what is what.


I have the following python code using ogr but OGREnvelope isn't really a OGRGeometry so I can't add it to a shape file or export it as wkt to import in QGIS.


for filename in glob.glob(path + "*.shp"):
ds = ogr.Open(filename)

layer1 = ds.GetLayer(0)
#print filename + " : " + str(layer1.GetExtent())
extent = layer1.GetExtent()
# I need to be able to export the extent region as a polygon.

I have been looking around but just can't seem to find anything. I really thought something like this would be easy.



Answer



You can make your own WKT from this. You can try adding something like this to your code.


print 'POLYGON((' \
+ str(extent[0]) + ' ' + str(extent[1]) + ', ' \

+ str(extent[0]) + ' ' + str(extent[3]) + ', ' \
+ str(extent[2]) + ' ' + str(extent[3]) + ', ' \
+ str(extent[2]) + ' ' + str(extent[1]) + ', ' \
+ str(extent[0]) + ' ' + str(extent[1]) + '))'

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...