Exemplo n.º 1
0
    def __init__(self, core, polygon_geog, x0, y0):
        """
        Initialises an TilingSystem class for a specified subgrid.

        Parameters
        ----------
        core : TPSCoreProperty
            defines core parameters of the (sub-) grid
        polygon_geog : OGRGeometry
            geometry defining the extent/outline of the subgrid
        x0 : int
            lower-left x (right) coordinates of the subgrid
        y0 : int
            lower-left y (up) coordinates of the subgrid
        """

        self.core = core
        self.x0 = x0
        self.y0 = y0
        self.xstep = self.core.tile_xsize_m
        self.ystep = self.core.tile_ysize_m
        self.polygon_proj = ptpgeometry.transform_geometry(
            polygon_geog, self.core.projection.osr_spref)
        self.bbox_proj = ptpgeometry.get_geometry_envelope(
            self.polygon_proj, rounding=self.core.sampling)
Exemplo n.º 2
0
    def __init__(self, core, polygon_geog, tilingsystem=None):
        """
        Initialises a TiledProjection().

        Parameters
        ----------
        core : TPSCoreProperty
            defines core parameters of the (sub-) grid
        polygon_geog : OGRGeometry
            geometry defining the extent/outline of the subgrid.
            if not given, a single global subgrid is assigned to the grid.
        tilingsystem : TilingSystem
            optional; an instance of TilingSystem()
            if not given, a single global tile is assigned to the grid.
        """

        self.core = core
        self.polygon_geog = ptpgeometry.segmentize_geometry(polygon_geog,
                                                            segment=0.5)
        self.polygon_proj = ptpgeometry.transform_geometry(
            self.polygon_geog, self.core.projection.osr_spref)
        self.bbox_proj = ptpgeometry.get_geometry_envelope(
            self.polygon_proj, rounding=self.core.sampling)

        # does this use anybody? its justs makes problems with the antimeridian!
        # self.bbox_geog = ptpgeometry.get_geometry_envelope(
        #     self.polygon_geog, rounding=self.core.sampling / 1000000.0)

        if tilingsystem is None:
            tilingsystem = GlobalTile(self.core, 'TG', self.get_bbox_proj())
        self.tilesys = tilingsystem
Exemplo n.º 3
0
    def get_extent_geometry_geog(self):
        """
        returns the extent-geometry of the tile in the lon-lat-space.

        Returns
        -------
        OGRGeometry

        """
        tile_geom = self.polygon_proj

        geo_sr = ptpgeometry.get_geog_spatial_ref()

        return ptpgeometry.transform_geometry(tile_geom, geo_sr, segment=25000)
Exemplo n.º 4
0
    def search_tiles_over_geometry(self, geometry, coverland=True):
        """
        Search tiles of the subgrid that are overlapping with the geometry.

        Parameters
        ----------
        geometry : OGRGeometry
            A point or polygon geometry representing the region of interest.
        coverland : Boolean
            option to search for tiles covering land at any point in the tile

        Returns
        -------
        overlapped_tiles : list
            Return a list of the overlapped tiles' name.
            If not found, return empty list.

        """
        overlapped_tiles = list()

        if geometry.GetGeometryName() in ['MULTIPOINT', 'POINT']:
            if geometry.Intersects(self.polygon_geog):
                # get intersect area with subgrid in latlon
                intersect_geometry = geometry.Intersection(self.polygon_geog)
                intersect_geometry = ptpgeometry.transform_geometry(
                    intersect_geometry, self.projection.osr_spref)
            else:
                return overlapped_tiles

        if geometry.GetGeometryName() in ['POLYGON', 'MULTIPOLYGON']:

            # get intersect area with subgrid in latlon
            intersect = ptpgeometry.get_lonlat_intersection(
                geometry, self.polygon_geog)

            # check if geom intersects subgrid
            if intersect.Area() == 0.0:
                return overlapped_tiles

            # transform intersection geometry back to the spatial ref system of the subgrid.
            # segmentise for high precision during reprojection.
            projected = intersect.GetSpatialReference().IsProjected()
            if projected == 0:
                max_segment = 0.5
            elif projected == 1:
                max_segment = 50000
            else:
                raise Warning(
                    'Please check unit of geometry before reprojection!')

            intersect_geometry = ptpgeometry.transform_geometry(
                intersect, self.projection.osr_spref, segment=max_segment)

        # get envelope of the geometry
        envelope = ptpgeometry.get_geometry_envelope(intersect_geometry)

        # get overlapped tiles
        tiles = self.tilesys.identify_tiles_overlapping_xybbox(envelope)

        for tile in tiles:

            # get tile object
            t = self.tilesys.create_tile(tile)

            # get only tile that overlaps with intersect_geometry
            if t.polygon_proj.Intersects(intersect_geometry):

                # get only tile if coverland is satisfied
                if not coverland or self.tilesys.check_tile_covers_land(
                        t.name):
                    overlapped_tiles.append(t.name)

        return overlapped_tiles
Exemplo n.º 5
0
    def _search_tiles_in_roi(self,
                             roi_geometry=None,
                             subgrid_ids=None,
                             coverland=False):
        """
        Internal function: 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
        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.
        """

        # load lat-lon spatial reference as the default
        geog_sr = TPSProjection(epsg=4326).osr_spref

        geom_sr = roi_geometry.GetSpatialReference()
        if geom_sr is None:
            roi_geometry.AssignSpatialReference(geog_sr)
        elif not geom_sr.IsSame(geog_sr):
            projected = roi_geometry.GetSpatialReference().IsProjected()
            if projected == 0:
                max_segment = 0.5
            elif projected == 1:
                max_segment = 50000
            else:
                raise Warning(
                    'Please check unit of geometry before reprojection!')
            roi_geometry = ptpgeometry.transform_geometry(roi_geometry,
                                                          geog_sr,
                                                          segment=max_segment)

        if roi_geometry.GetGeometryName() == 'MULTIPOLYGON':
            roi_polygons = []
            for i in range(roi_geometry.GetGeometryCount()):
                poly = roi_geometry.GetGeometryRef(i).Clone()
                poly.AssignSpatialReference(geog_sr)
                roi_polygons.append(poly)
        else:
            roi_polygons = [roi_geometry]

        overlapped_tiles = list()
        for roi_polygon in roi_polygons:
            # intersect the given grid ids and the overlapped ids
            overlapped_grids = self.locate_geometry_in_subgrids(roi_polygon)
            subgrid_ids = list(set(subgrid_ids) & set(overlapped_grids))

            # finding tiles
            for sgrid_id in subgrid_ids:
                overlapped_tiles.extend(
                    self.subgrids[sgrid_id].search_tiles_over_geometry(
                        roi_polygon, coverland=coverland))
        return list(set(overlapped_tiles))
Exemplo n.º 6
0
    def search_tiles_in_geometry(self, geom, coverland=True):
        """
        Search the tiles which are overlapping with the subgrid

        Parameters
        ----------
        geom : OGRGeometry
            A polygon geometry representing the region of interest.
        coverland : Boolean
            option to search for tiles covering land at any point in the tile

        Returns
        -------
        overlapped_tiles : list
            Return a list of the overlapped tiles' name.
            If not found, return empty list.
        """

        overlapped_tiles = list()

        # check if geom intersects subgrid
        if geom.Intersects(self.polygon_geog):
            # get intersect area with subgrid in latlon
            intersect = geom.Intersection(self.polygon_geog)
        else:
            return overlapped_tiles

        # get spatial reference of subgrid in grid projection
        grid_sr = self.projection.osr_spref

        # transform intersection geometry back to the spatial reference system
        # of the subgrid.
        # segmentise for high precision during reprojection.
        projected = intersect.GetSpatialReference().IsProjected()
        if projected == 0:
            max_segment = 0.5
        else:
            raise Warning('Please check unit of geometry before reprojection!')
        intersect = geometry.transform_geometry(intersect, grid_sr,
                                                segment=max_segment)

        # get envelope of the Geometry and cal the bounding tile of the
        envelope = intersect.GetEnvelope()
        lon_min = int(envelope[0]) // self.core.tile_xsize_m \
            * self.core.tile_xsize_m
        lon_max = (int(envelope[1]) // self.core.tile_xsize_m + 1) \
            * self.core.tile_xsize_m
        lat_min = int(envelope[2]) // self.core.tile_ysize_m * \
            self.core.tile_ysize_m
        lat_max = (int(envelope[3]) // self.core.tile_ysize_m + 1) * \
            self.core.tile_ysize_m


        # get overlapped tiles
        lonr = np.arange(
            lon_min, lon_max + self.core.tile_xsize_m, self.core.tile_xsize_m)
        latr = np.arange(
            lat_min, lat_max + self.core.tile_ysize_m, self.core.tile_ysize_m)

        for lon, lat in itertools.product(lonr, latr):
            geom_tile = geometry.extent2polygon(
                (lon, lat, lon + self.core.tile_xsize_m,
                 lat + self.core.tile_xsize_m), grid_sr)
            if geom_tile.Intersects(intersect):
                ftile = self.tilesys.point2tilename(lon, lat)
                if not coverland or self.tilesys.check_tile_covers_land(ftile):
                    overlapped_tiles.append(ftile)

        return overlapped_tiles