【问题标题】:How can I convert an excel spreadsheet (.xls) to a shapefile programmatically?如何以编程方式将 Excel 电子表格 (.xls) 转换为 shapefile?
【发布时间】:2010-07-05 07:38:05
【问题描述】:

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

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

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

【问题讨论】:

    标签: python excel gis shapefile


    【解决方案1】:

    这样的?

    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 是什么意思。导入时出错。
    【解决方案2】:

    这里有一个关于使用 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。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我不想走 CSV 路线,因为我想维护 excel 格式字符串表示的数据类型。
        【解决方案5】:

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多