Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm new to Python and have been trying to code a raster calculator style script. Basically, I want to convert a raster that has values 0 to -10,000 into a raster that is populated by 1s between certain values.

The reason I want to do it in Python is because I eventually want to automate this process so that I can pick out different values (ie 0 to -10, -5 to -25 etc) and end up with a raster of 1s for each. My aim then is to convert these to polygons so that I can then do a number of vector clip operations across about 800 shapefiles.

I've written a script which produces a new raster at the end but instead of this grid being full of 1s it has values 0-256, so somewhere in my code there's a bug. I'd be grateful if anyone can spot it! If anyone has a way to speed up the code or a better idea for how I can do all this also then that would also be interesting to know.

Here's my code:

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = 1 #this also doesn't work by the way, I have also tried            
#TRUE, no errors come up but it still doesn't let me overwrite the output file

#Set environment settings
env.workspace = "C:/folderlocation where inRaster is stored"

# Set local variables
inRaster = Raster("inputraster")

# Check out Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# depths wanted from inputraster raster
var = [-2500,-3000] #min depth, max depth

#Execute and save Con
OutRaster = Con((inRaster <= var[0])&(inRaster <= var[1]),1)

OutRaster.save("C:/drivelocation/outputfilename")

If anyone has any ideas they'd be gratefully received! Many thanks.

share|improve this question
    
The line where you say arcpy.env.overwriteOutput = 1, try using: arcpy.env.overwriteOutput = True Python is case-sensitive and in your comments you have TRUE. –  Fezter Nov 5 '12 at 1:52
    
Unfortunately I still can't get the overwrite function to work though - True has the same result as TRUE and 1 and I get the error. OutRaster.save("C:/...") RuntimeError: ERROR 000871: C:....: Unable to delete the output ?????????????????? Any more ideas? –  user12433 Nov 6 '12 at 6:01
add comment

1 Answer

up vote 5 down vote accepted

I think the problem is with this expression

OutRaster = Con((inRaster <= var[0])&(inRaster <= var[1]),1)

Con expects the following format

Con (in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})

Which in your case

Con((inRaster <= var[0])&(inRaster <= var[1]),1)

can be broken down into

  • in_conditional_raster, (inRaster <= var[0])&(inRaster <= var[1])

  • in_true_raster_or_constant, 1

I'm a bit confused as to what you're trying to do. You seem to want to turn into 1 the values that fit both of the following criteria

  • inRaster <= -2500
  • inRaster <= -3000

Those two expressions can be reduced to inRaster <= -3000.

enter image description here

So all pixels that have values less then or equal to -3000 have been turned to 1. The rest are left as is. At least that's what your code says. No way of knowing for sure though unless you post the attribute table of this raster or the actual raster file itself.

I think you meant (inRaster <= var[0])&(inRaster > var[1]). That way you'd catch values between -2500 and -3000.

enter image description here

Btw, what is the range of values in the original raster?

share|improve this answer
    
Thanks for your response - I now feel pretty silly. I think while I was trying different things with the code I managed to flip the greater-than sign to a less-than one and I hadn't picked it up. When I change the code to: InConRaster = ((inRaster <= var[0])&(inRaster >= var[1])) OutRaster = Con(InConRaster,1) it then works so thank you for the format tip. Just FYI, when I tried it with the correct sign but didn't change the con expression it still didn't work so it looks like this is the format that's needed. Really grateful for the help. Thank you –  user12433 Nov 6 '12 at 5:53
    
You're welcome :) Don't forget to click the check mark ;-) –  R.K. Nov 6 '12 at 7:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.