Select to view content in your preferred language

Run a python code in ArcGIS

7570
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
XanderBakker
Esri Esteemed Contributor

Oops, that's right the tool checks the license out, does the work and then returns the license... If you need the extension for the other processes in the tool, then the error occurs... Good to keep in mind! Thanks for posting back.

DuminduJayasekera
New Contributor III

Thanks again for your feedback

0 Kudos
DuminduJayasekera
New Contributor III

Hi Xander,

The process used to obtain viet_LULC_30.tif is correct. As you said it was resampled to 30 m. If you open the attribute table, the values are ranging from 0 to 16 and if you see into layer properties min is 0 and max is 16. So, I don't understand where you saw -1. Yes the code was written to use from 1 to 14 because those are the land use types I am interested.

OK. How can I convert that to python code?

Please see the link to download MajoritySTOht.tif  file

https://bft.usu.edu/t3vh9

Please open the Vietnam_Test tool that I am using to perform the GIS process and calculations.

Thanks again,

0 Kudos
JamesCrandall
MVP Frequent Contributor

The above code is run in Visual Studio

What project type in VS allows you run python code?  Or are you executing a .bat file from the application you developed in Visual Studio (there's lots of different types of apps you can develop, VS is just the IDE).

0 Kudos
XanderBakker
Esri Esteemed Contributor

Why not reclassify the LULC raster (holding the 14 integer values) using ReclassByASCIIFile with a ASCII file that has the relation between the input and output values like this:

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

... and then multiply the reclassified raster by the Simard raster.

0 Kudos