Skip to content

Commit

Permalink
Merge pull request #668 from jbouffard/feature/rasterframes
Browse files Browse the repository at this point in the history
RasterFrames Methods
  • Loading branch information
Jacob Bouffard committed Aug 9, 2018
2 parents 48e2e36 + c571b50 commit 6f617b7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SpatialTiledRasterLayer(
case GlobalLayout(tileSize, zoom, threshold) =>
val scheme = new ZoomedLayoutScheme(crs, tileSize, threshold)
val (_, reprojected) = TileRDDReproject(rdd, crs, Right(scheme.levelForZoom(zoom).layout), resampleMethod, partitioner)
SpatialTiledRasterLayer(Some(zoom), reprojected)
SpatialTiledRasterLayer(zoom, reprojected)

case LocalLayout(tileCols, tileRows) =>
val (_, reprojected) = rdd.reproject(crs, FloatingLayoutScheme(tileCols, tileRows), resampleMethod, partitioner)
Expand Down Expand Up @@ -546,6 +546,15 @@ object SpatialTiledRasterLayer {
SpatialTiledRasterLayer(Some(zoomLevel), tileLayer)
}

def apply(
zoomLevel: Integer,
rdd: RDD[(SpatialKey, MultibandTile)] with Metadata[TileLayerMetadata[SpatialKey]]
): SpatialTiledRasterLayer =
zoomLevel match {
case i: Integer => apply(Some(i.toInt), rdd)
case null => apply(None, rdd)
}

def apply(
zoomLevel: Option[Int],
rdd: RDD[(SpatialKey, MultibandTile)] with Metadata[TileLayerMetadata[SpatialKey]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class TemporalTiledRasterLayer(
case GlobalLayout(tileSize, zoom, threshold) =>
val scheme = new ZoomedLayoutScheme(crs, tileSize, threshold)
val (_, reprojected) = TileRDDReproject(rdd, crs, Right(scheme.levelForZoom(zoom).layout), resampleMethod, partitioner)
TemporalTiledRasterLayer(Some(zoom), reprojected)
TemporalTiledRasterLayer(zoom, reprojected)

case LocalLayout(tileCols, tileRows) =>
val (_, reprojected) = rdd.reproject(crs, FloatingLayoutScheme(tileCols, tileRows), resampleMethod, partitioner)
Expand Down Expand Up @@ -645,6 +645,15 @@ object TemporalTiledRasterLayer {
TemporalTiledRasterLayer(Some(zoomLevel), tileLayer)
}

def apply(
zoomLevel: Integer,
rdd: RDD[(SpaceTimeKey, MultibandTile)] with Metadata[TileLayerMetadata[SpaceTimeKey]]
): TemporalTiledRasterLayer =
zoomLevel match {
case i: Integer => apply(Some(i.toInt), rdd)
case null => apply(None, rdd)
}

def apply(
zoomLevel: Option[Int],
rdd: RDD[(SpaceTimeKey, MultibandTile)] with Metadata[TileLayerMetadata[SpaceTimeKey]]
Expand Down
62 changes: 62 additions & 0 deletions geopyspark/geotrellis/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,68 @@ def from_numpy_rdd(cls, layer_type, numpy_rdd, metadata, zoom_level=None):

return cls(layer_type, srdd)

@classmethod
def from_rasterframe(cls, rasterframe, zoom_level=None):
"""Creates a ``TiledRasterLayer from a ``pyrasterframes.RasterFrame``.
Note:
``pyrasterframes`` needs to initialized via the ``.withRasterFrames()``
extension method on the active ``SparkSession`` object in order to
use this method.
Args:
rasterframe (pyrasterframes.RasterFrame): The target ``RasterFrame`` that
will be converted into a ``TiledRasterLayer``.
zoom_level(int, optional): The ``zoom_level`` the resulting `TiledRasterLayer` should
have. If ``None``, then the returned layer's ``zoom_level`` will be ``None``.
Returns:
:class:`~geopyspark.geotrellis.layer.TiledRasterLayer`
"""

pysc = get_spark_context()

spatial_tiled_raster_layer = pysc._gateway.jvm.geopyspark.geotrellis.SpatialTiledRasterLayer
temporal_tiled_raster_layer = pysc._gateway.jvm.geopyspark.geotrellis.TemporalTiledRasterLayer

jrfctx = rasterframe._jrfctx

if rasterframe.temporalKeyColumn():
srdd = temporal_tiled_raster_layer.apply(zoom_level, jrfctx.toTemporalMultibandTileLayerRDD(rasterframe._jdf))
layer_type = LayerType.SPACETIME
else:
srdd = spatial_tiled_raster_layer.apply(zoom_level, jrfctx.toSpatialMultibandTileLayerRDD(rasterframe._jdf))
layer_type = LayerType.SPATIAL

return cls(layer_type, srdd)

def to_rasterframe(self, num_bands):
"""Converts a ``TiledRasterLayer`` to a ``pyrasterframes.RasterFrame``.
Note:
``pyrasterframes`` needs to initialized via the ``.withRasterFrames()``
extension method on the active ``SparkSession`` object in order to
use this method.
Args:
num_bands (int): The number of bands the ``TiledRasterLayer`` has.
Returns:
:class:`~geopyspark.geotrellis.layer.TiledRasterLayer`
"""

try:
from pyrasterframes import RasterFrame
except ImportError:
raise ImportError("pyrasterframes must be installed in order to use the to_rasterframe method")

rf_context = get_spark_context()._rf_context

if rf_context:
return RasterFrame(rf_context._jrfctx.asRF(self.srdd.rdd(), num_bands), rf_context._spark_session)
else:
raise AttributeError("RasterFrames has not been enabled in the active SparkSession")

def to_numpy_rdd(self):
"""Converts a ``TiledRasterLayer`` to a numpy RDD.
Expand Down

0 comments on commit 6f617b7

Please sign in to comment.