Select to view content in your preferred language

How to transfer a query from R. Calculator to ArcPy

4700
16
03-16-2015 07:22 AM
KONPETROV
Occasional Contributor III

Hello,

I am using ArcGis 10.2. I have 3 classified rasters and i want to use the following query from r. calculator:

                  (rclslope>=45) & (rclmosaic>=2000) & (rclfocal>15) to arc.py

how can i write it?

Is it better to use local - combine tool with arc.py to do it?

Thank you very much.

0 Kudos
16 Replies
KONPETROV
Occasional Contributor III

Runtime error

Traceback (most recent call last):

  File "<string>", line 5, in <module>

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Functions.py", line 244, in Con

    where_clause)

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper

    result = wrapper(*args, **kwargs)

  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Functions.py", line 228, in Wrapper

    ["IfThen", in_conditional_raster, in_true_raster_or_constant])

RuntimeError: ERROR 999998: Unexpected Error.

>>>

This keeps appearing with the following code:

import arcpy

from arcpy import env

from arcpy.sa import *

env.workspace = "C:/sapyexamples/data"

outCon = Con( ( "rclslope" >= 45 ) & ( "rclmosaic" >= 2000 ) & ( "rclfocal" > 15 ), 1 )

outCon.save("C:/sapyexamples/output/outCon")

0 Kudos
DanPatterson_Retired
Deactivated User

outCon = Con( ( Raster("rclslope") >= 45 ) & ( Raster("rclmosaic") >= 2000 ) & ( Raster("rclfocal") > 15 ), 1, 0 )

outCon.save("C:/sapyexamples/output/outCon")

KONPETROV
Occasional Contributor III

Mr. Wiens Unfortunately yes they exist!! Grid format also!! For God.

Mr. Patterson so finally we have that:

import arcpy

from arcpy import env

from arcpy.sa import *

env.workspace = "c:/sapyexamples/data"

outCon = Con( ( Raster("rclslope") >= 45 ) & ( Raster("rclmosaic") >= 2000 ) & ( Raster("rclfocal") > 15 ), 1, 0 )

outCon.save("c:/sapyexamples/output/outCon")

Right?

When i am loading it i am getting a raster with an attribute table with only one row and one colour all over the layer. No classes not anything. Why is that?

If i write the same proccedure in Raster Calculator (rclslope>=45) & (rclmosaic>=2000) & (rclfocal>15)

i ll get the result i want, here what am i doing wrong.

0 Kudos
DanPatterson_Retired
Deactivated User

the raster calculator is different that a script that uses rasters in a script. If the script gives you a result and you have a table, then you will have one of 2 values... 1 (where the condition is met) or 0 (where the condition is not met)  A smart approach would be to use the Con tool in arctoolbox then if that works, go to the Results window and use that as the snippet for your code see my blog for examples related to Results and other programming tips

KONPETROV
Occasional Contributor III

Thank you very much sir, i ll try it

0 Kudos
KONPETROV
Occasional Contributor III

Thank you both on of the problems was that : rclslope >= 45 !! e.g. rclslope it's already reclassified, so i just had to put slope. The other problem was something that had to do with my computer and Gis 10.2. so i put my computer on an earlier state to solve it. The code is working perfectly now with your corrections, that thing with spaces is very tricky.

0 Kudos
DanPatterson_Retired
Deactivated User

glad it is resolved ... tips for the future...never assume your inputs are correct, start from the beginning...always build incrementally, don't try to do a complex statement all at once, errors will be readily identified  good luck​