2010-07-05 75 views
3

我有一個Excel電子表格,我想以編程方式轉換爲ESRI shapefile。它包含兩列中的X和Y座標,以及其他列中的各種屬性數據。電子表格採用excel 97格式(即不是.xlsx)。如何以編程方式將Excel電子表格(.xls)轉換爲shapefile?

我希望能夠將其轉換爲點幾何形狀文件,每行的x,y對代表一個點。理想情況下,我希望有第三列指定x,y座標對的座標系,並讓excel文件包含異構座標系。

如何將此excel電子表格(.xls)以編程方式轉換爲shapefile?最好在Python中,但其他實現將被接受。

回答

2

xlrd是一個用於讀取Excel文件的python模塊,我沒有用過它自己強悍。

4

有在這裏使用GDAL創建shape文件一個Python教程:

http://invisibleroads.com/tutorials/gdal-shapefile-points-save.html

你只需要從Excel文件中的點替換源數據 - 如法比安指出,有庫讀取Excel文件(或將其另存爲DBF)。

或者,如果您有ESRI的ArcMap,請將Excel另存爲DBF文件(我不記得ArcMap是否直接讀取Excel),然後使用X,Y字段將此DBF添加爲「事件層」點。 ArcMap將顯示這些特徵,然後您可以右鍵單擊並將圖層導出到shapefile。

5

這樣的事情?

import xlrd 
book = xlrd_open_workbook("data.xls") 
sheet = book.sheet_by_index(0) 
data = [] #make a data store 
for i in xrange(sheet.nrows): 
    row = sheet.row_values(i) 
    x=row[0] 
    y=row[1] 
    data.append(x,y) 

import point_store 
point_store.save('points-shifted.shp', [data], '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') 
0

Arcmap支持名爲arcpy的庫的Python。正如我們所知,熊貓的工作方式與Excel相似,可以輕鬆讀取和處理數據。是的,有時它可以用於導出到.xls和.xlsx文件。我編寫了熊貓DataFrame和Arcmap shp之間的相互轉換函數。它是這樣的:

def Shp2dataframe(path): 

    fields=arcpy.ListFields(path) 

    table=[] 

    fieldname=[field.name for field in fields] 

    data=arcpy.SearchCursor(path) 

    for row in data: 

     r=[] 

     for field in fields: 

      r.append(row.getValue(field.name)) 

     table.append(r) 

    return pd.DataFrame(table,columns=fieldname) 


'''Fuction: 

make the table of pandas's DataFrame convert to the shp of esri 

Input: 

df -- pandas DataFrame from the shp converted 

outpath -- the shp output path 

geometryType -- the type of geomentey, eg:'POINT','POLYLINE','POLYGON','MULTIPOINT' 

temple -- the temple, at most time it is used the DataFrame's shp 

''' 
def Dataframe2ShpTemplate(df,outpath,geoType,template): 
out_path = outpath.replace(outpath.split('/')[-1],'') 

out_name = outpath.split('/')[-1] 

geometry_type = geoType 

feature_class = arcpy.CreateFeatureclass_management(

    out_path, out_name, geometry_type, template) 


desc = arcpy.Describe(outpath) 

if template=='': 

    fields = set(list(df.columns)+['Shape','FID']) 

    originfieldnames = [field.name for field in desc.fields] 

    for fieldname in fields: 

     if fieldname not in originfieldnames: 

      arcpy.AddField_management(outpath,fieldname,'TEXT') 

for row in df.index: 

    df['[email protected]'] = df['Shape'] 

    cursor = arcpy.da.InsertCursor(outpath,[field for field in df.columns]) 

    cursor.insertRow([df[field][row] for field in df.columns]) 

print 'Pandas to shp finish!' 

del cursor 
相關問題