Monday 17 September 2018

open source gis - Get all vertices of a polygon using OGR and Python



I'm having a little trouble with the Python OGR API. What I am trying to do is get all the coordinates of each vertex of the outer ring of a polygon.


This is what I have so far:


import osgeo.ogr
import glob

path = "/home/woo/maps/"
out = path + 'output.txt'

file = open(out,'w')
for filename in glob.glob(path + "*.shp"):

ds = osgeo.ogr.Open(filename)
layer1 = ds.GetLayer(0)
print layer1.GetExtent()
for feat in layer1:
geom = feat.GetGeometryRef()
ring = geom.GetGeometryRef(0)
points = ring.GetPointCount()
#Not sure what to do here



file.close()

I have heard that you can just for over the region but that only returns the rings in the polygon, not the nodes.


Anyone able to help.



Answer



It depends a bit on your file format and geometry, but in principle the continuation could look like this.


  for p in xrange(points):
lon, lat, z = ring.GetPoint(p)

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...