Exemplo n.º 1
0
def AssocRegion(guid):
    region = melt.fias_AONode(guid)
    if not region.kind:
        adm_id = FindAssocPlace(region, None)
        if adm_id != None:
            assoc = melt.PlaceAssoc(region.f_id, adm_id)
            region.session.add(assoc)
            region.session.commit()
            region = melt.fias_AONode(guid, 2, adm_id)

    AssociateO(region)
    region.session.commit()
    return ":".join((region.name, str(region.kind)))
Exemplo n.º 2
0
def AssocRegion(guid):
    region = melt.fias_AONode(guid)
    if not region.kind:
        adm_id = FindAssocPlace(region, None)
        if adm_id != None:
            assoc = melt.PlaceAssoc(region.f_id, adm_id)
            region.session.add(assoc)
            region.session.commit()
            region = melt.fias_AONode(guid, 2, adm_id)

    AssociateO(region)
    region.session.commit()
    return u":".join((region.name, str(region.kind)))
Exemplo n.º 3
0
def AssocRegion(guid, session):
    region = melt.fias_AONode(guid, session=session)
    logging.info(u"Start " + region.name)
    if not region.kind:
        adm_id = FindAssocPlace(region, None)
        if adm_id is not None:
            assoc = melt.PlaceAssoc(region.f_id, adm_id)
            session.add(assoc)
            session.commit()
            region = melt.fias_AONode(guid, 2, adm_id)

    AssociateO(region)
    session.commit()
    return u":".join((region.name, str(region.kind)))
Exemplo n.º 4
0
def found_view(request):
    guid = request.matchdict["guid"]
    typ = request.matchdict["typ"]
    bld = typ.endswith('_b')
    if typ not in ('all', 'found', 'street', 'not found',
                   'all_b', 'found_b', 'not found_b'):
        raise HTTPBadRequest()
    if not guid:
        #root is ok
        guid = None
    else:
        #Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    #Make check for area exist
    myself = melt.fias_AONode(guid)
    if guid and not myself.isok:
        raise HTTPNotFound()
    fullstat = guid is not None and myself.stat_db_full
    fullstat = fullstat or all([it.stat_db_full for it in myself.subO('all')])

    if bld:
        alist = myself.subB(typ)
        alist.sort(key=lambda el: el.onestr)
    else:
        alist = myself.subO(typ)
        alist.sort(key=lambda el: el.offname)

    if request.matchdict["offset"] or len(alist) > (off_border * 1.5):
        offset = int(request.matchdict['offset'])
        myself.offlinks = True
        alist = alist[offset:offset + off_border]
    else:
        myself.offlinks = False

    def links(self, typ_l):
        if typ_l in ('all', 'found', 'street', 'not found',
                     'all_b', 'found_b', 'not found_b'):
            return request.route_url('found0', guid=self.guid, typ=typ_l)
        elif typ_l == 'details':
            return request.route_url('details', guid=self.guid, kind='ao')
        elif typ_l == 'top':
            if self.parent.guid:
                return request.route_url('found0', guid=self.parentguid,
                                         typ='all')
            else:
                return request.route_url('foundroot0', typ='all')
        elif typ_l == "prev":
            return request.route_url('found', guid=self.guid, typ=typ,
                                     offset=max(0, offset - off_border))
        elif typ_l == "next":
            return request.route_url('found', guid=self.guid, typ=typ,
                                     offset=min(self.stat(typ) - 1,
                                                offset + off_border))

    return {"list": alist, "myself": myself, "links": links,
            'bld': bld, 'fullstat': fullstat}
Exemplo n.º 5
0
def details_view(request):
    #Make check for malformed guid
    try:
        guid = uuid.UUID(request.matchdict["guid"])
    except ValueError:
        raise HTTPBadRequest()
    myself = melt.fias_AONode(guid)
    statlink = request.route_url('found0', guid=guid, typ='all')
    return {"myself": myself, "statlink": statlink, "name": myself.name}
Exemplo n.º 6
0
def details_view(request):
    #Make check for malformed guid
    try:
        guid = uuid.UUID(request.matchdict["guid"])
    except ValueError:
        raise HTTPBadRequest()
    myself = melt.fias_AONode(guid)
    statlink = request.route_url('found0', guid=guid, typ='all')
    return {"myself": myself, "statlink": statlink, "name": myself.name}
Exemplo n.º 7
0
def node4guid(guid=None):
    if guid == 'None' or not guid:
        guid = None
    else:
        # Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    return melt.fias_AONode(guid, session=Session())
Exemplo n.º 8
0
def node4guid(guid=None):
    if guid == 'None' or not guid:
        guid = None
    else:
        # Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    return melt.fias_AONode(guid, session=Session())
Exemplo n.º 9
0
def found_view(request):
    guid = request.matchdict["guid"]
    typ = request.matchdict["typ"]
    if typ not in ('all', 'found', 'street', 'not found', 'all_b', 'found_b',
                   'not found_b'):
        raise HTTPBadRequest()
    if not guid:
        #root is ok
        guid = None
    else:
        #Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    #Make check for area exist
    myself = melt.fias_AONode(guid)
    if guid and not myself.isok:
        raise HTTPNotFound()
    fullstat = all([it.stat_db_full for it in myself.subO('all')])
    alist = myself.subO(typ)
    if typ.endswith('_b'):
        alist.sort(key=lambda el: el.onestr)
    else:
        alist.sort(key=lambda el: el.offname)
    if request.matchdict["offset"] or len(alist) > (off_border * 1.5):
        offset = int(request.matchdict['offset'])
        myself.offlinks = True
        alist = alist[offset:offset + off_border]
    else:
        myself.offlinks = False

    def links(self, typ_l):
        if typ_l in ('all', 'found', 'street', 'not found', 'all_b', 'found_b',
                     'not found_b'):
            return request.route_url('found0', guid=self.guid, typ=typ_l)
        elif typ_l == 'details':
            return request.route_url('details', guid=self.guid, kind='ao')
        elif typ_l == 'top':
            if self.parent.guid:
                return request.route_url('found0',
                                         guid=self.parentguid,
                                         typ='all')
            else:
                return request.route_url('foundroot0', typ='all')
        elif typ_l == "prev":
            return request.route_url('found',
                                     guid=self.guid,
                                     typ=typ,
                                     offset=max(0, offset - off_border))
        elif typ_l == "next":
            return request.route_url('found',
                                     guid=self.guid,
                                     typ=typ,
                                     offset=min(
                                         self.stat(typ) - 1,
                                         offset + off_border))

    return {
        "list": alist,
        "myself": myself,
        "links": links,
        'bld': typ.endswith('_b'),
        'fullstat': fullstat
    }
Exemplo n.º 10
0
def AssociateO(elem):
    '''Search and save association for all subelements of elem

    This function should work for elements with partitially associated subs
    as well as elements without associated subs
    '''
    if not elem.kind:
        return
    #Precache subs list
    # practically it's 'not found', filling others subs as side-effect
    elem.subO('all', True)
    #run processing for found to parse their subs
    for sub in tuple(elem.subO('found', True)):
        AssociateO(melt.fias_AONode(sub))
    #find new elements for street if any
    for sub in tuple(elem.subO('street', True)):
        sub_ = melt.fias_AONode(sub)
        streets=FindAssocStreet(sub_,elem.geom)
        if streets<>None:
            pre = elem.session.query(melt.StreetAssoc).filter_by(ao_id=sub.f_id).all()
            pre = set([it.osm_way for it in pre])
            for street in streets:
                if street not in pre:
                    assoc = melt.StreetAssoc(sub.f_id, street)
                    elem.session.add(assoc)
            elem.session.commit()
            AssociateO(sub_)
    #search for new areas
    subareas = Subareas(elem)
    for sub in tuple(elem.subO('not found', True)):
        if sub.fullname in way_only:
            continue
        sub_ = melt.fias_AONode(sub)
        adm_id = None
        if subareas:
            for name in sub_.names():
                if name in subareas:
                    adm_id = subareas.pop(name)
                    break
        if adm_id is None:
            adm_id = FindAssocPlace(sub_, elem.geom)
        if not (adm_id is None):
            assoc = melt.PlaceAssoc(sub.f_id, adm_id)
            elem.session.add(assoc)
            elem.child_found(sub, 'found')
            sub_.osmid = adm_id
            sub_.kind = 2
            AssociateO(sub_)
    #search for new streets
    for sub in tuple(elem.subO('not found', False)):
        if sub.fullname in pl_only:
            continue
        sub_ = melt.fias_AONode(sub)
        streets = FindAssocStreet(sub_, elem.geom)
        if not streets is None:
            for street in streets:
                assoc = melt.StreetAssoc(sub.f_id, street)
                elem.session.add(assoc)
            elem.session.commit()
            elem.child_found(sub, 'street')
            sub_.kind = 1
            sub_.osmid = streets[0]
            AssociateO(sub_)
    #Search for buildings
    AssocBuild(elem, 0)
    AssocBuild(elem, 1)

    elem.stat('not found', 1)
    elem.stat('not found_b', 1)
    elem.session.commit()
Exemplo n.º 11
0
def AssociateO(elem):
    '''Search and save association for all subelements of elem

    This function should work for elements with partitially associated subs
    as well as elements without associated subs
    '''
    if not elem.kind:
        return
    #Precache subs list
    elem.subO('all', False)
    #run processing for found to parse their subs
    for sub in tuple(elem.subO('found', False)):
        AssociateO(melt.fias_AONode(sub))
    #find new elements for street if any
    for sub in tuple(elem.subO('street', False)):
        sub_ = melt.fias_AONode(sub)
        streets = FindAssocStreet(sub_, elem.geom)
        if streets <> None:
            pre = elem.session.query(
                melt.StreetAssoc).filter_by(ao_id=sub.f_id).all()
            pre = set([it.osm_way for it in pre])
            for street in streets:
                if street not in pre:
                    assoc = melt.StreetAssoc(sub.f_id, street)
                    elem.session.add(assoc)
            elem.session.commit()
            AssociateO(sub_)
    #search for new areas
    subareas = Subareas(elem)
    for sub in tuple(elem.subO('not found', False)):
        if sub.fullname in way_only:
            continue
        sub_ = melt.fias_AONode(sub)
        adm_id = None
        if subareas:
            for name in sub_.names():
                if name in subareas:
                    adm_id = subareas.pop(name)
                    break
        if adm_id is None:
            adm_id = FindAssocPlace(sub_, elem.geom)
        if not (adm_id is None):
            assoc = melt.PlaceAssoc(sub.f_id, adm_id)
            elem.session.add(assoc)
            elem.child_found(sub, 'found')
            sub_.osmid = adm_id
            sub_.kind = 2
            AssociateO(sub_)
    #search for new streets
    for sub in tuple(elem.subO('not found', False)):
        if sub.fullname in pl_only:
            continue
        sub_ = melt.fias_AONode(sub)
        streets = FindAssocStreet(sub_, elem.geom)
        if not streets is None:
            #print sub.name, streets
            for street in streets:
                assoc = melt.StreetAssoc(sub.f_id, street)
                elem.session.add(assoc)
            elem.session.commit()
            elem.child_found(sub, 'street')
            sub_.kind = 1
            sub_.osmid = streets[0]
            AssociateO(sub_)
    #Search for buildings
    AssocBuild(elem, 0)
    AssocBuild(elem, 1)

    elem.session.commit()
Exemplo n.º 12
0
def found_view(request):
    guid = request.matchdict.get("guid")
    typ = request.matchdict.get("typ", "all")
    if request.matched_route.name == 'rest_json_list':
        typ = request.GET.get('kind', 'all')
    bld = typ.endswith('_b')
    if typ not in ('all', 'found', 'street', 'not found', 'all_b', 'found_b',
                   'not found_b'):
        raise HTTPBadRequest()
    if not guid or guid == 'None':
        #root is ok
        guid = None
    else:
        #Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    #Make check for area exist
    myself = melt.fias_AONode(guid)
    if guid and not myself.isok:
        raise HTTPNotFound()

    if bld:
        alist = myself.subB(typ)
        alist.sort(key=lambda el: el.onestr)
    else:
        alist = myself.subO(typ, not ('rest' in request.url))
        alist.sort(key=lambda el: el.offname)

    fullstat = guid is not None and myself.stat_db_full
    fullstat = fullstat or all([it.stat_db_full for it in myself.subO('all')])

    offset = int(request.matchdict.get("offset", 0))
    myself.need_more = (len(alist) > (off_border * 1.5)
                        and len(alist) > off_border + offset)
    if 'rest' in request.url:
        request.response.content_type = 'text/xml'
        myself.need_more = False
    if (offset or myself.need_more):
        myself.offlinks = True
        alist = alist[offset:offset + off_border]
    else:
        myself.offlinks = False

    def links(self, typ_l):
        if typ_l in ('all', 'found', 'street', 'not found', 'all_b', 'found_b',
                     'not found_b'):
            return request.route_url('found0', guid=self.guid, typ=typ_l)
        elif typ_l == 'details':
            return request.route_url('details', guid=self.guid, kind='ao')
        elif typ_l == 'top':
            if self.parent.guid:
                return request.route_url('found0',
                                         guid=self.parentguid,
                                         typ='all')
            else:
                return request.route_url('foundroot0', typ='all')
        elif typ_l == "prev":
            return request.route_url('found',
                                     guid=self.guid,
                                     typ=typ,
                                     offset=max(0, offset - off_border))
        elif typ_l == "next":
            return request.route_url('found',
                                     guid=self.guid,
                                     typ=typ,
                                     offset=min(
                                         self.stat(typ) - 1,
                                         offset + off_border))

    return {
        "list": alist,
        "myself": myself,
        "links": links,
        'bld': bld,
        'fullstat': fullstat
    }
Exemplo n.º 13
0
def found_view(request):
    guid = request.matchdict.get("guid")
    typ = request.matchdict.get("typ", "all")
    if request.matched_route.name == 'rest_json_list':
        typ = request.GET.get('kind', 'all')
    bld = typ.endswith('_b')
    if typ not in ('all', 'found', 'street', 'not found',
                   'all_b', 'found_b', 'not found_b'):
        raise HTTPBadRequest()
    if not guid or guid == 'None':
        #root is ok
        guid = None
    else:
        #Make check for malformed guid
        try:
            guid = uuid.UUID(guid)
        except ValueError:
            raise HTTPBadRequest()
    #Make check for area exist
    myself = melt.fias_AONode(guid)
    if guid and not myself.isok:
        raise HTTPNotFound()

    if bld:
        alist = myself.subB(typ)
        alist.sort(key=lambda el: el.onestr)
    else:
        alist = myself.subO(typ, not('rest' in request.url))
        alist.sort(key=lambda el: el.offname)


    fullstat = guid is not None and myself.stat_db_full
    fullstat = fullstat or all([it.stat_db_full for it in myself.subO('all')])

    offset = int(request.matchdict.get("offset", 0))
    myself.need_more = (len(alist) > (off_border * 1.5)
                        and len(alist) > off_border + offset)
    if 'rest' in request.url:
        request.response.content_type = 'text/xml'
        myself.need_more = False
    if (offset or myself.need_more):
        myself.offlinks = True
        alist = alist[offset:offset + off_border]
    else:
        myself.offlinks = False

    def links(self, typ_l):
        if typ_l in ('all', 'found', 'street', 'not found',
                     'all_b', 'found_b', 'not found_b'):
            return request.route_url('found0', guid=self.guid, typ=typ_l)
        elif typ_l == 'details':
            return request.route_url('details', guid=self.guid, kind='ao')
        elif typ_l == 'top':
            if self.parent.guid:
                return request.route_url('found0', guid=self.parentguid,
                                         typ='all')
            else:
                return request.route_url('foundroot0', typ='all')
        elif typ_l == "prev":
            return request.route_url('found', guid=self.guid, typ=typ,
                                     offset=max(0, offset - off_border))
        elif typ_l == "next":
            return request.route_url('found', guid=self.guid, typ=typ,
                                     offset=min(self.stat(typ) - 1,
                                                offset + off_border))
    return {"list": alist, "myself": myself, "links": links,
            'bld': bld, 'fullstat': fullstat}