Skip to content

Commit

Permalink
Merge pull request #673 from jbouffard/feature/layer-overwrite
Browse files Browse the repository at this point in the history
update_layer Function for catalog
  • Loading branch information
Jacob Bouffard committed Oct 18, 2018
2 parents de297e5 + 811d6d5 commit 00b3e59
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ class LayerWriterWrapper(attributeStore: AttributeStore, uri: String) {
case _ => throw new Exception
}

private def getLayerId(layerName: String, layer: TiledRasterLayer[_]): LayerId =
layer.zoomLevel match {
case Some(zoom) => LayerId(layerName, zoom)
case None => LayerId(layerName, 0)
}

def writeSpatial(
layerName: String,
spatialRDD: TiledRasterLayer[SpatialKey],
indexStrategy: String
): Unit = {
val id =
spatialRDD.zoomLevel match {
case Some(zoom) => LayerId(layerName, zoom)
case None => LayerId(layerName, 0)
}
val id = getLayerId(layerName, spatialRDD)
val indexMethod = getSpatialIndexMethod(indexStrategy)

layerWriter.write(id, spatialRDD.rdd, indexMethod)
}

Expand All @@ -93,12 +96,21 @@ class LayerWriterWrapper(attributeStore: AttributeStore, uri: String) {
timeResolution: String,
indexStrategy: String
): Unit = {
val id =
temporalRDD.zoomLevel match {
case Some(zoom) => LayerId(layerName, zoom)
case None => LayerId(layerName, 0)
}
val id = getLayerId(layerName, temporalRDD)
val indexMethod = getTemporalIndexMethod(timeString, timeResolution, indexStrategy)

layerWriter.write(id, temporalRDD.rdd, indexMethod)
}

def updateSpatial(
layerName: String,
spatialRDD: TiledRasterLayer[SpatialKey]
): Unit =
layerWriter.update(getLayerId(layerName, spatialRDD), spatialRDD.rdd)

def updateTemporal(
layerName: String,
temporalRDD: TiledRasterLayer[SpaceTimeKey]
): Unit =
layerWriter.update(getLayerId(layerName, temporalRDD), temporalRDD.rdd)
}
50 changes: 47 additions & 3 deletions geopyspark/geotrellis/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from geopyspark.geotrellis.layer import TiledRasterLayer


__all__ = ["read_layer_metadata", "read_value", "query", "write", "AttributeStore"]
__all__ = ["read_layer_metadata", "read_value", "query", "write", "update_layer", "AttributeStore"]

"""Instances of previously used AttributeStore keyed by their URI """
_cached_stores = {}
Expand Down Expand Up @@ -220,10 +220,9 @@ def write(uri,
uri (str): The Uniform Resource Identifier used to point towards the desired location for
the tile layer to written to. The shape of this string varies depending on backend.
layer_name (str): The name of the new, tile layer.
layer_zoom (int): The zoom level the layer should be saved at.
tiled_raster_layer (:class:`~geopyspark.geotrellis.layer.TiledRasterLayer`): The
``TiledRasterLayer`` to be saved.
index_strategy (str or :class:`~geopyspark.geotrellis.constants.IndexingMethod`): The
index_strategy (str or :class:`~geopyspark.geotrellis.constants.IndexingMethod`, optional): The
method used to orginize the saved data. Depending on the type of data within the layer,
only certain methods are available. Can either be a string or a ``IndexingMethod``
attribute. The default method used is, ``IndexingMethod.ZORDER``.
Expand Down Expand Up @@ -277,6 +276,51 @@ def write(uri,
raise ValueError("Cannot write {} layer".format(tiled_raster_layer.layer_type))


def update_layer(uri,
layer_name,
tiled_raster_layer,
store=None):
"""Updates a pre-existing layer with a new one by merging the values of the two
layers together.
Note: This function will throw an error if one of the following conditions are met:
- The specified layer does not exist
- The two layers have differnt types (cell type, layer type, etc.)
- The two layers :class:`~geopyspark.geotrellis.Bounds` do not intersect
Args:
uri (str): The Uniform Resource Identifier used to point towards the desired location for
the tile layer to written to. The shape of this string varies depending on backend.
layer_name (str): The name of the new, tile layer.
tiled_raster_layer (:class:`~geopyspark.geotrellis.layer.TiledRasterLayer`): The
``TiledRasterLayer`` to be saved.
store (str or :class:`~geopyspark.geotrellis.catalog.AttributeStore`, optional):
``AttributeStore`` instance or URI for layer metadata lookup.
"""

if tiled_raster_layer.zoom_level is None:
Log.warn(tiled_raster_layer.pysc, "The given layer doesn't not have a zoom_level. Writing to zoom 0.")

if store:
store = AttributeStore.build(store)
else:
store = AttributeStore.cached(uri)

pysc = tiled_raster_layer.pysc

writer = pysc._gateway.jvm.geopyspark.geotrellis.io.LayerWriterWrapper(
store.wrapper.attributeStore(), uri)

if tiled_raster_layer.layer_type == LayerType.SPATIAL:
writer.updateSpatial(layer_name, tiled_raster_layer.srdd)

elif tiled_raster_layer.layer_type == LayerType.SPACETIME:
writer.updateTemporal(layer_name, tiled_raster_layer.srdd)

else:
raise ValueError("Cannot use {} layer for overwrite".format(tiled_raster_layer.layer_type))


class AttributeStore(object):
"""AttributeStore provides a way to read and write GeoTrellis layer attributes.
Expand Down

0 comments on commit 00b3e59

Please sign in to comment.