Select to view content in your preferred language

ArcPy: Working with temporary rasters...

4759
6
08-10-2015 12:41 PM
MaribethMilner
New Contributor III

I'm new to GeoNet (and ArcPy)...

I wrote a script that will eventually be turned into a script tool. For now the code:

Creates a point (X)

Extracts values for that point from multiple grids (X)

Assigns the extracted grid values to variables (X)

For each grid...

Uses the extracted value of that grid (stored in a variable) to define an inference space for the grid (X)

Sets non-inference space cells to nulls

Creates a comprehensive inference space by adding all the grids together

Converts final inference space to a polygon

Selects field sites associated with the polygon

Saves the selected field sites' model parameters to an Excel file.

I can run each line one at a time from within the Python Window, but I can't run the code as a script loaded into the Python Window. When the code reaches the first SetNull command it generates the output AND errs out.

# Calculate each grid's inference space using Charlie's rules. Begin with ai

airast = arcpy.Raster("aikm")

if aikm < 6000:

    outairast = (airast > (aikm - 1000))*(airast < (aikm + 1000))

else:

    outairast = airast > 5000

# Set false (i.e. zero) values to null

outai_setnull = SetNull("outairast", "outairast", "VALUE < 1")

I set my workspace to the input grid location. I do not want the output to go to that folder. The temporary rasters are being stored in my Default.gdb.

Do I need to save (and then delete) outairast and outai_setnull in order for the script to work? If so... that doesn't sound very efficient.

0 Kudos
6 Replies
MaribethMilner
New Contributor III

Thanks for your feedback, Dan.

The grids are 250m to 1km resolution grids of Sub-Saharan Africa and I don't know anything about the users computers, so I'm reluctant to do anything in memory.

I also tried setting a scratch workspace, but the files locked and I couldn't delete them. (Win7)

0 Kudos
DanPatterson_Retired
MVP Emeritus

some tips...but not all the solutions...

  • a lock appears when 'something' is using it, specific examples would include arcmap,
  • 'something' depends on what you are trying to delete

quickest solution

  • if Arc-anything is being used, use 'it' to delete what isn't needed as you go along
  • every session, I create a new folder for temporary stuff...I don't use an old scratch folder, since yesterdays scatch may be todays golden file.
  • If you do that, then you can copy/move the good stuff to a new location and delete the bad
  • failing that, close the Arc-things and use Windows explorer to delete what you don't want
  • having said that, never try to delete Esri Grids using the above, since all the grids in a folder will share the same 'info', possibly toasting the good data as well as the bad...hence, use Arc-things to delete those.
  • barring the above, reboot...you can kill any lock file then

More tips will chime in I am sure, but this should get you started as a minimum...

...and as for in-memory workspace...don't be afraid, you can save stuff to a local machine...besides isn't everyone going all 'cloud'-y these days

0 Kudos
MaribethMilner
New Contributor III

As an fyi... Internet access in Africa can be a problem.

0 Kudos
DarrenWiens2
MVP Honored Contributor

What exactly is the error message at SetNull?

I'm guessing this has to do with the fact that when you run the code one line at a time in ArcMap, the raster "outairast" is added to the table of contents. It is then available to SetNull as the layer "outairast" (in quotes). When you run the script outside of ArcMap, there is no table of contents to add the result "outairast" to, therefore no layer called "outairast" (in quotes). It then interprets "outairast" as a string in the call to SetNull, which results in an error.

There is a raster held in the variable outairast (no quotes). I believe you should be able to access it using something like:

outai_setnull = SetNull(outairast, outairast, "VALUE < 1")
MaribethMilner
New Contributor III

When I tried running the script from Pythonwin the system couldn't find my Spatial Analyst license and the code erred when it enountered ExtractMultiValuesToPoints. The code didn't err when I imported spatial analyst (from arcpy.sa import *).

So I loaded the code in the Python Window where I knew there wouldn't be licensing issues and encountered the following error messages after the code created outairast:

ERROR 000865: Input conditional raster: outairast does not exist.

ERROR 000865: Input false raster or constant value: outairast does not exist.

Failed to execute (SetNull).

Just tried running SetNull without using quotes around outairast. It worked. Thank you very much.

0 Kudos