Hi,
I need to make it run the following code in arcGIS but this is not written to run in arcgis but in visual studio. I tried but it DOES NOT work. Can someone help me to write thin in python so that I can add as a script in ArcGIS.? I am planning to import this as a script after making this a working code.
Any help is highy appreciated.
Thanks.
------------------------------------------------------------------------
import arcpy
from arcpy import env
from arcpy.sa import *
#Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
arcpy.env.scratchWorkspace = "c:\temp\tmp"
env.workspace = "c:\DEMPreProcess"
LULC = Raster("C:\DEMPreProcess\VietnamLULC_Resample.tif")
Simard = Raster("C:\DEMPreProcess\MajoritySTOht.tif")
resultmap = Simard
for i in range(1,14):
if i == 1:
number = 0.4
elif i == 2:
number = 0.4
elif i == 3:
number = 0.4
elif i == 4:
number = 0.4
elif i == 5:
number = 0.4
elif i == 6:
number = 0.3
elif i == 7:
number = 0.3
elif i == 8:
number = 0.3
elif i == 9:
number = 0.3
elif i == 10:
number = 0.3
elif i == 11:
number = 0
elif i == 12:
number = 0.3
elif i == 13:
number = 0
elif i == 14:
number = 0.3
resultmap = Con(LULC == i,Simard*number,resultmap)
resultmap.save("C:\DEMPreProcess\MajoritySTOhtPer.tif")
Solved! Go to Solution.
If you can include a small subset of your data I can have a look at the calculation you intent to do, since doing 14 calculations for what can be done in one, doesn't sound correct to me.
You would have to adapt the code to allow for parameters: http://resources.arcgis.com/en/help/main/10.2/index.html#/Understanding_script_tool_parameters/00150...
I suppose all things listed below should be change into parameters. So something like this:
env.workspace = arcpy.GetParameterAsText(0) LULC = Raster(arcpy.GetParameterAsText(1)) Simard = Raster(arcpy.GetParameterAsText(2)) resultmap.save(arcpy.GetParameterAsText(3))
instead of this:
env.workspace = "c:\DEMPreProcess" LULC = Raster("C:\DEMPreProcess\VietnamLULC_Resample.tif") Simard = Raster("C:\DEMPreProcess\MajoritySTOht.tif") resultmap.save("C:\DEMPreProcess\MajoritySTOhtPer.tif")
Thanks Bakker for your willingness to help. I need to somehow include python code process to the blue color "python code process" as a tool as shown in the figure above. Please download the script, and input files (MajoritySTOht.tif, and viet_LULC_30.tif) using the following web links below. The output of the python code process is "MajoritySTOhtPer.tif" (please look in the code) and will be used to link to Focal Statistics process in the model builder.
Link to download my ArcGIs tool
Thanks and highly appreciate your help.
A few remarks:
never mind...
Find attached the toolbox with the script tool inside. You can drag and drop this script tool into your model and connect it:
The code of the script is as follows:
import arcpy from arcpy import env from arcpy.sa import * #Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput = True arcpy.env.workspace = "IN_MEMORY" # input parameters LULC = arcpy.GetParameterAsText(0) # r"D:\Xander\GeoNet\LULC\data\viet_LULC_450.tif" Simard = arcpy.GetParameterAsText(1) # r"D:\Xander\GeoNet\LULC\data\MajoritySTOht_450.tif" ras_LULC = Raster(LULC) ras_Simard = Raster(Simard) # output parameter result = arcpy.GetParameterAsText(2) # r"D:\Xander\GeoNet\LULC\data\MajoritySTOhtPer.tif" # internal vars fld_value = "Value" fld_remap = "remap" # remap dictionary dct = {1 : 0.4, 2 : 0.4, 3 : 0.4, 4 : 0.4, 5 : 0.4, 6 : 0.3, 7 : 0.3, 8 : 0.3, 9 : 0.3, 10 : 0.3, 11 : 0, 12 : 0.3, 13 : 0, 14 : 0.3} # add field for remap values arcpy.AddField_management(ras_LULC, fld_remap, "DOUBLE") # update the remap values base on dictionary curs = arcpy.UpdateCursor(ras_LULC) for row in curs: val = row.getValue(fld_value) if val in dct: remap = dct[val] else: # what should be done with the values in the TIFF that are not in your list? remap = 0 # val? row.setValue(fld_remap, remap) curs.updateRow(row) del curs, row # remap the raster ras_remap = Lookup(ras_LULC, fld_remap) # create result resultmap = ras_Simard * ras_remap resultmap.save(result) # release the SA license arcpy.CheckInExtension("Spatial")
Hi Xander,
I was able to import your tool and tried running. See the picture below. I got an error message (See the image)
As far as I see you are using tif files of 450 m resolution. But input rasters are in 30 m resolutions.
I appreciate your help.
Thanks.
Yep that is a memory problem (due to the size of your raster). You can change the "IN_MEMORY" on line 8 to an existing file geodatabase or scratch geodatabase, so it doesn't try to do it in memory
Thanks again for the reply. If I revise line 8 by like this will it work? or can I give a path? like this c:\temp\tmp
arcpy.env.workspace = "scratch geodatabase"
Please let me know,
Thanks a lot.
You could use:
arcpy.env.workspace = r"c:\temp\tmp"
or create a file geodatabase and point to that location.
Hi Xander,
Thanks so much for all your help and sorry for being late to reply you since I had to attend for urgent family matter. Could you please send me your attachment (toolbox.zip with the script) file again since I accidently deleted it.?
I think it is still attached to my post:
https://community.esri.com/servlet/JiveServlet/download/449840-112314/toolbox.zip
If you can't download it I will upload it again.