Example #1
0
def parse_mwm(mwm_name, osm2ft_name, override_version):
    region_name = os.path.splitext(os.path.basename(mwm_name))[0]
    logging.info(region_name)
    with open(osm2ft_name, "rb") as f:
        ft2osm = read_osm2ft(f, ft2osm=True, tuples=False)

    mwm_file = Mwm(mwm_name)
    version = override_version or mwm_file.version().version
    mwm_id = generate_id_from_name_and_version(region_name, version)
    QUEUES["mwm"].put((mwm_id, region_name, version))
    for feature in mwm_file:
        osm_id = ft2osm.get(feature.index(), None)
        readable_types = feature.readable_types()
        if osm_id is None:
            metadata = feature.metadata()
            if metadata is not None and MetadataField.sponsored_id in metadata:
                for t in readable_types:
                    if t.startswith("sponsored-"):
                        QUEUES["sponsored"].put(
                            (metadata[MetadataField.sponsored_id],
                             feature.index(), mwm_id, version,
                             SOURCE_TYPES[t[t.find("-") + 1:]]))
                        break
        else:
            for t in readable_types:
                if t.startswith(GOOD_TYPES):
                    QUEUES["mapping"].put(
                        (ctypes.c_long(osm_id).value, feature.index(), mwm_id,
                         version, SOURCE_TYPES["osm"]))
                    break
Example #2
0
def find_features(path: str, typ: str, string: str) -> List[Feature]:
    features = []
    index = int(string) if typ == "id" else None
    for feature in Mwm(path):
        found = False
        if typ == "n":
            for value in feature.names().values():
                if string in value:
                    found = True
                    break
        elif typ in ("t", "et"):
            for t in feature.types():
                readable_type_ = readable_type(t)
                if readable_type_ == string:
                    found = True
                    break
                elif typ == "t" and string in readable_type_:
                    found = True
                    break
        elif typ == "m":
            for f in feature.metadata():
                if string in f.name:
                    found = True
                    break
        elif typ == "id" and index == feature.index():
            found = True

        if found:
            features.append(feature)

    return features
Example #3
0
def dump_mwm(path, format, need_features):
    mwm = Mwm(path)
    if format == "str":
        print(mwm)
    elif format == "json":
        print(
            json.dumps(mwm.to_json(), ensure_ascii=False,
                       cls=EnumAsStrEncoder))

    if need_features:
        for ft in mwm:
            if format == "str":
                print(ft)
            elif format == "json":
                print(
                    json.dumps(ft.to_json(),
                               ensure_ascii=False,
                               cls=EnumAsStrEncoder))
Example #4
0
    def _find(self, leaf_id):
        result = {"countries": [], "cities": []}
        ft2osm = load_osm2ft(self.osm2ft_path, leaf_id)

        for feature in Mwm(os.path.join(self.mwm_path, leaf_id + ".mwm")):
            osm_id = ft2osm.get(feature.index(), None)
            types = feature.readable_types()

            if "sponsored-promo_catalog" in types and osm_id in self.cities:
                city = self._get_city(osm_id, types)
                result["cities"].append(city)

            if "place-country" in types and osm_id in self.countries:
                result["countries"].append(osm_id)

        return result
Example #5
0
def read_sections(path: str):
    return Mwm(path, parse=False).sections_info()
Example #6
0
def count_all_types(path: str):
    c = defaultdict(int)
    for ft in Mwm(path, parse=False):
        for t in ft.types():
            c[t] += 1
    return c