def test_bbox_union(): b1 = BoundingBox(0, 1, 10, 20) b2 = BoundingBox(5, 6, 11, 22) assert bbox_union([b1]) == b1 assert bbox_union([b2]) == b2 bb = bbox_union(iter([b1, b2])) assert bb == BoundingBox(0, 1, 11, 22) bb = bbox_union(iter([b2, b1] * 10)) assert bb == BoundingBox(0, 1, 11, 22)
def bounds(self): """ :rtype: rasterio.coords.BoundingBox """ bounds = self.metadata.grid_spatial['geo_ref_points'] return BoundingBox(left=bounds['ul']['x'], right=bounds['lr']['x'], top=bounds['ul']['y'], bottom=bounds['lr']['y'])
def test_boundingbox(): bb = BoundingBox(0, 3, 2, 4) assert bb.width == 2 assert bb.height == 1 assert bb.width == bb.span_x assert bb.height == bb.span_y bb = BoundingBox(0, 3, 2.1, 4) assert bb.width == 2 assert bb.height == 1 assert bb.span_x == 2.1 assert bb.width != bb.span_x assert bb.height == bb.span_y assert BoundingBox.from_xy(bb.range_x, bb.range_y) == bb assert BoundingBox.from_xy((1, 2), (10, 20)) == (1, 10, 2, 20) assert BoundingBox.from_xy((2, 1), (20, 10)) == (1, 10, 2, 20) assert BoundingBox.from_points((1, 11), (2, 22)) == (1, 11, 2, 22) assert BoundingBox.from_points((1, 22), (2, 11)) == (1, 11, 2, 22)
def subset(self, time_extent, full_extent): if self == self.FULL_LAYER_EXTENT: return full_extent if self == self.FULL_EXTENT_FOR_TIMES: return time_extent if self == self.OUTSIDE_OF_FULL_EXTENT: full_bbox = full_extent.boundingbox width = full_bbox.right - full_bbox.left height = full_bbox.top - full_bbox.bottom outside_bbox = BoundingBox( top=full_bbox.top + height, bottom=full_bbox.top + height * 0.8, left=full_bbox.left - width, right=full_bbox.right - width * 0.8, ) return geom_from_bbox(outside_bbox) if self == self.IN_FULL_BUT_OUTSIDE_OF_TIMES: outside_times = full_extent.difference(time_extent) outside_times = simplify_geom(outside_times) sub = self.CENTRAL_SUBSET_FOR_TIMES.subset( outside_times, full_extent) return sub bbox = time_extent.boundingbox width = bbox.right - bbox.left height = bbox.top - bbox.bottom if width < 0 or height < 0: print( "I think this should still work, but I haven't worked through it properly" ) # Slice on vertical coordinate (horizontal slice) centre_y = (bbox.top + bbox.bottom) / 2 if self == self.CENTRAL_SUBSET_FOR_TIMES: hslice_bbox = BoundingBox( left=bbox.left, right=bbox.right, top=centre_y + 0.02 * height, bottom=centre_y - 0.02 * height, ) hslice_geom = geom_from_bbox(hslice_bbox) hslice_geom = hslice_geom.intersection(time_extent) elif self == self.OFFSET_SUBSET_FOR_TIMES: offset_y = centre_y + 0.35 * height hslice_bbox = BoundingBox( left=bbox.left, right=bbox.right, top=offset_y + 0.02 * height, bottom=offset_y - 0.02 * height, ) hslice_geom = geom_from_bbox(hslice_bbox) hslice_geom = hslice_geom.intersection(time_extent) else: # if self == self.EDGE_SUBSET_FOR_TIMES: hslice_bbox = BoundingBox( left=bbox.left, right=bbox.right, top=bbox.bottom + 0.02 * height, bottom=bbox.bottom, ) hslice_geom = geom_from_bbox(hslice_bbox) hslice_geom = hslice_geom.intersection(time_extent) # Slice on horizontal coordinate (vertical slice) slice_bbox = hslice_geom.boundingbox centre_x = (slice_bbox.left + slice_bbox.right) / 2 if self == self.CENTRAL_SUBSET_FOR_TIMES: vslice_bbox = BoundingBox( left=centre_x - 0.02 * width, right=centre_x + 0.02 * width, top=slice_bbox.top, bottom=slice_bbox.bottom, ) vslice_geom = geom_from_bbox(vslice_bbox) elif self == self.OFFSET_SUBSET_FOR_TIMES: offset_x = centre_x + 0.25 * height vslice_bbox = BoundingBox( left=offset_x - 0.02 * width, right=offset_x + 0.02 * width, top=slice_bbox.top, bottom=slice_bbox.bottom, ) vslice_geom = geom_from_bbox(hslice_bbox) else: # if self == self.EDGE_SUBSET_FOR_TIMES: vslice_bbox = BoundingBox( left=slice_bbox.left, right=slice_bbox.left + 0.02 * width, top=slice_bbox.top, bottom=slice_bbox.bottom, ) vslice_geom = geom_from_bbox(hslice_bbox) return vslice_geom