Monday, 9 November 2015

geometry - Creating point at midpoint of polyline with ArcPy?



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?



Answer



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"

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...