Beispiel #1
0
def dataset2outlinepolygon(dataset):
    """ Return polygon formed by pixel edges. """
    nx, ny = dataset.RasterXSize, dataset.RasterYSize
    npoints = 2 * (nx + ny) + 1
    array = np.empty((npoints, 2))

    # Construct pixel indices arrays
    px = np.empty(npoints, dtype=np.uint64)
    py = np.empty(npoints, dtype=np.uint64)
    # top
    a, b = 0, nx
    px[a:b] = np.arange(nx)
    py[a:b] = 0
    # right
    a, b = nx, nx + ny
    px[a:b] = nx
    py[a:b] = np.arange(ny)
    # bottom
    a, b = nx + ny, 2 * nx + ny
    px[a:b] = np.arange(nx, 0, -1)
    py[a:b] = ny
    # left
    a, b = 2 * nx + ny, 2 * (nx + ny)
    px[a:b] = 0
    py[a:b] = np.arange(ny, 0, -1)
    # repeat first
    px[-1], py[-1] = 0, 0

    # Use geotransform to convert the points to coordinates
    xul, dxx, dxy, yul, dyx, dyy = dataset.GetGeoTransform()
    array[:, 0] = xul + dxx * px + dxy * py
    array[:, 1] = yul + dyx * px + dyy * py

    return vectors.array2polygon(array)
Beispiel #2
0
def dataset2outline(dataset, projection=None):
    """
    Return a polygon formed by dataset edges, wrapped in a Geometry.

    TODO Make use of np.dot, ideal for geotransform stuff.
    """
    nx, ny = dataset.RasterXSize, dataset.RasterYSize
    if projection is None:
        # will not be transformed, corners are enough
        p, a, b, q, c, d = dataset.GetGeoTransform()
        points = np.dot([[a, b], [c, d]], [[0, nx, nx, 0, 0],
                                           [0, 0, ny, ny, 0]]) + [[p], [q]]
        polygon = vectors.points2polygon(points.transpose())
    else:
        # take all pixelcrossings along the edge into account
        npoints = 2 * (nx + ny) + 1
        array = np.empty((npoints, 2))

        # Construct pixel indices arrays
        px = np.empty(npoints, dtype=np.uint64)
        py = np.empty(npoints, dtype=np.uint64)
        # top
        a, b = 0, nx
        px[a:b] = np.arange(nx)
        py[a:b] = 0
        # right
        a, b = nx, nx + ny
        px[a:b] = nx
        py[a:b] = np.arange(ny)
        # bottom
        a, b = nx + ny, 2 * nx + ny
        px[a:b] = np.arange(nx, 0, -1)
        py[a:b] = ny
        # left
        a, b = 2 * nx + ny, 2 * (nx + ny)
        px[a:b] = 0
        py[a:b] = np.arange(ny, 0, -1)
        # repeat first
        px[-1], py[-1] = 0, 0

        # Use geotransform to convert the points to coordinates
        xul, dxx, dxy, yul, dyx, dyy = dataset.GetGeoTransform()
        array[:, 0] = xul + dxx * px + dxy * py
        array[:, 1] = yul + dyx * px + dyy * py

        polygon = vectors.array2polygon(array)

    geometry = vectors.Geometry(polygon)
    if projection is None or projection == dataset.GetProjection():
        return geometry
    geometry.transform(source=dataset.GetProjection(), target=projection)
    return geometry