代码之家  ›  专栏  ›  技术社区  ›  fmark

如何以编程方式将excel电子表格(.xls)转换为形状文件?

  •  5
  • fmark  · 技术社区  · 16 年前

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

    5 回复  |  直到 16 年前
        1
  •  5
  •   ebt    16 年前

    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')
    
        2
  •  4
  •   geographika    16 年前

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

    您只需要用Excel文件中的点替换源数据——正如Fabian指出的,有一些库可以读取Excel文件(或者将其保存为DBF)。

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

        3
  •  2
  •   John Machin Santi    16 年前
        4
  •  1
  •   sgillies    16 年前

    您可能希望GDAL/OGR libs使用Python来实现这一点,并且在安装了这些库之后,只需使用 ogr2ogr 效用说明见 http://nautilus.baruch.sc.edu/twiki_dmcc/bin/view/Main/OGR_example#Converting_from_CSV_to_shapefile .

        5
  •  0
  •   shikanon    11 年前

     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