Exemplo n.º 1
0
def clip_grid_by_polygons(A, geotransform, polygons):
    """Clip raster grid by polygon.

    Args:
        * A: MxN array of grid points
        * geotransform: 6-tuple used to locate A geographically
            (top left x, w-e pixel resolution, rotation,
            top left y, rotation, n-s pixel resolution)
        * polygons: list of polygon geometry objects or list of polygon arrays

    Returns:
        points_covered: List of (points, values) - one per input polygon.

    Implementing algorithm suggested in
    https://github.com/AIFDR/inasafe/issues/91#issuecomment-7025120

    .. note:: Grid points are considered to be pixel-registered which means
        that each point represents the center of its grid cell.
        The required half cell shifts are taken care of by the
        function :func:`geotransform_to_axes`.

        If multiple polygons overlap, the one first encountered will be used.

    """

    # Convert raster grid to Nx2 array of points and an N array of pixel values
    ny, nx = A.shape
    x, y = geotransform_to_axes(geotransform, nx, ny)
    points, values = grid_to_points(A, x, y)

    # Generate list of points and values that fall inside each polygon
    points_covered = []
    remaining_points = points
    remaining_values = values

    for polygon in polygons:
        #print 'Remaining points', len(remaining_points)

        if hasattr(polygon, 'outer_ring'):
            outer_ring = polygon.outer_ring
            inner_rings = polygon.inner_rings
        else:
            # Assume it is an array
            outer_ring = polygon
            inner_rings = None

        inside, outside = in_and_outside_polygon(remaining_points,
                                                 outer_ring,
                                                 holes=inner_rings,
                                                 closed=True,
                                                 check_input=False)
        # Add features inside this polygon
        points_covered.append((remaining_points[inside],
                               remaining_values[inside]))

        # Select remaining points to clip
        remaining_points = remaining_points[outside]
        remaining_values = remaining_values[outside]

    return points_covered
Exemplo n.º 2
0
    def get_geometry(self):
        """Return longitudes and latitudes (the axes) for grid.

        Note:
            Return two vectors (longitudes and latitudes) corresponding to
            grid. The values are offset by half a pixel size to correspond to
            pixel registration.

            I.e. If the grid origin (top left corner) is (105, 10) and the
            resolution is 1 degrees in each direction, then the vectors will
            take the form

            longitudes = [100.5, 101.5, ..., 109.5]
            latitudes = [0.5, 1.5, ..., 9.5]
        """

        # Get parameters for axes
        g = self.get_geotransform()
        nx = self.columns
        ny = self.rows

        # Compute x and y axes
        x, y = geotransform_to_axes(g, nx, ny)

        # Return them
        return x, y
Exemplo n.º 3
0
def clip_grid_by_polygons(A, geotransform, polygons):
    """Clip raster grid by polygon.

    Args:
        * A: MxN array of grid points
        * geotransform: 6-tuple used to locate A geographically
            (top left x, w-e pixel resolution, rotation,
            top left y, rotation, n-s pixel resolution)
        * polygons: list of polygon geometry objects or list of polygon arrays

    Returns:
        points_covered: List of (points, values) - one per input polygon.

    Implementing algorithm suggested in
    https://github.com/AIFDR/inasafe/issues/91#issuecomment-7025120

    .. note:: Grid points are considered to be pixel-registered which means
        that each point represents the center of its grid cell.
        The required half cell shifts are taken care of by the
        function :func:`geotransform_to_axes`.

        If multiple polygons overlap, the one first encountered will be used.

    """

    # Convert raster grid to Nx2 array of points and an N array of pixel values
    ny, nx = A.shape
    x, y = geotransform_to_axes(geotransform, nx, ny)
    points, values = grid_to_points(A, x, y)

    # Generate list of points and values that fall inside each polygon
    points_covered = []
    remaining_points = points
    remaining_values = values

    for polygon in polygons:
        #print 'Remaining points', len(remaining_points)

        if hasattr(polygon, 'outer_ring'):
            outer_ring = polygon.outer_ring
            inner_rings = polygon.inner_rings
        else:
            # Assume it is an array
            outer_ring = polygon
            inner_rings = None

        inside, outside = in_and_outside_polygon(remaining_points,
                                                 outer_ring,
                                                 holes=inner_rings,
                                                 closed=True,
                                                 check_input=False)
        # Add features inside this polygon
        points_covered.append(
            (remaining_points[inside], remaining_values[inside]))

        # Select remaining points to clip
        remaining_points = remaining_points[outside]
        remaining_values = remaining_values[outside]

    return points_covered
Exemplo n.º 4
0
    def get_geometry(self):
        """Return longitudes and latitudes (the axes) for grid.

        Note:
            Return two vectors (longitudes and latitudes) corresponding to
            grid. The values are offset by half a pixel size to correspond to
            pixel registration.

            I.e. If the grid origin (top left corner) is (105, 10) and the
            resolution is 1 degrees in each direction, then the vectors will
            take the form

            longitudes = [100.5, 101.5, ..., 109.5]
            latitudes = [0.5, 1.5, ..., 9.5]
        """

        # Get parameters for axes
        g = self.get_geotransform()
        nx = self.columns
        ny = self.rows

        # Compute x and y axes
        x, y = geotransform_to_axes(g, nx, ny)

        # Return them
        return x, y