예제 #1
0
def test_nearblack_lib_3():

    src_ds = gdal.Nearblack('',
                            '../gdrivers/data/rgbsmall.tif',
                            format='MEM',
                            maxNonBlack=0,
                            setAlpha=True)
    ds = gdal.Nearblack('', src_ds, format='MEM', maxNonBlack=0, setAlpha=True)
    assert ds is not None

    assert ds.GetRasterBand(4).Checksum() == 22002, 'Bad checksum band 0'

    ds = None
예제 #2
0
def test_nearblack_lib_3():

    src_ds = gdal.Nearblack('', '../gdrivers/data/rgbsmall.tif', format='MEM', maxNonBlack=0, setAlpha=True)
    ds = gdal.Nearblack('', src_ds, format='MEM', maxNonBlack=0, setAlpha=True)
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(4).Checksum() != 22002:
        print(ds.GetRasterBand(4).Checksum())
        gdaltest.post_reason('Bad checksum band 0')
        return 'fail'

    ds = None

    return 'success'
예제 #3
0
def polygonize(input_file: str, output_file: str, output_type: str = "GeoJSON", band: int = None):
    """
    Polygonization groups similar pixel values into bins and draws a boundary around them.
    This is often used as a way to display raster information in a vector format. That can still be done here,
    but if a band isn't provided the function will try to guess at the mask band and will use that as both the
    converted layer and the mask.  The result should be a polygon of anywhere there are not black or not transparent
    pixels.

    :param input_file: The raster file to use to polygonize.
    :param output_file: The vector output file for the new data.
    :param output_type: The file type for output data (should be a vector type).
    :param band: The band to use for polygonization.
    :return:
    """

    src_ds = gdal.Open(input_file)

    if src_ds is None:
        logger.error("Unable to open source.")
        raise Exception("Failed to open the file.")

    try:
        band_index = band
        if not band_index:
            if src_ds.RasterCount == 4:
                band_index = 4
            elif src_ds.RasterCount == 3:
                # Likely RGB (jpg) add a transparency mask and use that.

                # Clean up pixel values of 1 0 0 or 0 0 1 caused by interleaving.
                nb_file = "/vsimem/nb"
                gdal.Nearblack(nb_file, input_file)

                # Convert to geotiff so that we can remove black pixels and use alpha mask for the polygon.
                tmp_file = "/vsimem/tmp.tif"
                convert_raster(nb_file, tmp_file, driver="gtiff", warp_params={"dstAlpha": True, "srcNodata": "0 0 0"})

                del nb_file
                src_ds = gdal.Open(tmp_file)
                band_index = 4
            elif src_ds.RasterCount == 2:
                band_index = 2
            else:
                band_index = 1
        mask_band = src_ds.GetRasterBand(band_index)
    except RuntimeError as e:
        logger.error(e)
        raise Exception("Unable to get raster band.")

    drv = ogr.GetDriverByName(output_type)
    dst_ds = drv.CreateDataSource(output_file)
    dst_layer = dst_ds.CreateLayer(output_file)

    # Use the mask band for both the polygonization and as a mask.
    gdal.Polygonize(mask_band, mask_band, dst_layer, -1, [])
    # Close files to read later.
    del dst_ds
    del src_ds

    return output_file
예제 #4
0
def test_nearblack_lib_8():

    src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
    ds = gdal.GetDriverByName('MEM').CreateCopy('', src_ds)
    ret = gdal.Nearblack(ds, ds, maxNonBlack=0)
    if ret != 1:
        gdaltest.post_reason('failure')
        return 'fail'

    if ds.GetRasterBand(1).Checksum() != 21106:
        print(ds.GetRasterBand(1).Checksum())
        gdaltest.post_reason('Bad checksum band 1')
        return 'fail'

    if ds.GetRasterBand(2).Checksum() != 20736:
        print(ds.GetRasterBand(2).Checksum())
        gdaltest.post_reason('Bad checksum band 2')
        return 'fail'

    if ds.GetRasterBand(3).Checksum() != 21309:
        print(ds.GetRasterBand(3).Checksum())
        gdaltest.post_reason('Bad checksum band 3')
        return 'fail'

    return 'success'
예제 #5
0
def test_nearblack_lib_8():

    src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
    ds = gdal.GetDriverByName('MEM').CreateCopy('', src_ds)
    ret = gdal.Nearblack(ds, ds, maxNonBlack=0)
    assert ret == 1

    assert ds.GetRasterBand(1).Checksum() == 21106, 'Bad checksum band 1'

    assert ds.GetRasterBand(2).Checksum() == 20736, 'Bad checksum band 2'

    assert ds.GetRasterBand(3).Checksum() == 21309, 'Bad checksum band 3'
예제 #6
0
def test_nearblack_lib_7():

    ds = gdal.Nearblack('',
                        'data/whiteblackred.tif',
                        format='MEM',
                        colors=((0, 0, 0), (255, 255, 255)))
    assert ds is not None

    assert (ds.GetRasterBand(1).Checksum() == 418 and \
       ds.GetRasterBand(2).Checksum() == 0 and \
       ds.GetRasterBand(3).Checksum() == 0), 'Bad checksum'

    ds = None
예제 #7
0
def test_nearblack_lib_4():

    src_ds = gdal.Warp('', '../gdrivers/data/rgbsmall.tif', format='MEM', warpOptions=["INIT_DEST=255"], srcNodata=0)
    ds = gdal.Nearblack('', src_ds, format='MEM', white=True, maxNonBlack=0, setAlpha=True)
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(4).Checksum() != 24151:
        print(ds.GetRasterBand(4).Checksum())
        gdaltest.post_reason('Bad checksum band 0')
        return 'fail'

    ds = None

    return 'success'
예제 #8
0
def test_nearblack_lib_5():

    ds = gdal.Nearblack('/vsimem/test_nearblack_lib_5.tif',
                        '../gdrivers/data/rgbsmall.tif',
                        format='GTiff',
                        maxNonBlack=0,
                        setMask=True)
    assert ds is not None

    assert ds.GetRasterBand(1).GetMaskBand().Checksum() == 22002, \
        'Bad checksum mask band'

    ds = None

    gdal.Unlink('/vsimem/test_nearblack_lib_5.tif')
    gdal.Unlink('/vsimem/test_nearblack_lib_5.tif.msk')
예제 #9
0
def test_nearblack_lib_5():

    ds = gdal.Nearblack('/vsimem/test_nearblack_lib_5.tif', '../gdrivers/data/rgbsmall.tif', format='GTiff', maxNonBlack=0, setMask=True)
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(1).GetMaskBand().Checksum() != 22002:
        print(ds.GetRasterBand(1).GetMaskBand().Checksum())
        gdaltest.post_reason('Bad checksum mask band')
        return 'fail'

    ds = None

    gdal.Unlink('/vsimem/test_nearblack_lib_5.tif')
    gdal.Unlink('/vsimem/test_nearblack_lib_5.tif.msk')

    return 'success'
예제 #10
0
def test_nearblack_lib_7():

    ds = gdal.Nearblack('', 'data/whiteblackred.tif', format='MEM', colors=((0, 0, 0), (255, 255, 255)))
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(1).Checksum() != 418 or \
       ds.GetRasterBand(2).Checksum() != 0 or \
       ds.GetRasterBand(3).Checksum() != 0 :
        print(ds.GetRasterBand(1).Checksum())
        print(ds.GetRasterBand(2).Checksum())
        print(ds.GetRasterBand(3).Checksum())
        gdaltest.post_reason('Bad checksum')
        return 'fail'

    ds = None

    return 'success'
예제 #11
0
def test_nearblack_lib_4():

    src_ds = gdal.Warp('',
                       '../gdrivers/data/rgbsmall.tif',
                       format='MEM',
                       warpOptions=["INIT_DEST=255"],
                       srcNodata=0)
    ds = gdal.Nearblack('',
                        src_ds,
                        format='MEM',
                        white=True,
                        maxNonBlack=0,
                        setAlpha=True)
    assert ds is not None

    assert ds.GetRasterBand(4).Checksum() == 24151, 'Bad checksum band 0'

    ds = None
예제 #12
0
def test_nearblack_lib_1():

    src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
    ds = gdal.Nearblack('', src_ds, format='MEM', maxNonBlack=0, nearDist=15)
    assert ds is not None

    assert ds.GetRasterBand(1).Checksum() == 21106, 'Bad checksum band 1'

    assert ds.GetRasterBand(2).Checksum() == 20736, 'Bad checksum band 2'

    assert ds.GetRasterBand(3).Checksum() == 21309, 'Bad checksum band 3'

    src_gt = src_ds.GetGeoTransform()
    dst_gt = ds.GetGeoTransform()
    for i in range(6):
        assert abs(src_gt[i] - dst_gt[i]) <= 1e-10, 'Bad geotransform'

    dst_wkt = ds.GetProjectionRef()
    assert dst_wkt.find('AUTHORITY["EPSG","4326"]') != -1, 'Bad projection'

    src_ds = None
    ds = None
예제 #13
0
def test_nearblack_lib_1():

    src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
    ds = gdal.Nearblack('', src_ds, format='MEM', maxNonBlack=0, nearDist=15)
    if ds is None:
        return 'fail'

    if ds.GetRasterBand(1).Checksum() != 21106:
        print(ds.GetRasterBand(1).Checksum())
        gdaltest.post_reason('Bad checksum band 1')
        return 'fail'

    if ds.GetRasterBand(2).Checksum() != 20736:
        print(ds.GetRasterBand(2).Checksum())
        gdaltest.post_reason('Bad checksum band 2')
        return 'fail'

    if ds.GetRasterBand(3).Checksum() != 21309:
        print(ds.GetRasterBand(3).Checksum())
        gdaltest.post_reason('Bad checksum band 3')
        return 'fail'

    src_gt = src_ds.GetGeoTransform()
    dst_gt = ds.GetGeoTransform()
    for i in range(6):
        if abs(src_gt[i] - dst_gt[i]) > 1e-10:
            gdaltest.post_reason('Bad geotransform')
            return 'fail'

    dst_wkt = ds.GetProjectionRef()
    if dst_wkt.find('AUTHORITY["EPSG","4326"]') == -1:
        gdaltest.post_reason('Bad projection')
        return 'fail'

    src_ds = None
    ds = None

    return 'success'