Coenni
Coenni

Reputation: 197

How to convert shape file type from polyline to polygon?

I have a shape file whose file type is polyline. But I want convert it to polygon. Is it possible with free sources? How can I convert shape file type from polyline to polygon?

Upvotes: 2

Views: 4312

Answers (1)

Charles
Charles

Reputation: 1830

This can be done pretty easily in python with the help of the PySAL library.

For example given a shapefile named "tst.shp" with two polylines, Two Polylines in QGIS

We can open the shapefile convert the polylines to polygons and write out a new shapefile.

>>> import pysal
>>> shps = pysal.open('tst.shp','r')
>>> o = pysal.open('tst_polygons.shp','w')
>>> for polyline in shps:
...     verts = polyline.vertices
...     if verts[0] != verts[-1]: #make sure the polylines are closed rings
...         verts = verts+verts[0:1]
...     o.write(pysal.cg.Polygon(verts))
...
>>> o.close()

Now we have polygons, Two Polygons in QGIS

If you need a way to accomplish this without programming, try asking your questions at http://gis.stackexchange.com

Upvotes: 2

Related Questions