I currently have a code that is performing most of what i want it to do but I need to associate summed values with the corresponding parcels. Right now I believe the code is giving me the sum of all frontage that falls within a buffer area. I need to figure out how to sum each of the lines that make up individual parcels, sum them and add them to a field in the parcel layer.
# Import the Arcpy Module
import arcinfo
import arcpy
from arcpy import env
# Set up the working Environment
env.workspace = r"D:\Travis\Personal\Geoff\Nantucket\Zoning\Backlots\Backlots.gdb"
env.overwriteOutput = True
#Variables
Parcels = "R40_80000sqft"
Street_Center = "ROAD_CL_2017_12"
arcpy.MakeFeatureLayer_management(Parcels,"Parcels_lyr")
arcpy.MakeFeatureLayer_management(Street_Center,"Street_Lyr")
Street_Lyr = "Street_Lyr"
Parcels_lyr = "Parcels_Lyr"
#Add a field to parcels to be populated with building area
arcpy.AddField_management(Parcels,"Frontage","DOUBLE")
# Buffer Street Center Lines
arcpy.Buffer_analysis(Street_Lyr,"Street_Buff","40 Feet","FULL","ROUND","ALL")
Street_Buff = "Street_Buff"
#Convert parcel layer polygons to lines
arcpy.PolygonToLine_management(Parcels_lyr,"R40_PolyToLine")
R40_Line = "R40_PolyToLine"
#Convert parcel lines to a layer for use in select by location
arcpy.MakeFeatureLayer_management(R40_Line,"R40_Line_Lyr")
R40_Line_Lyr = "R40_Line_Lyr"
#Create a cursor to select all parcel lines that fall within the street buffer
with arcpy.da.UpdateCursor(Parcels_lyr, ['Shape@','Frontage']) as linecursor:
for linerow in linecursor:
#get the geometry to use in the spatial selection
geom = linerow[0]
#select parcel lines from the street buffer using the geom variable
arcpy.SelectLayerByLocation_management(R40_Line_Lyr, "COMPLETELY_WITHIN", Street_Buff, "", "NEW_SELECTION")
#get the Length of the selected features and sum
lengthsum = 0
with arcpy.da.SearchCursor(R40_Line_Lyr,['SHAPE@LENGTH']) as newcursor:
for newrow in newcursor:
lengthsum = lengthsum + newrow[0]
print lengthsum
del newcursor
parcelrow[1]= lengthsum
if lengthsum != 0:
parcelrow[2] = lengthsum
else:
parcelrow[2] = 0
linecursor.updateRow(linerow)
del linecursor
Hi Travis,
You are correct that is using all of the lines. You'll see that you create your update cursor for the parcel layer, but then in your select by location you are using the entire R40_Line_Lyr. You will need to someone how only use the lines in this layer that correspond to the parcel. When using the Polyogn to Line tool it looks like it retain the original OBJECTID in the LEFT_FID and RIGHT_FID field. You can call the OBJECTID field in your update cursor, and then create the feature layer of the parcel line based off of this. I haven't tested but you could do something like:
#Create a cursor to select all parcel lines that fall within the street buffer
with arcpy.da.UpdateCursor(Parcels_lyr, ['Shape@','Frontage', 'OID@']) as linecursor:
for linerow in linecursor:
#get the geometry to use in the spatial selection
geom = linerow[0]
oid = str(linerow[2])
R40_Line = "R40_PolyToLine"
#Convert parcel lines to a layer for use in select by location
arcpy.MakeFeatureLayer_management(R40_Line,"R40_Line_Lyr", "LEFT_FID = " + oid)
#select parcel lines from the street buffer using the geom variable
arcpy.SelectLayerByLocation_management("R40_Line_Lyr", "COMPLETELY_WITHIN", Street_Buff, "", "NEW_SELECTION")
My current code is 0's for length sum in line 71. Is this happening because the indentation of the if statement is wrong(Line 77)? I also turned off the Make Feature Layer in line 46 since I am making a feature layer in line 60 now.
# Import the Arcpy Module
import arcinfo
import arcpy
from arcpy import env
# Set up the working Environment
env.workspace = r"D:\Travis\Personal\Geoff\Nantucket\Zoning\Backlots\Backlots.gdb"
env.overwriteOutput = True
#Variables
Parcels = "R40_80000sqft"
Street_Center = "ROAD_CL_2017_12"
arcpy.MakeFeatureLayer_management(Parcels,"Parcels_lyr")
arcpy.MakeFeatureLayer_management(Street_Center,"Street_Lyr")
Street_Lyr = "Street_Lyr"
Parcels_lyr = "Parcels_Lyr"
#Add a field to parcels to be populated with building area
arcpy.AddField_management(Parcels,"Frontage","DOUBLE")
# Buffer Street Center Lines
arcpy.Buffer_analysis(Street_Lyr,"Street_Buff","40 Feet","FULL","ROUND","ALL")
Street_Buff = "Street_Buff"
#Convert parcel layer polygons to lines
arcpy.PolygonToLine_management(Parcels_lyr,"R40_PolyToLine")
R40_Line = "R40_PolyToLine"
###Convert parcel lines to a layer for use in select by location
##arcpy.MakeFeatureLayer_management(R40_Line,"R40_Line_Lyr")
##
##R40_Line_Lyr = "R40_Line_Lyr"
#Create a cursor to select all parcel lines that fall within the street buffer
with arcpy.da.UpdateCursor(Parcels_lyr, ['Shape@','Frontage','OID@']) as linecursor:
for linerow in linecursor:
#get the geometry to use in the spatial selection
geom = linerow[0]
oid = str(linerow[2])
R40_Line = "R40_PolyToLine"
#Convert parcel lines to a layer for use in a select by location
arcpy.MakeFeatureLayer_management(R40_Line,"R40_Line_Lyr","LEFT_FID = " + oid)
#select parcel lines from the street buffer using the geom variable
arcpy.SelectLayerByLocation_management("R40_Line_Lyr", "COMPLETELY_WITHIN", Street_Buff, "", "NEW_SELECTION")
#get the Length of the selected features and sum
lengthsum = 0
with arcpy.da.SearchCursor("R40_Line_Lyr",['SHAPE@LENGTH']) as newcursor:
for newrow in newcursor:
lengthsum = lengthsum + newrow[0]
print lengthsum
del newcursor
parcelrow[1]= lengthsum
if lengthsum != 0:
parcelrow[2] = lengthsum
else:
parcelrow[2] = 0
linecursor.updateRow(linerow)
del linecursor
Try running the Tabulate Intersection tool that Marianne Rohrbach mentioned in your previous post. This appears to do exactly what you're looking to accomplish.