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)
newRaster.save(output_path)
The error I get:
ERROR:
Traceback (most recent call last):
File "D:\PythonScripts\FrequencyBackup.py", line 66, in <module>
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 access inRas.meanCellHeight
in Python console of ArcGIS, it returns a value. How can I access it from within my script?
2 Answers 2
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 call meanCellWdith
and meanCellHeight
in this type of object
But using dsc.meanCellHeight
and dsc.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 variable inRas
is 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)
-
I have typecasted the object into Raster and the ERROR has gone I have checked all the parameters passing to the arcpy.NumpyArrayToRaster method still the resulted converted image is not spatially referencedGurminder Bharani– Gurminder Bharani2015年12月17日 12:12:22 +00:00Commented Dec 17, 2015 at 12:12
-
If you've got a separate question, ask it as a separate question. Multiple questions are better than putting lots of problems into one :)Erica– Erica2015年12月17日 12:23:17 +00:00Commented Dec 17, 2015 at 12:23