def dataset2pixelpolygon(dataset): """ Return polygon corresponding to the first pixel of dataset. """ xul, dxx, dxy, yul, dyx, dyy = dataset.GetGeoTransform() points = ((xul, yul), (xul + dxx, yul + dyx), (xul + dxx + dxy, yul + dyx + dyy), (xul + dxy, yul + dyy), (xul, yul)) return vectors.points2polygon(points)
def dataset2pixel(dataset, projection=None): """ Return a polygon formed by pixel edges, wrapped in a Geometry. """ p, a, b, q, c, d = dataset.GetGeoTransform() points = np.dot([[a, b], [c, d]], [[0, 1, 1, 0, 0], [0, 0, 1, 1, 0]]) + [[p], [q]] polygon = vectors.points2polygon(points.transpose()) geometry = vectors.Geometry(polygon) if projection is None or projection == dataset.GetProjection(): return geometry geometry.transform(source=dataset.GetProjection(), target=projection) return geometry
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
def polygon(self): """ Return extent geometry. """ x1, y1, x2, y2 = self.extent points = (x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1) return vectors.points2polygon(points)