Single band raster with multiple values to multiple rasters with single value.

2040
4
Jump to solution
06-14-2017 10:47 AM
by Anonymous User
Not applicable

Hoping I can get some direction here for iterating in model builder, I've looked around the forums/internet and haven't found what I am looking for, which I feel is a fairly straight forward model. 

I have a single raster with values from 1-90, each correspond to a land use class. I want to create a model that will extract each value and save it as a new raster. So instead of having one raster with 90 values, I will have 90 rasters each with its own land use value. I could do this as a batch processing "extract by attributes" but that get tedious fast when you want to extract 90 classes. 

I have tried some iterate functions coupled with the "extract by attributes" but I am a beginner at model builder and am not setting the model up correctly. The output rasters I am getting are just copies of the input raster, named by their land use class, but they still have all 90 classes in each of them. 

Does anyone have an example model I could look at or suggestions on what I may be doing incorrectly, that would be much appreciated!

Thanks!  

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You can use the script below to create a raster for each land use class:

def main():
    import arcpy
    import os

    ras = r'C:\Datos\test\data.gdb\myRaster'  # path to input raster
    ws, ras_name = os.path.split(ras)
    base_name = "landuse"  # base name for output rasters

    # settings
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = ws
    arcpy.env.cellSize = ras
    arcpy.env.extent = ras

    fld_value = 'VALUE'
    lst_values = [r.getValue(fld_value) for r in arcpy.SearchCursor(dataset=ras, fields=(fld_value))]

    arcpy.CheckOutExtension("Spatial")
    raster = arcpy.Raster(ras)
    for value in lst_values:
        new_ras = arcpy.sa.Con(raster == value, value)
        new_ras_name = '{0}{1}'.format(base_name, value)
        print "saving:", new_ras_name
        new_ras.save(new_ras_name)


if __name__ == '__main__':
    main()

 However, I seriously doubt that creating a service with 90 layers will be faster than a service based on 1. 

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

What is the end use? It may be easier to convert to a featureclass/ shapefile without generalization then subdivide that file into its constituent parts in vector form using tools to do so (split layer by attributes is one)

Mind you, if you need each landuse to be a separate raster then that would be a different story

0 Kudos
by Anonymous User
Not applicable

Unfortunately the data set is at continental scale and 30m resolution so shapfiles would not be ideal. I'll be using it for a webmap so the file sizes need to be small, and rasters with a single class offer us the smallest file sizes and quick loading. I mean I basically need what that split shapfile by attribute tool does but for rasters. Thanks for the input though!

0 Kudos
XanderBakker
Esri Esteemed Contributor

You can use the script below to create a raster for each land use class:

def main():
    import arcpy
    import os

    ras = r'C:\Datos\test\data.gdb\myRaster'  # path to input raster
    ws, ras_name = os.path.split(ras)
    base_name = "landuse"  # base name for output rasters

    # settings
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = ws
    arcpy.env.cellSize = ras
    arcpy.env.extent = ras

    fld_value = 'VALUE'
    lst_values = [r.getValue(fld_value) for r in arcpy.SearchCursor(dataset=ras, fields=(fld_value))]

    arcpy.CheckOutExtension("Spatial")
    raster = arcpy.Raster(ras)
    for value in lst_values:
        new_ras = arcpy.sa.Con(raster == value, value)
        new_ras_name = '{0}{1}'.format(base_name, value)
        print "saving:", new_ras_name
        new_ras.save(new_ras_name)


if __name__ == '__main__':
    main()

 However, I seriously doubt that creating a service with 90 layers will be faster than a service based on 1. 

by Anonymous User
Not applicable

Thanks Xander! This worked perfectly for a a subset of data I clipped but when I try the full dataset it freezes. It may still be running though when it hangs so I am going to try leaving it overnight and seeing if that works. 

Really appreciate the help with this, cheers, 

M

0 Kudos