How to get three exported png files using the for loop in the code below?












-1















the code below is performing the following actions:




  1. plotting xy coordinates from csv

  2. creating shapefile

  3. loading shapefile in ArcMap

  4. labeling map features

  5. Selecting specific map features

  6. Zooming in on selected features

  7. Exporting a PNG file

  8. 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)









share|improve this question





























    -1















    the code below is performing the following actions:




    1. plotting xy coordinates from csv

    2. creating shapefile

    3. loading shapefile in ArcMap

    4. labeling map features

    5. Selecting specific map features

    6. Zooming in on selected features

    7. Exporting a PNG file

    8. 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)









    share|improve this question



























      -1












      -1








      -1








      the code below is performing the following actions:




      1. plotting xy coordinates from csv

      2. creating shapefile

      3. loading shapefile in ArcMap

      4. labeling map features

      5. Selecting specific map features

      6. Zooming in on selected features

      7. Exporting a PNG file

      8. 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)









      share|improve this question
















      the code below is performing the following actions:




      1. plotting xy coordinates from csv

      2. creating shapefile

      3. loading shapefile in ArcMap

      4. labeling map features

      5. Selecting specific map features

      6. Zooming in on selected features

      7. Exporting a PNG file

      8. 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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 19:58









      dustinos3

      4801818




      4801818










      asked Nov 21 '18 at 18:25









      hector.h2913hector.h2913

      21




      21
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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





          share|improve this answer


























          • 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














          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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





          share|improve this answer


























          • 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


















          1














          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





          share|improve this answer


























          • 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
















          1












          1








          1







          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





          share|improve this answer















          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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





















          • 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






















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)