12

Is it possible to set env.workspace = "in_memory" in ArcPy using both ArcGIS Pro and the ArcGIS 10.2.2 (or 10.3) architectures?

What I am trying to do is get the output of a snap pour point operation written to memory as opposed to disk.

I realize I could write to disk then bring it into memory but this would not help. I am trying to optimize a series of tasks minimizing the use of writing to physical media the output of a process that is merely required as an input to the next process.

Midavalo
  • 29,696
  • 10
  • 48
  • 104

2 Answers2

15

I'm going to throw an answer on here because both answers thus far aren't 100% correct.

There are 2 items which can vary from tool to tool.

  1. if it honors the workspace environment (this item is always documented on the tool help page)
  2. if it can make use of the in_memory workspace (this item may not be explicitly documented. You're more likely to see a note if it DOES NOT support in_memory)

To simply answer the "can you set the environment workspace to in_memory". The answer is YES.

>>> import arcpy
>>> arcpy.env.workspace = r"in_memory"
>>> arcpy.CopyFeatures_management(r"c:\temp\foo.shp", "myinmemoutput")
<Result 'in_memory\\myinmemoutput'>
>>> arcpy.Exists("myinmemoutput")
True

Snap Pour Point does honor the workspace environment per it's documentation and explained Python samples. And a test shows you can write output to in_memory and work with that variable reference...to put into another tool, or save the result

>>> import arcpy
>>> arcpy.env.workspace = r"in_memory"
>>> arcpy.CheckOutExtension("SPATIAL")
u'CheckedOut'
>>> snapOut = arcpy.sa.SnapPourPoint("e:/gpservices101/hydro/US30m/test.gdb/sourcepoint", "e:/gpservices101/hydro/US30m/Region08a/Input/elev_cm", 1,"PourPtID")
>>> snapOut
in_memory\SnapPou_sour1
>>> arcpy.Exists(snapOut)
True
>>> snapOut.save(r"c:\temp\todisk.tif")
>>> arcpy.Exists(r"c:\temp\todisk.tif")
True
Midavalo
  • 29,696
  • 10
  • 48
  • 104
KHibma
  • 16,786
  • 1
  • 31
  • 55
-3

Unfortunately, I don't think setting the workspace to the in_memory location like in your question is possible; In such a way that geoprocessing tools will default their output locations to the in_memory workspace.

I just now tried to set the workspace to '\\in_memory' myself, and tested it with a simple CopyFeatures_management() tool run. The string representation of arcpy.env.workspace is indeed set to '\\in_memory', but I'm thinking the CopyFeatures_management() tool is not respecting the workspace environment setting in this case.

It would be awesome though, if we could set in_memory as the default workspace at the map document level. Maybe you could raise this as an ArcGIS Idea?

Best Luck.

EDIT:

Hey I was thinking also since you are using the Snap Pour Point Tool, we may need to approach this a different way. Snap Pour Point appears to act directly on the pour points feature class and does not create an output feature class as part of the tool result. Could we create a temporary, in_memory copy of the pour points and work from there?

EDIT to EDIT:

Oh true, ok you've mentioned you realize this already - sorry.

Jim
  • 1,129
  • 5
  • 12