if arcpy.Exists(r"C:\GIS\temp_data.gdb"): arcpy.Delete_management(r"C:\GIS\temp_data.gdb") arcpy.CreateFileGDB_management(r"C:\GIS\temp_data.gdb")
I see the main benefit (in addition to portability) of using the scratch environments being the users set their own path in their environmental variables and therefore know where to look if they need that data. If you set the path yourself, it may be in an unfamiliar location and the user may not be code-savvy enough to be able to open your source to find the location, so they have to either track you down or hope you print output file paths to the results window.
Also the arcpy.CreateScratchName('spam', workspace=arcpy.env.scratchGDB/Folder/Workspace) method guarantees a unique name with a digit appended to it, which is useful so users don't have to remember to delete whats in the scratch workspace every run of the script or conversely worry that data is being overwritten if you enabled overwrite output. Sure you can arcpy.CreateUniqueName to your own workspace, but between creating, deleting and checking for the gdb you created yourself you're shaving off a few extra lines of code and increasing readability. To clear out temp data created within my own script, I simply append all paths generated from arcpy.CreateScratchName to a list and at the end of the script do a arcpy.Delete_management loop through the list, unless the user has checked a box in the tool parameters to keep intermediate data.
The most effective way to speed up performance with scratch is to use the "in_memory" workspace. This is very very fast, but only recommended if you know the temporary files will not be large. Do not set the scratch workspace to in_memory, just use it explicitly:tmpFC = arcpy.CreateScratchName("xx1", "", "featureclass", "in_memory") ... arcpy.Delete_management(tmpFC)
tmpFC = arcpy.CreateScratchName("xx1", "", "featureclass", "in_memory") ... arcpy.Delete_management(tmpFC)
import os TEMP = os.getenv("TEMP") # this is a folder env.scratchWorkspace = TEMP tmpFC = arcpy.CreateScratchName("x1", "", "featureclass") # %TEMP%\x10.shp # to use file GDB instead (not a bad plan) do this: env.workspace = TEMP env.scratchWorkspace = env.scratchGDB tmpFC = arcpy.CreateScratchName("x1", "", "featureclass") # %TEMP%\scratch.gdb\x10 Hope this helps.
import os TEMP = os.getenv("TEMP") # this is a folder env.scratchWorkspace = TEMP tmpFC = arcpy.CreateScratchName("x1", "", "featureclass") # %TEMP%\x10.shp # to use file GDB instead (not a bad plan) do this: env.workspace = TEMP env.scratchWorkspace = env.scratchGDB tmpFC = arcpy.CreateScratchName("x1", "", "featureclass") # %TEMP%\scratch.gdb\x10
Why would I want to manually set a scratch workspace outside of the method I implied above?
import os from arcpy import env scrWS = arcpy.CreateScratchname("", ".gdb", "workspace", env.scratchFolder) arcpy.CreateFileGeodatabase(os.path.dirname(scrWS), os.path.basename(scrWS)) env.scratchWorkspace = scrWS tmpFC = arcpy.CreateScratchname("xyz", "", "featureclass", scrWS) # path for new temp FC # ,,,, # then at the end of your script, clean up. # Note that you better delete any layers based on your temp data first # or the delete will not work (file locking) arcpy.Delete_management(scrWS)
Tools that run will use the scratch workspace to write (and delete) their own intermediate files.
So far, all I am getting out of it is that it is a place for temporal data and that it garantees that the scratch workspace will exist if the tool is to be ported.
from arcpy import env env.scratchWorkspace = r"C:\GIS\temp_data.gdb" env.scratchWorkspace = env.scratchFolder # the call env.scratchFolder will locate (or create) scratchFolder # - based on the current scratch workspace. guaranteed writable # Since the workspace is a gdb, it will create a folder parallel to the gdb named "scratch" tmpFC = arcpy.CreateScratchName("", "", "featureclass", env.scratchFolder) # Then CreateScratchName creates a unique name for you to use for your temp # feature class. Since you used a folder, it's a shapefile path (including the .shp) arcpy.CopyFeatures(inFC, tmpFC) ## at the end of your script, always clean up arcpy.Delete_management(tmpFC)
Can anyone enlighten me as to why I would use a scratch workplace over this method? Does it speed up performace in some way?
import os TEMP = os.getenv("TEMP") # this is a folder Windows promises will exist env.scratchWorkspace = TEMP tmpFC = arcpy.CreateScratchName("x1", "", "featureclass", env.scratchFolder) # %TEMP%\x10.shp # to use file GDB instead (not a bad plan) do this: tmpFC = arcpy.CreateScratchName("x1", "", "featureclass", env.scratchGDB) # %TEMP%\scratch.gdb\x10
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.