- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试清理我的脚本,这样我就不必每次使用它时都更改变量。
我有美国每个州(加上华盛顿特区和波多黎各)的 arcgis 表。我想迭代文件夹中的这些表,一次将每个表连接到一个 shapefile,将连接的要素复制到不同地理数据库中的新要素类,在相应的州名称之后命名该要素类,然后删除联接并继续到下一个状态。
当谈到Python时,我认为自己是一个新手。多年来我一直在尝试自学,但一直没有很好的机会参加深入的类(class),而且与我一起工作的人也没有对此非常了解。我知道有更有效的脚本编写方法,例如循环、函数和条件语句,但我不知道如何正确设置它们。
因此,我创建了一个脚本来完成我今天需要完成的工作,但我想让该脚本更加动态。我不想更改每个表名称或新要素类名称。我试图查找如何为部分代码创建自定义函数,但同时也有一个循环,以便它知道迭代文件夹中的每个表。我不确定是否需要先有循环,然后是函数,或者在函数内部有循环。然后我不确定如何获取输出要素类的正确名称,我知道有一种方法可以用 %s 引用动态变量,但不确定如何将其合并到这里。
layer = arcpy.GetParameterAsText(0)
inField = "GEOID"
jTable = r'k:\geospatial\data\census\national\census_fact_finder_data\census_tract_year_built\aa_by_state\xls_pcts\tables'
jField = "GEOID"
outFC = r'K:\GEOSPATIAL\DATA\Census\National\Census_Fact_Finder_Data\Shapefiles\CFF_Census_Tracts\PCTs\FCC_CT_YB_PCT.gdb'
arcpy.AddMessage("Processing Arizona...")
#join table to census tract layer
arcpy.AddMessage("Joining Arizona table to Census Tracts...")
tract_join = arcpy.AddJoin_management(layer, inField,jTable + "\\az_pcts", jField, "KEEP_COMMON")
#Copy joined features to new feature class in geodatabase
arcpy.AddMessage("Exporting joined features to FCC_CT_YB_PCT geodatabase...")
arcpy.CopyFeatures_management(tract_join, outFC + "\Arizona_PCT")
#remove all joins
arcpy.AddMessage("Removing joins to process next table...")
arcpy.RemoveJoin_management(layer)
arcpy.AddMessage("Arizona Complete")
因此,在上面的示例中,它将亚利桑那州表 (az_pcts
) 连接到人口普查区图层 (layer = arcpy.GetParameterAsText(0)
),复制连接的数据添加到新数据库的功能并将其命名为 Arizona_PCT
,然后删除连接并继续处理下一个表。我为每个状态表重复了同样的结构,并将所有路径结尾更改为我想要的内容。如果有人有任何建议,甚至是片段,我们将不胜感激。
最佳答案
首先,欢迎来到 Stack Overflow。您可以使用模型构建器完成所有这些操作。看: What is model builder and quick tutorial
但是我不明白你在文件夹中找到了什么样的表?通常,您需要在文件地理数据库、个人地理数据库或 RDBMS 表中指定文件夹,而不是文件夹。但我跳过了这个问题,并在上面为您提供了示例代码。
无论如何,我为你准备了一些代码。您可以关注:
import arcpy
# iterating all tables in an environment , and make join them with a shapefile
# these are constant variables
shapefilepath = r"c:\users\someplace\someshape.shp"
commoncolumn = "SAMECOLUMN" # this column must be same in other shapefiles too
# If all shapefile samecolumns are different each other, you need to make a list like this
commoncolumns_ordered = ['SAMECOLUMN1', 'SAMECOLUMN2', ] # .. goes away
mainfolder = r"c:\users\someplace"
tablegdb = r"c:\users\someplace\somegdb.gdb" # we'll search our tables here
arcpy.env.workspace = tablegdb # we will work on here
mytables = arcpy.ListTables("*") # you can filter your tables as starting or ending with a letter.
for table in mytables:
# you need to make view from all tables
name = arcpy.Describe(table).name.encode('utf-8') # my table name
table_view = arcpy.MakeTableView_management(table, name)
# ok so we have our view. Otherwise, we would not be able to use this as an input for add join tool
"""
There are couple differences between add join and join field tools. Read them:
Add join help : https://pro.arcgis.com/en/pro-app/tool-reference/data-management/add-join.htm
Join field help : https://pro.arcgis.com/en/pro-app/tool-reference/data-management/join-field.htm
* We don't have to make table view if we use join field
"""
# i assume that both common columns, # fields are same.
out_join = arcpy.AddJoin_management(table_view, commoncolumn, shapefilepath, commoncolumn)
# extracting them is not useful. But I'll write it down:
arcpy.Copy_management(out_join, out_data="%s\\%s" % (out_gdb, name))
# some notes:
# If your samecolumn fields are different between each other in tables
# you need to iterate them like this:
for table, column in zip(mytables, commoncolumns_ordered):
print (table)
print (column)
# do others
关于python - Arcpy 脚本循环 - 如何循环遍历文件夹中的表并对每个表执行 arcpy join 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55504614/
我正在尝试清理我的脚本,这样我就不必每次使用它时都更改变量。 我有美国每个州(加上华盛顿特区和波多黎各)的 arcgis 表。我想迭代文件夹中的这些表,一次将每个表连接到一个 shapefile,将连
我正在编写一个程序,它接受使用 pgsql2shp.exe 导出的两个形状文件的输入,并在它们之间进行交集。这是我的代码 print sys.argv[1], sys.argv[2] intersec
我正在尝试获取要素类的数据库路径,该要素类可能在也可能不在要素数据集中。我正在使用要素类的 os.path.dirname。如果要素类不在要素数据集中(很好),这将为我提供数据库路径,但如果要素类在要
我正在尝试检查 arcpy 中是否提供三个扩展。如何返回列出可用扩展的声明? import arcpy if arcpy.CheckExtension("3D, Network, Spatial")
R 包 sf 在“几何二元谓词”标题下有一组令人惊叹的函数,详细描述了 here . 如链接中所述,如果仅提供一个 sf 对象,则这些函数将递归地应用于同一数据集中的所有几何图形(参见下面的示例) p
我希望能够获取一个包含每个要素类记录和一些元数据字段(如摘要、描述等)的 Excel 文件,并将其转换为要素类元数据。根据我所做的研究,我似乎需要将 Excel 表中的每条记录转换为 xml,然后从那
我正在尝试使用更新游标来替换字段中的缩写(例如:ST 到 STREET,AVE 到 AVENUE 等),但似乎无法让它工作。我已经尝试了每一种排列,但显然遗漏了一些东西。代码运行但未对该字段进行任何更
所以我试图编写一个脚本,其中包含许多用户定义的变量。我已经到了最后一步,但似乎无法让它正确溶解。 目的:该脚本应该让我定义一个 shapefile/layer 文件,缓冲区要使用的距离,创建缓冲区然后
我在属性表中有一个文本字段,如下所示: NAME_ID Smith-123-456 Stewart-13-0931-2312 Brown-47 Jack-678-987-3-A 我创建了一个新的文本字
有没有办法在 python 中使用新添加的图层刷新 ArcMap (.mxd) 文档而不保存文档? 我已经使用Python完成了一系列任务的自动化,最后一步是将输出转储到arcmap文档中并弹出该文档
我正在开发一种工具,可以识别功能并使用现有工具修复它们。然而,目前我正在努力将我的想法转化为代码。目的是使用“按位置选择要素”图层中的搜索光标来识别并选择原始数据中的要素并执行另一个工具。截至目前,我
我已经创建了 python 脚本工具,并且能够通过提供如下要素类来选择图层。 import arcpy arcpy.env.workspace = "C:/project/pmms.gdb" arcp
我正在尝试对同一脚本中的多个工作空间使用 arcpy.ListFeatureClasses()。查看其他示例脚本,我想我可以简单地将工作空间分配给一个变量,然后在 arcpy.ListFeatureC
我希望使用 reticulate 在 R 中导入 arcpy 库。 我尝试在 R 中运行此代码: library(reticulate) use_python("C:/Python27/ArcGIS1
我遇到了 ArcPy 和 Python 编码之间的奇怪行为。我使用安装了用于 VS (PTVS) 的 Python 工具的 VisualStudio 2010 Shell。我通过一个简单的脚本文件隔离
作为毕业论文的结果,我正在尝试使用 Python 脚本为 ArcGIS 创建一个新的工具箱。问题是我被我的代码困住了,因为 Arcpy 中的现有函数都没有做我需要做的事情。所以我的问题是,是否可以在
不确定我是否在正确的位置发帖,但我在让我的 python GIS 程序在 Windows 7 64 位上运行时遇到了问题。这些程序适用于 XP 32 位。我做了很多研究并尝试更改我的 PythonPa
环境:Win 7 企业版,SP1 | ArcGIS Desktop 10.0(Python 2.6.5) 我正在尝试将 Arcpy Polyline 对象作为值存储在 Python 字典中,以便可以在
我正在将一些旧工具和模型从 9.3 更新到 10.3,并在尝试从网络分析图层保存特定图层时遇到问题。 下面是我的脚本的副本及其生成的错误: arcpy.CheckOutExtension("Netwo
ogr2ogr 或 arcpy 可以直接将 csv 转换为 shapefile 吗? 我正在尝试使用一个小脚本自动化一些流程,并希望我可以使用我不熟悉的 ogr2ogr 或 arcpy 轻松完成。 任
我是一名优秀的程序员,十分优秀!