Run a python code in ArcGIS

6991
34
Jump to solution
01-13-2015 05:26 PM
DuminduJayasekera
New Contributor III

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
DuminduJayasekera
New Contributor III

Thanks a lot again.

Can you please explain the procedure how you created the toolbox and the python script (saved as python) so that I can insert in the model builder?

XanderBakker
Esri Esteemed Contributor

Basically, you download the tool to a location on disk. Unzip the file. Open you existing model . Next navigate with the Catalog window to the location of the toolbox you just downloaded and unzipped. Drag the script on yout model window. It will appear with white colors and unconnected.

explanation1.png

Now double click on the script tool "SRTM Correction Tree Offsets". In the dialog you will be able to select the intermediate results and inputs your are creating in your model:

explanation2.png

Select the input rasters and specify the output result. When you click OK, you will see the result in the model:

explanation3.png

Connect the output of the script tool to other processes in case necessary.

Please remember that in case the question has been answered, to mark the post that answered your question as "Correct Answer". In case you feel that other posts were Helpful you can mark them as such.

DuminduJayasekera
New Contributor III

Thanks so much again for your detailed explanation. I really appreciate it and it was very useful.

Also, I want to know how you created the toolbox (test.tbx) and inserted the python script (SRTM Correction Tree Offsets.py) inside the toolbox. (Please see the images below). What text editor you used such as notepad and then saved the script as .py?

Or, else, did you use any python compilers before inserting into the toolbox?

Please let me know.

Thanks.

toolbox.bmp

script.bmp

XanderBakker
Esri Esteemed Contributor

You can create a new toolbox in the Catalog window inside ArcMap of in ArcCatalog. I used the Catalog Window in ArcMap. Go to the folder where you want to create the toolbox, right click and select the option "New > Toolbox" from the context sensitive menu.

Rename the Toolbox (by default named "Toolbox.tbx")  into a name that suites you (leave the extension .tbx unchanged). Next you can add the script: right click on the toolbox select the option "Add> Script" from the context sensitive menu.

Next follow the instructions here about adding a script tool:

http://resources.arcgis.com/en/help/main/10.2/index.html#/Adding_a_script_tool/00150000001r000000/

and this topic that talks about how to structure the folders of your tool:

http://resources.arcgis.com/en/help/main/10.2/index.html#//01m10000000s000000#GUID-5118AC85-57E4-402... 

and this topic about setting the parameters for the tool.

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

For coding I use PyScripter which you can download for free here:

Downloads - pyscripter - An open-source Python Integrated Development Environment (IDE) - Google Pro...  (use the 32 bits version, since ArcGIS for Desktop 10.x is 32 bits)

DuminduJayasekera
New Contributor III

Thanks so much again. I really appreciate it.

DuminduJayasekera
New Contributor III

Hi Dan,

One last thing I wanted to know, when I ran entire the entire GIS model including your tool with the script, it gives me an error (This tool is not licensed) after it ran your script and stop processing the next GIS tool. So, I closed ArcGIS and ran the remaining steps. IS there a way to prevent so it every step runs smoothly?

Please let me know,

Thanks.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Did you have a Spatial Analyst license available at the moment you ran the model? The code just checks out the extension but does not check if it is available. To see how you should do this, look at the help here: ArcGIS Help (10.2, 10.2.1, and 10.2.2)  scroll down to the python sample...

DuminduJayasekera
New Contributor III

Yes I have spatial analyst license available and I have checked before I ran the model. Do I need to modify the code accordingly?

0 Kudos
XanderBakker
Esri Esteemed Contributor

In theory not, but I'm not sure what caused the error...

DuminduJayasekera
New Contributor III

I commented the first line below and it worked. Thanks again for your input.

# release the SA license

# arcpy.CheckInExtension("Spatial")