예제 #1
0
    def read(self, col, row, zdt=None, zoom=None):
        """Reads a single ``Tile`` from a GeoTrellis catalog.
           When requesting a tile that does not exist, ``None`` will be returned.

        Args:
            col (int): The col number of the tile within the layout. Cols run east to west.
            row (int): The row number of the tile within the layout. Row run north to south.
            zdt (``datetime.datetime``): The time stamp of the tile if the data is spatial-temporal.
                This is represented as a ``datetime.datetime.`` instance.  The default value is,
                ``None``. If ``None``, then only the spatial area will be queried.
            zoom (int, optional): The zoom level of the layer that is to be read.
                Defaults to ``self.zoom``

        Returns:
            :class:`~geopyspark.geotrellis.Tile`
        """

        zoom = zoom or self.zoom or 0
        zoom = zoom and int(zoom)

        if zdt:
            if zdt.tzinfo:
                zdt = zdt.astimezone(pytz.utc).isoformat()
            else:
                zdt = zdt.replace(tzinfo=pytz.utc).isoformat()

        value = self.wrapper.readTile(self.layer_name, zoom, col, row, zdt)
        return value and multibandtile_decoder(value)
예제 #2
0
    def renderEncoded(self, scala_array):
        """A function to convert an array to an image.

        Args:
            scala_array: A linear array of bytes representing the protobuf-encoded
                contents of a tile

        Returns:
            bytes representing an image
        """
        try:
            tile = multibandtile_decoder(scala_array)
            image = self.render_function(tile)
            bio = io.BytesIO()
            image.save(bio, 'PNG')
            return bio.getvalue()
        except Exception:
            from traceback import print_exc
            print_exc()
예제 #3
0
    def compositeEncoded(self, all_scala_arrays): # return `bytes`
        """A function to convert an array to an image.

        Args:
            all_scala_arrays (array of bytes): An array containing the encoded
                representations of the incoming tiles

        Returns:
            [bytes] representing an image
        """

        try:
            tiles = [multibandtile_decoder(scala_array) for scala_array in all_scala_arrays]
            image = self.composite_function(tiles)
            bio = io.BytesIO()
            image.save(bio, 'PNG')
            return bio.getvalue()
        except Exception:
            from traceback import print_exc
            print_exc()
예제 #4
0
def read_value(layer_type,
               uri,
               layer_name,
               layer_zoom,
               col,
               row,
               zdt=None,
               options=None,
               **kwargs):
    """Reads a single ``Tile`` from a GeoTrellis catalog.
    Unlike other functions in this module, this will not return a ``TiledRasterLayer``, but rather a
    GeoPySpark formatted raster. This is the function to use when creating a tile server.

    Note:
        When requesting a tile that does not exist, ``None`` will be returned.

    Args:
        layer_type (str or :class:`geopyspark.geotrellis.constants.LayerType`): What the spatial type
            of the geotiffs are. This is represented by either constants within ``LayerType`` or by
            a string.
        uri (str): The Uniform Resource Identifier used to point towards the desired GeoTrellis
            catalog to be read from. The shape of this string varies depending on backend.
        layer_name (str): The name of the GeoTrellis catalog to be read from.
        layer_zoom (int): The zoom level of the layer that is to be read.
        col (int): The col number of the tile within the layout. Cols run east to west.
        row (int): The row number of the tile within the layout. Row run north to south.
        zdt (``datetime.datetime``): The time stamp of the tile if the data is spatial-temporal.
            This is represented as a ``datetime.datetime.`` instance.  The default value is,
            ``None``. If ``None``, then only the spatial area will be queried.
        options (dict, optional): Additional parameters for reading the tile for specific backends.
            The dictionary is only used for ``Cassandra`` and ``HBase``, no other backend requires
            this to be set.
        **kwargs: The optional parameters can also be set as keywords arguments. The keywords must
            be in camel case. If both options and keywords are set, then the options will be used.

    Returns:
        :class:`~geopyspark.geotrellis.Tile`
    """

    if not _in_bounds(layer_type, uri, layer_name, layer_zoom, col, row):
        return None
    else:
        options = options or kwargs or {}

        if zdt:
            zdt = zdt.astimezone(pytz.utc).isoformat()
        else:
            zdt = ''

        if uri not in _mapped_cached:
            _construct_catalog(get_spark_context(), uri, options)

        cached = _mapped_cached[uri]

        key = map_key_input(LayerType(layer_type).value, True)

        values = cached.value_reader.readTile(key,
                                              layer_name,
                                              layer_zoom,
                                              col,
                                              row,
                                              zdt)

        return multibandtile_decoder(values)