Trying to maintain spatial reference on newly created numpy array

I am trying to copy the values from raster into newly created array and then trying to save the array with same spatial reference as raster file

inRas = arcpy.GetParameterAsText(0) double_layer = arcpy.GetParameterAsText(1) num_layer = int(double_layer) output_path = arcpy.GetParameterAsText(2) # information on input raster dsc = arcpy.Describe(inRas) sr = dsc.SpatialReference ext = dsc.Extent ll = arcpy.Point(ext.XMin,ext.YMin) arr = arcpy.RasterToNumPyArray(inRas, nodata_to_value=0) shape = arr.shape arcpy.AddMessage("image array created") freq = numpy.empty(shape=(1, shape[1], shape[2])) freq_numpy = numpy.uint8(freq) arcpy.AddMessage("frequency array created") #count = 1 # check for the 46 layers # count is giving me total number of pixels for x in range(0, shape[1]): for y in range(0, shape[2]): freq = 0 for z in range(0, num_layer): # z is number of layer if arr[z][x][y] == 1: freq = freq + 1 freq_numpy[0][x][y] = freq arcpy.AddMessage("code finish file stored at"+ output_path) newRaster = arcpy.NumPyArrayToRaster(freq_numpy,ll,inRas.meanCellWdith,inRas.meanCellHeight) arcpy.DefineProjection_management(newRaster, sr)

The error I get:

ERROR: Traceback (most recent call last): File "", line 66, in  cellHeight = inRas.meanCellHeight AttributeError: 'unicode' object has no attribute 'meanCellHeight' Failed to execute (Frequency). Failed at Thu Dec 17 16:20:53 2015 (Elapsed Time: 4.75 seconds)

When I try to accessinRas.meanCellHeightin Python console of ArcGIS, it returns a value. How can I access it from within my script?

Your inRas can be path of raster or a RasterDataset object. In this error, I can see inRas is an Str Object. Your can't callmeanCellWdithandmeanCellHeightin this type of object

But usingdsc.meanCellHeightanddsc.meanCellHeight, you can do wath you want

You don't use the good object…

You need to create a raster object in order to access properties. Right now, your variableinRasis just a string pointing to the raster on disk, and ArcPy doesn't recognize it as a "raster".

inRasName = arcpy.GetParameterAsText(0) inRas = Raster(inRasName)

Note: I think you'll still need to use the string when converting to a NumPy array…

arr = arcpy.RasterToNumPyArray(inRasName, nodata_to_value=0)

Watch the video: TRYING TO MAINTAIN: LISTENING SESSION (September 2021).