如何使用下面的代码中的for循环获取三个导出的png文件?

时间:2018-11-21 18:25:28

标签: python for-loop arcpy

下面的代码正在执行以下操作:

  1. 从csv绘制xy坐标
  2. 创建shapefile
  3. 在ArcMap中加载shapefile
  4. 标记地图特征
  5. 选择特定的地图特征
  6. 放大所选功能
  7. 导出PNG文件
  8. 为其他两组功能重复最后三个步骤

代码运行没有错误,但是我没有得到三个PNG文件,每组功能一个(在代码中定义为sql_1,sql_2,sql_3)。相反,我得到了我要导出的最后一组功能(sql_3)的一个导出的PNG文件

如何使用下面的代码中的for循环获取三个导出的png文件?

import pandas as pd
import arcpy

# Declare Variable for Location of csv File with Data
in_csv = 'C:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez 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:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez 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:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez project\ld2.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:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez 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:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez project\expmap1.png'
# Export Map
    arcpy.mapping.ExportToPNG(mxd, Make_Export, df)

1 个答案:

答案 0 :(得分:1)

我认为您需要更改导出名称以与sql语句一致,否则您将覆盖初始输出。

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:\Users\Hector Hernandez\Documents\GitHub\pratt-savi-810-2018-10\projects\hhernandez project\{}'.format(output)
    # Export Map
    arcpy.mapping.ExportToPNG(mxd, Make_Export, df)
    number += 1 # this will increment the output number for the filename