Extract points using arcpy.Polyline methods

I was given a SQL table of coordinates and names for average speed over distance cameras. My task was to represent these points, along with their respective roads, in some sort of web app, showing traffic flow in near real-time.

The points depicted the physical location of the cameras, so visually they were not exactly on the road lines. I thought about snapping the points to the start of the closest line, but where’s the fun in that?

#
# @date 01/04/2015
# @author Cindy Williams
#
# Creates a point feature class for points derived from
# various polyline methods. This example uses polyline.firstPoint
# but can be replaced with any of the polyline methods which
# generate points.
#
# For use as a standalone script.
#
import arcpy
from arcpy import env
env.workspace = r"C:\Some\Arb\Folder\work.gdb"
lyr_line = arcpy.management.MakeFeatureLayer("ftr_line")
# Get spatial reference of line feature class
sr = arcpy.Describe(lyr_line).spatialReference.exporttostring()
pts = [] # List to hold points
fields = ["SHAPE@", "FTR_ID", "FTR_NAME"]
# Create a point feature class based on line feature class
arcpy.management.CreateFeatureclass(env.workspace, "ftr_point", "POINT", lyr_line, "#","#", sr)
lyr_pnt = arcpy.management.MakeFeatureLayer("ftr_point")
# Loop over line features using a search cursor
with arcpy.da.SearchCursor(lyr_line, fields, "#", sr) as cursor:
for row in cursor:
pts.append([arcpy.PointGeometry(row[0].firstPoint, sr, row[1], row[2]])
print("Appended first point for " + row[2])
# Insert points into point feature class using an insert cursor
curs = arcpy.da.InsertCursor(lyr_pnt, fields)
for pt in pts:
curs.insertRow(pt)
del curs
print("Script complete.")

In Line 20, one can just directly set the spatial reference without needing to get the string representation:

sr = arcpy.Describe(lyr_line).spatialReference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.