Ejemplo n.º 1
0
def _write_polygon(geobox, polygon, zoom_fill):
    geobox_ext = geobox.extent
    if geobox_ext.within(polygon):
        data = numpy.full([geobox.height, geobox.width], fill_value=1, dtype="uint8")
    else:
        data = numpy.zeros([geobox.height, geobox.width], dtype="uint8")
        if polygon.type == 'Polygon':
            coordinates_list = [polygon.json["coordinates"]]
        elif polygon.type == 'MultiPolygon':
            coordinates_list = polygon.json["coordinates"]
        else:
            raise Exception("Unexpected extent/geobox polygon geometry type: %s" % polygon.type)
        for polygon_coords in coordinates_list:
            pixel_coords = [~geobox.transform * coords for coords in polygon_coords[0]]
            rs, cs = skimg_polygon([c[1] for c in pixel_coords], [c[0] for c in pixel_coords],
                                   shape=[geobox.width, geobox.height])
            data[rs, cs] = 1

    with MemoryFile() as memfile:
        with memfile.open(driver='PNG',
                          width=geobox.width,
                          height=geobox.height,
                          count=len(zoom_fill),
                          transform=Affine.identity(),
                          nodata=0,
                          dtype='uint8') as thing:
            for idx, fill in enumerate(zoom_fill, start=1):
                thing.write_band(idx, data * fill)
        return memfile.read()
Ejemplo n.º 2
0
def _write_polygon(geobox, polygon, zoom_fill):
    geobox_ext = geobox.extent
    if geobox_ext.within(polygon):
        data = numpy.full([geobox.width, geobox.height],
                          fill_value=1,
                          dtype="uint8")
    else:
        data = numpy.zeros([geobox.width, geobox.height], dtype="uint8")
        if not geobox_ext.disjoint(polygon):
            intersection = geobox_ext.intersection(polygon)
            crs_coords = intersection.json["coordinates"][0]
            pixel_coords = [
                ~geobox.transform * coords for coords in crs_coords
            ]
            rs, cs = skimg_polygon(
                [int_trim(c[1], 0, geobox.height - 1) for c in pixel_coords],
                [int_trim(c[0], 0, geobox.width - 1) for c in pixel_coords])
            data[rs, cs] = 1

    with MemoryFile() as memfile:
        with memfile.open(driver='PNG',
                          width=geobox.width,
                          height=geobox.height,
                          count=len(zoom_fill),
                          transform=Affine.identity(),
                          nodata=0,
                          dtype='uint8') as thing:
            for idx, fill in enumerate(zoom_fill, start=1):
                thing.write_band(idx, data * fill)
        return memfile.read()
Ejemplo n.º 3
0
from polygon import polygon_mask, polygon

# Test data

nb_vertices = 100
shape = 2048, 2048
#shape = 4096, 4096
print('Nb vertices: %d, image shape: %d, %d' %
      (nb_vertices, shape[0], shape[1]))

row = np.random.randint(-int(shape[0]/10), int(1.1 * shape[0]), nb_vertices).astype(np.float32)
col = np.random.randint(-int(shape[0]/10), int(1.1 * shape[1]), nb_vertices).astype(np.float32)


st = time.time()
skimg_row, skimg_col = skimg_polygon(row, col, shape)
dt = time.time() - st
print('skimage: %f s' % dt)

st = time.time()
result = polygon(row, col, shape)
dt = time.time() - st
assert np.all(np.equal(skimg_row, result[0]))
assert np.all(np.equal(skimg_col, result[1]))
print('using line buffer: %f s' % dt)

st = time.time()
result = polygon_mask(row, col, shape, asmask=False)
dt = time.time() - st
assert np.all(np.equal(skimg_row, result[0]))
assert np.all(np.equal(skimg_col, result[1]))