def test_zonal_statistics_median_datacube(self): layer = self.create_spacetime_layer() imagecollection = GeotrellisTimeSeriesImageCollection(gps.Pyramid({0: layer}), InMemoryServiceRegistry()) polygon = Polygon(shell=[ (0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0) ]) result = imagecollection.zonal_statistics(polygon, "median") assert result.data == {'2017-09-25T11:37:00Z': [[1.0, 2.0]]} covjson = result.to_covjson() assert covjson["ranges"] == { "band0": { "type": "NdArray", "dataType": "float", "axisNames": ["t", "composite"], "shape": (1, 1), "values": [1.0] }, "band1": { "type": "NdArray", "dataType": "float", "axisNames": ["t", "composite"], "shape": (1, 1), "values": [2.0] } }
def test_zonal_statistics_datacube(self): layer = self.create_spacetime_layer() imagecollection = GeotrellisTimeSeriesImageCollection(gps.Pyramid({0: layer}), InMemoryServiceRegistry()) polygon = Polygon(shell=[ (0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0) ]) polygon2 = Polygon(shell=[ (2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0), (2.0, 2.0) ]) regions = GeometryCollection([polygon, MultiPolygon([polygon2])]) for use_file in [True,False]: with self.subTest(): if use_file: with NamedTemporaryFile(delete=False,suffix='.json',mode='r+') as fp: json.dump(mapping(regions),fp) regions_serialized = fp.name else: regions_serialized = regions result = imagecollection.zonal_statistics(regions_serialized, "mean") assert result.data == { '2017-09-25T11:37:00Z': [[1.0, 2.0], [1.0, 2.0]] } result._regions = regions covjson = result.to_covjson() assert covjson["ranges"] == { "band0": { "type": "NdArray", "dataType": "float", "axisNames": ["t", "composite"], "shape": (1, 2), "values": [1.0, 1.0] }, "band1": { "type": "NdArray", "dataType": "float", "axisNames": ["t", "composite"], "shape": (1, 2), "values": [2.0, 2.0] }, }
def test_zonal_statistics_for_unsigned_byte_layer(self): layer = self.create_spacetime_unsigned_byte_layer() # layer.to_spatial_layer().save_stitched('/tmp/unsigned_byte_layer.tif') imagecollection = GeotrellisTimeSeriesImageCollection(gps.Pyramid({0: layer}), InMemoryServiceRegistry()) polygon = Polygon(shell=[ (0.0, 0.0), (2.0, 0.0), (2.0, 4.0), (0.0, 4.0), (0.0, 0.0) ]) result = imagecollection.zonal_statistics(polygon, "mean") # FIXME: the Python implementation doesn't return a time zone (Z) assert result.data == {'2017-09-25T11:37:00': [[220.0]]} covjson = result.to_covjson() assert covjson["ranges"] == { "band0": { "type": "NdArray", "dataType": "float", "axisNames": ["t", "composite"], "shape": (1, 1), "values": [220.0] } }