Calculating a value from two feature class attribute fields

433
3
08-02-2021 12:35 PM
GISAdministratorFranklin
New Contributor

I'm new to Python so thanks for your patience.

I'm working with two feature classes: Buildings and Parcels.  What I'm trying to achieve is as follow:

  1.  Select a Parcel in ArcMap
  2. The script will run the Select by Location tool to find the Building polygon within the Parcel polygon
  3. The script will then divide the Building feature class's area field by the Parcel feature class's area field.
  4. The output merely needs to be a numeric value.  

Any help would be greatly appreciated!

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Did you look at 

Spatial Join (Analysis)—ArcGIS Pro | Documentation

to get your started.  It would enable you to sum the area of the buildings in the process.

The division can be done in a new field after the fact


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

Are you creating a script tool for this? 

You could try something like this to get you started, though you need to make sure that the selected parcel parameter type in your script tool is set to a feature layer (so only the selected parcel is used):

selectedParcel = arcpy.GetParameter(0) # I assume this is for a script tool
buildingsFC = arcpy.GetParameterAsText(1) # path to buildings features

# Get the selected parcels area
parcelArea = arcpy.da.SearchCursor(selectedParcel, 'SHAPE@AREA').next()

# intersect to find buildings
selectedBuilding = arcpy.management.SelectLayerByLocation(buildingsFC, 'INTERSECT', selectedParcel, 'NEW_SELECTION')

# iterate over selected buildings and calculate math stuff
with arcpy.da.SearchCursor(selectedBuilding , 'SHAPE@AREA') as sCur:
    for row in sCur:
        print(f'area calc: {row[0]/ parcelArea}')

 

0 Kudos
GISAdministratorFranklin
New Contributor

Thank you!

0 Kudos