前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >高质量编码-物探Excel转换为shp文件

高质量编码-物探Excel转换为shp文件

原创
作者头像
MiaoGIS
修改2020-11-30 18:04:43
1.1K0
修改2020-11-30 18:04:43
举报
文章被收录于专栏:Python in AI-IOTPython in AI-IOT

城市地下管网如给水、排水、燃气、电力、通讯、热力等管线,通过物探手段普查,最后形成GIS系统进行综合管理。

有时候物探数据保存在Excel表中。

比如:下面对给水管线进行物探的结果存在两张表里,一个表示物探点表,一个是起点号和终点号对应物探点表中记录的管线表。

我们使用Python来完成这项工作,使用xlrd库读取Excel,使用shapefile库生成shapefile文件。效果如下:

Python代码如下:

# -*- coding:utf-8 -*-
import xlrd
import shapefile

def genPoint(excelPath,shpPath="point",idPointField="物探点号",xField="X坐标",yField="Y坐标",zField=None,mField=None,mode=0):
    pointExcel=xlrd.open_workbook(excelPath)
    pointSheet=pointExcel.sheet_by_index(0)

    #shp=shapefile.Writer(shapefile.POINT) #2.7
    shp=shapefile.Writer(shpPath,shapefile.POINTZ)
    fields=list(map(lambda x:x.value,pointSheet.row(0)))
    if xField not in fields or yField not in fields or idPointField not in fields:
        return
    
    xIndex=fields.index(xField)
    yIndex=fields.index(yField)
    hasZ=False
    hasM=False
    if zField in fields:
        hasZ=True
        zIndex=fields.index(zField)
    if mField in fields:
        hasM=True
        mIndex=fields.index(mField)
    hasM=mField in fields
    includeFields=[xField,yField,idPointField]
    includeIndexs=list(map(lambda x:fields.index(x),includeFields))

    resultFields=fields if mode==1 else ["x","y","num"]
    
    for field in resultFields:
        shp.field(field)

    for i in range(1,pointSheet.nrows):
        #shp.pointz(pointSheet.row(i)[xIndex].value,pointSheet.row(i)[yIndex].value,pointSheet.row(i)[zIndex].value if hasZ else 0,pointSheet.row(i)[mIndex].value if hasM else 0) #2.7
        shp.pointz(pointSheet.row(i)[xIndex].value,pointSheet.row(i)[yIndex].value,pointSheet.row(i)[zIndex].value if hasZ else 0,pointSheet.row(i)[mIndex].value if hasM else 0)
        if(mode==1):
            shp.record(*list(map(lambda x:x.value if x.value!='' else '',pointSheet.row(i))))
        else:
            shp.record(*list(map(lambda x:pointSheet.row(i)[x].value,includeIndexs)))
    #shp.save(shpPath) #2.7
    shp.close()

def genLine(pointExcelPath,lineExcelPath,shpPath="line",idPointField="物探点号",idLineField="管线编号",idLineField1="起点号",idLineField2="终点号",xField="X坐标",yField="Y坐标",mode=0):
    
    pointExcel=xlrd.open_workbook(pointExcelPath)
    pointSheet=pointExcel.sheet_by_index(0)
    lineExcel=xlrd.open_workbook(lineExcelPath)
    lineSheet=lineExcel.sheet_by_index(0)
    #shp=shapefile.Writer(shapefile.POLYLINE) #2.7
    shp=shapefile.Writer(shpPath,shapefile.POLYLINE)
    pointFields=list(map(lambda x:x.value,pointSheet.row(0)))
    lineFields=list(map(lambda x:x.value,lineSheet.row(0)))
    if xField not in pointFields or yField not in pointFields  or idPointField not in pointFields:
        return
    if idLineField1 not in lineFields or idLineField2 not in lineFields  or idLineField not in lineFields:
        return 
    xIndex=pointFields.index(xField)
    yIndex=pointFields.index(yField)
    idIndex=pointFields.index(idPointField)
    idLineIndex1=lineFields.index(idLineField1 )
    idLineIndex2=lineFields.index(idLineField2)
    dictPoints={}
    for i in range(1,pointSheet.nrows):
        dictPoints[pointSheet.row(i)[idIndex].value]=[pointSheet.row(i)[xIndex].value,pointSheet.row(i)[yIndex].value]

    includeFields=[idLineField,idLineField1,idLineField2]
    includeIndexs=list(map(lambda x:lineFields.index(x),includeFields))
    resultFields=lineFields if mode==1 else ["num","from","to"]
    
    for field in resultFields:
        shp.field(field)
    for i in range(1,lineSheet.nrows):
        idValue1=lineSheet.row(i)[idLineIndex1].value
        idValue2=lineSheet.row(i)[idLineIndex2].value
        if dictPoints.get(idValue1)!=None and dictPoints.get(idValue2)!=None:
            #shp.poly(parts=[[[dictPoints.get(idValue1)[0],dictPoints.get(idValue1)[1]],[dictPoints.get(idValue2)[0],dictPoints.get(idValue2)[1]]]],shapeType=shapefile.POLYLINE) #2.7
            shp.line([[[dictPoints.get(idValue1)[0],dictPoints.get(idValue1)[1]],[dictPoints.get(idValue2)[0],dictPoints.get(idValue2)[1]]]])
            
            if(mode==1):
                shp.record(*list(map(lambda x:x.value if x.value!='' else '',lineSheet.row(i))))
            else:
                shp.record(*list(map(lambda x:lineSheet.row(i)[x].value,includeIndexs)))
    #shp.save(shpPath) #2.7
    shp.close()
if __name__=='__main__':
    genPoint('D:/JS_POINT.xlsx',shpPath='D:/point',idPointField="物探点号",xField="Y坐标",yField="X坐标",mode=1)
    genLine('D:/JS_POINT.xlsx','D:/JS_LINE.xlsx',shpPath='D:/line',idPointField="物探点号",idLineField="管线编号",idLineField1="起点号",idLineField2="终点号",xField="Y坐标",yField="X坐标",mode=1)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
物联网
腾讯连连是腾讯云物联网全新商业品牌,它涵盖一站式物联网平台 IoT Explorer,连连官方微信小程序和配套的小程序 SDK、插件和开源 App,并整合腾讯云内优势产品能力,如大数据、音视频、AI等。同时,它打通腾讯系 C 端内容资源,如QQ音乐、微信支付、微保、微众银行、医疗健康等生态应用入口。提供覆盖“云-管-边-端”的物联网基础设施,面向“消费物联”和 “产业物联”两大赛道提供全方位的物联网产品和解决方案,助力企业高效实现数字化转型。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档