I have a two polygon feature classes stored in a geodatabase: Natural reserves and Forest cover.
Using ArcGIS Desktop 10.4, I would like to subset National reserves where the forest covers more then 5% of their area.
Therefore, I wanted to:
- intersect the natural reserves and forest
- dissolve the forest (multiple islets might present the forest in one reserve) polygons in Reserves
- spatial join the Shape_Area of forest per reserve to attribute table of original natural reserves
- calculate the % of forest coverage subset only reserves covered by forest by more than 5%
My problem is when I apply Spatial Join, some of my forest
Area values (
Area_SMK)are added as
Null. I have tried to define
fieldmappings, and also copied the Shape_Area of Forest FC to new columns, to avoid the same field name (Shape_Area) in my output data; neither one helped:
How can I avoid the Null values acquisition?
Is there maybe a safer way to subset my Natural reserves by the proportion of the forest?
My python code:
import arcpy # SEt working directory arcpy.env.workspace = "C:/Users/Projects/analyzed.gdb" # Read input Forest and Reserves data inSMK = "outSMKpoly" inNR = "NR_inSMK" # Set local variables dissFields = ["NAM_lat", "PSB"] try: # Allow files to overwrite arcpy.env.overwriteOuput = True # Process: Intersect SMK and NR intersectOutput = "intSMK_NR" if arcpy.Exists(intersectOutput): arcpy.Delete_management(intersectOutput) arcpy.Intersect_analysis([inSMK, inNR], intersectOutput) # Process: dissolve SMK by fields NAM_lat and PSB outDiss = "outDiss" if arcpy.Exists(outDiss): arcpy.Delete_management(outDiss) arcpy.Dissolve_management(intersectOutput, outDiss, dissFields) # add new field to outDiss arcpy.AddField_management(outDiss, "Area_SMK", "DOUBLE") # copy values from Shape_area to new field arcpy.CalculateField_management(outDiss, "Area_SMK", "!Shape_Area!", "PYTHON_9.3") # Process: define the field mapping for correct Spatial join fieldmappings = arcpy.FieldMappings() # Add all files from inputs fieldmappings.addTable(inNR) fieldmappings.addTable(outDiss) # Name fields I want to keep keepers = ["NAM_lat", "PSB", "Area_SMK", "Shape_Area", "Shape_length"] # Remove output fields I don't want for field in fieldmappings.fields: if field.name not in keepers: fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name)) # Process: Spatial join the Shape_area of dissolved # SMK to the NR area outSpatJoin = "outJoin" if arcpy.Exists(outSpatJoin): arcpy.Delete_management(outSpatJoin) arcpy.SpatialJoin_analysis(inNR, outDiss, outSpatJoin, field_mapping = fieldmappings, join_operation = "JOIN_ONE_TO_ONE", match_option = "COMPLETELY_CONTAINS") print "intersected" except Exception as err: print(err.args) # clean up the data arcpy.Delete_management(intersectOutput)