2

I have a polyline shapefile (that contains multiple lines) and am trying to create a point at the midpoint of each line using Python. I am currently using the ET Geowizards tool 'PolylineToPoints' which does exactly what I want it to... but I am trying to avoid using ET Geowizards (as it is causing other issues).

Is there a way to do this in Python using Arc tools and functions?

  • 1
    When you say centroid, do you mean the midpoint of the line, or the vertices along the line? – DPierce Jan 2 '14 at 16:33
  • sorry, it should be midpoint. i will change it in the question. – Afidus Jan 2 '14 at 16:34
  • @Afidus I've updated my answer to reflect this change in the question. – dmahr Jan 2 '14 at 16:46
5

this should do it in arcgis...

import arcpy, math, datetime, numpy

print ("starting")
start = datetime.datetime.now() # for calculating time of process

#setting the containers
midpoint = #put your file to be populated in here, make sure it already exists
polyline = #put your polyline file in here

#housekeeping
arcpy.DeleteFeatures_management(midpoint)
arcpy.env.overwriteOutput = True

#generating the mid point
with arcpy.da.SearchCursor(polyline, "SHAPE@") as in_cursor, \
arcpy.da.InsertCursor(midpoint, "SHAPE@") as out_cursor:
for row in in_cursor:
    midpoint = row[0].positionAlongLine(0.50,True).firstPoint
    out_cursor.insertRow([midpoint])

#tidy up
del rows, row, updateRows_midpoint, outRow, out_cursor, midpoint, polyline
print "Done in ",datetime.datetime.now() - start, " seconds"
  • How do you use the arcpy module outside ArcGIS? – FaCoffee Dec 12 '16 at 18:17
6

UPDATE: Just saw that the question was changed to ask for the midpoint of a polyline, rather than a centroid. You can use Field Calculator to determine this location, as I explained in this answer.

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.


The easiest method to find the centroid of a line is to use the Feature To Point geoprocessing tool, where your polyline shapefile are the in_features, your output points are the out_features. However, this tool is only available with the ArcGIS Desktop Advanced license (formerly ArcInfo).


If you don't have access to the Feature To Point tool, you can calculate the centroid of a line "manually" in the attribute table. Here's how you would do it:

  • Add two new Double fields to your polyline shapefile attribute table.

  • Use the Calculate Geometry function to store the X and Y coordinates of the poyline centroids in the Double fields you just created.

  • Export the attribute table to a new table.

  • Use the Make XY Event Layer geoprocessing tool on the two coordinate fields to create a point event layer based on the centroid coordinates.

  • Export the event layer to a new file.

  • thanks for your answer! i do have access to arcinfo, but i am trying to avoid it if necessary. i will try the manual approach and get back to you. – Afidus Jan 2 '14 at 16:42
  • If you are still running 10.0 (as I am), then you can use the python Shapely library to do the same thing. – dklassen Jan 3 '14 at 16:35
3

with arcinfo, there is a built in function :

arcpy.FeatureVerticesToPoints_management(input,output,"MID")
  • 1
    thanks! although i do have access to arcinfo...the script i am creating is meant to avoid any advanced licenses so that anyone can use it. – Afidus Jan 3 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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