Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_fake_datasets(num_datasets):
start_time = datetime.datetime(2001, 2, 15)
delta = datetime.timedelta(days=16)
for i in range(num_datasets):
fakedataset = MagicMock()
fakedataset.extent = geometry.box(left=grid, bottom=-grid, right=2*grid, top=-2*grid, crs=fakecrs)
fakedataset.center_time = start_time + (delta * i)
yield fakedataset
def test_ops():
box1 = geometry.box(10, 10, 30, 30, crs=epsg4326)
box2 = geometry.box(20, 10, 40, 30, crs=epsg4326)
box3 = geometry.box(20, 10, 40, 30, crs=epsg4326)
box4 = geometry.box(40, 10, 60, 30, crs=epsg4326)
no_box = None
assert box1 != box2
assert box2 == box3
assert box3 != no_box
union1 = box1.union(box2)
assert union1.area == 600.0
inter1 = box1.intersection(box2)
assert bool(inter1)
assert inter1.area == 200.0
inter2 = box1.intersection(box4)
assert not bool(inter2)
import datetime
# ----- fake a datacube -----
# e.g. let there be a dataset that coincides with a grid cell
fakecrs = geometry.CRS('EPSG:4326')
grid = 100 # spatial frequency in crs units
pixel = 10 # square pixel linear dimension in crs units
# if cell(0,0) has lower left corner at grid origin,
# and cell indices increase toward upper right,
# then this will be cell(1,-2).
gridspec = GridSpec(crs=fakecrs, tile_size=(grid, grid), resolution=(-pixel, pixel)) # e.g. product gridspec
fakedataset = MagicMock()
fakedataset.extent = geometry.box(left=grid, bottom=-grid, right=2*grid, top=-2*grid, crs=fakecrs)
fakedataset.center_time = t = datetime.datetime(2001, 2, 15)
fakeindex = PickableMock()
fakeindex._db = None
fakeindex.datasets.get_field_names.return_value = ['time'] # permit query on time
fakeindex.datasets.search_eager.return_value = [fakedataset]
# ------ test without padding ----
from datacube.api.grid_workflow import GridWorkflow
gw = GridWorkflow(fakeindex, gridspec)
# Need to force the fake index otherwise the driver manager will
# only take its _db
gw.index = fakeindex
query = dict(product='fake_product_name', time=('2001-1-1 00:00:00', '2001-3-31 23:59:59'))
assert not box1.is_empty
assert box1.area == 400.0
assert box1.boundary.length == 80.0
assert box1.centroid == geometry.point(20, 20, crs)
triangle = geometry.polygon([(10, 20), (20, 20), (20, 10), (10, 20)], crs=crs)
assert triangle.envelope == geometry.BoundingBox(10, 10, 20, 20)
outer = next(iter(box1))
assert outer.length == 80.0
box1copy = geometry.box(10, 10, 30, 30, crs=crs)
assert box1 == box1copy
assert box1.convex_hull == box1copy # NOTE: this might fail because of point order
box2 = geometry.box(20, 10, 40, 30, crs=crs)
assert box1 != box2
bbox = geometry.BoundingBox(1, 0, 10, 13)
assert bbox.width == 9
assert bbox.height == 13
assert bbox.points == [(1, 0), (1, 13), (10, 0), (10, 13)]
assert bbox.transform(Affine.identity()) == bbox
assert bbox.transform(Affine.translation(1, 2)) == geometry.BoundingBox(2, 2, 11, 15)
pt = geometry.point(3, 4, crs)
assert pt.json['coordinates'] == (3.0, 4.0)
assert 'Point' in str(pt)
assert bool(pt) is True
assert pt.__nonzero__() is True
def test_tests():
box1 = geometry.box(10, 10, 30, 30, crs=epsg4326)
box2 = geometry.box(20, 10, 40, 30, crs=epsg4326)
box3 = geometry.box(30, 10, 50, 30, crs=epsg4326)
box4 = geometry.box(40, 10, 60, 30, crs=epsg4326)
minibox = geometry.box(15, 15, 25, 25, crs=epsg4326)
assert not box1.touches(box2)
assert box1.touches(box3)
assert not box1.touches(box4)
assert box1.intersects(box2)
assert box1.intersects(box3)
assert not box1.intersects(box4)
assert not box1.crosses(box2)
assert not box1.crosses(box3)
assert not box1.crosses(box4)
def test_ops():
box1 = geometry.box(10, 10, 30, 30, crs=epsg4326)
box2 = geometry.box(20, 10, 40, 30, crs=epsg4326)
box3 = geometry.box(20, 10, 40, 30, crs=epsg4326)
box4 = geometry.box(40, 10, 60, 30, crs=epsg4326)
no_box = None
assert box1 != box2
assert box2 == box3
assert box3 != no_box
union1 = box1.union(box2)
assert union1.area == 600.0
inter1 = box1.intersection(box2)
assert bool(inter1)
assert inter1.area == 200.0
inter2 = box1.intersection(box4)
assert not bool(inter2)
assert inter2.is_empty
def test_props():
crs = epsg4326
box1 = geometry.box(10, 10, 30, 30, crs=crs)
assert box1
assert box1.is_valid
assert not box1.is_empty
assert box1.area == 400.0
assert box1.boundary.length == 80.0
assert box1.centroid == geometry.point(20, 20, crs)
triangle = geometry.polygon([(10, 20), (20, 20), (20, 10), (10, 20)], crs=crs)
assert triangle.envelope == geometry.BoundingBox(10, 10, 20, 20)
outer = next(iter(box1))
assert outer.length == 80.0
box1copy = geometry.box(10, 10, 30, 30, crs=crs)
assert box1 == box1copy
assert box1.convex_hull == box1copy # NOTE: this might fail because of point order
"""
LAYER_TEMPLATE = """
{name}
<title>{title}</title>
{abstract}
{metadata}
"""
LAYER_SPEC = {
'ls8_nbar_rgb': {
'product': 'ls8_nbar_albers',
'bands': ('red', 'green', 'blue'),
'extents': geometry.box(100, -50, 160, 0, crs=geometry.CRS('EPSG:4326')),
'time': {
'start': datetime(2013, 1, 1),
'end': datetime(2017, 1, 1),
'period': timedelta(days=0)
}
},
'ls8_l1t_rgb': {
'product': 'ls8_l1t_scene',
'bands': ('red', 'green', 'blue'),
'extents': geometry.box(100, -50, 160, 0, crs=geometry.CRS('EPSG:4326')),
'time': {
'start': datetime(2013, 1, 1),
'end': datetime(2017, 1, 1),
'period': timedelta(days=0)
}
},
raise ValueError('Unknown CRS')
crs_var.semi_major_axis = crs.semi_major_axis
crs_var.semi_minor_axis = crs.semi_minor_axis
crs_var.inverse_flattening = crs.inverse_flattening
crs_var.crs_wkt = crs.wkt
crs_var.spatial_ref = crs.wkt
dims = crs.dimensions
xres, xoff = data_resolution_and_offset(nco[dims[1]])
yres, yoff = data_resolution_and_offset(nco[dims[0]])
crs_var.GeoTransform = [xoff, xres, 0.0, yoff, 0.0, yres]
left, right = nco[dims[1]][0] - 0.5 * xres, nco[dims[1]][-1] + 0.5 * xres
bottom, top = nco[dims[0]][0] - 0.5 * yres, nco[dims[0]][-1] + 0.5 * yres
_write_geographical_extents_attributes(nco, geometry.box(left, bottom, right, top, crs=crs))
return crs_var