shapelib/raster.py
def _rasterize_rasterio(geom, pixratio, xrange=None, yrange=None, imageout=None):
    """
    rasterize `geom` to a 2D array

    Uses rasterio
    """
    try:
        import rasterio
        from rasterio import features
    except ImportError:
        return None
    geoms = [geom.__geo_interface__]
    x0, y0, x1, y1 = _geomselectrange(geom, xrange, yrange)
    cols = int(abs(x1-x0) * pixratio + 0.5)
    rows = int(abs(y1-y0) * pixratio + 0.5)
    transform = [0, 1.0/pixratio, 0, 0, 0, 1.0/pixratio]
    foreground = 'white'
    print("cols: {cols}, rows: {rows}".format(**locals()))
    with rasterio.drivers():
        array = features.rasterize(geoms, out_shape=(rows, cols), transform=transform)
        array_uint8 = array.astype(np.uint8)
        array_uint8 = np.flipud(array_uint8)
        if imageout:
            with rasterio.open(
                imageout, "w",
                driver='GTiff',
                width=cols, height=rows,
                transform=transform,
                count=1, dtype=np.uint8, nodata=0,
                crs={'init': 'EPSG:4326'}
            ) as out:
                if foreground == 'black':
                    outarray = array_uint8
                else:
                    outarray = 255 - array_uint8
                out.write_band(1, outarray)
    array_uint8 /= 255
    y, x = array_uint8.shape
    assert (x, y) == (cols, rows)
    return _rasterize_out(array_uint8, imageout)

    rasterize `geom` to a 2D array

    Uses rasterio
    
rasterize `geom` to a 2D array
Uses rasterio
rasterize ` geom ` to a 2D array Uses rasterio
def _rasterize_rasterio(geom, pixratio, xrange=None, yrange=None, imageout=None):
    try:
        import rasterio
        from rasterio import features
    except ImportError:
        return None
    geoms = [geom.__geo_interface__]
    x0, y0, x1, y1 = _geomselectrange(geom, xrange, yrange)
    cols = int(abs(x1-x0) * pixratio + 0.5)
    rows = int(abs(y1-y0) * pixratio + 0.5)
    transform = [0, 1.0/pixratio, 0, 0, 0, 1.0/pixratio]
    foreground = 'white'
    print("cols: {cols}, rows: {rows}".format(**locals()))
    with rasterio.drivers():
        array = features.rasterize(geoms, out_shape=(rows, cols), transform=transform)
        array_uint8 = array.astype(np.uint8)
        array_uint8 = np.flipud(array_uint8)
        if imageout:
            with rasterio.open(
                imageout, "w",
                driver='GTiff',
                width=cols, height=rows,
                transform=transform,
                count=1, dtype=np.uint8, nodata=0,
                crs={'init': 'EPSG:4326'}
            ) as out:
                if foreground == 'black':
                    outarray = array_uint8
                else:
                    outarray = 255 - array_uint8
                out.write_band(1, outarray)
    array_uint8 /= 255
    y, x = array_uint8.shape
    assert (x, y) == (cols, rows)
    return _rasterize_out(array_uint8, imageout)
def _rasterize_rasterio ( geom , pixratio , xrange = None , yrange = None , imageout = None ) : try : import rasterio from rasterio import features except ImportError : return None geoms = [ geom . __geo_interface__ ] x0 , y0 , x1 , y1 = _geomselectrange ( geom , xrange , yrange ) cols = int ( abs ( x1 - x0 ) * pixratio + 0.5 ) rows = int ( abs ( y1 - y0 ) * pixratio + 0.5 ) transform = [ 0 , 1.0 / pixratio , 0 , 0 , 0 , 1.0 / pixratio ] foreground = 'white' print ( "cols: {cols}, rows: {rows}" . format ( ** locals ( ) ) ) with rasterio . drivers ( ) : array = features . rasterize ( geoms , out_shape = ( rows , cols ) , transform = transform ) array_uint8 = array . astype ( np . uint8 ) array_uint8 = np . flipud ( array_uint8 ) if imageout : with rasterio . open ( imageout , "w" , driver = 'GTiff' , width = cols , height = rows , transform = transform , count = 1 , dtype = np . uint8 , nodata = 0 , crs = { 'init' : 'EPSG:4326' } ) as out : if foreground == 'black' : outarray = array_uint8 else : outarray = 255 - array_uint8 out . write_band ( 1 , outarray ) array_uint8 /= 255 y , x = array_uint8 . shape assert ( x , y ) == ( cols , rows ) return _rasterize_out ( array_uint8 , imageout )
rasterize `geom` to a 2D array
Uses rasterio
rasterize ` geom ` to a 2D array Uses rasterio
"""
    rasterize `geom` to a 2D array

    Uses rasterio
    """
geom
pixratio
xrange
yrange
imageout