Wednesday 31 October 2018

arcpy - Append/Insert geoprocessing results into an existing feature class (polyline midpoints)


I plan to run a scheduled server job on a feature class in an Oracle 18c/10.7.1 geodatabase.


The job would:



  1. Truncate the rows in an existing feature class


  2. Generate new rows from a geoprocessing tool (FeatureVerticesToPoints / midpoints)

  3. Insert/append the geoprocessing rows into the feature class


Note: I would like to do this without needing to temporarily store any sort of intermediate results in a temporary feature class/GDB.




I've gotten part-way there:


import arcpy
conn = "Database Connections\\DEV.sde\\"
input_lines = "OWNER.ROAD"
existing_fc = "OWNER.SCHED_ROAD_MIDPOINT"


arcpy.TruncateTable_management(conn + existing_fc)
arcpy.FeatureVerticesToPoints_management(conn + input_lines, conn + existing_fc, "MID")

#arcpy.Append_management(, , "TEST", "", "")



Question:


I'm not sure how to use FeatureVerticesToPoints_management as the input in the append process.


Is there a way to append/insert geoprocessing results into an existing feature class using ArcPy?




Answer



I've built on @user2856's answer:



  1. Loop through a list that has 3 rows. Each row has an input FC, target FC, and an ID field.

  2. For each row in the list:

    • Truncate the target FC

    • Populate the target FC with midpoints from the input FC (target FC fields are: SHAPE, MIDPOINT_X, and MIDPOINT_Y)

    • Populate the ID field in the target FC







Caution! The target FCs will get truncated! All records in the target FCs will be deleted.


import arcpy
conn = "Database Connections\\\\"

#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 = [\
[".", ".S_SIDEWLK_MIDPOINT", "SDW_ID" ],\
["." , ".S_SEWER_MIDPOINT" , "SEWER_ID" ],\
["." , ".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."

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...