Select to view content in your preferred language

Run a python code in ArcGIS

8158
34
Jump to solution
01-13-2015 05:26 PM
DuminduJayasekera
Regular Contributor

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")

Tags (1)
0 Kudos
34 Replies
XanderBakker
Esri Esteemed Contributor

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")
DuminduJayasekera
Regular Contributor

pic.bmp

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.

https://bft.usu.edu/s467k

https://bft.usu.edu/mt9zj

https://bft.usu.edu/wqlyb   

Link to download my ArcGIs tool

https://bft.usu.edu/tlxxz  

Thanks and highly appreciate your help.

0 Kudos
XanderBakker
Esri Esteemed Contributor

A few remarks:

  • viet_LULC_30.tif is a large raster (2GB) with a resolution of 30 meters, but the detail inside is only using a resolution of 450m (was this raster resampled with a factor 15? If so, why?). I notice that it has values from -1 to 16. In your code you only account for value 1 to 14.Values -1, 0, 15 and 16 will remain untouched. Is that correct?
  • One of the downloads (WRFV3.6.1.TAR.gz) seems to be C code. I asume this should not be used.
  • MajoritySTOht.tif is not included in the downloads. Pease attach the version with 450m resolution to keep things small.
  • The Toolbox Lee_Filtering contains 5 models. I won't touch these, I will simply show how to include the script in the model
  • The script SRTMCorrectionTreeOffsets.py contains the code you originally po
XanderBakker
Esri Esteemed Contributor

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:

model_script.png

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")
DuminduJayasekera
Regular Contributor

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.

layout.bmp

error.bmp

I appreciate your help.

Thanks.

0 Kudos
XanderBakker
Esri Esteemed Contributor

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

DuminduJayasekera
Regular Contributor

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.

0 Kudos
XanderBakker
Esri Esteemed Contributor

You could use:

arcpy.env.workspace = r"c:\temp\tmp"

or create a file geodatabase and point to that location.

DuminduJayasekera
Regular Contributor

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

0 Kudos
XanderBakker
Esri Esteemed Contributor

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.