Search code examples
gisrasterarcpy

GIS/ArcPy: extracting land use data from a raster and assignig to shapefile polygons


I have two maps for the same area (1) a raster land use map and (2) a shapefile with thousands of sub-watersheds. I am trying to assign the land use type from the raster (map 1) to each of the sub-watershed (map 2) based on the majority rule. I tried spatial join, but the result seems to be wrong. What is the best way of doing it either in ArcMap or through arcpy?


Solution

  • I would recommend using Zonal Statistics as Table (Spatial Analyst) to accomplish this task. Here is the general workflow:

    1. Run Zonal Statistics as Table using the "Majority" statistic.
    2. Join the table with the watershed feature class using Join Field (Data Management)

    import arcpy, os
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")
    
    # Your watershed feature class
    watersheds = r'C:\path\to\your\geodatabase.gdb\watersheds'
    
    # Your land cover raster
    raster = r'C:\path\to\your\landcover_raster.tif'
    
    # The workspace where the output table will go
    zone_table = r'C:\path\to\your\geodatabase.gdb'
    
    # Perform the zonal statistics and output a table
    arcpy.sa.ZonalStatisticsAsTable (watersheds, 'watershed_id', raster, zone_table, 'DATA', 'MAJORITY')
    
    # Join the table to the watershed feature class by the OBJECTID field (for feature class)
    arcpy.JoinField_management(watersheds, 'watershed_id', zone_table, 'OBJECTID', 'MAJORITY')