我怎样才能将excel电子表格(.xls)以编程方式转换为shapefile?

我有一个Excel电子表格,我想以编程方式转换为ESRI shapefile文件。 它包含两列中的X和Y坐标,以及其他列中的各种属性数据。 电子表格是Excel 97格式(即不是.xlsx)。

我想能够将其转换为点几何形状文件,每行的x,y对代表一个点。 理想情况下,我希望有第三列指定x,y坐标对的坐标系,并使excel文件包含异构坐标系。

我怎样才能将这个excel电子表格(.xls)以编程方式转换成shapefile? 最好在Python中,但其他的实现将被接受。

像这样的东西?

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') 

这里有一个使用GDAL创buildshapefile的Python教程:

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

您只需要用Excel文件中的点replace源数据,正如Fabian指出的,有库可以读取Excel文件(或将其另存为DBF)。

或者,如果您有ESRI的ArcMap,请将Excel保存为DBF文件(我不记得ArcMap是否直接读取Excel),然后使用X,Y字段将此DBF添加为“事件层”来表示点。 ArcMap将显示这些特征,然后可以右键单击并将图层导出到shapefile。

xlrd是一个用于读取Excel文件的python模块,我自己也没用过。

您可能希望GDAL / OGR库使用Python来完成,安装完成后,使用ogr2ogr工具更容易,如http://nautilus.baruch.sc.edu/twiki_dmcc/bin/view/ Main / OGR_example#Converting_from_CSV_to_shapefile 。

Arcmap支持名为arcpy的库的Python。 正如我们所知,pandas的工作方式与Excel相似,可以轻松读取和处理数据。 是的,有时可以用于导出到.xls和.xlsx文件。 我编写了一个pandasDataFrame和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['SHAPE@'] = 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