How can get the actual Cell Size form "Analysis Cell Size" parameter

2634
19
Jump to solution
05-11-2017 08:34 AM
MohammadrezaNikfal1
Occasional Contributor

I am trying to build a Python Toolbox. I need to get the cell size using the following code snippet. Using this parameter you can directly enter the cell size as a number or pick a GIS layer to let the ArcPy to calculate the cell size (If the layer is raster, it will be same as the raster's cell size, otherwise it will be the minimum extent /250).  

It is working well and I can use the defined parameter (cell_Size) as Cellsize in any tool (i.e. "Point to Raster"). In my program I need the cell size value (as number) however when you use "cell_Size.valueAsText", you get just the root of the layer as string!

I need the property that I can get the calculated cell size by ArcPy. I tried "cell_Size.Cellsize" , "cell_Size.size" etc. but they are not working. 

    # First parameter  
    cell_Size = arcpy.Parameter(  
    displayName = "Cell Size",  
    name = "cell_Size",  
    datatype = "Analysis Cell Size",  
    parameterType="Required",  
    direction="Input ")
0 Kudos
19 Replies
XanderBakker
Esri Esteemed Contributor

Actually, I have seen this type of behavior. It is possible to select a featureclass to determine the cellsize. Not in the geoprocessing environment, but in other parts.

Maybe you could try something like:

def GetCellSize(user_cell_size):
    try:
        # parse to double
        cell_size = float(user_cell_size)
    except:
        # not a numeric value
        desc = arcpy.Describe(user_cell_size)
        if hasattr(desc, "datasetType"):
            if desc.datasetType == 'FeatureClass':
                extent = desc.extent
                max_size = max(extent.width, extent.height)
                cell_size = max_size / 250.0
            elif desc.datasetType == 'RasterDataset':
                raster = arcpy.Raster(user_cell_size)
                cell_size = (raster.meanCellHeight + raster.meanCellWidth) / 2.0
            else:
                cell_size = None

    return cell_size
MohammadrezaNikfal1
Occasional Contributor

Thank you,

Do you know how can filter just Raster layers during the parameter definition as follows:

cell_Size = arcpy.Parameter(
    displayName="Cell Size",
    name="cell_Size",
    datatype="Analysis Cell Size",
    parameterType="Required",
    direction="Input")

 

I tried 

cell_Size.filter.list = ["Raster"]

But it does not work

0 Kudos
XanderBakker
Esri Esteemed Contributor
0 Kudos
MohammadrezaNikfal1
Occasional Contributor

Right know, I found out that we can not use the "Raster" as filter. It is really weird! 

Take a look at the following table:

0 Kudos
XanderBakker
Esri Esteemed Contributor

So, it's not possible to use "DERasterDataset", like in the example mentioned here: Defining parameter data types in a Python toolbox—Geoprocessing and Python | ArcGIS Desktop 

0 Kudos
MohammadrezaNikfal1
Occasional Contributor

Yes, however, when you are developing a python toolbox and using "Analysis Cell Size" parameter to get the cell size, it shows you every thing!

0 Kudos
curtvprice
MVP Esteemed Contributor

If you set the cell size environment to the result of valueAsText  and the parameter is a raster path, the value set will be the raster's meanCellHeight (as string).

>>> from arcpy import env
>>> env.cellSize = "copy_ras"
>>> env.cellSize
u'10'‍‍‍‍‍‍‍‍

If the user does not select a dataset but instead leaves it at Maximum of Inputs, etc. will be a number or a string "MAXOF", "MINOF", etc.

arcpy.env.cellSize = cell_Size.valueAsText
try:
    cell_Size = float(env.cellSize)
except:
    arcpy.AddError("Cell size not numeric: {}".format(cell_Size))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You could do this test in the parameter validation if you wanted to trap it before the tool runs.

Another approach I have used is to define my cell size parameter as Double and used parameter validation to have it default to the environment cell size (if set).

MohammadrezaNikfal1
Occasional Contributor

Yes, After few days struggling, finally, I used Double as cell size.

0 Kudos
curtvprice
MVP Esteemed Contributor

mrnikfal  If this was your solution, please mark my answer as correct so others can find it, thanks.

0 Kudos
DanPatterson_Retired
MVP Emeritus

double/float.... as a parameter, see the first post

0 Kudos