2010-07-05 115 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 
相关问题