12

How can the mid-point of a polyline be calculated using the geoprocessing framework, e.g. in a Python script?

The polyline.centroid property returns The true centroid if it is within or on the feature; otherwise, the label point is returned. The centroid is rarely located on non-straight lines, which is useless for my purposes.

The Feature Vertices to Points tool has a midpoint option but this requires ArcInfo, which I don't currently have.

An option could be to add measures to the polylines and create a route event 50% along the line.

Another workaround is to use the Calculate Geometry option in ArcMap, but ideally I need to automate this process in a script.

Any better/faster suggestions?

I am limited to ArcGIS 10.0 (without access to an ArcInfo level license) for the moment.

6
  • 5
    See this question. In the end I used the linear referencing functions in Shapely to determine the mid-point.
    – Mike T
    Commented Aug 21, 2012 at 4:29
  • 1
    I think @MikeToews has the right idea, and just use the linear referencing functions in ArcGIS as opposed to taking it into Shapely. It looks like this is the function you would want: Make Route Event Layer - Linear Referencing. You can probably make the event table on the fly. This is for 10.1, but should be the same in 10.0. Commented Aug 21, 2012 at 7:58
  • 1
    This blog seems to contain an algorithm that could be converted to Python/ArcPy: rbrundritt.wordpress.com/2008/10/14/…
    – PolyGeo
    Commented Aug 21, 2012 at 9:33
  • Does someone did it in javascript? I did it successfully only in Arcpy. Sincerely Ezequias
    – Ezequias
    Commented Jan 21, 2019 at 19:23
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Bera
    Commented Jan 21, 2019 at 19:46

2 Answers 2

23

The Polyline class has a new method called "positionAlongLine" in ArcGIS 10.1. This will return a PointGeometry object with exactly one point at a specified distance from the starting end of the line, or a fraction of the distance between the start and end. To find the midpoint, you would just need to do positionAlongLine(0.5,True). To find the midpoints for lines and add their coordinates to the attribute table, you could do Field Calculator on the following statement:

  • !Shape!.positionAlongLine(0.5,True).firstPoint.X
  • !Shape!.positionAlongLine(0.5,True).firstPoint.Y

Note that you need to be using the Python parser in field calculator for this to work.

If you wanted to access this point object in Python, you would just do the following:

Input_shp = "C:\Temp\Line.shp"
Cursor = arcpy.SearchCursor(Input_shp)
for Feature in Cursor:
    Midpoint = Feature.shape.positionAlongLine(0.50,True).firstPoint
    print Midpoint.X
    print Midpoint.Y
2
  • thanks, this looks great. I should have mentioned that I'm currently limited to 10.0 though, so the Route option above may have to suffice for now Commented Aug 21, 2012 at 22:49
  • Error: The Row contains a bad value. Using arcGIS10, with Python checked in Model Builder, I used the same statement: !Shape!.positionAlongLine(0.5,True).firstPoint.X
    – juasmilla
    Commented Jun 2, 2014 at 16:41
1

With ET Geo Wizards license you could try the function "Polyline to Point" with the option "Middle points" and "Remove Duplicate Points" (optional): http://www.ian-ko.com/ET_GeoWizards/UserGuide/convertPl2Pnt.htm

You can implement ET Geo Wizards like this:

toolbox_ETgeowizards = arcpy.GetParameterAsText(0) # Location ET Toolbox (Type: Toolbox)
arcpy.ImportToolbox(toolbox_ETgeowizards)

...

arcpy.ET_GPPolylineToPoints(pInFeatureClass, sOutFileName, sExportOption, bRemoveDuplicates)

For input variables of the ET function, please see: http://www.ian-ko.com/ET_GeoWizards/UserGuide/Scripting/scripting_PolylineToPoint.htm

0

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.