Skip to content

Commit

Permalink
Merge pull request #678 from phloem7/write-all-pyramids
Browse files Browse the repository at this point in the history
Write all pyramids
  • Loading branch information
Jacob Bouffard committed Oct 17, 2018
2 parents 575be04 + 772be94 commit de297e5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
42 changes: 41 additions & 1 deletion geopyspark/geotrellis/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
SpaceTimePartitionStrategy,
RasterizerOptions)
from geopyspark.geotrellis.histogram import Histogram
from geopyspark.geotrellis.constants import (Operation,
from geopyspark.geotrellis.constants import (IndexingMethod,
Operation,
Neighborhood as nb,
ResampleMethod,
ClassificationStrategy,
Expand Down Expand Up @@ -2418,6 +2419,45 @@ def get_histogram(self):

# Keep it in non rendered form so we can do map algebra operations to it

def write(self, uri, layer_name, index_strategy=IndexingMethod.ZORDER, time_unit=None, time_resolution=None,
store=None):
"""Writes each tiled layer of the pyramid to a specified destination.
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.
index_strategy (str or :class:`~geopyspark.geotrellis.constants.IndexingMethod`): The
method used to organize 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``.
time_unit (str or :class:`~geopyspark.geotrellis.constants.TimeUnit`, optional): Which time
unit should be used when saving spatial-temporal data. This controls the resolution of
each index. Meaning, what time intervals are used to separate 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.
"""
from geopyspark import write
for layer in self.levels.values():
write(uri=uri,
layer_name=layer_name,
tiled_raster_layer=layer,
index_strategy=index_strategy,
time_unit=time_unit,
time_resolution=time_resolution,
store=store)

def __add__(self, value):
if isinstance(value, Pyramid):
return Pyramid({k: l.__add__(r) for k, l, r in _common_entries(self.levels, value.levels)})
Expand Down
19 changes: 18 additions & 1 deletion geopyspark/tests/geotrellis/tiled_layer_tests/pyramiding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
import numpy as np
import pytest

from geopyspark import geotiff
from geopyspark.geotrellis import (Extent,
ProjectedExtent,
TileLayout,
Tile,
LayoutDefinition,
GlobalLayout,
LocalLayout,
SpatialPartitionStrategy)
SpatialPartitionStrategy,
catalog)
from geopyspark.geotrellis.constants import LayerType
from geopyspark.geotrellis.layer import Pyramid, RasterLayer
from geopyspark.tests.base_test_class import BaseTestClass
from geopyspark.tests.python_test_utils import file_path


class PyramidingTest(BaseTestClass):
Expand Down Expand Up @@ -161,6 +164,20 @@ def pyramid_building_check(self, result):
previous_layout_cols = layout_cols
previous_layout_rows = layout_rows

def test_write_pyramid_layers(self):
max_zoom = 5
tif = file_path('srtm_52_11.tif')
raster_layer = geotiff.get(layer_type=LayerType.SPATIAL, uri=tif)
tiled_raster_layer = raster_layer.tile_to_layout(GlobalLayout(zoom=max_zoom), target_crs=3857)
pyramided_layer = tiled_raster_layer.pyramid()

layer_name = 'pyramid-test-layer'
uri = 'file:///' + file_path('pyramid-test-catalog')

pyramided_layer.write(uri, layer_name)

self.assertTrue(catalog.read_layer_metadata(uri, layer_name, 0))
self.assertTrue(catalog.read_layer_metadata(uri, layer_name, max_zoom))

if __name__ == "__main__":
unittest.main()

0 comments on commit de297e5

Please sign in to comment.