• 将excel表格或csv转换为Shapefile文件


    读取csv转为shp

    构造读取csv函数

    def read_csv(fp):
        ret = []
        with open(fp, 'rb') as f:
            for line in f:
                ret.append(line.decode('utf-8').strip().split(","))
        return ret

    原始数据如下

    from _datetime import datetime
    import shapefile
    
    data = read_csv("test3.csv")
    
    #打开shp
    w=shapefile.Writer(shapefile.POINT)
    #shapefile文件要求”几何数据”与”属性数据”要有一一对应的关系,如果有”几何数据”而没有相应的属性值存在,那么在使用ArcGIS软件打开所创建的shapefile文件时会出错
    #为了避免这种情况的发生,可以设置 sf.autoBalance = 1,以确保每创建一个”几何数据”,该库会自动创建一个属性值(空的属性值)来进行对应。
    #autoBalance默认为0
    
    w.autoBalance = 1
    
    #增加属性字段 设置类型与长度
    w.field('id', 'N', 12)
    w.field('date', 'D')
    w.field('city', 'C', 100)
    w.field('location', 'C', 100)
    w.field('lng', 'F', 10, 5)
    w.field('lat', 'F', 10, 5)
    
    for r in data[1:]:  #从第二行开始
        record = [
            int(r[0]),
            datetime.strftime(datetime.strptime(r[1], '%d/%m/%Y'),'%Y%m%d'),#把日/月/年转为年月日格式
            r[2],
            r[3],
            float(r[4]),
            float(r[5])]
        w.record(*record)
        w.point(float(r[-2]), float(r[-1]))
    w.save("sites.shp")

    读取excel文件转为shp

    import xlrd
    import shapefile
    xls=xlrd.open_workbook("sites.xlsx")
    sheet=xls.sheet_by_index(0)
    #打开shp
    w=shapefile.Writer(shapefile.POINT)
    #shapefile文件要求”几何数据”与”属性数据”要有一一对应的关系,如果有”几何数据”而没有相应的属性值存在,那么在使用ArcGIS软件打开所创建的shapefile文件时会出错。
    #为了避免这种情况的发生,可以设置 sf.autoBalance = 1,以确保每创建一个”几何数据”,该库会自动创建一个属性值(空的属性值)来进行对应。
    #autoBalance默认为0。
    w.autoBalance = 1
    
    #将数据从excel移动到shp
    for i in range(sheet.ncols):#读取第一行表头信息 遍历第一行表头每一列
        w.field(str(sheet.cell(0,i).value),"C",40) #对每一列构造属性字段 字符类型
    for i in range(1, sheet.nrows):#从第二行开始遍历每一行
        values=[]
        for j in range(sheet.ncols):
            values.append(sheet.cell(i,j).value)
        w.record(*values)
        #以最后两列获取经纬度信息
        w.point(float(values[-2]),float(values[-1]))
    w.save("sites_.shp")

    注意

    在ArcGIS中打开属性表或许会出现乱码问题

     原因

    1.Arcgis本身问题

    2.Pyshp库不支持中文内容

    解决方案

    方案1:最简单的方法——分析是否还需要用到含有中文的字段,若后续不需要则可以删去相应中文字段。再用print(type(...))查看数据类型,你可以把所有的非str类型转换成str,不过更推荐根据数据类型设置你的字段属性

    代码改为

    import shapefile
    
    def read_csv(fp):
        ret = []
        with open(fp, 'rb') as f:
            for line in f:
                ret.append(line.decode('utf-8').strip().split(","))
        return ret
         
    data = read_csv("sites.csv")
    w=shapefile.Writer(shapefile.POINT)
    
    w.autoBalance = 1
    w.field('id', 'N', 12)
    w.field('lng', 'F', 10, 5)
    w.field('lat', 'F', 10, 5)
    
    for r in data[1:]:
        record = [
            int(r[0]),
            float(r[4]),
            float(r[5])]
        w.record(*record)
        w.point(float(r[-2]), float(r[-1]))
    w.save("sites.shp")

    方案2:(从根本解决)更改arcgis注册表并根据版本安装补丁+修改pyshp库中的shapefile.py
    STEP1:

    修改arcgis注册表并根据版本安装补丁(目前esri仅提供10.2.1与10.2.2版本的补丁)
    10.2.1与10.2.2版本:
    教程:https://blog.csdn.net/kikitaMoon/article/details/19116415
    10.3及以后:
      http://www.cnblogs.com/liweis/p/4629265.html
      https://blog.csdn.net/hailiannanhai/article/details/78099074

    STEP2:

    修改pyshp库中的shapefile.py(由于pycharm可以很方便修改编码格式,因此强烈建议用pycharm进行修改)
    Pycharm安装(Professional Edition)与使用教程:
    https://blog.csdn.net/qsir/article/details/79362549
    特别注意:在教程中的第11步:软件激活方式中,在IntelliJ IDEA 注册码网站,我们可以用学校的邮箱注册,即可免费获取激活码,使用专业版pycharm。不然只有30天试用期。不推荐用教程中的激活码。

    在Pycharm中,左上角点击file-setting-file encodings按下图修改,保证输出是utf格式:

     

     在Pycharm中,用英文输入修改pyshp中的shapefile.py(992-999行红框中内容):

  • 相关阅读:
    topcoder srm 320 div1
    topcoder srm 325 div1
    topcoder srm 330 div1
    topcoder srm 335 div1
    topcoder srm 340 div1
    topcoder srm 300 div1
    topcoder srm 305 div1
    topcoder srm 310 div1
    topcoder srm 315 div1
    如何统计iOS产品不同渠道的下载量?
  • 原文地址:https://www.cnblogs.com/icydengyw/p/11796054.html
Copyright © 2020-2023  润新知