Exemplo 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
Exemplo n.º 2
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 and lat and z:
            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:
            values['overlay'] = json.dumps(
                {
                    'type': 'FeatureCollection',
                    'features': features
                },
                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
Exemplo n.º 3
0
 def _validate_lon(self, val):
     if val < -180 or val > 180:
         raise errors.InvalidCoordError(
             "Longitude must be between -180 and 180")
     return val
Exemplo n.º 4
0
 def _validate_lat(self, val):
     if val < -85.0511 or val > 85.0511:
         raise errors.InvalidCoordError(
             "Latitude must be between -85.0511 and 85.0511")
     return val
Exemplo n.º 5
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
Exemplo n.º 6
0
def validate_bearing(val):
    if val < 0 or val > 365:
        raise errors.InvalidCoordError("Bearing must be between 0 and 365")
    return val
Exemplo n.º 7
0
def validate_pitch(val):
    if val < 0 or val > 60:
        raise errors.InvalidCoordError("Pitch must be between 0 and 60")
    return val
Exemplo n.º 8
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