def test_segmentize_geometry(): """Segmentize function.""" # Polygon polygon = box(-18, -9, 18, 9) out = segmentize_geometry(polygon, 1) assert out.is_valid # wrong type with pytest.raises(TypeError): segmentize_geometry(polygon.centroid, 1)
def bbox(self, out_crs=None): """ Return data bounding box. Parameters ---------- out_crs : ``rasterio.crs.CRS`` rasterio CRS object (default: CRS of process pyramid) Returns ------- bounding box : geometry Shapely geometry object """ out_crs = self.pyramid.crs if out_crs is None else out_crs with rasterio.open(self.path) as inp: inp_crs = inp.crs out_bbox = bbox = box(*inp.bounds) # If soucre and target CRSes differ, segmentize and reproject if inp_crs != out_crs: # estimate segmentize value (raster pixel size * tile size) # and get reprojected bounding box return reproject_geometry(segmentize_geometry( bbox, inp.transform[0] * self.pyramid.tile_size), src_crs=inp_crs, dst_crs=out_crs) else: return out_bbox
def get_best_zoom_level(input_file, tile_pyramid_type): """ Determine the best base zoom level for a raster. "Best" means the maximum zoom level where no oversampling has to be done. Parameters ---------- input_file : path to raster file tile_pyramid_type : ``TilePyramid`` projection (``geodetic`` or ``mercator``) Returns ------- zoom : integer """ tile_pyramid = TilePyramid(tile_pyramid_type) with rasterio.open(input_file, "r") as src: xmin, ymin, xmax, ymax = reproject_geometry( segmentize_geometry( box(src.bounds.left, src.bounds.bottom, src.bounds.right, src.bounds.top), get_segmentize_value(input_file, tile_pyramid)), src_crs=src.crs, dst_crs=tile_pyramid.crs).bounds x_dif = xmax - xmin y_dif = ymax - ymin size = float(src.width + src.height) avg_resolution = ((x_dif / float(src.width)) * (float(src.width) / size) + (y_dif / float(src.height)) * (float(src.height) / size)) for zoom in range(0, 40): if tile_pyramid.pixel_x_size(zoom) <= avg_resolution: return zoom - 1