Novice needs Help Automating an Organized Workspace and Map Document

3625
3
Jump to solution
04-10-2015 11:56 AM
RachaelJohnson
Occasional Contributor

Hi, this is day 3 of my Python experience and what I have so far works but results in a disorganized, cluttered mess in my map document.  I need to clean it up before I go any further and get things any more complicated.  I suspect I will shove a lot of questions into one blog post so I will number them as I go to make responding easy.

I have been reading about the os package and arcpy.mapping and arcpy.env and workspaces and default GDBs and things.

  1. So far, I have been writing my code in PyCharm and copy/pasting it into the ArcGIS Python window so I can check that it works.  arcpy.GetParameterAsText(0) does not ask for my input whether I run it in PyCharm or ArcGIS.  Is that something that only works when creating a script tool? 
  2. Can you set the default GDB in python or is that something that HAS to happen when the map is created? 
  3. Does os.makedirs() accept a full file path or does it have to reference the current directory? I would like the user to input the .mxd file and use indexing to remove the .mxd extension to create a folder with the same name as the map document, and again to create a scratch folder.  ... And also I want to create a GDB with the same file path and name (for the default GDB).
  4. When running my scripts, I found that most of my files went into the default GDB but one went into my scratch folder.  Is there a rhyme or reason for determining where the intermediate files go?  I would like the useful files to go into the default GDB and the intermediate files to go into the scratch folder.  After the workspace is established, can I specify where I want created files to go in the workspace by shortening the file path to something like "/MapDocument/MapDocument.gdb/Polygon" or "MapDocument/scratch/Polygon"? where Polygon is the name of an intermediate shapefile?
  5. Is there a way to stop the intermediate files from being added to the Map Document automatically?  As a follow up to number 5, if I turn automatic addition off in the environments, how do I add one to the map? Arcpy.mapping.AddLayer() or something else?
  6. Is there a way to add a group layer to the map without having to create a blank group layer and saving it and referencing it?  That does not seem good for automation.
  7. Similarly, how would I modify arcpy.mapping.MoveLayer() to move a layer into a group layer?
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

1. Yes that is for getting input for a script tool.

2. You can set your workspace using arcpy.env.workspace here is a link for all environment controls ArcGIS Help 10.1

3. Not familiar enough with the os module to help unfortunately.  I believe it can take a full file path

4. You can set a scratch workspace using the environment settings (See #2) as far as the rhyme or reason for a file going to scratch workspace vs default workspace, someone smarter can get to that.

5. You can set if outputs get added to map with environment settings (See #2)  look for arcpy.env.addOutputsToMap

6. From what I know, no there is not.

7. Use arcpy.mapping.AddLayerToGroup to add a layer to a group layer

View solution in original post

3 Replies
IanMurray
Frequent Contributor

1. Yes that is for getting input for a script tool.

2. You can set your workspace using arcpy.env.workspace here is a link for all environment controls ArcGIS Help 10.1

3. Not familiar enough with the os module to help unfortunately.  I believe it can take a full file path

4. You can set a scratch workspace using the environment settings (See #2) as far as the rhyme or reason for a file going to scratch workspace vs default workspace, someone smarter can get to that.

5. You can set if outputs get added to map with environment settings (See #2)  look for arcpy.env.addOutputsToMap

6. From what I know, no there is not.

7. Use arcpy.mapping.AddLayerToGroup to add a layer to a group layer

RachaelJohnson
Occasional Contributor

2. The workspace is different from the default GDB though.  If I save a raster output, it goes into the workspace.  If I don't, it stays in the default GDB. 

Thanks for your help!

0 Kudos
BlakeTerhune
MVP Regular Contributor

1. You can try using raw_input()

3. Use os.path to construct and deconstruct file paths.