def getNoSampleGrid(yespoints, xvar, yvar, dx, h1, h2):
"""Return the grid from which "no" pixels can successfully be sampled.
Args:
yespoints: Sequence of (x,y) points (meters) where
landslide/liquefaction was observed.
xvar: Numpy array of centers of columns of sampling grid.
yvar: Numpy array of centers of rows of sampling grid.
dx: Sampling resolution in x and y (meters).
h1: Minimum buffer size for sampling non-hazard points.
h2: Maximum buffer size for sampling non-hazard points.
Returns:
Grid of shape (len(yvar),len(xvar)) where 1's represent pixels from
which "no" values can be sampled.
"""
shp = (len(xvar), len(yvar))
west = xvar.min() - dx / 2.0 # ??
north = yvar.max() + dx / 2.0 # ??
affine = affine_from_corner(west, north, dx, dx)
donuts = []
holes = []
for h, k in yespoints:
donut = createCirclePolygon(h, k, h2, dx)
hole = createCirclePolygon(h, k, h1, dx)
donuts.append(donut)
holes.append(hole)
donutburn = ((mapping(g), 1) for g in donuts)
holeburn = ((mapping(g), 2) for g in holes)
# we only want those pixels set where the polygon encloses the center point
alltouched = False
donutimg = rasterio.features.rasterize(donutburn,
out_shape=shp,
transform=affine,
all_touched=alltouched)
holeimg = rasterio.features.rasterize(holeburn,
out_shape=shp,
transform=affine,
all_touched=alltouched)
holeimg[holeimg == 0] = 1
holeimg[holeimg == 2] = 0
sampleimg = np.bitwise_and(donutimg, holeimg)
return sampleimg
评论列表
文章目录