Exemplo n.º 1
0
def get_gZone():
    err = []
    ghash_to_write = [[], [], [], [], [], [], [], [], [], [], []]
    ghash_str = [[], [], [], [], [], [], [], [], [], [], []]
    names = []
    with open('../data/g_boundery.txt', 'r') as f:
        gZ = {}
        gZone = []
        gG = json.load(f)
        for z in gG['features']:
            coors = z['geometry']['coordinates'][0][0]
            name = z['properties']['name']
            names.append(name)
            gZone.append(Path(coors))
    for ghash in guangzhou_geohash5:
        count = []
        for i, z in enumerate(gZone):
            pos = decode(ghash)[::-1]
            #生成四个点
            delta = 0.01
            pos1 = (pos[0] + delta, pos[1])
            pos2 = (pos[0] - delta, pos[1])
            pos3 = (pos[0], pos[1] - delta)
            pos4 = (pos[0], pos[1] + delta)
            num = 0
            for p in (pos1, pos2, pos3, pos4):
                if z.contains_point(p):
                    num += 1
            count.append(num)
        max_count = max(count)
        if max_count:
            ind = count.index(max_count)

            if names[ind] in gZ:
                gZ[names[ind]].append(ghash)
            else:
                gZ[names[ind]] = [ghash]
            ghash_to_write[ind].append(wgs_gcj(*decode(ghash)[::-1]))
            ghash_str[ind].append(ghash)
        else:
            err.append(wgs_gcj(*decode(ghash)[::-1]))
    # for i in range(11):
    #     with open(f'gZones{i}.json','w') as f:
    #         # input(ghash_to_write[i])
    #         json.dump(ghash_to_write[i],f)
    #     with open(f'geoHase{i}.json','w') as f:
    #         json.dump(ghash_str[i],f)
    # lst = []
    # for i in ghash_to_write:
    #     lst += i
    # with open('gZones_all.json', 'w') as f:
    #     json.dump(lst, f)
    #
    # with open('gZones_err.json', 'w') as f:
    #     # input(ghash_to_write[i])
    #     json.dump(err, f)

    return gZ, gG
Exemplo n.º 2
0
def insert_map():
    session = NetworkSession()
    lst = '['
    drop_table(Nodes, Base, engine)
    drop_table(Records, Base, engine)
    node_id = 0
    with shp.Reader(file, encoding='utf-8') as f:
        for col in f.iterShapeRecords():
            shape = col.shape
            points = shape.points
            if col.record.code in allow_codes:
                from_node = points[0]
                to_node = points[-1]
                if any(boundery.contains_points([from_node,
                                                 to_node])):  # 只要有一个点在广州内
                    # 先查询站点是否在数据库内,再写入
                    record = col.record
                    itr = (from_node, to_node)
                    ids = []
                    for pos in itr:
                        id = get_id(session, *pos)
                        if not id:
                            node_id += 1
                            id = node_id
                            n = Nodes(id=id,
                                      osm_id=record.osm_id,
                                      longitude=pos[0],
                                      latitude=pos[1])
                            session.add(n)
                        else:
                            id = id[0]
                        ids.append(id)
                    r = Records(bridge=bool_map[record.bridge],
                                oneway=bool_map[record.oneway],
                                tunnel=bool_map[record.tunnel],
                                ref=record.ref,
                                name=record.name,
                                code=record.code,
                                fclass=record.fclass,
                                from_node=ids[0],
                                to_node=ids[1])
                    lst = lst + str(
                        {
                            "start": wgs_gcj(from_node[1], from_node[0])[::-1],
                            "end": wgs_gcj(to_node[1], to_node[0])[::-1]
                        }) + ',\n'
                    session.add(r)
                    session.commit()
        session.close()
    with open('lst.json', 'w') as f:
        f.write(lst + ']')
Exemplo n.º 3
0
def boundury_view():
    lst = []
    with open('../data/g_boundery.txt', 'r') as f:
        gZone = []
        gG = json.load(f)
        for j, z in enumerate(gG['features']):
            coors = z['geometry']['coordinates'][0][0]
            coors = [wgs_gcj(*d) for d in coors]
            # input(coors)
            for i in range(len(coors) - 1):
                lst.append({
                    'start': coors[i],
                    'end': coors[i + 1],
                    'count': j
                })
            gZone.append(Path(coors))
    with open('gZone.json', 'w') as f:
        json.dump(lst, f)
Exemplo n.º 4
0
    def planing(self, demand, wtype=0):
        #[(lon,lat),(lon,lat)]

        closeQueue = []
        d_tmpcosts = {}
        gdist = {}  # 存放距离点的表
        orign, destination = demand
        startNode = self.node_match(orign)
        endNode = self.node_match(destination)
        print(startNode, endNode)
        if startNode == endNode:
            return [], 0
        gdist[startNode] = 0
        d_tmpcosts[startNode] = (0, 0)
        openQueue = [startNode]
        heapQueue = []
        heapq.heappush(heapQueue, (0, startNode))  #加入
        parentTable = {}  # 存放父节点的表
        while openQueue:
            curNode = heapq.heappop(heapQueue)[1]
            openQueue.remove(curNode)
            closeQueue.append(curNode)
            if curNode == endNode: break  #找到路径
            nextNodeList = self.adj_map[curNode]  # 子节点的索引
            # 处理子节点
            for i in nextNodeList:
                nextNode = i[0]
                nextNodeIndex = i[1]
                if nextNode not in closeQueue:
                    if nextNode in openQueue:
                        if gdist[curNode] + self.records[nextNodeIndex][
                                0] < gdist[nextNode]:
                            gdist[nextNode] = gdist[curNode] + self.records[
                                nextNodeIndex][wtype]  # 更新子节点的距离
                            tmpcost = d_tmpcosts[curNode]
                            d_tmpcosts[nextNode] = (
                                self.records[nextNodeIndex][0] + tmpcost[0],
                                self.records[nextNodeIndex][1] + tmpcost[1]
                            )  #距离时间
                            parentTable[nextNode] = (curNode, nextNodeIndex
                                                     )  # 更新父节点
                    else:
                        dist = gdist[curNode] + self.records[nextNodeIndex][
                            wtype]
                        gdist[nextNode] = dist  # 更新子节点的距离
                        tmpcost = d_tmpcosts[curNode]
                        d_tmpcosts[nextNode] = (
                            self.records[nextNodeIndex][0] + tmpcost[0],
                            self.records[nextNodeIndex][1] + tmpcost[1]
                        )  # 距离时间
                        parentTable[nextNode] = (curNode, nextNodeIndex
                                                 )  # 更新父节点
                        openQueue.append(nextNode)
                        heapq.heappush(heapQueue, (dist, nextNode))
            # 从大到小排序
            # openQueue = sorted(openQueue, key=lambda k: gdist[k] + self.hdist(k,endNode),
            #                    reverse=True)
        if endNode != closeQueue[-1]: return [], None  #没找到路径
        pathIndex = []
        lastNode = closeQueue[-1]
        costs = [0, 0]
        while True:
            if not parentTable.get(lastNode):
                break
            pos = self.nodes[parentTable[lastNode][0]][1:]
            pos = list(wgs_gcj(*pos))
            pathIndex.append(pos)
            lastNode = parentTable[lastNode][0]
        d, t = d_tmpcosts[endNode]
        d = round(d / 1000, 1)
        t = int(t / 60)
        return pathIndex[::-1], (d, t)  #gdist[endNode]  #[(lon,lat)]
Exemplo n.º 5
0
def get_gHase():
    lst = []
    for hash in guangzhou_geohash5:
        lst.append(wgs_gcj(*decode(hash)[::-1]))
    with open('gHase.json', 'w') as f:
        json.dump(lst, f)