Bounty: 50
I have an Oracle GIS database that has spatial tables. The records in the spatial tables have a geometry column that contains polylines.
I’ve written a Python 2.7.16 script that calculates the midpoint of the polylines and inserts the midpoint information into parallel tables. The script will be run as part of a nightly scheduled job on a server.
Note: I use the short form FC in the code, which stands for Feature Class. A
feature class is an ESRI GIS term that can be thought of as a table with a geometry column.
What the code does:
- Loops through a list. Each row in the list has an input FC, a target FC, and an ID field.
- For each row in the list:
- Truncate the target FC
- Populate the target FC with midpoints from the input FC
So yeah, I’m new to Python. How can the code be improved?
import arcpy
conn = "Database Connections\<YOUR CONNECTION.sde>\"
#This script only works for polyline feature classes (not polygons or points).
#Caution! The target FCs will get truncated! All records in the target FCs will be deleted.
# INPUT FC TARGET FC ID FIELD (for both the input FC and the target FC)
lstFC = [
["<OWNER>.<SIDEWALK>", "<OWNER>.S_SIDEWLK_MIDPOINT", "SDW_ID" ],
["<OWNER>.<SEWER>" , "<OWNER>.S_SEWER_MIDPOINT" , "SEWER_ID" ],
["<OWNER>.<ROAD>" , "<OWNER>.S_ROAD_MIDPOINT" , "ROAD_ID" ],
]
def ReloadMidpoints():
for fc in lstFC:
inputFC = conn + (fc)[0]
targetFC = conn + (fc)[1]
idField = (fc)[2]
arcpy.TruncateTable_management(targetFC)
print "<" + targetFC + "> has been truncated."
with arcpy.da.SearchCursor(inputFC, ['SHAPE@', idField]) as s_rows:
with arcpy.da.InsertCursor(targetFC, ['SHAPE@', idField, "MIDPOINT_X","MIDPOINT_Y"]) as i_rows:
for row in s_rows:
Midpoint = row[0].positionAlongLine(0.50,True).firstPoint
rowVals = [(Midpoint.X,Midpoint.Y),s_rows[1],Midpoint.X,Midpoint.Y]
i_rows.insertRow(rowVals)
print "<" + targetFC + "> has been reloaded with midpoints that were generated from <" + (inputFC) + ">."
print ""
ReloadMidpoints()
print "Complete."
Get this bounty!!!