def fetch(self, url, tmp_file, date_string=None):
        pip = PointInPolygon(self.polygon_id, 60)

        traffic_signs = []
        reader = json.loads(open(self.mapping, 'r').read())
        try:
            for row in reader:
                traffic_signs += row['object']
        except:
            self.logger.err(row)
            raise

        start_time = (datetime.today() - timedelta(days=365 * 2)).timestamp()
        MLY_ACCESS_TOKEN = 'MLY|3804396159685239|c5712d0fb9ef5d4dfef585154b00ffa7'

        with open(tmp_file, 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(
                ['id', 'first_seen_at', 'last_seen_at', 'value', 'X', 'Y'])

            tiles = list(self.tile_generator(pip.polygon.polygon))
            n_tiles = len(tiles)
            if n_tiles > 350000:
                self.logger.log(
                    f"To many tiles for the area ({n_tiles}), abort.")
                return True

            for (n, [z, x, y]) in enumerate(tiles):
                if n % 500 == 0:
                    self.logger.log(f"{n} / {n_tiles}")

                if self.layer == 'trafficsigns':
                    url = f"https://tiles.mapillary.com/maps/vtp/mly_map_feature_traffic_sign/2/{z}/{x}/{y}/?access_token={MLY_ACCESS_TOKEN}"
                elif self.layer == 'points':
                    url = f"https://tiles.mapillary.com/maps/vtp/mly_map_feature_point/2/{z}/{x}/{y}/?access_token={MLY_ACCESS_TOKEN}"

                r = downloader.request_get(url).content
                # Tile on cache alternative
                # r = downloader.urlopen(url, delay=60, mode='rb').read()

                if not r:
                    continue
                geojson = vt_bytes_to_geojson(r, x, y, z)
                # {'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-109.24706518650055, 45.18212758443923]}, 'properties': {'first_seen_at': 1506796436000, 'id': 224422696115054, 'last_seen_at': 1506796436000, 'value': 'complementary--keep-left--g1'}}]}

                features = filter(
                    lambda feature: feature['geometry']['type'] == 'Point' and
                    feature['properties']['last_seen_at'] / 1000 > start_time
                    and pip.point_inside_polygon(*feature['geometry'][
                        'coordinates']), geojson['features'])
                for feature in features:
                    p = feature['properties']
                    row = [
                        p['id'],
                        int(p['first_seen_at'] / 1000),
                        int(p['last_seen_at'] / 1000), p['value']
                    ]
                    writer.writerow(row + feature['geometry']['coordinates'])

        return True
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    parser.add_argument("uri", help="URI (url or filepath)")
    parser.add_argument("-l",
                        "--layer",
                        help="include only the specified layer",
                        type=str)
    parser.add_argument(
        "-x",
        help="tile x coordinate (normally inferred from the URL)",
        type=int)
    parser.add_argument(
        "-y",
        help="tile y coordinate (normally inferred from the URL)",
        type=int)
    parser.add_argument(
        "-z",
        help="tile z coordinate (normally inferred from the URL)",
        type=int)
    args = parser.parse_args()

    if _is_url(args.uri):
        matches = search(XYZ_REGEX, args.uri)
        if matches is None:
            raise ValueError("Unknown url, no `/z/x/y` pattern.")
        z, x, y = map(int, matches.groups())
        r = urlopen(args.uri)
        content = r.read()
    else:
        if args.x is None or args.y is None or args.z is None:
            raise ValueError(
                "You provided a path to a file. Therefore you must specify x, y and z."
            )
        x = args.x
        y = args.y
        z = args.z
        with open(args.uri, "rb") as f:
            content = f.read()

    geojson_result = vt_bytes_to_geojson(content, x, y, z, args.layer)
    print(json.dumps(geojson_result))
def checkme(z1, x1, y1):
    url = f"https://tiles.mapillary.com/maps/vtp/mly1/2/{z1}/{x1}/{y1}?access_token={MAP_ACCESS_TOKEN}"
    if url in seen:
        return 1
    seen.append(url)
    r = requests.get(url)
    assert r.status_code == 200, r.content
    vt_content = r.content

    features = vt_bytes_to_geojson(vt_content, x1, y1, z1)

    if str(user_id) in (json.dumps(features)):
        if z1 < maxzoom:
            print(z1, x1, y1)
            checkme(z1 + 1, 2 * x1, 2 * y1)
            checkme(z1 + 1, 2 * x1 + 1, 2 * y1)
            checkme(z1 + 1, 2 * x1, 2 * y1 + 1)
            checkme(z1 + 1, 2 * x1 + 1, 2 * y1 + 1)
        else:
            for f in features["features"]:

                if (f["geometry"]["type"]).lower().endswith(
                        "string") and f["properties"]["creator_id"] == user_id:
                    output['features'].append(f)
z = 14
ll_lat = 49.6265
ll_lon = 28.9991
ur_lat = 51.3965
ur_lon = 31.9874
llx, lly = deg2num(ll_lat, ll_lon, z)
urx, ury = deg2num(ur_lat, ur_lon, z)

#remove the layers you don't want to use
#description are in official docs: https://www.mapillary.com/developer/api-documentation/

types = ["mly_map_feature_traffic_sign"]

for type in types:
    output = {"type": "FeatureCollection", "features": []}
    for x in range(min(llx, urx), max(llx, urx), 1):
        for y in range(min(lly, ury), max(lly, ury), 1):
            print(type, x, y)
            url = f"https://tiles.mapillary.com/maps/vtp/{type}/2/{z}/{x}/{y}?access_token={MAP_ACCESS_TOKEN}"
            r = requests.get(url)
            assert r.status_code == 200, r.content
            vt_content = r.content
            features = vt_bytes_to_geojson(vt_content, x, y, z)
            for f in features["features"]:
                output['features'].append(f)
    with open(
            outputfolder + os.path.sep + type + "_" + str(z) + "_" + str(llx) +
            "_" + str(urx) + "_" + str(lly) + "_" + str(ury) + ".geojson",
            "w") as fx:
        json.dump(output, fx)