Esempio n. 1
0
    def region(self, ctr_lat, ctr_lon, mcc, cells):
        region = None
        regions = [cell.region for cell in cells]
        unique_regions = set(regions)
        if len(unique_regions) == 1:
            region = regions[0]
        else:
            # Choose the area region based on the majority of cells
            # inside each region.
            grouped_regions = defaultdict(int)
            for reg in regions:
                grouped_regions[reg] += 1
            max_count = max(grouped_regions.values())
            max_regions = sorted([k for k, v in grouped_regions.items()
                                  if v == max_count])
            # If we get a tie here, randomly choose the first.
            region = max_regions[0]
            if len(max_regions) > 1:
                # Try to break the tie based on the center of the area,
                # but keep the randomly chosen region if this fails.
                area_region = GEOCODER.region_for_cell(
                    ctr_lat, ctr_lon, mcc)
                if area_region is not None:
                    region = area_region

        return region
Esempio n. 2
0
    def validate(cls, entry, _raise_invalid=False, **kw):
        validated = super(CellShard, cls).validate(
            entry, _raise_invalid=_raise_invalid, **kw
        )

        if validated is not None:
            if "cellid" not in validated:
                validated["cellid"] = (
                    validated["radio"],
                    validated["mcc"],
                    validated["mnc"],
                    validated["lac"],
                    validated["cid"],
                )

            if (
                ("region" not in validated or not validated["region"])
                and validated["lat"] is not None
                and validated["lon"] is not None
            ):
                validated["region"] = GEOCODER.region_for_cell(
                    validated["lat"], validated["lon"], validated["mcc"]
                )

        return validated
def upgrade():
    bind = op.get_bind()
    from ichnaea.geocode import GEOCODER

    log.info('Update cell_area regions.')
    stmt = '''\
UPDATE cell_area
SET `region` = "{code}"
WHERE `radio` IN (0, 1, 2, 3) AND `mcc` = {mcc} AND `region` IS NULL
'''
    length = len(MCC_TO_REGION)
    for i, (mcc, code) in enumerate(MCC_TO_REGION.items()):
        op.execute(sa.text(stmt.format(code=code, mcc=mcc)))
        if (i > 0 and i % 10 == 0):
            log.info('Updated %s of %s regions.', i, length)
    log.info('Updated %s of %s regions.', length, length)

    stmt = 'SELECT COUNT(*) FROM cell_area WHERE region IS NULL'
    todo = bind.execute(stmt).fetchone()[0]
    log.info('Updating remaining %s areas.', todo)

    stmt = '''\
SELECT HEX(`areaid`), `mcc`, `lat`, `lon`
FROM cell_area
WHERE `region` IS NULL
'''
    rows = bind.execute(stmt).fetchall()

    areas = {}
    i = 0
    for row in rows:
        if (i > 0 and i % 5000 == 0):
            log.info('Geocoded %s of %s areas.', i, todo)
        code = GEOCODER.region_for_cell(row.lat, row.lon, row.mcc)
        if code not in areas:
            areas[code] = []
        areas[code].append(row[0])
        i += 1
    log.info('Geocoded %s of %s areas.', todo, todo)

    stmt = '''\
UPDATE cell_area
SET `region` = "{code}"
WHERE `areaid` in ({ids})
'''
    for code, areaids in areas.items():
        if not code:
            continue
        ids = 'UNHEX("' + '"), UNHEX("'.join(areaids) + '")'
        op.execute(sa.text(stmt.format(code=code, ids=ids)))
        log.info('Updated %s region.', code)
def upgrade():
    bind = op.get_bind()
    from ichnaea.geocode import GEOCODER

    log.info('Update cell_area regions.')
    stmt = '''\
UPDATE cell_area
SET `region` = "{code}"
WHERE `radio` IN (0, 1, 2, 3) AND `mcc` = {mcc} AND `region` IS NULL
'''
    length = len(MCC_TO_REGION)
    for i, (mcc, code) in enumerate(MCC_TO_REGION.items()):
        op.execute(sa.text(stmt.format(code=code, mcc=mcc)))
        if (i > 0 and i % 10 == 0):
            log.info('Updated %s of %s regions.', i, length)
    log.info('Updated %s of %s regions.', length, length)

    stmt = 'SELECT COUNT(*) FROM cell_area WHERE region IS NULL'
    todo = bind.execute(stmt).fetchone()[0]
    log.info('Updating remaining %s areas.', todo)

    stmt = '''\
SELECT HEX(`areaid`), `mcc`, `lat`, `lon`
FROM cell_area
WHERE `region` IS NULL
'''
    rows = bind.execute(stmt).fetchall()

    areas = {}
    i = 0
    for row in rows:
        if (i > 0 and i % 5000 == 0):
            log.info('Geocoded %s of %s areas.', i, todo)
        code = GEOCODER.region_for_cell(row.lat, row.lon, row.mcc)
        if code not in areas:
            areas[code] = []
        areas[code].append(row[0])
        i += 1
    log.info('Geocoded %s of %s areas.', todo, todo)

    stmt = '''\
UPDATE cell_area
SET `region` = "{code}"
WHERE `areaid` in ({ids})
'''
    for code, areaids in areas.items():
        if not code:
            continue
        ids = 'UNHEX("' + '"), UNHEX("'.join(areaids) + '")'
        op.execute(sa.text(stmt.format(code=code, ids=ids)))
        log.info('Updated %s region.', code)
Esempio n. 5
0
    def validate(cls, entry, _raise_invalid=False, **kw):
        validated = super(CellAreaMixin, cls).validate(
            entry, _raise_invalid=_raise_invalid, **kw)
        if validated is not None and 'areaid' not in validated:
            validated['areaid'] = (
                validated['radio'],
                validated['mcc'],
                validated['mnc'],
                validated['lac'],
            )

            if (('region' not in validated or not validated['region']) and
                    validated['lat'] is not None and
                    validated['lon'] is not None):
                validated['region'] = GEOCODER.region_for_cell(
                    validated['lat'], validated['lon'], validated['mcc'])

        return validated
Esempio n. 6
0
    def validate(cls, entry, _raise_invalid=False, **kw):
        validated = super(CellAreaMixin, cls).validate(
            entry, _raise_invalid=_raise_invalid, **kw)
        if validated is not None and 'areaid' not in validated:
            validated['areaid'] = (
                validated['radio'],
                validated['mcc'],
                validated['mnc'],
                validated['lac'],
            )

            if (('region' not in validated or not validated['region']) and
                    validated['lat'] is not None and
                    validated['lon'] is not None):
                validated['region'] = GEOCODER.region_for_cell(
                    validated['lat'], validated['lon'], validated['mcc'])

        return validated