I have a polyline shape file which contain Left Id and Right ID information and struggling in converting this polyline layer to polygon with ID via arcpy.

I am using ArcGIS(editor) license so feature to polygon tool is not available to me. I also know there are number of methods available like this and this one in ArcGIS if you have editor license but i want to use arcpy to automate my workflow.

Any ideas how i can do it using arcpy?enter image description here

  • Your diagram has polylines with arrowheads at both ends. In order to know which side of the polyline the numbered polygon falls wouldn't these need to be unidirectional?
    – PolyGeo
    Feb 22, 2012 at 7:09
  • I have used arrowheads on both ends, i think it was bad idea. Direction of polylines is mixed but if we query this table "L_ID = 2 or R_ID = 2" will select all the polylines required to construct polygon with ID = 2.
    – iRfAn
    Feb 22, 2012 at 8:19
  • 1
    Without being able to go from the end of one polyline to the start of the next to determine the polygon to enclose I think you would need some other way to determine the order of polylines around a polygon.
    – PolyGeo
    Feb 22, 2012 at 22:44

3 Answers 3


I don't see a strightforward way of doing this in ArcPy normal licensing without re-inventing wheels..

Just read about Shapely (download link),a python package, which includes the "polygonize" function from familiar JTS/GEOS suites..

My idea is that we can import the Shapely package in ArcPy and get the WKT of the ESRI polyline geometries and pass them to the polygonizer. This will give the resulting polygons. These could be converted back to ESRI polyline geometries. Although i should say, my Python knowledge is terrible..this is just a generic idea..

here is a sample of polygonizer..

>>> from shapely.geometry import LineString, Point
    >>> from shapely.ops import polygonize
    >>> lines = [
    ... LineString(((0, 0), (1, 1))),
    ... LineString(((0, 0), (0, 1))),
    ... LineString(((0, 1), (1, 1))),
    ... LineString(((1, 1), (1, 0))),
    ... LineString(((1, 0), (0, 0))),
    ... LineString(((5, 5), (6, 6))),
    ... Point(0, 0),
    ... ]
    >>> result = polygonize(lines)
    >>> list(result)
    [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>]

    >>> lines2 = [
    ... ((0, 0), (1, 1)),
    ... ((0, 0), (0, 1)),
    ... ((0, 1), (1, 1)),
    ... ((1, 1), (1, 0)),
    ... ((1, 0), (0, 0)),
    ... ((5, 5), (6, 6)),
    ... ]
    >>> result2 = polygonize(lines2)
    >>> list(result2)
    [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>]

Also i am not sure what we can do with the LEFT_ID and RIGHT_ID. I think these are irrelevant. Update me if i am wrong.

  • I think you are on the right track here. There are 2 items I think need to be worked out. The first is the order of the lines that would be input into the polygonizer function. They have to be in the correct order or the polygon will likely have twisting issues. I don't know much about that function, so it may not matter. The way the Left_ID and Right_ID could be handy is that you can group based on the ID's to determine what lines should be included in a particular polygon. May 31, 2012 at 5:29
  • @GetSpatial order is no issue in the polygonizer function.this is what is mentioned in the JTS site."Polygonizes a set of Geometrys which contain linework that represents the edges of a planar graph. Any dimension of Geometry is handled - the constituent linework is extracted to form the edges. The edges must be correctly noded; that is, they must only meet at their endpoints. The Polygonizer will still run on incorrectly noded input but will not form polygons from incorrected noded edges." vividsolutions.com/jts/javadoc/com/vividsolutions/jts/operation/…
    – vinayan
    May 31, 2012 at 5:45
  • 1
    well that takes care of the order problem. I think the Left and Right ID's could still be used to make sure that the polygon's are attributed with the correct ID tag. I might give it a shot in the next couple of days to put together some code to do this. May 31, 2012 at 7:32
  • @GetSpatial - great..i have done it using a C# port of JTS and arcobjects..but python would be nice..
    – vinayan
    May 31, 2012 at 7:56

maybe you could use arcpy.FeatureToRaster_conversion() to create a raster from you polylines and then arcpy.RasterToPolygon_conversion() to create the polygon


If you want to use Python and the GeoProcessor to build polygons from you lines you need to have a look at the Help file and search for a page called working with geometry in Python.

You'll need to create a cursor over your polylines and string them together, you may need to "explode" them into points first? Anyway not sure how you can resolve the issue of the order you visit the polylines. I don't know (as I've never tried it in Python) if the resulting polygon geometry can be simplified, i.e. the order of vertices is made correct when you create the polygon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.