def boundary(gt, sref, shape): """ Creates raster boundary polygon from geotransformation and shape parameters. Parameters ---------- gt: tuple Geotransformation parameters. sref: osr.SpatialReference Spatial reference of the boundary polygon. shape: tuple Defines the size of the boundary polygon/raster (rows, columns). Returns ------- ogr.Geometry Boundary polygon with the given spatial reference system assigned. """ boundary_extent = (gt[0], gt[3] + shape[0] * gt[5], gt[0] + shape[1] * gt[1], gt[3]) boundary_spref = osr.SpatialReference() boundary_spref.ImportFromWkt(sref) bbox = [(boundary_extent[0], boundary_extent[1]), (boundary_extent[2], boundary_extent[3])] boundary_geom = geometry.bbox2polygon(bbox, boundary_spref) return boundary_geom
def any_geom2ogr_geom(geom, osr_sref): """ Transforms an extent represented in different ways or a Shapely geometry object into an OGR geometry object. Parameters ---------- geom : ogr.Geometry or shapely.geometry or list or tuple, optional A vector geometry. If it is of type list/tuple representing the extent (i.e. [x_min, y_min, x_max, y_max]), `osr_spref` has to be given to transform the extent into a georeferenced polygon. osr_sref : osr.SpatialReference, optional Spatial reference of the given geometry `geom`. Returns ------- ogr.Geometry Vector geometry as an OGR Geometry object. """ if isinstance(geom, (tuple, list)) and (not isinstance(geom[0], (tuple, list))) and \ (len(geom) == 4) and osr_sref: geom_ogr = geometry.bbox2polygon(geom, osr_sref) elif isinstance(geom, (tuple, list)) and (isinstance(geom[0], (tuple, list))) and \ (len(geom) == 2) and osr_sref: edge = ogr.Geometry(ogr.wkbLinearRing) geom = [ geom[0], (geom[0][0], geom[1][1]), geom[1], (geom[1][0], geom[0][1]) ] for point in geom: if len(point) == 2: edge.AddPoint(float(point[0]), float(point[1])) edge.CloseRings() geom_ogr = ogr.Geometry(ogr.wkbPolygon) geom_ogr.AddGeometry(edge) geom_ogr.AssignSpatialReference(osr_sref) elif isinstance(geom, (tuple, list)) and isinstance(geom[0], (tuple, list)) and osr_sref: edge = ogr.Geometry(ogr.wkbLinearRing) for point in geom: if len(point) == 2: edge.AddPoint(float(point[0]), float(point[1])) edge.CloseRings() geom_ogr = ogr.Geometry(ogr.wkbPolygon) geom_ogr.AddGeometry(edge) geom_ogr.AssignSpatialReference(osr_sref) elif isinstance(geom, shapely.geometry.Polygon): geom_ogr = ogr.CreateGeometryFromWkt(geom.wkt) geom_ogr.AssignSpatialReference(osr_sref) elif isinstance(geom, ogr.Geometry): geom_sref = geom.GetSpatialReference() if geom_sref is None: geom.AssignSpatialReference(osr_sref) geom_ogr = geom else: raise GeometryUnkown(geom) return geom_ogr
def __init__(self, core, continent): _core = copy.copy(core) _core.tag = continent _core.projection = TPSProjection(epsg=4326) self.core = _core self.polygon_geog = bbox2polygon([(-179.9999999, -90.0), (179.9999999, 90.0)], self.core.projection.osr_spref) self.tilesys = CopernicusTilingSystem(self.core, self.polygon_geog) super(CopernicusSubgrid, self).__init__(self.core, self.polygon_geog, self.tilesys)
def get_extent_geometry_proj(self): """ returns the extent-geometry of the tile in the projected space. Returns ------- OGRGeometry """ return ptpgeometry.bbox2polygon( (self._limits_m()[0:2], self._limits_m()[2:4]), self.core.projection.osr_spref, segment=self.x_size_px * self.core.sampling / 4)
def search_tiles_in_roi(self, roi_geometry=None, bbox=None, points=None, osr_spref=None, subgrid_ids=None, coverland=False): """ Search the tiles of the grid which intersect by the given area. Parameters ---------- roi_geometry : geometry a polygon or multipolygon geometry object representing the ROI bbox : list a list of coordinate-tuples representing a rectangle-shape region-of-interest in the format of [(left, lower), (right, upper)] points : list a list of points-of-interest as tuples in the format of [(x1, y1), (x2, y2), ...] osr_spref : OGRSpatialReference spatial reference of input coordinates in extent sgrid_ids : string or list of strings subgrid IDs, e.g. specifying over which continent you want to search. Default value is None for searching all subgrids. coverland : Boolean option to search for tiles covering land at any point in the tile Returns ------- list return a list of the overlapped tiles' name. If not found, return empty list. """ # check input grids if subgrid_ids is None: subgrid_ids = self.subgrids.keys() if isinstance(subgrid_ids, str): subgrid_ids = [subgrid_ids] if set(subgrid_ids).issubset(set(self.subgrids.keys())): subgrid_ids = list(subgrid_ids) else: raise ValueError("Invalid argument: grid must one of [ %s ]." % " ".join(self.subgrids.keys())) if roi_geometry is None and bbox is None and points is None: print("Error: Either roi_geometry, bbox, or points must be given " "as the region-of-interest!") return list() # obtain the ROI if roi_geometry is None: if osr_spref is None: projection = TPSProjection(epsg=4326) osr_spref = projection.osr_spref if points is not None: roi_geometry = ptpgeometry.points2geometry(points, osr_spref) elif bbox is not None: segment = 50000 if osr_spref.IsProjected() else 0.5 roi_geometry = ptpgeometry.bbox2polygon(bbox, osr_spref, segment=segment) # switch for ROI defined by a single polygon or point(s) if roi_geometry.GetGeometryName() in [ 'POLYGON', 'MULTIPOINT', 'POINT' ]: tiles = self._search_tiles_in_roi(roi_geometry=roi_geometry, subgrid_ids=subgrid_ids, coverland=coverland) # switch for ROI defined by multiple polygons if roi_geometry.GetGeometryName() == 'MULTIPOLYGON': tiles = [] # search tiles for each polygon individually for i_polygon in list(range(roi_geometry.GetGeometryCount())): geometry = roi_geometry.GetGeometryRef(i_polygon) i_tiles = self._search_tiles_in_roi(roi_geometry=geometry, subgrid_ids=subgrid_ids, coverland=coverland) tiles += i_tiles # reduce to unique list of tiles tiles = list(set(tiles)) return tiles