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

2020-11-30 18:04:43 浏览数 (2)

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

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

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

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

Python代码如下:

代码语言:python代码运行次数:0复制
# -*- 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)

0 人点赞