Selecting map features and zooming in on selected features does not work
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Below is my code for projecting xy coordinates from a csv, creating a shapefile from it, loading a shapefile into an existing mxd document, adding labels, selecting features, zooming in on selected features and exporting a map. Ultimately, I want to export about three different maps so i would have to loop through selected features, zooming and exporting. My code runs with no errors. However the selecting features and zooming in on selected features does not work. I simplified the code right now until i can move on to something more complex involving labeling multiple features, selecting multiple features, and looping through the select, zoom and export of maps. Below is my code. I think the first problem is how do i fix the selecting features and zooming to selected feature. Any help is appreciated.
import pandas as pd
import arcpy
in_csv = 'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test/brg.csv'
df = pd.read_csv(in_csv)
out_csv = in_csv
# project xy coordinates
arcpy.MakeXYEventLayer_management(
out_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# create a shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test',
'brg.shp'
)
# loading shapefile in map document
# get the map document
mxd = arcpy.mapping.MapDocument(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testld.mxd")
# Set the workspace
arcpy.env.workspace = r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Test"
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testbrg.shp")
# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
# save the mxd file
mxd.save()
# adding labeling
layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
mxd.save()
# select features & zoom
nycbrg = arcpy.mapping.ListLayers(mxd, "brg", df)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", """ "brg" = 'Brooklyn Bridge' """)
df.zoomToSelectedFeatures()
# export map
arcpy.mapping.ExportToPNG(mxd, r'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testexpmap1.png')
python zooming arcpy
add a comment |
Below is my code for projecting xy coordinates from a csv, creating a shapefile from it, loading a shapefile into an existing mxd document, adding labels, selecting features, zooming in on selected features and exporting a map. Ultimately, I want to export about three different maps so i would have to loop through selected features, zooming and exporting. My code runs with no errors. However the selecting features and zooming in on selected features does not work. I simplified the code right now until i can move on to something more complex involving labeling multiple features, selecting multiple features, and looping through the select, zoom and export of maps. Below is my code. I think the first problem is how do i fix the selecting features and zooming to selected feature. Any help is appreciated.
import pandas as pd
import arcpy
in_csv = 'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test/brg.csv'
df = pd.read_csv(in_csv)
out_csv = in_csv
# project xy coordinates
arcpy.MakeXYEventLayer_management(
out_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# create a shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test',
'brg.shp'
)
# loading shapefile in map document
# get the map document
mxd = arcpy.mapping.MapDocument(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testld.mxd")
# Set the workspace
arcpy.env.workspace = r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Test"
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testbrg.shp")
# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
# save the mxd file
mxd.save()
# adding labeling
layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
mxd.save()
# select features & zoom
nycbrg = arcpy.mapping.ListLayers(mxd, "brg", df)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", """ "brg" = 'Brooklyn Bridge' """)
df.zoomToSelectedFeatures()
# export map
arcpy.mapping.ExportToPNG(mxd, r'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testexpmap1.png')
python zooming arcpy
add a comment |
Below is my code for projecting xy coordinates from a csv, creating a shapefile from it, loading a shapefile into an existing mxd document, adding labels, selecting features, zooming in on selected features and exporting a map. Ultimately, I want to export about three different maps so i would have to loop through selected features, zooming and exporting. My code runs with no errors. However the selecting features and zooming in on selected features does not work. I simplified the code right now until i can move on to something more complex involving labeling multiple features, selecting multiple features, and looping through the select, zoom and export of maps. Below is my code. I think the first problem is how do i fix the selecting features and zooming to selected feature. Any help is appreciated.
import pandas as pd
import arcpy
in_csv = 'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test/brg.csv'
df = pd.read_csv(in_csv)
out_csv = in_csv
# project xy coordinates
arcpy.MakeXYEventLayer_management(
out_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# create a shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test',
'brg.shp'
)
# loading shapefile in map document
# get the map document
mxd = arcpy.mapping.MapDocument(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testld.mxd")
# Set the workspace
arcpy.env.workspace = r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Test"
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testbrg.shp")
# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
# save the mxd file
mxd.save()
# adding labeling
layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
mxd.save()
# select features & zoom
nycbrg = arcpy.mapping.ListLayers(mxd, "brg", df)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", """ "brg" = 'Brooklyn Bridge' """)
df.zoomToSelectedFeatures()
# export map
arcpy.mapping.ExportToPNG(mxd, r'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testexpmap1.png')
python zooming arcpy
Below is my code for projecting xy coordinates from a csv, creating a shapefile from it, loading a shapefile into an existing mxd document, adding labels, selecting features, zooming in on selected features and exporting a map. Ultimately, I want to export about three different maps so i would have to loop through selected features, zooming and exporting. My code runs with no errors. However the selecting features and zooming in on selected features does not work. I simplified the code right now until i can move on to something more complex involving labeling multiple features, selecting multiple features, and looping through the select, zoom and export of maps. Below is my code. I think the first problem is how do i fix the selecting features and zooming to selected feature. Any help is appreciated.
import pandas as pd
import arcpy
in_csv = 'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test/brg.csv'
df = pd.read_csv(in_csv)
out_csv = in_csv
# project xy coordinates
arcpy.MakeXYEventLayer_management(
out_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# create a shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
'C:/UsersHector Hernandez/Documents/GitHub/pratt-savi-810-2018-10/students/hhernandez2783/Test',
'brg.shp'
)
# loading shapefile in map document
# get the map document
mxd = arcpy.mapping.MapDocument(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testld.mxd")
# Set the workspace
arcpy.env.workspace = r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Test"
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# create a new layer
newlayer = arcpy.mapping.Layer(r"C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testbrg.shp")
# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"AUTO_ARRANGE")
# save the mxd file
mxd.save()
# adding labeling
layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
mxd.save()
# select features & zoom
nycbrg = arcpy.mapping.ListLayers(mxd, "brg", df)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", """ "brg" = 'Brooklyn Bridge' """)
df.zoomToSelectedFeatures()
# export map
arcpy.mapping.ExportToPNG(mxd, r'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10studentshhernandez2783Testexpmap1.png')
python zooming arcpy
python zooming arcpy
edited Nov 16 '18 at 16:33
petezurich
3,86081936
3,86081936
asked Nov 16 '18 at 16:31
hector.h2913hector.h2913
21
21
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think the layer object is not being defined correctly. The documentation for ListLayers has 1 required argument map_document_or_layer
(this would be mxd in your code) and 2 optional arguments wildcard
and data_frame
( i don't think you actually need those in your code).
I would try this nycbrg = arcpy.mapping.ListLayers(mxd)[0]
. Although you have already identified the layer with layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
so you shouldn't need to do it twice. It looks like the problem is in identifying the layer to which the select and zoom functions are referring to.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53341921%2fselecting-map-features-and-zooming-in-on-selected-features-does-not-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the layer object is not being defined correctly. The documentation for ListLayers has 1 required argument map_document_or_layer
(this would be mxd in your code) and 2 optional arguments wildcard
and data_frame
( i don't think you actually need those in your code).
I would try this nycbrg = arcpy.mapping.ListLayers(mxd)[0]
. Although you have already identified the layer with layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
so you shouldn't need to do it twice. It looks like the problem is in identifying the layer to which the select and zoom functions are referring to.
add a comment |
I think the layer object is not being defined correctly. The documentation for ListLayers has 1 required argument map_document_or_layer
(this would be mxd in your code) and 2 optional arguments wildcard
and data_frame
( i don't think you actually need those in your code).
I would try this nycbrg = arcpy.mapping.ListLayers(mxd)[0]
. Although you have already identified the layer with layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
so you shouldn't need to do it twice. It looks like the problem is in identifying the layer to which the select and zoom functions are referring to.
add a comment |
I think the layer object is not being defined correctly. The documentation for ListLayers has 1 required argument map_document_or_layer
(this would be mxd in your code) and 2 optional arguments wildcard
and data_frame
( i don't think you actually need those in your code).
I would try this nycbrg = arcpy.mapping.ListLayers(mxd)[0]
. Although you have already identified the layer with layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
so you shouldn't need to do it twice. It looks like the problem is in identifying the layer to which the select and zoom functions are referring to.
I think the layer object is not being defined correctly. The documentation for ListLayers has 1 required argument map_document_or_layer
(this would be mxd in your code) and 2 optional arguments wildcard
and data_frame
( i don't think you actually need those in your code).
I would try this nycbrg = arcpy.mapping.ListLayers(mxd)[0]
. Although you have already identified the layer with layer = arcpy.mapping.ListLayers(mxd, "brg")[0] #Indexing list for 1st layer
so you shouldn't need to do it twice. It looks like the problem is in identifying the layer to which the select and zoom functions are referring to.
answered Nov 16 '18 at 21:03
HotpepperHotpepper
23618
23618
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53341921%2fselecting-map-features-and-zooming-in-on-selected-features-does-not-work%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