-3

I am trying to calculate raster reclassify range based on min/max values that are split in equal areas and build up the container for arcpy reclassify.

I am using ArcGIS 10.5.

So far, I've hit an error that says:

Traceback (most recent call last):
  File "C:\Users\Erick\Documents\ArcGIS\Projects\PythonProject\Rasterized.py", line 25, in <module>
    elevMinInital2.append(RasterRange)
AttributeError: 'str' object has no attribute 'append'

Output should look like based on min value of 0 and max value of 25 as extracted from a raster (the values will change depends on raster):

rasterRange = "[[0,5,1],[5,10,2],[10,15,3],[15,20,4],[20,25,5]]"

This is my code:

import arcpy
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *

inRaster1 = arcpy.GetParameterAsText(0)

elevMINResult = arcpy.GetRasterProperties_management(inRaster1, "MINIMUM")  
elevMAXResult = arcpy.GetRasterProperties_management(inRaster1, "MAXIMUM")  

elevMin = float(elevMINResult.getOutput(0))  
elevMax = float(elevMAXResult.getOutput(0))  

divideValue = (elevMax - elevMin)  

RasterRange = []

divideNumber = 5
TempDivideNumber = 4
elevMinAdd = INT(divideValue / divideNumber)

elevMinInital = elevMin + elevMinAdd
elevMinInital2 = ("[{}, {}, 1]".format(elevMin, elevMinInital))
elevMinMin = elevMinInital

elevMinInital2.append(RasterRange)

while divide in TempDivideNumber:
    if divide == 1:
        print "Do nothing"
    else:
        elevMinTemp = elevMinMin
        elevMinMin = elevMinMin + elevMinAdd
        elevText = "[{},{},{}]",format(elevMinMinTemp, elevMinMin, divide)
        elevText.append(RasterRange)

elevFinal = "[{},{},5]".format(elevMinMin, elevMax)
elevFinal.append(RasterRange)
  • 2
    What is your question? What happens when you run your script? Are you getting an error, or not the expected output, or no output at all? Please edit your question to include detail about the problem you have, any errors you are getting, and where you are stuck. – Midavalo Mar 25 '17 at 21:31
0

You have an error on this line

elevMinInital2.append(RasterRange)

The error says 'str' object has no attribute 'append'. This is because you have a string variable that looks something like

"[1.0, 0.0, 1]"

rather than a List (which you can append to) which would look like

[1.0, 0.0, 1]

You need to change the creation of your elevMinInital2 variable to be a list rather than a string. Try this instead:

elevMinInital2 = [elevMin, elevMinInital, 1]

You can refer to existing variables directly inside lists, you don't need to construct them using string formatting. Using the above will allow you to append to the list:

elevMinInital2.append(RasterRange)

You will have the same problem with elevText and `elevFinal. Again, create them as lists rather than strings.

elevText = [elevMinMinTemp, elevMinMin, divide]
elevText.append(RasterRange)

and

elevFinal = [elevMinMin, elevMax, 5]
elevFinal.append(RasterRange)

That all said, based on your comment at the start of your question that you're expecting something like

rasterRange = [[0,5,1],[5,10,2],[10,15,3],[15,20,4],[20,25,5]]

You may want to switch your append around, to something like

RasterRange.append(elevMinInital2)

RasterRange.append(elevText)

RasterRange.append(elevFinal)
  • Thank you! I made the necessary changes and noticed that the python is now saying that: Traceback (most recent call last): File "C:\Users\Erick\Documents\ArcGIS\Projects\PythonProject\Rasterized.py", line 27, in <module> for divide in TempDivideNumber: TypeError: 'int' object is not iterable Failed to execute (Rasterize). – ASL_GIS Mar 26 '17 at 19:52
  • @ASL_GIS You can't iterate an interger. You probably want to change it to something like for divide in range(1, TempDivideNumber):. And if you want to include the max TempDivideNumber you either need to increase the number by 1 or make it for divide in range(1, TempDivideNumber + 1):. That said, I'm unsure what you're actually trying to do here, so if you don't want to loop through a range of numbers you'll need to provide more info about what you're trying to do. If that is the case, I suggest you ask a new question with your updated code snippet and your new error message. – Midavalo Mar 26 '17 at 20:05
0

Thanks to everyone. The solution is below:

import arcpy
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *

inRaster1 = arcpy.GetParameterAsText(0)

elevMINResult = arcpy.GetRasterProperties_management(inRaster1, "MINIMUM")  
elevMAXResult = arcpy.GetRasterProperties_management(inRaster1, "MAXIMUM")  

elevMin = float(elevMINResult.getOutput(0))  
elevMax = float(elevMAXResult.getOutput(0))  

divideValue = (elevMax - elevMin)  

RasterRange = []

divideNumber = 5
TempDivideNumber = 4
elevMinAdd = int(divideValue / divideNumber)

elevMinInital = elevMin + elevMinAdd
elevMinInital2 = [elevMin, elevMinInital, 1]
elevMinMin = elevMinInital

RasterRange.append(elevMinInital2)

for divide in range(2, TempDivideNumber):
    divide += 1
    elevMinTemp = elevMinMin
    elevMinMin = elevMinMin + elevMinAdd
    elevText = [elevMinTemp, elevMinMin, divide]
    RasterRange.append(elevText)

elevFinal = [elevMinMin, elevMax, 5]
RasterRange.append(elevFinal)

arcpy.AddMessage("Collected the following info: {}".format(RasterRange))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.