网络分析

#def class
class Bird:
    fly='Whirring'
    def __init__(self):
        self.hungry=True
    def eat(self):
        if self.hungry:
            print('Aaaah...')
            self.hungry=False
        else:
            print('No,Thanks!')
class Apodidae(Bird):
    def __init__(self):
        super(Apodidae,self).__init__()
        self.sound='Squawk!'
    def sing(self):
        print(self.sound)


#读取KML中的路径数据
txtfile=''
prjFile='WGS 1984.prj'
def KMLRouteLoad(txtfile,prjFile):
    import re,arcpy,os,codecs
    from arcpy import env
    workspace=os.path.dirname(txtfile)
    env.workspace=workspace
    tf=os.path.basename(txtfile)
    reader=codecs.open(txtfile,'r','utf-8')
    inputname=tf.split('.')[0]
    patA=re.compile('<name>.*</name>')
    patB=re.compile('<coordinates>')
    ncdic={}
    while True:
        line=reader.readline()
        if len(line)==0:
            break
        line=line.strip()
        if line=='<Placemark>':
            while True:
                line=reader.readline()
                line=line.strip()
                if line=='</Placemark>':
                    break
                m=pat.match(line)
                if m:
                    names=re.sub('<name>(.*?)</name>',r'\1',line)
                n=patB.match(line)
                if n:
                    line=reader.readline()
                    line=line.strip()
                    coordis=line.split(' ')
                    coordislst=[]
                    for s in coordis:
                        xyz=s.split(',')
                        xyz=[float(a) for a in xyz]
                        coordislst.append(xyz)
            ncdic[names]=coordislst
    reader.close()
    arcpy.CreateFeatureclass_management(workspace,inputname,'POLYLINE','','','',prjFile)
    fieldName1='Name'
    arcpy.AddField_management(inputname,fieldName1,'TEXT')
    keys=ncdic.keys()
    cursor=arcpy.da.InsertCursor(inputname,['SHAPE@','Name'])
    for key in keys:
       dic=ncdic[key]
       pointA=arcpy.Array([arcpy.Point(*coords) for coords in dic])
       pl=arcpy.Polyline(pointA,prjFile)
       cursor.insertRow([pl,key])
    del cursor
    return inputname,ncdic
pg,nd=KMLRouteLoad(txtfile,prjFile)

#创建文件数据库
wspath=''
def CreateFGB(wspath):
    import arcpy,os
    from arcpy import env
    env.overwriteOutput=True
    env.workspace=wspath
    fgdbname=wspath.split('\\')[-1]
    if arcpy.Exists(fgdbname+'.gdb'):
        print(fgdbname+'.gdb'+'already exists!')
    else:
        fgb=arcpy.CreateFileGDB_management(wspath,fgdbname)
        return fgb
fgd=CreateFGB(wspath)

#在文件数据库创建要素集
wspath=''
prjFile=''#投影参考,遥感数据文件
fd='NetworkDataset'
def CreateFD(wspath,prjFile,fd):
    import arcpy,os
    from arcpy import env
    env.overwriteOutput=True
    env.workspace=wspath
    if arcpy.Exists(fd):
        print(fd+'dataset already exists!')
    else:
        fd=arcpy.CreateFeatureDataset_management(wspath,fd,prjFile)
        return fd
fd=CreateFD(wspath,prjFile,fd)

#源数据导入数据集
wspath=''#文件库数据集
copyfc=''#源数据要素
def copyfctofd(wspath,copyfc):
    import arcpy,os
    from arcpy import env
    env.workspace=wspath
    basen=os.path.basename(copyfc)[:-4]
    if arcpy.Exists(basen+'_copy'):
        print(basen+'already exists!')
    else:
        fccopy=arcpy.FeatureClassToFeatureClass_conversion(copyfc,wspath,basen+'_copy')
        return fccopy
fcc=copyfctofd(wspath,copyfc)

#最近设施点分析
#在ARCGIS里面打断路径交叉点
import arcpy
from arcpy import env
try:
    wspath=''
    env.workspace=wspath
    env.overwriteOutput=True
    inNetworkDataset='NetworkDataset_ND'#指定网络数据集
    outNALayerName='ClosestFacilities'#指定输出图层名称
    impedanceAttribute='Length'#指定阻抗属性
    accumulateAttributeName=['Length']#指定累积的成本属性列表
    inFacilities='Facilities_copy'#指定设施点文件
    inIncidents='Source_copy'#指定事件点文件
    outLayerFile=outNALayerName+'.lyr'#输出图层名称
    outNALayer=arcpy.na.MakeClosestFacilityLayer(inNetworkDataset,outNALayerName,
        impedanceAttribute,'TRAVEL_TO','',1,accumulateAttributeName,'NO_UTURNS')
    #建立最近设施点分析图层
    outNALayer=outNALayer.getOutput(0)#获取最近设施点分析图层名称
    subLayerNames=arcpy.na.GetNAClassNames(outNALayer)#获取所有子图层的名称,字典
    facilitiesLayerName=subLayerNames['Facilities']#获取设施点图层
    incidentsLayerName=subLayerNames['Incidents']#获取事件点图层
    arcpy.na.AddLocations(outNALayer,facilitiesLayerName,inFacilities,'','')#添加设施点位置
    arcpy.na.AddLocations(outNALayer,incidentsLayerName,inIncidents,'','')#添加事件点位置
    arcpy.na.Solve(outNALayer)#求解
    arcpy.SaveToLayerFile_management(outNALayer,outLayerFile,'RELATIVE')
    print('Script completed successfully')
except Exception as e:
    import traceback,sys
    tb=sys.exc_info()[2]
    print('an error occured on line %i' % tb.tb_lineno)
    print(str(e))
                    
    






在这里插入代码片

猜你喜欢

转载自blog.csdn.net/qq_41950131/article/details/91346497