1

I have a feature with polylines and want to get the midpoint of every line. Following pything script I found here works great:

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"

But, all the rest of the attributes are lost. Only the Geometry is written into the new feature. I'm aware why, but I don't know how to do it.

How do I get them into the midpoint-feature?

Can it be done in the for-loop?

1
  • why not just add an intersect operation at the end of your script and save that as the output instead? Jan 9, 2017 at 15:05

3 Answers 3

2

This code seems a bit "pointless" pun intended! Why not simply use the Feature Vertices To Points tool which according to the help file under the usage section will maintain the original attributes. A single tool, no lines of code required. The only limitation and you don't state in your question which license you have, is that this tool executes under an Advanced license level.

1

If you have an advanced license, see Hornbydd's answer. Otherwise, here's an alternative method. List fields that appear in both feature classes and that aren't guid, OID, or geometry fields. Add these fields to your cursors. Something like this:

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

#get list of fields
inFlds = [f.name for f in arcpy.ListFields (polyline)
          if not f.type in ["Guid", "OID", "Geometry"]]
#get out fields
outFlds = [f.name for f in arcpy.ListFields (midpoint)
           if not f.type in ["Guid", "OID", "Geometry"]
           and f.name in inFlds]

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

#tidy up
del rows, row, updateRows_midpoint, outRow, out_cursor, midpoint, polyline
print "Done in ",datetime.datetime.now() - start, " seconds"
0

Once you have your points, you could run this to join the line data:

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
outmidpoint = #route to output file

#joining attributes
arcpy.SpatialJoin_analysis(midpoint ,polyline ,outmidpoint ,"JOIN_ONE_TO_ONE","KEEP_ALL")

#The End
print "Done in ",datetime.datetime.now() - start, " seconds"

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.