2016-05-19 48 views
0

我有一張我想加入ArcGIS shapefile的表。我的問題是該表具有兩個標識字段(即「計劃編號」和「合同編號」),並且該shapefile具有一個標識字段(即「名稱」)。我想將shapefile的「名稱」加入「計劃編號」或「合同編號」。將dbf表中的兩個字段之一加入到單個ArcGIS shapefile字段中 - Python

作爲背景,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。

REVISION 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

AttributeError引用一個Result對象在我的'dbf'包中沒有這樣的東西 - 是否有其他一些'dbf'被使用而不是我的? –

+0

你是對的。但是,我遇到了「import dbf」命令的問題。請參閱修訂後的問題。 –

+0

@Ethan導入dbf後,我仍然收到引用'Result'對象的相同AttributeError。我導入xlrd將我的Excel錶轉換爲dbf表... xlrd可能與dbf模塊衝突嗎? –

回答

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) 
+0

謝謝Ethan。請參閱修訂後的問題並附上錯誤消息 –

相關問題