Ejemplo n.º 1
0
    def image(self,
              mapid,
              lon=None,
              lat=None,
              z=None,
              features=None,
              width=600,
              height=600,
              image_format='png256',
              sort_keys=False,
              retina=False):

        if lon is not None and lat is not None and z is not None:
            auto = False
            lat = self._validate_lat(lat)
            lon = self._validate_lon(lon)
        else:
            auto = True

        width = self._validate_image_size(width)
        height = self._validate_image_size(height)

        values = dict(mapid=mapid,
                      lon=str(lon),
                      lat=str(lat),
                      z=str(z),
                      width=str(width),
                      height=str(height))

        if features:
            collection = normalize_geojson_featurecollection(features)
            values['overlay'] = json.dumps(collection,
                                           separators=(',', ':'),
                                           sort_keys=sort_keys)

            self._validate_overlay(values['overlay'])

            if auto:
                pth = '/{mapid}/geojson({overlay})/auto/{width}x{height}'
            else:
                pth = ('/{mapid}/geojson({overlay})/{lon},{lat},{z}'
                       '/{width}x{height}')
        else:
            if auto:
                raise errors.InvalidCoordError(
                    "Must provide features if lat, lon, z are None")

            # No overlay
            pth = '/{mapid}/{lon},{lat},{z}/{width}x{height}'

        uri = URITemplate(self.baseuri + pth).expand(**values)

        # @2x.format handled separately to avoid HTML escaping the ampersand
        twox = '@2x' if retina else ''
        full_fmt = '{0}.{1}'.format(twox, image_format)
        uri += full_fmt

        res = self.session.get(uri)
        self.handle_http_error(res)
        return res
Ejemplo n.º 2
0
def test_nonsense():
    with pytest.raises(ValueError):
        normalize_geojson_featurecollection(123)

    with pytest.raises(ValueError):
        normalize_geojson_featurecollection({'foo': 'bar'})

    with pytest.raises(ValueError):
        normalize_geojson_featurecollection({'type': 'not-geojson'})
Ejemplo n.º 3
0
    def image(self, mapid, lon=None, lat=None, z=None, features=None,
              width=600, height=600, image_format='png256', sort_keys=False):

        if lon is not None and lat is not None and z is not None:
            auto = False
            lat = self._validate_lat(lat)
            lon = self._validate_lon(lon)
        else:
            auto = True

        width = self._validate_image_size(width)
        height = self._validate_image_size(height)

        values = dict(
            mapid=mapid,
            lon=str(lon),
            lat=str(lat),
            z=str(z),
            width=str(width),
            height=str(height),
            fmt=image_format)

        if features:
            collection = normalize_geojson_featurecollection(features)
            values['overlay'] = json.dumps(
                collection, separators=(',', ':'), sort_keys=sort_keys)

            self._validate_overlay(values['overlay'])

            if auto:
                pth = '/{mapid}/geojson({overlay})/auto/{width}x{height}.{fmt}'
            else:
                pth = ('/{mapid}/geojson({overlay})/{lon},{lat},{z}'
                       '/{width}x{height}.{fmt}')
        else:
            if auto:
                raise errors.InvalidCoordError(
                    "Must provide features if lat, lon, z are None")

            # No overlay
            pth = '/{mapid}/{lon},{lat},{z}/{width}x{height}.{fmt}'

        uri = URITemplate(self.baseuri + pth).expand(**values)
        res = self.session.get(uri)
        self.handle_http_error(res)
        return res
Ejemplo n.º 4
0
def test_mix():
    objs = (geom, feat, coll, coll2)
    res = normalize_geojson_featurecollection(objs)
    assert res['type'] == 'FeatureCollection'
    assert len(res['features']) == 5
Ejemplo n.º 5
0
def test_mult_coll():
    colls = (coll, coll)
    res = normalize_geojson_featurecollection(colls)
    assert res['type'] == 'FeatureCollection'
    assert res == coll2
Ejemplo n.º 6
0
def test_mult_feat():
    feats = (feat, feat)
    res = normalize_geojson_featurecollection(feats)
    assert res['type'] == 'FeatureCollection'
    assert res == coll2
Ejemplo n.º 7
0
def test_mult_geom():
    geoms = (geom, geom)
    res = normalize_geojson_featurecollection(geoms)
    assert res['type'] == 'FeatureCollection'
    assert res == coll2
Ejemplo n.º 8
0
def test_coll():
    res = normalize_geojson_featurecollection(coll)
    assert res['type'] == 'FeatureCollection'
    assert res == coll
Ejemplo n.º 9
0
    def image(self,
              username,
              style_id,
              lon=None,
              lat=None,
              zoom=None,
              features=None,
              pitch=0,
              bearing=0,
              width=600,
              height=600,
              twox=False,
              sort_keys=False,
              attribution=None,
              logo=None,
              before_layer=None):

        params = {}
        if attribution is not None:
            params['attribution'] = 'true' if attribution else 'false'
        if logo is not None:
            params['logo'] = 'true' if logo else 'false'
        if before_layer is not None:
            params['before_layer'] = before_layer

        if lon is not None and lat is not None and zoom is not None:
            auto = False
            lat = validate_lat(lat)
            lon = validate_lon(lon)
        else:
            auto = True

        pitch = validate_pitch(pitch)
        bearing = validate_bearing(bearing)
        width = validate_image_size(width)
        height = validate_image_size(height)

        values = dict(username=username,
                      style_id=style_id,
                      pitch=str(pitch),
                      bearing=str(bearing),
                      lon=str(lon),
                      lat=str(lat),
                      zoom=str(zoom),
                      auto=auto,
                      twox='@2x' if twox else '',
                      width=str(width),
                      height=str(height))

        if features:
            collection = normalize_geojson_featurecollection(features)
            values['overlay'] = json.dumps(collection,
                                           separators=(',', ':'),
                                           sort_keys=sort_keys)

            validate_overlay(values['overlay'])

            pth = '/{username}/{style_id}/static/geojson({overlay})/'
            if auto:
                # TODO what about {bearing} and {pitch}
                pth += 'auto/{width}x{height}{twox}'
            else:
                pth += '{lon},{lat},{zoom},{bearing},{pitch}/{width}x{height}{twox}'
        else:
            if auto:
                raise errors.InvalidCoordError(
                    "Must provide features or lat, lon, z")

            # No overlay
            pth = (
                '/{username}/{style_id}/static/'
                '{lon},{lat},{zoom},{bearing},{pitch}/{width}x{height}{twox}')

        uri = URITemplate(self.baseuri + pth).expand(**values)
        res = self.session.get(uri, params=params)
        self.handle_http_error(res)
        return res
Ejemplo n.º 10
0
    def image(self,
              username,
              style_id,
              lon=None,
              lat=None,
              zoom=None,
              features=None,
              pitch=0,
              bearing=0,
              width=600,
              height=600,
              retina=None,
              sort_keys=False,
              attribution=None,
              logo=None,
              before_layer=None,
              twox=None):

        params = {}
        if attribution is not None:
            params['attribution'] = 'true' if attribution else 'false'
        if logo is not None:
            params['logo'] = 'true' if logo else 'false'
        if before_layer is not None:
            params['before_layer'] = before_layer

        # twox as a deprecated alias for retina
        if retina is None:
            if twox is not None:
                warnings.warn('twox is a deprecated alias for retina',
                              errors.MapboxDeprecationWarning)
                retina = twox
        else:
            if twox is not None:
                raise errors.ValidationError(
                    'Conflicting args; Remove twox and use retina')

        if lon is not None and lat is not None and zoom is not None:
            auto = False
            lat = validate_lat(lat)
            lon = validate_lon(lon)
        else:
            auto = True

        pitch = validate_pitch(pitch)
        bearing = validate_bearing(bearing)
        width = validate_image_size(width)
        height = validate_image_size(height)

        values = dict(username=username,
                      style_id=style_id,
                      pitch=str(pitch),
                      bearing=str(bearing),
                      lon=str(lon),
                      lat=str(lat),
                      zoom=str(zoom),
                      auto=auto,
                      width=str(width),
                      height=str(height))

        if features:
            collection = normalize_geojson_featurecollection(features)
            values['overlay'] = json.dumps(collection,
                                           separators=(',', ':'),
                                           sort_keys=sort_keys)

            validate_overlay(values['overlay'])

            pth = '/{username}/{style_id}/static/geojson({overlay})/'
            if auto:
                # TODO what about {bearing} and {pitch}
                pth += 'auto/{width}x{height}'
            else:
                pth += '{lon},{lat},{zoom},{bearing},{pitch}/{width}x{height}'
        else:
            if auto:
                raise errors.InvalidCoordError(
                    "Must provide features or lat, lon, z")

            # No overlay
            pth = ('/{username}/{style_id}/static/'
                   '{lon},{lat},{zoom},{bearing},{pitch}/{width}x{height}')

        uri = URITemplate(self.baseuri + pth).expand(**values)

        # @2x handled separately to avoid HTML escaping the ampersand
        if retina:
            uri += '@2x'

        res = self.session.get(uri, params=params)
        self.handle_http_error(res)
        return res