Select to view content in your preferred language

Trouble using "in_memory" to temporarily store geomety

2780
11
Jump to solution
04-14-2014 07:02 AM
JamesMcBroom1
Emerging Contributor
Here's my script that I'm writing before I convert it into an Add-in button. It seems like it's actually creating a feature class called "in_memory" in my default.gdb...

layers = get_layers() bigArea = 0 bigAreaPIN = None with arcpy.da.SearchCursor(layers['parcels_fc'], ["Shape_Area", "PIN10"]) as rows:     for row in rows:         if row[0] > bigArea:             bigArea = row[0]             bigAreaPIN = row[1]         else: pass  del rows  print bigArea, bigAreaPIN  arcpy.Dissolve_management(layers['parcels_fc'], "in_memory")  with arcpy.da.SearchCursor("in_memory", ["SHAPE@"]) as rows:     for row in rows:         newPolygon = row[0] del rows  subparcels = []  with arcpy.da.UpdateCursor(layers['parcels_fc'], ["PIN10", "SHAPE@"]) as rows:     for row in rows:         if row[0] == bigAreaPIN:             row[1] = newPolygon             rows.updateRow(row)         else:             subparcels.append(row[0])             rows.deleteRow() del rows  with arcpy.da.InsertCursor(layers['superparcel'], ["original", "superparcel"]) as insert:     for x in subparcels:         insert.insertRow((x,bigAreaPIN)) del insert  arcpy.RefreshActiveView() arcpy.Delete_management("in_memory")


Any idea what I'm doing wrong here?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Use
arcpy.env.overwriteOutput = True

I'm not sure why delete wouldn't remove your temp feature class unless you are mixing workspaces. in_memory is process specific, so if you run a tool from one instance, another instance would not be referencing the same in_memory workspace.

View solution in original post

0 Kudos
11 Replies
MathewCoyle
Honored Contributor
Yes, that is exactly what you are telling it to do. If you want to actually utilize the in_memory workspace you must include that as a path.
arcpy.Dissolve_management(layers['parcels_fc'], r"in_memory\working")
arcpy.da.SearchCursor(r"in_memory\working", ["SHAPE@"])
0 Kudos
JamesMcBroom1
Emerging Contributor
OK - I am now seeing the correct in_memory title in the TOC sources, and the script works fine.

But when I try and run it a second time, I get another error telling me that dataset in_memory/dissolved already exists, even though:

arcpy.Delete_management(r"in_memory/dissolved") returns True.

But then I try:

arcpy.Exists(r"in_memory/dissolved")

and that returns True. How can this be? Is it because I am working from the Python console?
0 Kudos
MathewCoyle
Honored Contributor
Use
arcpy.env.overwriteOutput = True

I'm not sure why delete wouldn't remove your temp feature class unless you are mixing workspaces. in_memory is process specific, so if you run a tool from one instance, another instance would not be referencing the same in_memory workspace.
0 Kudos
JamesMcBroom1
Emerging Contributor
Using..

arcpy.env.overwriteOutput = True

The process runs again, but on the 2nd time around it updates with the newPolygon geometry from the 1st time.
0 Kudos
MathewCoyle
Honored Contributor
How are you executing this code? Through an add-in? Toolbox? Something else?
0 Kudos
JamesMcBroom1
Emerging Contributor
I write in PyScripter and then copy-and-paste into the ArcMap python window. Once it 'works' that's when I will drop the code into an add-in that I can share with my colleagues.
0 Kudos
benberman
Regular Contributor
The correct syntax for in_memory is "in_memory\\file". When you want to clear in memory you should simply do
arcpy.Delete_management("in_memory")
0 Kudos
JamesMcBroom1
Emerging Contributor
Changed my code to include the double slash and I'm still getting an error about in_memory/dissolved existing when I run the script for a second time.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Changed my code to include the double slash and I'm still getting an error about in_memory/dissolved existing when I run the script for a second time.


I didn't fully review your codebase, but when I am doing iterative intermediate data creation then I will simply use the in_memory space as temporary workspace for the iteration.  That is, once a proc/step is complete then I remove everything from the workspace.  Here's a def I add to my .py file and simply call from the loop(s)


 def clearINMEM():
     """ clear out the IN_MEMORY workspace of any featureclasses """

     arcpy.env.workspace = "IN_MEMORY"
     
     fcs = arcpy.ListFeatureClasses()
     
     ### for each FeatClass in the list of fcs's, delete it.
     for f in fcs:
         arcpy.Delete_management(f)
        
0 Kudos