Ejemplo n.º 1
0
 def load_map(self):
     s = Session()
     # gHase映射
     self.query_geohash()
     # nodes表
     for n in s.query(Nodes):
         if n.nid not in self.nodes:
             self.nodes[n.nid] = [[n.rid],
                                  float(n.longitude),
                                  float(n.latitude)]
         else:
             self.nodes[n.nid][0].append(n.rid)
     # records表
     for r in s.query(Records):
         self.records[r.id] = [
             float(r.length), 50, 0.6852578070969514, r.oneway, r.from_node,
             r.to_node
         ]  #求平均
     # for i in range(24):
     for q in s.query(Quality2):
         a_time = float(q.average_time)
         density = float(q.density)
         if self.records[q.rid][1] == 0:
             self.records[q.rid][1] += a_time
         else:
             self.records[q.rid][1] = (a_time + self.records[q.rid][1]) / 2
         if self.records[q.rid][2] == 0:
             self.records[q.rid][2] = density
         else:
             self.records[q.rid][2] = (self.records[q.rid][2] + density) / 2
     self.adj_map = self.trans_map(self.records.keys())
     return self.adj_map, self.nodes
Ejemplo n.º 2
0
 def to_db(self, allow_codes):
     '''地图入库'''
     # 更新
     session = Session()
     drop_table(Nodes, Base, engine=engine)
     drop_table(Records, Base, engine=engine)
     d_pos = {}  #{(lon,lat):node}   ,用于去重
     for i, (shape, record) in enumerate(self.itrRecord()):
         # if i == 100:break
         points = shape.points
         code = record.code
         # 筛选
         if code in allow_codes:
             from_pos = points[0]
             to_pos = points[-1]
             itr = (from_pos, to_pos)
             if any(self.boundery.contains_points(itr)):
                 # 分级存储索引
                 if code in self.d_level:
                     self.d_level[code].append(i)
                 else:
                     self.d_level[code] = [i]
                 l_node = []
                 # 存储路网
                 for pos in itr:
                     if pos in d_pos:
                         # 单向路判断?
                         node = d_pos[pos]
                     else:
                         # ind = self.get_gZone(pos)
                         # if ind is not None:
                         #     self.gZone[ind].append(pos)
                         self.nid += 1
                         node = self.nid
                         d_pos[pos] = node
                         n = Nodes(id=node,
                                   osm_id=record.osm_id,
                                   longitude=pos[0],
                                   latitude=pos[1],
                                   geohash5=encode(pos[1], pos[0], 5))
                         session.add(n)
                     l_node.append(node)
                 r = Records(bridge=self.bool_map[record.bridge],
                             oneway=self.bool_map[record.oneway],
                             tunnel=self.bool_map[record.tunnel],
                             ref=record.ref,
                             name=record.name,
                             code=record.code,
                             fclass=record.fclass,
                             from_node=l_node[0],
                             to_node=l_node[1])
                 session.add(r)
                 record.extend(l_node)  #加入属性
         session.commit()
     session.close()
Ejemplo n.º 3
0
class Cprice:
    def __init__(self):
        self.session = Session()    #session的生命周期跟实例一样

    def insert(self,**kwargs):
        inst = Price(**kwargs)
        self.session.add(inst)
        self.session.commit()

    def query(self,**kwargs):
        return self.session.query(Price).filter_by(**kwargs).all()
Ejemplo n.º 4
0
 def load_map(self):
     s = Session()
     # gHase映射
     self.query_geohash()
     # nodes表
     for n in s.query(Nodes):
         self.nodes.append([n.id, n.osm_id, n.longitude, n.latitude])
     # records表
     for r in s.query(Records):
         self.records.append([r.oneway, r.from_node, r.to_node])
     #self.adj_map,_ = self.trans_map(range(len(self.nodes)))
     return self.adj_map, self.nodes
Ejemplo n.º 5
0
def get_data(day, zone):
    end_day = (day + timedelta(days=7)).timestamp()  #一周的时间
    day = day.timestamp()
    s = Session()
    d_data = {}  #  用字典来分段
    for rst in s.query(ZoneQuality).filter(ZoneQuality.g_zone == zone).all():
        date_time = rst.date_time
        if (day <= date_time.timestamp() <= end_day):
            hour = date_time.hour
            delta = 2 - hour % 4
            date_time = date_time + timedelta(hours=delta)
            if date_time in d_data:
                a_time, dens, flow = d_data[date_time]
                d_data[date_time] = [(a_time + rst.average_speed) / 2,
                                     (dens + rst.density) / 2,
                                     (flow + rst.flow) / 2]
            else:
                d_data[date_time] = [rst.average_speed, rst.density, rst.flow]
    df = pd.DataFrame(d_data).T
    df.columns = ['average_speed', 'density', 'flow']
    return df
Ejemplo n.º 6
0
class Cprice:
    def __init__(self):
        self.session = Session()  #session的生命周期跟实例一样

    def insert(self, key='url = ..', **kwargs):
        var, val = key.split('=')  #变量名,取值
        inst = self.session.query(Price).filter(Price.href == val).first()
        delta = 0  # 附带比较功能
        if inst is None:
            kwargs[var] = val
            inst = Price(**kwargs)
            self.session.add(inst)
        else:
            delta = self.update(inst, **kwargs)
        self.session.commit()
        return delta

    def update(self, inst, **kwargs):
        delta = kwargs['price'] - inst.price
        inst.update_time = kwargs['origin_time']  #
        inst.price = kwargs['price']
        return delta
Ejemplo n.º 7
0
def get_data_1(day, zone):
    day = datetime.strptime(day[:10], "%Y-%m-%d")
    end_day = (day + timedelta(days=2)).timestamp()  #一周的时间
    day = (day - timedelta(days=5)).timestamp()
    s = Session()
    d_data = {}  #  用字典来分段
    qry = s.query(ZoneQuality_1)
    for rst in qry.filter(ZoneQuality_1.g_zone == zone).all():
        date_time = rst.date_time
        if (day <= date_time.timestamp() <= end_day):
            # hour = date_time.hour
            # delta =  2 - hour%4
            # date_time = date_time - timedelta(days=1)
            if date_time in d_data:
                a_time, dens, flow = d_data[date_time]
                d_data[date_time] = [(a_time + rst.average_speed) / 2,
                                     (dens + rst.density) / 2,
                                     (flow + rst.flow) / 2]
            else:
                d_data[date_time] = [rst.average_speed, rst.density, rst.flow]
    df = pd.DataFrame(d_data).T
    df.columns = ['average_speed', 'density', 'flow']
    return df
Ejemplo n.º 8
0
class Curls:
    def __init__(self):
        self.session = Session()  #session的生命周期跟实例一样

    def insert_urls(self, url, status):
        insert_time = datetime.datetime.now()
        inst = self.session.query(storeUrls).filter(
            storeUrls.url == url).first()
        if inst is None:
            p = storeUrls(url=url, origin_time=insert_time, setting=status)
            self.session.add(p)
            self.session.commit()
        else:
            inst.origin_time = insert_time
            self.session.commit()

    def query(self, **kwargs):
        return self.session.query(storeUrls).filter_by(**kwargs).all()
Ejemplo n.º 9
0
class Curls:
    def __init__(self):
        self.session = Session()    #session的生命周期跟实例一样

    def update(self,key='url = ..',**kwargs):
        var,val = key.split('=')    #变量名,取值
        inst = self.session.query(storeUrls).filter(storeUrls.url == val).first()
        delta = 0 # 附带比较功能
        if inst is None:
            kwargs[var] = val
            inst = storeUrls(**kwargs)
            self.session.add(inst)
        else:
            # inst.__dict__.update(kwargs)    # 无效
            for k,v in kwargs.items():
                exec(f'inst.{k}="{v}"')
            delta = self.update_(inst)
        self.session.commit()
        return delta
    def update_(self,inst):
        return 0
    def query(self,**kwargs):
        return self.session.query(storeUrls).filter_by(**kwargs).all()
Ejemplo n.º 10
0
 def __init__(self):
     self.session = Session()  #session的生命周期跟实例一样
Ejemplo n.º 11
0
 def query_geohash(self):
     s = Session()
     for id, ghash in s.query(Nodes.id, Nodes.geohash5).all():
         self.d_gHash[ghash] = self.d_gHash.get(ghash, []) + [id]
     return self.d_gHash