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

1135
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
1 Solution

Accepted Solutions
MohammadrezaNikfal1
Occasional Contributor

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

View solution in original post

0 Kudos
19 Replies
DanPatterson_Retired
MVP Esteemed Contributor

example = '5'

float(example)  => 5  # now a number

0 Kudos
MohammadrezaNikfal1
Occasional Contributor

The parameter type is "Analysis Cell Size".For example:

A = cell_Size.valueAsText

A is "C:\Temp\fgdb.gdb\ASAS"

Although, A apparently is a string, you can use A as cellsize in different tools! but can not get the numeric cell size.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

ok... it is a filename, which you make into a raster, then get its cell size... I think this is similar to previous issues you were having with what is returned by parameters.

MohammadrezaNikfal1
Occasional Contributor

Thank you Dan,

In my program, I need the cell size numeric value for a bunch of calculations, before generating the raster. I was thinking about making a virtual raster just to get the size. It is possible but it is weird.

0 Kudos
XanderBakker
Esri Esteemed Contributor

As Dan mentions, when you have a name of a raster as cell size, it means that that raster exists. It references an existing raster and the cell size should be obtained from that raster. You can convert in into a raster object (as Dan mentioned) and access the meanCellHeight and meanCellWidth properties:

import arcpy

def main():
    print GetCellSize('5')
    print GetCellSize('1.234')
    print GetCellSize(r'C:\GeoNet\ModRutaEscombro\gdb\ModRutaEscombros.gdb\CostDist')


def GetCellSize(user_cell_size):
    try:
        # parse to double
        cell_size = float(user_cell_size)
    except:
        # not a numeric value, maybe a reference to an existing raster
        try:
            raster = arcpy.Raster(user_cell_size)
            cell_size = (raster.meanCellHeight + raster.meanCellWidth) / 2.0
        except:
            cell_size = None

    return cell_size

if __name__ == '__main__':
    main()

This will yield in my case:  

5.0
1.234
47.0
MohammadrezaNikfal1
Occasional Contributor

As I said, when you are using "Analysis Cell Size" as an input data for python toolbox, you can enter number or pick a raster and "Feature class".

in the case of "Feature class", the cell size would be layer extent / 250 (Ersi's default)

Your solution is working for raster and number, however the problem still exists for "Feature class"

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

There is no cell size for feature classes. 

If you want to convert a featureclass to a raster, then you have to specify one manually or select an existing raster. 

It is as designed.

0 Kudos
MohammadrezaNikfal1
Occasional Contributor

Agree, but when you use the ArcGIS toolbox for example Convert point to raster. For cell size you can just pick Raster. however, when you use "Analysis Cell Size" in your tool which exactly has the same interface of cell size in  Convert point to raster tool, you can pick both raster and feature!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Not in my ArcMap 10.5 or ArcGIS PRO 1.4.1... point to raster will not even show me featureclasses or shapefiles as a selection option... only rasters, or specify one manually

0 Kudos