How to get three exported png files using the for loop in the code below?
the code below is performing the following actions:
- plotting xy coordinates from csv
- creating shapefile
- loading shapefile in ArcMap
- labeling map features
- Selecting specific map features
- Zooming in on selected features
- Exporting a PNG file
- Repeating the last three steps for two other sets of features
the code runs with no error but I am not getting three PNG files, one for each set of features (defined as sql_1, sql_2, sql_3 in code). Instead I am getting one exported PNG file of the last set of features I want to export (sql_3)
What can I do get three exported png files using the for loop in the code below?
import pandas as pd
import arcpy
# Declare Variable for Location of csv File with Data
in_csv = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.csv'
# Project xy Coordinates
arcpy.MakeXYEventLayer_management(
in_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# Declare Variable for Output Location of Shapefile & Location of Workspace for Loading Shapefile
out_shp_worksp = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project'
# Create a Shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
out_shp_worksp,
'brg.shp'
)
# Loading Shapefile in Map Document
# Declare Variable of Location of mxd File with Basemap for Shapefile Load
load_base = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectld2.mxd'
# get the map document
mxd = arcpy.mapping.MapDocument(load_base)
# Set the workspace
arcpy.env.workspace = out_shp_worksp
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# Declare Variable of Location to Save Shapefile
save_ly = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.shp'
# create a new layer
newlayer = arcpy.mapping.Layer(save_ly)
# 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()
sql_1 = """ "brg" = 'Brooklyn Bridge' OR "brg" = 'Manhattan Bridge' OR "brg" = 'Williamsburg Bridge' OR "brg" = 'Ed Koch Queensboro Bridge' """
sql_2 = """ "brg" = 'George Washington Bridge' OR "brg" = 'Lincoln Tunnel' OR "brg" = 'Holland Tunnel' """
sql_3 = """ "brg" = 'Bayonne Bridge' OR "brg" = 'Goethals Bridge' OR "brg" = 'Outerbridge Crossing Bridge' """
SQL_List = [sql_1, sql_2, sql_3]
for x in SQL_List:
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectexpmap1.png'
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
python for-loop arcpy
add a comment |
the code below is performing the following actions:
- plotting xy coordinates from csv
- creating shapefile
- loading shapefile in ArcMap
- labeling map features
- Selecting specific map features
- Zooming in on selected features
- Exporting a PNG file
- Repeating the last three steps for two other sets of features
the code runs with no error but I am not getting three PNG files, one for each set of features (defined as sql_1, sql_2, sql_3 in code). Instead I am getting one exported PNG file of the last set of features I want to export (sql_3)
What can I do get three exported png files using the for loop in the code below?
import pandas as pd
import arcpy
# Declare Variable for Location of csv File with Data
in_csv = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.csv'
# Project xy Coordinates
arcpy.MakeXYEventLayer_management(
in_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# Declare Variable for Output Location of Shapefile & Location of Workspace for Loading Shapefile
out_shp_worksp = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project'
# Create a Shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
out_shp_worksp,
'brg.shp'
)
# Loading Shapefile in Map Document
# Declare Variable of Location of mxd File with Basemap for Shapefile Load
load_base = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectld2.mxd'
# get the map document
mxd = arcpy.mapping.MapDocument(load_base)
# Set the workspace
arcpy.env.workspace = out_shp_worksp
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# Declare Variable of Location to Save Shapefile
save_ly = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.shp'
# create a new layer
newlayer = arcpy.mapping.Layer(save_ly)
# 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()
sql_1 = """ "brg" = 'Brooklyn Bridge' OR "brg" = 'Manhattan Bridge' OR "brg" = 'Williamsburg Bridge' OR "brg" = 'Ed Koch Queensboro Bridge' """
sql_2 = """ "brg" = 'George Washington Bridge' OR "brg" = 'Lincoln Tunnel' OR "brg" = 'Holland Tunnel' """
sql_3 = """ "brg" = 'Bayonne Bridge' OR "brg" = 'Goethals Bridge' OR "brg" = 'Outerbridge Crossing Bridge' """
SQL_List = [sql_1, sql_2, sql_3]
for x in SQL_List:
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectexpmap1.png'
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
python for-loop arcpy
add a comment |
the code below is performing the following actions:
- plotting xy coordinates from csv
- creating shapefile
- loading shapefile in ArcMap
- labeling map features
- Selecting specific map features
- Zooming in on selected features
- Exporting a PNG file
- Repeating the last three steps for two other sets of features
the code runs with no error but I am not getting three PNG files, one for each set of features (defined as sql_1, sql_2, sql_3 in code). Instead I am getting one exported PNG file of the last set of features I want to export (sql_3)
What can I do get three exported png files using the for loop in the code below?
import pandas as pd
import arcpy
# Declare Variable for Location of csv File with Data
in_csv = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.csv'
# Project xy Coordinates
arcpy.MakeXYEventLayer_management(
in_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# Declare Variable for Output Location of Shapefile & Location of Workspace for Loading Shapefile
out_shp_worksp = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project'
# Create a Shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
out_shp_worksp,
'brg.shp'
)
# Loading Shapefile in Map Document
# Declare Variable of Location of mxd File with Basemap for Shapefile Load
load_base = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectld2.mxd'
# get the map document
mxd = arcpy.mapping.MapDocument(load_base)
# Set the workspace
arcpy.env.workspace = out_shp_worksp
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# Declare Variable of Location to Save Shapefile
save_ly = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.shp'
# create a new layer
newlayer = arcpy.mapping.Layer(save_ly)
# 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()
sql_1 = """ "brg" = 'Brooklyn Bridge' OR "brg" = 'Manhattan Bridge' OR "brg" = 'Williamsburg Bridge' OR "brg" = 'Ed Koch Queensboro Bridge' """
sql_2 = """ "brg" = 'George Washington Bridge' OR "brg" = 'Lincoln Tunnel' OR "brg" = 'Holland Tunnel' """
sql_3 = """ "brg" = 'Bayonne Bridge' OR "brg" = 'Goethals Bridge' OR "brg" = 'Outerbridge Crossing Bridge' """
SQL_List = [sql_1, sql_2, sql_3]
for x in SQL_List:
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectexpmap1.png'
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
python for-loop arcpy
the code below is performing the following actions:
- plotting xy coordinates from csv
- creating shapefile
- loading shapefile in ArcMap
- labeling map features
- Selecting specific map features
- Zooming in on selected features
- Exporting a PNG file
- Repeating the last three steps for two other sets of features
the code runs with no error but I am not getting three PNG files, one for each set of features (defined as sql_1, sql_2, sql_3 in code). Instead I am getting one exported PNG file of the last set of features I want to export (sql_3)
What can I do get three exported png files using the for loop in the code below?
import pandas as pd
import arcpy
# Declare Variable for Location of csv File with Data
in_csv = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.csv'
# Project xy Coordinates
arcpy.MakeXYEventLayer_management(
in_csv,
'Brg_Lng',
'Brg_Lat',
'in_memory_xy_layer',
)
# Declare Variable for Output Location of Shapefile & Location of Workspace for Loading Shapefile
out_shp_worksp = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project'
# Create a Shapefile
arcpy.FeatureClassToFeatureClass_conversion(
'in_memory_xy_layer',
out_shp_worksp,
'brg.shp'
)
# Loading Shapefile in Map Document
# Declare Variable of Location of mxd File with Basemap for Shapefile Load
load_base = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectld2.mxd'
# get the map document
mxd = arcpy.mapping.MapDocument(load_base)
# Set the workspace
arcpy.env.workspace = out_shp_worksp
# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# Declare Variable of Location to Save Shapefile
save_ly = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project/brg.shp'
# create a new layer
newlayer = arcpy.mapping.Layer(save_ly)
# 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()
sql_1 = """ "brg" = 'Brooklyn Bridge' OR "brg" = 'Manhattan Bridge' OR "brg" = 'Williamsburg Bridge' OR "brg" = 'Ed Koch Queensboro Bridge' """
sql_2 = """ "brg" = 'George Washington Bridge' OR "brg" = 'Lincoln Tunnel' OR "brg" = 'Holland Tunnel' """
sql_3 = """ "brg" = 'Bayonne Bridge' OR "brg" = 'Goethals Bridge' OR "brg" = 'Outerbridge Crossing Bridge' """
SQL_List = [sql_1, sql_2, sql_3]
for x in SQL_List:
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez projectexpmap1.png'
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
python for-loop arcpy
python for-loop arcpy
edited Nov 21 '18 at 19:58
dustinos3
4801818
4801818
asked Nov 21 '18 at 18:25
hector.h2913hector.h2913
21
21
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you need to change the name of the export to coincide with the sql statement, otherwise you will just overwrite the initial output.
number = 1
for x in SQL_List:
output = 'example{}.png'.format(number) # this will add the number to the file name
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project{}'.format(output)
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
number += 1 # this will increment the output number for the filename
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
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%2f53418385%2fhow-to-get-three-exported-png-files-using-the-for-loop-in-the-code-below%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 you need to change the name of the export to coincide with the sql statement, otherwise you will just overwrite the initial output.
number = 1
for x in SQL_List:
output = 'example{}.png'.format(number) # this will add the number to the file name
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project{}'.format(output)
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
number += 1 # this will increment the output number for the filename
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
add a comment |
I think you need to change the name of the export to coincide with the sql statement, otherwise you will just overwrite the initial output.
number = 1
for x in SQL_List:
output = 'example{}.png'.format(number) # this will add the number to the file name
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project{}'.format(output)
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
number += 1 # this will increment the output number for the filename
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
add a comment |
I think you need to change the name of the export to coincide with the sql statement, otherwise you will just overwrite the initial output.
number = 1
for x in SQL_List:
output = 'example{}.png'.format(number) # this will add the number to the file name
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project{}'.format(output)
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
number += 1 # this will increment the output number for the filename
I think you need to change the name of the export to coincide with the sql statement, otherwise you will just overwrite the initial output.
number = 1
for x in SQL_List:
output = 'example{}.png'.format(number) # this will add the number to the file name
# Select Features & Zoom
nycbrg = arcpy.mapping.ListLayers(mxd)[0]
arcpy.SelectLayerByAttribute_management(nycbrg, "NEW_SELECTION", x)
df.zoomToSelectedFeatures()
mxd.save()
# Declare Variable of Where to Save Map Export
Make_Export = 'C:UsersHector HernandezDocumentsGitHubpratt-savi-810-2018-10projectshhernandez project{}'.format(output)
# Export Map
arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
number += 1 # this will increment the output number for the filename
edited Nov 26 '18 at 14:43
answered Nov 21 '18 at 18:35
HotpepperHotpepper
23618
23618
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
add a comment |
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
thank you @Hotpepper. I got an error message that number variable was not defined. so i defined it as number = 1. the code worked but still just gave me the export of sql_3 (last map export) and not the first two. any other thoughts?
– hector.h2913
Nov 22 '18 at 16:01
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%2f53418385%2fhow-to-get-three-exported-png-files-using-the-for-loop-in-the-code-below%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