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
3
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
Dec 6 at 1:22
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 Dec 6 at 1:59
PolyGeo♦
53.1k1779238
53.1k1779238
asked Dec 6 at 1:01
Rachel
293
293
3
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
Dec 6 at 1:22
add a comment |
3
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
Dec 6 at 1:22
3
3
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
Dec 6 at 1:22
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
Dec 6 at 1:22
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
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
Dec 6 at 1:40
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
Dec 6 at 1:43
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 |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
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
Dec 6 at 1:40
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
Dec 6 at 1:43
add a comment |
up vote
3
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
Dec 6 at 1:40
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
Dec 6 at 1:43
add a comment |
up vote
3
down vote
up vote
3
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 Dec 6 at 1:44
answered Dec 6 at 1:38
ahmadhanb
21.6k31951
21.6k31951
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
Dec 6 at 1:40
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
Dec 6 at 1:43
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
Dec 6 at 1:40
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
Dec 6 at 1:43
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
Dec 6 at 1:40
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
Dec 6 at 1:40
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
Dec 6 at 1:43
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
Dec 6 at 1:43
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
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 Dec 6 at 1:39
Michael Stimson
21k22260
21k22260
add a comment |
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
3
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
Dec 6 at 1:22