# Finding mid-point of line using ArcPy?

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.

-
See this question. In the end I used the linear referencing functions in Shapely to determine the mid-point. – Mike T Aug 21 '12 at 4:29
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. – Get Spatial Aug 21 '12 at 7:58
This blog seems to contain an algorithm that could be converted to Python/ArcPy: rbrundritt.wordpress.com/2008/10/14/… – PolyGeo Aug 21 '12 at 9:33

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
``````
-
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 – Stephen Lead Aug 21 '12 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 Jun 2 '14 at 16:41

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

-
That's good to know - thanks. His tools should be standard functionality, huh. – Stephen Lead Oct 8 '13 at 19:39