Adding shapefile to multiple MXDs using ArcPy?
up vote
1
down vote
favorite
My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.
Does anyone have an idea?
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
arcpy
add a comment |
up vote
1
down vote
favorite
My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.
Does anyone have an idea?
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
arcpy
2
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.
Does anyone have an idea?
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
arcpy
My ArcMap verison is 10.6. The purpose is to add a wetland shapefile to multiple MXDs. It doesn't work and the error said the addLayer is not defined. My script is below.
Does anyone have an idea?
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktopExampleWetland.shp"
wetland = r"C:UsersRachelDesktop"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
arcpy
arcpy
edited 1 hour ago
PolyGeo♦
52.9k1779237
52.9k1779237
asked 2 hours ago
Rachel
263
263
2
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago
add a comment |
2
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago
2
2
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.
@ahmadhanb is correct, you're transposing your workspace and wetland:
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.
add a comment |
up vote
2
down vote
There are many issues in your code:
- You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
- No indent after for loop
Here is a working code that I used
import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop"
workspace.
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.
@ahmadhanb is correct, you're transposing your workspace and wetland:
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.
add a comment |
up vote
2
down vote
You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.
@ahmadhanb is correct, you're transposing your workspace and wetland:
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.
add a comment |
up vote
2
down vote
up vote
2
down vote
You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.
@ahmadhanb is correct, you're transposing your workspace and wetland:
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.
You haven't got any indentation in your code sample, that may just be this version of the code has been badly formatted but as it reads the iteration for mxd in mxd_list: has no instructions and will cause an error. In python indentation is crucial to understanding what the code does.
@ahmadhanb is correct, you're transposing your workspace and wetland:
import arcpy,os
arcpy.env.workspace = ws = r"C:UsersRachelDesktop"
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd, "Main Map")[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
However I would dissuade you from using your desktop as your workspace. User folders have funny permissions and can be subject to quota management; please consider making a folder at the root level (call it GIS perhaps, example C:GIS) and work from there to avoid potential problems. By all means add a shortcut to it on your desktop for ease of access.
answered 1 hour ago
Michael Stimson
20.9k22260
20.9k22260
add a comment |
add a comment |
up vote
2
down vote
There are many issues in your code:
- You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
- No indent after for loop
Here is a working code that I used
import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop"
workspace.
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
add a comment |
up vote
2
down vote
There are many issues in your code:
- You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
- No indent after for loop
Here is a working code that I used
import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop"
workspace.
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
add a comment |
up vote
2
down vote
up vote
2
down vote
There are many issues in your code:
- You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
- No indent after for loop
Here is a working code that I used
import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop"
workspace.
There are many issues in your code:
- You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists
- No indent after for loop
Here is a working code that I used
import arcpy,os
arcpy.env.workspace = r"C:UsersRachelDesktop"
ws = arcpy.env.workspace
wetland = r"C:UsersRachelDesktopExampleWetland.shp"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
dFrame = arcpy.mapping.ListDataFrames(current_mxd)[0]
addLayer = arcpy.mapping.Layer(wetland)
arcpy.mapping.AddLayer(dFrame, addLayer)
current_mxd.save()
print("done")
del mxd_list
The above code assumes that the mxd files are stored inside r"C:UsersRachelDesktop"
workspace.
edited 1 hour ago
answered 1 hour ago
ahmadhanb
21.2k31950
21.2k31950
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
add a comment |
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
1
1
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
I think this could be called a 'snap' ahmadhanb.. the same solution at approximately the same time. I was a little surprised with arcpy.env.workspace = ws = r'c:somepath' as it actually worked, I can think of a few times this quirk could come in handy.
– Michael Stimson
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
You are right but @MichaelStimson and your answer is very informative. It is actually my first time to see this kind of assignment. I removed this line from my answer.
– ahmadhanb
1 hour ago
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305151%2fadding-shapefile-to-multiple-mxds-using-arcpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You are mixing the env and wetland layer. The wetland layer should point to the shapefile and the arcpy.env.workspace should point to the folder where the shapefile exists.
– ahmadhanb
2 hours ago