Search code examples
pythonrasterarcpy

Setting Default Cell Size


I'm having issues trying to set a default cell size for polygon to raster conversion. I need to convert a buffered stream (polygon) to a raster layer, so that I can burn the stream into a DEM. I'd like to automate this process to include it in a larger script.

My main problem is that the PolygonToRaster_conversion() tool is not allowing me to set the cell size to a raster layer value. It's also not obeying the default raster cell size I'm trying to set in the environment. Instead, it consistently uses the default "extent divided by 250".

Here is my script for this process:

# Input Data
Input_DEM = "C:\\GIS\\DEM\\dem_30m.grid"
BufferedStream = "C:\\GIS\\StreamBuff.shp"

# Environment Settings
arcpy.env.cellSize = Input_DEM

# Convert to Raster
StreamRaster = "C:\\GIS\\Stream_Rast.grid"
arcpy.PolygonToRaster_conversion(BufferedStream, "FID", StreamRaster, "CELL_CENTER", "NONE", Input_DEM)

This produces the following error: "Cell size must be greater than zero."

The same error occurs if I type out the path for the DEM layer.

I've also tried manually typing in a number for the cell size. This works, but I want to generalize the usability of this tool.

What I really don't understand is that I used the DEM layer as the cell size manually through the ArcGIS interface and this worked perfectly!!

Any help will be greatly appreciated!!!


Solution

  • There are several options here. First, you can use the raster band properties to extract the cell size and insert that into the PolygonToRaster function. Second, try using the MINOF parameter in the cell size environment setting.

    import arcpy
    
    # Input Data
    Input_DEM = "C:\\GIS\\DEM\\dem_30m.grid"
    BufferedStream = "C:\\GIS\\StreamBuff.shp"
    
    # Use the describe function to get at cell size
    desc = arcpy.Describe(Input_DEM)
    cellsize = desc.meanCellWidth
    
    # Convert to Raster
    StreamRaster = "C:\\GIS\\Stream_Rast.grid"
    arcpy.PolygonToRaster_conversion(BufferedStream, "FID", StreamRaster, "CELL_CENTER", "NONE", cellsize)