我有一个表格,我想加入ArcGIS shapefile。我的问题是该表有两个Identity字段(即“Plan Number”和“Contract Number”),shapefile有一个Identity字段(即“Name”)。我想将shapefile的“名称”加入“计划编号”或“合同编号”。
作为背景,shapefile是通过在ArcGIS中手动绘制多边形来创建的。这些多边形代表了各种项目。标识符“名称”可以是项目的初始计划编号,也可以是项目预算后存在的合同编号。没有预算时存在计划编号,合同编号稍后会出现。创建多边形并在“名称”字段中填写项目已达到的任何识别阶段(计划编号或合同编号)。因此,shapefile字段“名称”包含计划编号或合同编号。
同时,我们有一个包含规划编号和合同编号的两个字段的所有项目的复杂数据库:
PLN ------------合同-----相------------长度----- NTP -------- -SC -------------注释
1415-003 ----- WD-2506 ----预规划---- 45 ---------- 1/1/1900 ---- 1900年1月20日-----测试
为了创建我的代码,我创建了一个链接到数据库的简单xml表。该xml表具有PLN(计划编号)字段和合同(合同编号)字段。在我的代码中,我将此xml转换为dbf。我现在正试图找到一种方法将Shapefile“Name”加入到“PLN”或“Contract”中。
请参阅以下代码:
#Convert xlsx to table:
import xlrd
in_excel= r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\JoinTest.xlsx'
out_table= r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\JoinTest.gdb'
# Perform the conversion
join_table= arcpy.ExcelToTable_conversion(in_excel, out_table)
print join_table
# Join
# Set the local parameters
inFeatures = r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\CDDprojects.shp'
joinField =
joinTable = join_table
fieldList = ["PLN", "Contract", "Phase", "Length", "NTP", "SC", "Notes]
我不确定在joinField中输入什么,如果还有其他代码我应该包含。
修订1: 我使用了Ethan的代码但收到了错误消息:
with master_table.open():
with minimal_table.open():
minimal_index = dbf.create_index(minimal_table, lambda record: record.name)
错误如下:
Traceback (most recent call last):
File "W:\Engineering\ENGINEER\LAMP (062012)\Database\VisualDatabase\LAMP.py", line 53, in <module>
with master_table.open():
AttributeError: 'Result' object has no attribute 'open'
修订2: 我是初学者,所以也许我错过了一些相当简单的东西。当我尝试导入dbf时,我的代码后收到错误:
Traceback (most recent call last):
File "W:\Engineering\ENGINEER\LAMP (062012)\Database\VisualDatabase\LAMP.py", line 50, in <module>
import dbf
ImportError: No module named dbf
我下载了dbf模块,但在运行安装程序时,收到此错误:
Warning (from warnings module):
File "C:\Python27\ArcGIS10.3\lib\distutils\dist.py", line 267
warnings.warn(msg)
UserWarning: Unknown distribution option: 'install_requires'
我不确定安装dbf我做错了什么。
修订3: 我已经安装了dbf模块,并且已成功导入到arcpy中。但是,我仍然收到相同的错误消息:
Traceback (most recent call last):
File "W:\Engineering\ENGINEER\LAMP (062012)\Database\VisualDatabase\LAMP.py", line 56, in <module>
with master_table.open():
AttributeError: 'Result' object has no attribute 'open'
我的代码是:
#Convert xlsx to table:
import xlrd
in_excel= r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\JoinTest.xlsx'
out_table= r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\JoinTest.gdb'
# Perform the conversion
join_table= arcpy.ExcelToTable_conversion(in_excel, out_table)
import enum
import dbf
# table with all projects at all stages
master_table = join_table
# table with single project and most up-to-date stage
minimal_table = r'W:\\Engineering\\ENGINEER\\LAMP (062012)\\Database\\VisualDatabase\\Planning_Out\\CDDprojects.dbf'
with master_table.open(): (LINE 56 which the AttributeError calls)
with minimal_table.open():
minimal_index = dbf.create_index(minimal_table, lambda record: record.name)
# cycle through master, updating minimal if necessary
for master in master_table:
# look for PLN # first
found = minimal_index.search(master.PLN)
if not found:
# if record doesn't exist with PLN #, try CONTRACT #
found = minimal_index.search(master.Contract)
我在这里使用dbf模块:https://pypi.python.org/pypi/dbf
感谢。
答案 0 :(得分:0)
我还没有使用过arcpy(而且我并不完全确定我明白你要做什么),但是使用my dbf
module这就是你要做的更新/添加主表到形状表的dbf文件:
import dbf
# table with all projects at all stages
master_table = dbf.Table(complex_table)
# table with single project and most up-to-date stage
minimal_table = dbf.Table(single_project_table)
with master_table.open():
with minimal_table.open():
minimal_index = dbf.create_index(minimal_table, lambda record: record.name)
# cycle through master, updating minimal if necessary
for master in master_table:
# look for PLN # first
found = minimal_index.search(master.pln)
if not found:
# if record doesn't exist with PLN #, try CONTRACT #
found = minimal_index.search(master.contract)
if not found:
# not there at all, add it
minimal_table.append(master.contract or master.pln, master.phase, master.length, ...)
break
# have a match, update it
found.name = master.contract or master.pln
# plus any other updates you need
# ...
# and then write the record
dbf.write(found)