HOW TO
In ArcGIS Pro, when there are line features in a polygon, it is possible to extend the line features to the polygon boundary for various spatial analysis and visualization purposes such as to construct a floodplain mapping project for a river basin. The image below shows an example of line features not connected to the polygon boundary.
This article provides the workflow to extend line features in a polygon feature using ArcPy in ArcGIS Pro.
Note: This workflow requires the full script to run in the ArcGIS Pro Python window and an additional line feature class as the output layer.
def main(): print ("Start process...") import arcpy fc_line = r'<filePath1>' fc_pol = r'<filePath2>' fc_out = r'<filePath3>'
print ("Get polygon and boundary") polygon = arcpy.da.SearchCursor(fc_pol, ('SHAPE@')).next()[0] boundary = polygon.boundary()
print ("start loop...") lst_feats = [] cnt = 0 with arcpy.da.SearchCursor(fc_line, ('SHAPE@')) as curs: for row in curs: cnt += 1 if cnt % 10 == 0: print (" - processing line", cnt) polyline = row[0] pnt1 = polyline.firstPoint pnt2 = polyline.lastPoint pntg1_snap = boundary.snapToLine(pnt1) pntg2_snap = boundary.snapToLine(pnt2) polyline_out = ExtendPolyline(polyline, pntg1_snap.firstPoint, pntg2_snap.firstPoint) lst_feats.append(polyline_out)
print ("store results...") arcpy.CopyFeatures_management(lst_feats, fc_out)
def ExtendPolyline(polyline, pnt1, pnt2): sr = polyline.spatialReference lst_pnts = [] for part in polyline: for pnt in part: lst_pnts.append(pnt) lst_pnts.insert(0, pnt1) lst_pnts.append(pnt2) return arcpy.Polyline(arcpy.Array(lst_pnts), sr)
if __name__ == '__main__': main()
The code block below demonstrates the example of the full working script.
def main(): print ("Start process...") import arcpy fc_line = r'C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\Line1' fc_pol = r'C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\TestPolygons' fc_out = r'C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\snapped_lines' print ("Get polygon and boundary") polygon = arcpy.da.SearchCursor(fc_pol, ('SHAPE@')).next()[0] boundary = polygon.boundary() print ("start loop...") lst_feats = [] cnt = 0 with arcpy.da.SearchCursor(fc_line, ('SHAPE@')) as curs: for row in curs: cnt += 1 if cnt % 10 == 0: print (" - processing line"), cnt polyline = row[0] pnt1 = polyline.firstPoint pnt2 = polyline.lastPoint pntg1_snap = boundary.snapToLine(pnt1) pntg2_snap = boundary.snapToLine(pnt2) polyline_out = ExtendPolyline(polyline, pntg1_snap.firstPoint, pntg2_snap.firstPoint) lst_feats.append(polyline_out) print ("store results...") arcpy.CopyFeatures_management(lst_feats, fc_out) def ExtendPolyline(polyline, pnt1, pnt2): sr = polyline.spatialReference lst_pnts = [] for part in polyline: for pnt in part: lst_pnts.append(pnt) lst_pnts.insert(0, pnt1) lst_pnts.append(pnt2) return arcpy.Polyline(arcpy.Array(lst_pnts), sr) if __name__ == '__main__': main()
The image below shows the line features are extended to the polygon boundary.
Get help from ArcGIS experts
Download the Esri Support App