Skip to content

Commit

Permalink
Merge pull request #542 from jbouffard/feature/temporal-resolution
Browse files Browse the repository at this point in the history
Better Temporal Resolution Control When Writing Layer
  • Loading branch information
Jacob Bouffard committed Nov 16, 2017
2 parents 151b6c3 + 0984003 commit dcdab7e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,27 @@ class LayerWriterWrapper(attributeStore: AttributeStore, uri: String) {

private def getTemporalIndexMethod(
timeString: String,
timeResolution: String,
indexStrategy: String
) =
(indexStrategy, timeString) match {
case ("zorder", "millis") => ZCurveKeyIndexMethod.byMilliseconds(1)
case ("zorder", "seconds") => ZCurveKeyIndexMethod.bySecond
case ("zorder", "minutes") => ZCurveKeyIndexMethod.byMinute
case ("zorder", "hours") => ZCurveKeyIndexMethod.byHour
case ("zorder", "days") => ZCurveKeyIndexMethod.byDay
case ("zorder", "months") => ZCurveKeyIndexMethod.byMonth
case ("zorder", "years") => ZCurveKeyIndexMethod.byYear
case ("hilbert", _) => {
(indexStrategy, timeString, timeResolution) match {
case ("zorder", "millis", null) => ZCurveKeyIndexMethod.byMilliseconds(1)
case ("zorder", "millis", r) => ZCurveKeyIndexMethod.byMilliseconds(r.toLong)
case ("zorder", "seconds", null) => ZCurveKeyIndexMethod.bySecond
case ("zorder", "seconds", r) => ZCurveKeyIndexMethod.bySeconds(r.toInt)
case ("zorder", "minutes", null) => ZCurveKeyIndexMethod.byMinute
case ("zorder", "minutes", r) => ZCurveKeyIndexMethod.byMinutes(r.toInt)
case ("zorder", "hours", null) => ZCurveKeyIndexMethod.byHour
case ("zorder", "hours", r) => ZCurveKeyIndexMethod.byHours(r.toInt)
case ("zorder", "days", null) => ZCurveKeyIndexMethod.byDay
case ("zorder", "days", r) => ZCurveKeyIndexMethod.byDays(r.toInt)
case ("zorder", "weeks", null) => ZCurveKeyIndexMethod.byDays(7)
case ("zorder", "weeks", r) => ZCurveKeyIndexMethod.byDays(r.toInt * 7)
case ("zorder", "months", null) => ZCurveKeyIndexMethod.byMonth
case ("zorder", "months", r) => ZCurveKeyIndexMethod.byMonths(r.toInt)
case ("zorder", "years", null) => ZCurveKeyIndexMethod.byYear
case ("zorder", "years", r) => ZCurveKeyIndexMethod.byYears(r.toInt)
case ("hilbert", _, _) => {
timeString.split(",") match {
case Array(minDate, maxDate, resolution) =>
HilbertKeyIndexMethod(ZonedDateTime.parse(minDate),
Expand Down Expand Up @@ -85,14 +95,15 @@ class LayerWriterWrapper(attributeStore: AttributeStore, uri: String) {
layerName: String,
temporalRDD: TiledRasterLayer[SpaceTimeKey],
timeString: String,
timeResolution: String,
indexStrategy: String
): Unit = {
val id =
temporalRDD.zoomLevel match {
case Some(zoom) => LayerId(layerName, zoom)
case None => LayerId(layerName, 0)
}
val indexMethod = getTemporalIndexMethod(timeString, indexStrategy)
val indexMethod = getTemporalIndexMethod(timeString, timeResolution, indexStrategy)
layerWriter.write(id, temporalRDD.rdd, indexMethod)
}
}
14 changes: 14 additions & 0 deletions geopyspark/geotrellis/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def write(uri,
tiled_raster_layer,
index_strategy=IndexingMethod.ZORDER,
time_unit=None,
time_resolution=None,
store=None):
"""Writes a tile layer to a specified destination.
Expand All @@ -250,6 +251,15 @@ def write(uri,
each index. Meaning, what time intervals are used to seperate each record. While this is
set to ``None`` as default, it must be set if saving spatial-temporal data.
Depending on the indexing method chosen, different time units are used.
time_resolution (str or int, optional): Determines how data for each ``time_unit`` should be
grouped together. By default, no grouping will occur.
As an example, having a ``time_unit`` of ``WEEKS`` and a ``time_resolution`` of 5 will
cause the data to be grouped and stored together in units of 5 weeks. If however
``time_resolution`` is not specified, then the data will be grouped and stored in units
of single weeks.
This value can either be an ``int`` or a string representation of an ``int``.
store (str or :class:`~geopyspark.geotrellis.catalog.AttributeStore`, optional):
``AttributeStore`` instance or URI for layer metadata lookup.
"""
Expand All @@ -274,9 +284,13 @@ def write(uri,
IndexingMethod(index_strategy).value)

elif tiled_raster_layer.layer_type == LayerType.SPACETIME:
if time_resolution:
time_resolution = str(time_resolution)

writer.writeTemporal(layer_name,
tiled_raster_layer.srdd,
TimeUnit(time_unit).value,
time_resolution,
IndexingMethod(index_strategy).value)
else:
raise ValueError("Cannot write {} layer".format(tiled_raster_layer.layer_type))
Expand Down
1 change: 1 addition & 0 deletions geopyspark/geotrellis/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class TimeUnit(Enum):
MINUTES = 'minutes'
HOURS = 'hours'
DAYS = 'days'
WEEKS = 'weeks'
MONTHS = 'months'
YEARS = 'years'

Expand Down

0 comments on commit dcdab7e

Please sign in to comment.