コード例 #1
0
ファイル: catch_road.py プロジェクト: Steelwall2014/WalkScore
def Make_Discre_Road_Points(road_geo:ogr.Geometry, 
                           start_points:dict, 
                           seg_length:float):
    '''
    在road_geo上每隔seg_length取一个出发点(魔改版)
    start_point将会是一个元组而不是Startpoint对象
    (离散道路点, 前点, 后点)
    '''

     
    count = road_geo.GetPointCount()
    total_length = road_geo.Length()
    
    '''如果是在递归中seg_length比路的全长还要长,那就把路的最后一个点作为出发点'''
    if seg_length >= total_length:
        start_point_coord = road_geo.GetPoint_2D(count-1)
        point_A_coord = road_geo.GetPoint_2D(count-2)
        point_B_coord = road_geo.GetPoint_2D(count-1)
        start_points[start_point_coord] = (point_A_coord, point_B_coord)
        return 
    
    first_to_current_length = 0
    temp_length = 0
    temp_x1 = None
    temp_y1 = None
    temp_x2 = None
    temp_y2 = None
    
    '''计算当前线段的长度和累计长度'''
    for index in range(count-1):
        temp_x1 = road_geo.GetX(index)              
        temp_y1 = road_geo.GetY(index)  
        temp_x2 = road_geo.GetX(index+1)
        temp_y2 = road_geo.GetY(index+1)
        temp_length = math.sqrt( (temp_x1-temp_x2)**2 + (temp_y1-temp_y2)**2 )
        first_to_current_length = first_to_current_length + temp_length
        if seg_length < first_to_current_length:
            break
    '''这时从第零个点到第index+1个点的距离大于seg_length了'''

    '''算出发点的坐标'''
    len1 = seg_length - (first_to_current_length - temp_length)
    start_point_x = (len1 / temp_length) * (temp_x2 - temp_x1) + temp_x1
    start_point_y = (len1 / temp_length) * (temp_y2 - temp_y1) + temp_y1
    if temp_x1 == 0:
        print([temp_x1, temp_y1])
    start_points[(start_point_x, start_point_y)] = ((temp_x1, temp_y1), (temp_x2, temp_y2))
    
    '''给出发点后面的点做一个线的几何体,用来进行下一个递归'''
    temp_road_geo = ogr.Geometry(ogr.wkbLineString)
    temp_road_geo.AddPoint(start_point_x, start_point_y)
    for i in range(index+1, count):
        temp_x = road_geo.GetX(i)                   
        temp_y = road_geo.GetY(i)        
        temp_road_geo.AddPoint(temp_x, temp_y)
     
    Make_Discre_Road_Points(temp_road_geo, start_points, seg_length)
コード例 #2
0
def Make_Start_Points_Road(road_geo:ogr.Geometry, 
                           start_points:dict, 
                           seg_length:float):
    '''在road_geo上每隔seg_length取一个出发点'''

     
    count = road_geo.GetPointCount()
    total_length = road_geo.Length()

    '''如果是在递归中seg_length比路的全长还要长,那就把路的最后一个点作为出发点'''
    if seg_length >= total_length:
        start_point = road_geo.GetPoint_2D(count-1)        
        start_points[start_point] = [road_geo.GetPoint_2D(count-2), road_geo.GetPoint_2D(count-1)] 
        #start_points = {出发点: [前驱点, 后继点]}
        return 
    
    first_to_current_length = 0
    temp_length = 0
    temp_x1 = 0
    temp_y1 = 0
    temp_x2 = 0
    temp_y2 = 0
    
    '''计算当前线段的长度和累计长度'''
    for index in range(count-1):
        temp_x1 = road_geo.GetX(index)              
        temp_y1 = road_geo.GetY(index)  
        temp_x2 = road_geo.GetX(index+1)
        temp_y2 = road_geo.GetY(index+1)
        temp_length = math.sqrt( (temp_x1-temp_x2)**2 + (temp_y1-temp_y2)**2 )
        first_to_current_length = first_to_current_length + temp_length
        if seg_length < first_to_current_length:
            pre_point = (temp_x1, temp_y1)
            nex_point = (temp_x2, temp_y2)
            break
    '''这时从第零个点到第index+1个点的距离大于seg_length了'''

    '''算出发点的坐标'''
    len1 = seg_length - (first_to_current_length - temp_length)
    start_point_x = (len1 / temp_length) * (temp_x2 - temp_x1) + temp_x1
    start_point_y = (len1 / temp_length) * (temp_y2 - temp_y1) + temp_y1
    start_points[(start_point_x, start_point_y)] = [pre_point, nex_point]
    
    '''给出发点后面的点做一个线的几何体,用来进行下一个递归'''
    temp_road_geo = ogr.Geometry(ogr.wkbLineString)
    temp_road_geo.AddPoint(start_point_x, start_point_y)
    for i in range(index+1, count):
        temp_x = road_geo.GetX(i)                   
        temp_y = road_geo.GetY(i)        
        temp_road_geo.AddPoint(temp_x, temp_y)
      
    Make_Start_Points_Road(temp_road_geo, start_points, seg_length)