1

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 17, 2015 at 10:59

2 Answers 2

1

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...

answered Dec 17, 2015 at 11:22
0

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)
answered Dec 17, 2015 at 11:55
2
  • 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 referenced Commented 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 :) Commented Dec 17, 2015 at 12:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.