Select to view content in your preferred language

find Workspace based on MXD

1327
5
Jump to solution
06-07-2012 06:15 AM
AnthonyTimpson2
Regular Contributor
Hello

I am trying to make a script a bit more friendly by removing the need for a user to manually alter the directory location of a workspace.

Is it possible to read something in arcpy.mapping.MapDocument("CURRENT") so that all a person has to do is open the MXD of the project and run the script and the software will find the relevant directories within the appropriate MBD
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
To get the mxd path.
mxd = arcpy.mapping.MapDocument("current") mxd_path = mxd.filePath

View solution in original post

0 Kudos
5 Replies
JimCousins
MVP Alum
you should include a "findAndReplaceWorkspacePaths" method on the "CURRENT" map document. If you acquire the path to the .mxd, you could use that within the Replace portion of the findAndReplaceWorkspacePaths, and set the workspace to the same relative path as the map.
Regards,
Jim
0 Kudos
AnthonyTimpson2
Regular Contributor
you should include a "findAndReplaceWorkspacePaths" method on the "CURRENT" map document. If you acquire the path to the .mxd, you could use that within the Replace portion of the findAndReplaceWorkspacePaths, and set the workspace to the same relative path as the map.
Regards,
Jim


This sounds like a fantastic idea, being of limited skills, where would I begin? could you help with some pseudocode? I'd love the challenge of sorting out the actual code, but some hints on directions i should start plodding would be super 🙂

thanks for your help
0 Kudos
MathewCoyle
Honored Contributor
To get the mxd path.
mxd = arcpy.mapping.MapDocument("current") mxd_path = mxd.filePath
0 Kudos
curtvprice
MVP Alum
I am trying to make a script a bit more friendly by removing the need for a user to manually alter the directory location of a workspace.


Here's a shortcut to Jim's approach: if you provide layers from the mxd as arguments to your tool instead of dataset paths, and set up the mxd to use relative paths, you don't have to worry about paths at all. Once you have the layer inside the script, you can use arcpy.Describe(lyr).catalogPath to get its full path if you need to locate the workspace.

If you would rather use full dataset paths instead of layers as script parameters, here's the rest of Mathew's method to the next step of finding the folder where the current map document lives:

import os
mxd = arcpy.mapping.MapDocument("current")
mxd_path = mxd.filePath
mxd_dir = os.path.dirname(mxd_path)


It may be easier to help you if you provided some information about what the script does, for example, what you are doing with the workspace path once you get it.
0 Kudos
FabianBlau
Deactivated User
You can run a script from outside ArcGIS. ArcGIS has to be installed of course.
This makes it maybe even easier/faster for the user.
0 Kudos