Ejemplo n.º 1
0
def get_building_areas_image(lat, long):
    res = mapbox.StaticStyle(access_token=MAPBOX_ACCESS_TOKEN).image(
        username='******',
        style_id='ck1wjiell0iru1crquvcgkswj',
        lat=lat,
        lon=long,
        zoom=19)
    return res
Ejemplo n.º 2
0
def test_bad_tilesize():
    with pytest.raises(mapbox.errors.ValidationError):
        mapbox.StaticStyle(access_token='pk.test').tile('mapbox',
                                                        'streets-v9',
                                                        10,
                                                        163,
                                                        395,
                                                        tile_size=333)
Ejemplo n.º 3
0
def test_staticmap_twox_deprecated_error():
    with pytest.raises(mapbox.errors.ValidationError):
        mapbox.StaticStyle(access_token='pk.test').image('mapbox',
                                                         'streets-v9',
                                                         -61.7,
                                                         12.1,
                                                         12.5,
                                                         pitch=25,
                                                         bearing=75,
                                                         retina=True,
                                                         twox=True)
Ejemplo n.º 4
0
def test_staticmap_wmts():

    responses.add(
        responses.GET,
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/wmts?access_token=pk.test',
        match_querystring=True,
        body='<Capabilities xmlns=...',
        status=200,
        content_type='application/xml')

    res = mapbox.StaticStyle(access_token='pk.test').wmts(
        'mapbox', 'streets-v9')
    assert res.status_code == 200
Ejemplo n.º 5
0
def test_staticmap_tile():

    responses.add(
        responses.GET,
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/tiles/512/10/163/395?access_token=pk.test',
        match_querystring=True,
        body='png123',
        status=200,
        content_type='image/png')

    res = mapbox.StaticStyle(access_token='pk.test').tile(
        'mapbox', 'streets-v9', 10, 163, 395)
    assert res.status_code == 200
Ejemplo n.º 6
0
def test_staticmap_lonlatzpitchbearing():

    responses.add(
        responses.GET,
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/static/-61.7,12.1,12.5,75,25/600x600?access_token=pk.test',
        match_querystring=True,
        body='png123',
        status=200,
        content_type='image/png')

    res = mapbox.StaticStyle(access_token='pk.test').image('mapbox',
                                                           'streets-v9',
                                                           -61.7,
                                                           12.1,
                                                           12.5,
                                                           pitch=25,
                                                           bearing=75)
    assert res.status_code == 200
Ejemplo n.º 7
0
def test_staticmap_twox_deprecated():
    responses.add(
        responses.GET,
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/static/-61.7,12.1,12.5,75,25/600x600@2x?access_token=pk.test',
        match_querystring=True,
        body='png123',
        status=200,
        content_type='image/png')

    with pytest.warns(mapbox.errors.MapboxDeprecationWarning):
        res = mapbox.StaticStyle(access_token='pk.test').image('mapbox',
                                                               'streets-v9',
                                                               -61.7,
                                                               12.1,
                                                               12.5,
                                                               pitch=25,
                                                               bearing=75,
                                                               twox=True)
    assert res.status_code == 200
Ejemplo n.º 8
0
def test_staticmap_options():

    responses.add(
        responses.GET,
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/static/-61.7,12.1,12.5,0,0/600x600?access_token=pk.test&attribution=true&logo=false&before_layer=a',
        match_querystring=True,
        body='png123',
        status=200,
        content_type='image/png')

    res = mapbox.StaticStyle(access_token='pk.test').image('mapbox',
                                                           'streets-v9',
                                                           -61.7,
                                                           12.1,
                                                           12.5,
                                                           attribution=True,
                                                           logo=False,
                                                           before_layer='a')
    assert res.status_code == 200
Ejemplo n.º 9
0
def test_staticmap_lonlatz_features(points):

    overlay = json.dumps({
        'type': 'FeatureCollection',
        'features': points
    },
                         separators=(',', ':'))
    overlay = quote(overlay)

    url = (
        'https://api.mapbox.com/styles/v1/mapbox/streets-v9/static/geojson({0})/'
        '-61.7,12.1,12,0,0/600x600?access_token=pk.test'.format(overlay))

    responses.add(responses.GET,
                  url,
                  match_querystring=True,
                  body='png123',
                  status=200,
                  content_type='image/png')

    res = mapbox.StaticStyle(access_token='pk.test').image(
        'mapbox', 'streets-v9', -61.7, 12.1, 12, points)
    assert res.status_code == 200
Ejemplo n.º 10
0
def is_land(lat, lon, zoom):
    """
    Determines if a given lat/lon is land (specifically, whether a birds-eye
    view of that position is less than 98% water) by querying Mapbox for a
    static map with land and water having different colors.
    """
    client = mapbox.StaticStyle()
    dimension = 100
    # Mapbox static maps include a logo in the bottom left, so we request a
    # slightly taller than desired image and then crop out the top and bottom
    # (so the resulting image is still square).
    logo_size = 20

    response = client.image(
        username="******",
        style_id="cjqlx253r2wcl2sms5lxw13rh",
        lat=lat,
        lon=lon,
        zoom=zoom,
        height=dimension + 2 * logo_size,
        width=dimension,
    )
    if response.status_code != 200:
        raise Exception(
            "Failed to fetch map image from Mapbox for detecting land")

    img = Image.open(io.BytesIO(response.content)).convert("RGBA")
    width, height = img.size
    diff = (height - width) / 2
    img = img.crop((0, diff, width, height - diff))
    total_pixel_count = width**2

    water_pixel_count = sum(count for count, color in img.getcolors()
                            if is_roughly_water_color(color))

    water_ratio = water_pixel_count / total_pixel_count
    return water_ratio < 0.98
Ejemplo n.º 11
0
def test_class_attrs():
    """Get expected class attr values"""
    serv = mapbox.StaticStyle()
    assert serv.api_name == 'styles'
    assert serv.api_version == 'v1'
Ejemplo n.º 12
0
def test_staticmap_auto_nofeatures(points):
    with pytest.raises(ValueError):
        mapbox.StaticStyle(access_token='pk.test').image(
            'mapbox', 'streets-v9')
Ejemplo n.º 13
0
def get_map_image(lat, long):
    res = mapbox.StaticStyle(access_token=MAPBOX_ACCESS_TOKEN).image(
        username='******', style_id='satellite-v9', lat=lat, lon=long, zoom=19)
    return res