Esempio n. 1
0
 def __call__(self, tile: Tile) -> Tile:
     if self.content_type is None and tile.content_encoding is None and tile.data is not None:
         data = str(tile.data)
         if data.startswith("{"):
             tile.content_type = "application/json"
         elif data.startswith("\x89PNG\x0d\x0a\x1a\x0a"):
             tile.content_type = "image/png"
         elif data.startswith("\xff\xd8"):
             tile.content_type = "image/jpeg"
     else:
         tile.content_type = self.content_type
     return tile
Esempio n. 2
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     try:
         tile.content_type = self.content_type
         tile.data = self.db[str(tile.tilecoord).encode("utf-8")]
         return tile
     except KeyError:
         return None
def _decode_tile(data: bytes, tile: Tile) -> None:
    """Decode a tile."""
    image_len = struct.unpack("q", data[:8])[0]
    tile.data = data[8:(image_len + 8)]
    other = json.loads((data[(8 + image_len):]).decode("utf-8"))
    tile.content_encoding = other["content_encoding"]
    tile.content_type = other["content_type"]
Esempio n. 4
0
 def __call__(self, tile: Tile) -> Tile:
     if tile.content_type != self.content_type:
         assert tile.data is not None
         bytes_io = BytesIO()
         PIL.Image.open(BytesIO(tile.data)).save(bytes_io, self.format, **self.kwargs)
         tile.content_type = self.content_type
         tile.data = bytes_io.getvalue()
     return tile
Esempio n. 5
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     try:
         tile.data = self.tiles[tile.tilecoord]
     except KeyError:
         return None
     if self.content_type is not None:
         tile.content_type = self.content_type
     return tile
Esempio n. 6
0
 def get_one(self, tile):
     if self.bounding_pyramid is not None:
         if tile.tilecoord not in self.bounding_pyramid:
             return None
     try:
         tile = Tile(tile.tilecoord, data=self.tiles[tile.tilecoord])
     except KeyError:
         return None
     if self.content_type is not None:
         tile.content_type = self.content_type
     return tile
Esempio n. 7
0
 def get_one(self, tile):
     if self.bounding_pyramid is not None:
         if tile.tilecoord not in self.bounding_pyramid:
             return None
     try:
         tile = Tile(tile.tilecoord, data=self.tiles[tile.tilecoord])
     except KeyError:
         return None
     if self.content_type is not None:
         tile.content_type = self.content_type
     return tile
Esempio n. 8
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         response = self.client.get_object(Bucket=self.bucket, Key=key_name)
         tile.data = response["Body"].read()
         tile.content_encoding = response.get("ContentEncoding")
         tile.content_type = response.get("ContentType")
     except botocore.exceptions.ClientError as exc:
         if _get_status(exc) == 404:
             return None
         else:
             tile.error = exc
     return tile
Esempio n. 9
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     key_name = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     try:
         blob = self.container_client.get_blob_client(blob=key_name)
         tile.data = blob.download_blob().readall()
         properties = blob.get_blob_properties()
         tile.content_encoding = properties.content_settings.content_encoding
         tile.content_type = properties.content_settings.content_type
     except ResourceNotFoundError:
         return None
     except Exception as exc:
         LOGGER.exception(exc)
         tile.error = exc
     return tile
Esempio n. 10
0
 def get_one(self, tile: Tile) -> Optional[Tile]:
     try:
         filename = self.tilelayout.filename(tile.tilecoord, tile.metadata)
     except Exception as e:
         tile.error = e
         return tile
     try:
         with open(filename, "rb") as file:
             tile.data = file.read()
         if self.content_type is not None:
             tile.content_type = self.content_type
         return tile
     except OSError as e:
         if e.errno == errno.ENOENT:
             return None
         else:
             raise
Esempio n. 11
0
 def __call__(self, tile: Tile) -> Tile:
     assert tile.data is not None
     image = PIL.Image.open(BytesIO(tile.data))
     for tilestore in self.tilestores:
         t = tilestore.get_one(Tile(tile.tilecoord))
         if t is not None:
             assert t.data is not None
             image2 = PIL.Image.open(BytesIO(t.data))
             image.paste(image2, None, image2)
     content_type = self.content_type
     if content_type is None:
         content_type = tile.content_type
     assert content_type is not None
     bytes_io = BytesIO()
     image.save(bytes_io, FORMAT_BY_CONTENT_TYPE[content_type], **self.kwargs)
     tile.content_type = content_type
     tile.data = bytes_io.getvalue()
     return tile
Esempio n. 12
0
    def get_one(self, tile: Tile) -> Optional[Tile]:
        if tile is None:
            return None
        if self.bounding_pyramid is not None:
            if tile.tilecoord not in self.bounding_pyramid:
                return None
        tilelayout = self.tilelayouts[hash(tile.tilecoord) % len(self.tilelayouts)]
        try:
            url = tilelayout.filename(tile.tilecoord, tile.metadata)
        except Exception as e:
            tile.error = e
            return tile

        logger.info("GET %s", url)
        try:
            response = self.session.get(url)
            if response.status_code in (404, 204):
                return None
            tile.content_encoding = response.headers.get("Content-Encoding")
            tile.content_type = response.headers.get("Content-Type")
            if response.status_code < 300:
                if response.status_code != 200:
                    tile.error = f"Unsupported status code {response.status_code}: {response.reason}"
                if tile.content_type:
                    if tile.content_type.startswith("image/"):
                        tile.data = response.content
                    else:
                        tile.error = response.text
                else:
                    if self.allows_no_contenttype:
                        tile.data = response.content
                    else:
                        tile.error = "The Content-Type header is missing"

            else:
                tile.error = response.reason
        except requests.exceptions.RequestException as e:
            tile.error = e
        return tile
Esempio n. 13
0
 def get_all(self):
     for tilecoord, data in self.tiles.iteritems():
         tile = Tile(tilecoord, data=data)
         if self.content_type is not None:
             tile.content_type = self.content_type
         yield tile
Esempio n. 14
0
 def get_all(self):
     for tilecoord, data in self.tiles.iteritems():
         tile = Tile(tilecoord, data=data)
         if self.content_type is not None:
             tile.content_type = self.content_type
         yield tile