Creating lines from polygon borders with polygon attributes using ArcGIS/ArcPy?
You can do this with the Intersect tool. Normally, performing an intersect with polygons will only return the overlapping area. But if you change the output_type
from INPUT
to LINE
, then you can get just the collinear borders between polygons.
If this is our input:
And we change the output_type
parameter:
We get the green lines as output:
The output contains two line features for every border segment. To collapse this down to one feature per border segment, you'll want to run Dissolve with the multi_part
parameter set to SINGLE_PART
. Then, run a Spatial Join with your dissolved lines as the target_features
, the intersected lines as the join_features
, your field_mapping
setup with two fields for every input field (one using the FIRST
merge type and the other using the LAST
merge type), and the match_option
set to ARE_IDENTICAL_TO
. Your output attribute table will look like the following:
You can do this with python if you are at least at ArcGIS 10.1. If you have ArcInfo, you can use the Feature to Line tool. Otherwise, you can use this simple script. This script does not support true curves though. The resulting lines, if topologically correct should overlap then you can run an intersect of this line fc on itself to find the boundaries that overlap, i.e. the border lines.
import arcpy
import os
arcpy.env.overwriteOutput = True
def polys_to_lines(fc, new_fc):
path, name = os.path.split(new_fc)
sm = 'SAME_AS_TEMPLATE'
arcpy.CreateFeatureclass_management(path, name, 'POLYLINE', fc, sm, sm, fc)
fields = [f.name for f in arcpy.ListFields(new_fc)
if f.type not in ('OID', 'Geometry')]
# get attributes
with arcpy.da.SearchCursor(fc, ['SHAPE@'] + fields) as rows:
values = [(r[0].boundary(),) + tuple(r[1:]) for r in rows]
# insert rows
with arcpy.da.InsertCursor(new_fc, ['SHAPE@'] + fields) as irows:
for vals in values:
irows.insertRow(vals)
print 'Created: "{0}"'.format(name)
return new_fc
if __name__ == '__main__':
fc = r'C:\TEMP\parcel_test.shp'
new = r'C:\TEMP\parcel_Linetest2.shp'
polys_to_lines(fc, new)