我该如何通过程序将Excel电子表格(.xls)转换为shapefile文件?

5
我有一个Excel电子表格,想要以程序化的方式将其转换为ESRI shapefile。其中包含两列X和Y坐标,以及其他列中的各种属性数据。该电子表格采用Excel 97格式(即不是.xlsx格式)。
我想将其转换为点几何图形shapefile,每行的x,y对表示一个点。理想情况下,我想要第三列指定x,y坐标对的坐标系,并且让Excel文件包含异构坐标系。
如何以编程方式将此Excel电子表格(.xls)转换为shapefile?最好使用Python,但其他实现也将被接受。
5个回答

5

something like this?

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

这个 point_store 是什么意思?在导入时我遇到了一个错误。 - Tek Kshetri

4

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

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

你只需要用Excel文件中的点替换源数据 - 正如Fabian所指出的,有库可以读取Excel文件(或将其保存为DBF格式)。

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


2

xlrd 是一个用于读取Excel文件的Python模块,虽然我自己没有使用过。


1

我宁愿不选择CSV的方式,因为我希望保持Excel格式字符串所表达的数据类型。 - fmark

0

Arcmap支持名为arcpy的Python库。正如我们所知,Pandas的工作方式类似于Excel,可以轻松地读取和处理数据。是的,有时它也可以用来导出.xls和.xlsx文件。我编写了一个函数,用于将Pandas的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['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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接