Skip to content

Add functionality for generation of the sample by vector extent #12

@AlexeyTrekin

Description

@AlexeyTrekin

Something like this

Find raster boundaries for the vector grid cells

def pix_from_coords(x, y, transform, size):
return int(round((x - transform[2])/transform[0])), int(round((y - transform[5])/transform[4] ))

def calc_raster_boundaries(xmin, xmax, ymin, ymax, transform, size):
# in given crs.
# Assume that xmin -> minimum column, ymax -> minimum row
#print(xmin,xmax, ymin, ymax)
x1, y1 = pix_from_coords(xmin, ymax, transform, size)
x2, y2 = pix_from_coords(xmax, ymin, transform, size)
#print(x1, x2, y1, y2)

x1 = min(max(0, x1), size[1])
y1 = min(max(0, y1), size[0])
x2 = min(max(0, x2), size[1])
y2 = min(max(0, y2), size[0])

if y1 == y2 or x1 == x2:
    return 0,0,0,0

return x1, y1, x2-x1, y2-y1

#save a spatial subset

def subsample(input_ds, output_ds, grid, cell):
bc = BandCollection(parse_directory(input_ds, RGB))
#print(bc.crs, bc.transform)
xmin, ymin, xmax, ymax = grid[cell].shape.bounds
x1, y1, width, height = calc_raster_boundaries(xmin, xmax, ymin, ymax, bc[0].transform, bc[0].shape)

if width == 0 or height == 0:
    print('Subset in {} cell is empty'.format(cell))
    return
else:
    print(width, height)

sample = bc.sample(x=x1, y=y1, width=width, height=height)
return sample

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions