Select to view content in your preferred language

Run a python code in ArcGIS

8148
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
1 Solution

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

View solution in original post

34 Replies
DanPatterson_Retired
MVP Emeritus

What is the error message? and what is it supposed to do?

0 Kudos
DuminduJayasekera
Regular Contributor

Hi Dan,

I am new to python but what I want to write a script in python to take the integer values from  i=1 to 14 in VietnamLULC_Resample.tif file and then multiplied by the interger values in the MajoritySTOht.tif file for each corresponding i.

I need to include this process as a pythin script to this model builder.

Thanks and help is appreciated.

pic.bmp

0 Kudos
DanPatterson_Retired
MVP Emeritus

It is hard to tell from your code formatting, but I think your last two lines should be indented so that they are within the for loop...currently, they are outside the loop by all appearances.  You should use the Advanced Editor, then select >> and choose Syntax Highlighting and format your code in the original thread to Python syntax to make it easier to read and so indentation can be confirmed.

DuminduJayasekera
Regular Contributor

Dan,

The above code is run in Visual Studio but I want to convert it to pythin to include in the model bilder.

Where can I find the Advanced editor and syntax highlighting?

Thanks again for your reply.

0 Kudos
XanderBakker
Esri Esteemed Contributor

In the upper right corner of the editor (when editing a post) you will see the link "Use advanced editor". The code you supplied is Python code. It is possible that Visual Studio has been used to run it, but the IDE has no influence on the code itself.

DuminduJayasekera
Regular Contributor

OK. But How can I include this code into my model buider environment? and to make it run?

Thanks and highly appreciate your help. I am very new to python and GIS model builder.

0 Kudos
JamesCrandall
MVP Frequent Contributor

Create a new geoprocessing tool, set the source to your .py file, add the tool to your model.

0 Kudos
DuminduJayasekera
Regular Contributor

Hi James,

Could you please illustrate how to do that? I am totally new to this.

Thanks again and appreciate it.

0 Kudos