5

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

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

如何以编程方式将此 Excel 电子表格 (.xls) 转换为 shapefile?最好使用 Python,但也可以接受其他实现。

4

5 回答 5

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')
于 2010-07-05T08:06:34.833 回答
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。

于 2010-07-05T07:57:56.407 回答
2

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

于 2010-07-05T07:52:09.690 回答
1

您可能希望 GDAL/OGR 库使用 Python 执行此操作,并且在安装这些库之后,使用ogr2ogr实用程序会更容易,如http://nautilus.baruch.sc.edu/twiki_dmcc/bin/view/中所述主要/OGR_example#Converting_from_CSV_to_shapefile

于 2010-07-05T09:07:43.900 回答
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
于 2015-06-22T17:12:45.537 回答