2

I am writing a script that deals with a raster that will need a subset of the raster outputted and the rest of the original raster needs to be "no data".

To clarify, say, there is a raster and with random values 1-10. I want to input this raster and return a new raster that is all no data except for where the original raster = 5. I thought that the spatial analyst tool "con" would handle this but I can't seem to get it to work. Here is a code sample.

fillRaster = str(Playa_Name) + "_fillRaster"
arcpy.gp.Fill_sa(oneFoot, fillRaster)

extractPoint = str(Playa_Name) + "_extractPoint"
arcpy.gp.ExtractValuesToPoints_sa(waterBodyPoints, fillRaster, extractPoint, "NONE", "VALUE_ONLY")

extractPoint_2 = str(Playa_Name) + "_extractPoint_2"
arcpy.TableSelect_analysis(extractPoint, extractPoint_2, ' "PLAYA_NUM" = ' + "'%s'" %Playa_Name)

field = ["RASTERVALU"]

arcpy.AddMessage("Working...")

with arcpy.da.SearchCursor(extractPoint_2, field) as cursor:
    for row in cursor:
        elevation = row[0]

ofElev = elevation

conRaster = str(Playa_Name) + "_conRaster"
whereClause = ' "VALUE" <> ' + "'%s'" %elevation
outCon = Con(fillRaster, elevation, "", whereClause)
outCon.save(conRaster)

The part that's not working is the very end where I use the Con() function.

Am I using it incorrectly?

I've looked at several examples but can't seem to figure it out. The only other thing I can think of is maybe the "VALUE" field is incorrect, is this not the default name given to a raster's "attributes". Using "VALUE" with the con works perfectly in ModelBuilder which is where this script is being adapted from.

4
  • Have you tried getting rid of the (often problematic) where clause? E.g. outCon = Con(fillRaster <> elevation, elevation)
    – phloem
    Oct 21 '14 at 22:25
  • 1
    You're using mixed formatting in whereClause = ' "VALUE" <> ' + "'%s'" %elevation, for the sake of consistency either append using + or % format... value shouldn't need to be quoted try whereClause = "Value <> %d" % elevation instead and see what you get. Oct 22 '14 at 4:02
  • @MichaelMiles-Stimson, Great, you are correct sir, this works perfectly. I have some work to do in understanding when and where to use the different formats for such things...
    – GeoJohn
    Oct 22 '14 at 14:56
  • @phloem, I did try including the condition with the input raster and was still unsuccessful. Thank you for your input though! However, It's totally possible that I did not do it correctly.
    – GeoJohn
    Oct 22 '14 at 14:58
2

To clarify, say, there is a raster and with random values 1-10. I want to input this raster and return a new raster that is all no data except for where the original raster = 5

I use numpy.where for that. Something like:

    numpy.where ( [condition], [if TRUE do this], [if FALSE do this] ) 

    outarray = numpy.where((outarray==5),5 , 9999)

This keeps the outarray at 5 where it is 5 and sets it to 9999 where it is not 5.

I did not try myself if you instead 9999 just can set numpy.nan in the line above, maybe something like that is possible. Otherwise set it to 9999 or so and define this later as NaN value.

I wrote some more on using numpy.where here: http://geoinformaticstutorial.blogspot.no/2014/01/raster-calculations-with-numpywhere.html

1
  • I'm not familiar with numpy for use with rasters, but I will definitely check this out as a possible solution. Thanks!
    – GeoJohn
    Oct 22 '14 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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