Beispiel #1
0
def test_data_dir_overlapping(tmp_path):
    kwargs = {
        "crs": "EPSG:4326",
        "transform": affine.Affine(0.2, 0, -114, 0, -0.2, 46),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10,
        "nodata": 0,
    }

    with rasterio.open(tmp_path.joinpath("nw1.tif"), "w", **kwargs) as dst:
        data = numpy.ones((10, 10), dtype=rasterio.uint8)
        dst.write(data, indexes=1)

    with rasterio.open(tmp_path.joinpath("nw3.tif"), "w", **kwargs) as dst:
        data = numpy.ones((10, 10), dtype=rasterio.uint8) * 3
        dst.write(data, indexes=1)

    kwargs["transform"] = affine.Affine(0.2, 0, -113, 0, -0.2, 45)

    with rasterio.open(tmp_path.joinpath("se.tif"), "w", **kwargs) as dst:
        data = numpy.ones((10, 10), dtype=rasterio.uint8) * 2
        dst.write(data, indexes=1)

    return tmp_path
Beispiel #2
0
def test_update_spatial_epsg(data):
    tiffname = str(data.join('RGB.byte.tif'))
    with rasterio.open(tiffname, 'r+') as f:
        f.transform = affine.Affine(1.0, 0.0, 1.0, 0.0, -1.0, 0.0)
        f.crs = 'EPSG:4326'
    with rasterio.open(tiffname) as f:
        assert f.transform == affine.Affine(1.0, 0.0, 1.0, 0.0, -1.0, 0.0)
        assert f.transform.to_gdal() == (1.0, 1.0, 0.0, 0.0, 0.0, -1.0)
        assert f.crs == {'init': 'epsg:4326'}
Beispiel #3
0
def test_get_tiled_transform_shape():
    src_res = 10
    src_transform = affine.Affine(src_res, 0, 36000, 0, -src_res, 18000)
    src_shape = (1000, 2000)
    dst_res = src_res * 10
    dst_transform, dst_shape = tiling.get_tiled_transform_shape(src_transform, src_shape, dst_res)
    expected_transform = src_transform * affine.Affine(10, 0, 0, 0, 10, 0)
    assert dst_transform == expected_transform
    assert dst_shape == (100, 200)
 def __init__(self,
              input_dim=(1, 28, 28),
              conv_param={
                  'filter_num': 30,
                  'filter_size': 5,
                  'pad': 0,
                  'stride': 1
              },
              hidden_size=100,
              output_size=10,
              weight_init=0.01):
     # 学习这些python的编程规范和思想
     filter_num = conv_param['filter_num']
     filter_size = conv_param['filter_size']
     filter_pad = conv_param['pad']
     filter_stride = conv_param['stride']
     input_size = input_dim[1]
     conv_output_size = (input_size - filter_size +
                         2 * filter_pad) // filter_stride + 1
     # 不知道这个参数有什么用?
     pool_output_size = int(
         (filter_num * (conv_output_size / 2) * (conv_output_size / 2)))
     # 需要int这个函数,中间出现了除法
     self.params = {}
     #  这里就看出W的形状了
     self.params['W1'] = weight_init * np.random.randn(
         filter_num, input_dim[0], filter_size, filter_size)
     self.params['b1'] = np.zeros(filter_num)
     self.params['W2'] = weight_init * np.random.randn(
         pool_output_size, hidden_size)
     self.params['b2'] = np.zeros(hidden_size)
     self.params['W3'] = weight_init * np.random.rand(
         hidden_size, output_size)
     self.params['b3'] = np.zeros(output_size)
     #  一个卷积层,两个全连接层
     self.layers = OrderedDict()
     self.layers['Conv1'] = cnn.Convelution(self.params['W1'],
                                            self.params['b1'],
                                            conv_param['stride'],
                                            conv_param['pad'])
     self.layers['Relu1'] = affine.ReLu()  # 有时候把这个归结为卷积层,在模型中要指定激活函数,自动做好。
     self.layers['Pool1'] = cnn.Pooling(pool_h=2, pool_w=2,
                                        stride=2)  # 单独定制
     self.layers['Affine1'] = affine.Affine(self.params['W2'],
                                            self.params['b2'])
     #  全连接层也要有激活函数层。
     self.layers['Relu2'] = affine.ReLu()
     self.layers['Affine2'] = affine.Affine(self.params['W3'],
                                            self.params['b3'])
     self.last_layer = affine.SoftmaxWithLoss()  # 该层用于计算损失函数
def _create_xform(path):
    # From https://stackoverflow.com/questions/2922532/obtain-latitude-and-longitude-from-a-geotiff-file
    gdal.UseExceptions()
    ds = gdal.Open(path)
    old_cs = osr.SpatialReference()
    old_cs.ImportFromWkt(ds.GetProjectionRef())

    # create the new coordinate system
    wgs84_wkt = """
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.01745329251994328,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]]"""
    new_cs = osr.SpatialReference()
    new_cs.ImportFromWkt(wgs84_wkt)

    gt = ds.GetGeoTransform()
    c, a, b, f, d, e = gt
    gta = affine.Affine(a, b, c, d, e, f)

    # Apparently GDAL can segfault if stuff goes out of scope since it doesn't
    # handle garbage collection correctly. So
    return old_cs, new_cs, gta, locals()
Beispiel #6
0
def downscale_wrapper( arr, affine, crs, baseline, output_filename, downscaling_operation, post_downscale_function ):
	# rotate
	if ( self.data.ds.lon > 200.0 ).any() == True:
		dat, lons = utils.shiftgrid( 180., self.data.anomalies, self.historical.ds.lon, start=False )
		a,b,c,d,e,f,g,h,i = affine #flip it to the greenwich-centering
		src_transform = affine.Affine( a, b, -180.0, d, e, 180.0 )
	else:
		dat, lons = ( self.data.ds, self.historical.ds.lon )
		src_transform = affine
	
	# reproject / resample
	src_crs = {'init':'epsg:4326'}
	src_nodata = None # DangerTown™
	baseline_meta = baseline.meta
	baseline_meta.update( compress='lzw' )
	output_arr = np.empty_like( baeline.read( 1 ) )

	# TODO: make this function available for manipulation if used for different needs
	reproject( arr, output_arr, src_transform=src_transform, src_crs=src_crs, src_nodata=src_nodata, \
			dst_transform=baseline_meta['affine'], dst_crs=baseline_meta['crs'],\
			dst_nodata=None, resampling=RESAMPLING.cubic_spline, SOURCE_EXTRA=1000 )
	
	# downscale
	return utils.downscale( arr, output_arr, output_filename, downscaling_operation, \
					baseline_meta, post_downscale_function, mask=None, mask_value=0 )
Beispiel #7
0
def points_to_2d(ds):
    """Map points dataset back to 2D coordinates

    Parameters
    ----------
    ds : Dataset
        dataset with points

    Returns
    -------
    ds
        dataset with lon, lat
    """
    shape = height, width = (ds.attrs['height'], ds.attrs['width'])
    transform = affine.Affine(*ds.attrs['transform'][:6])
    lon, lat = lonlat_from_transform(transform, width=width, height=height)
    coords = dict(lon=lon, lat=lat)

    darrs = {}
    for name, da in ds.data_vars.items():
        data = np.ma.zeros(shape=shape, dtype=da.dtype)
        data[:] = np.ma.masked
        data[ds['j'], ds['i']] = da.values
        darrs[name] = xr.DataArray(data, dims=('lat', 'lon'), name=name)

    dsout = xr.Dataset(darrs, coords=coords)
    dsout['time'] = ds['time']

    return dsout
Beispiel #8
0
def invalid_raster_file(tmpdir_factory):
    """A raster file that is all nodata"""
    import affine

    raster_data = np.full((256, 256), 0, dtype='uint16')
    profile = {
        'driver': 'GTiff',
        'dtype': 'uint16',
        'nodata': 0,
        'width': raster_data.shape[1],
        'height': raster_data.shape[0],
        'count': 1,
        'crs': {'init': 'epsg:32637'},
        'transform': affine.Affine(
            2.0, 0.0, 694920.0,
            0.0, -2.0, 2055666.0
        )
    }

    outpath = tmpdir_factory.mktemp('raster')
    unoptimized_raster = outpath.join('img-raw.tif')
    with rasterio.open(str(unoptimized_raster), 'w', **profile) as dst:
        dst.write(raster_data, 1)

    optimized_raster = outpath.join('img.tif')
    cloud_optimize(unoptimized_raster, optimized_raster)

    return optimized_raster
Beispiel #9
0
def unoptimized_raster_file(tmpdir_factory):
    import affine

    np.random.seed(17)
    raster_data = np.random.randint(0, np.iinfo(np.uint16).max, size=(1024, 1024), dtype='uint16')
    nodata = 10000

    # include some big nodata regions
    ix, iy = np.indices(raster_data.shape)
    circular_mask = np.sqrt((ix - raster_data.shape[0] / 2) ** 2
                            + (iy - raster_data.shape[1] / 2) ** 2) > 400
    raster_data[circular_mask] = nodata
    raster_data[200:600, 400:800] = nodata
    raster_data[500, :] = nodata

    profile = {
        'driver': 'GTiff',
        'dtype': 'uint16',
        'nodata': nodata,
        'width': raster_data.shape[1],
        'height': raster_data.shape[0],
        'count': 1,
        'crs': {'init': 'epsg:32637'},
        'transform': affine.Affine(
            2.0, 0.0, 694920.0,
            0.0, -2.0, 2055666.0
        )
    }

    outpath = tmpdir_factory.mktemp('raster')
    unoptimized_raster = outpath.join('img-raw.tif')
    with rasterio.open(str(unoptimized_raster), 'w', **profile) as dst:
        dst.write(raster_data, 1)

    return unoptimized_raster
Beispiel #10
0
def raster_file(tmpdir_factory):
    import affine

    raster_data = np.arange(-128 * 256, 128 * 256, dtype='int16').reshape(256, 256)

    # Sprinkle in some more nodata
    raster_data.flat[::5] = 10000

    profile = {
        'driver': 'GTiff',
        'dtype': 'int16',
        'nodata': 10000,
        'width': raster_data.shape[1],
        'height': raster_data.shape[0],
        'count': 1,
        'crs': {'init': 'epsg:32637'},
        'transform': affine.Affine(
            2.0, 0.0, 694920.0,
            0.0, -2.0, 2055666.0
        )
    }

    outpath = tmpdir_factory.mktemp('raster')
    unoptimized_raster = outpath.join('img-raw.tif')
    with rasterio.open(str(unoptimized_raster), 'w', **profile) as dst:
        dst.write(raster_data, 1)

    optimized_raster = outpath.join('img.tif')
    cloud_optimize(unoptimized_raster, optimized_raster)

    return optimized_raster
Beispiel #11
0
 def __init__(
     self,
     dem: np.ndarray,
     cellsize: tuple,
     diff: callable = diff_finite,
     indexing: str = 'xy',
     transform: list = None,
     crs: str = None,
     **kwargs,
 ):
     self._cache = {}
     self._dem = _as_float_array(dem)
     self._diff = diff
     self._transform = transform
     self._crs = crs
     self._kwargs = kwargs
     if isinstance(cellsize, numbers.Number):
         self._cellsize = (cellsize, cellsize)
     elif isinstance(cellsize, (tuple, list)):
         self._cellsize = cellsize
     else:
         raise TypeError(
             "Expected a number for `cellsize`, but received a {!r}: {!r}".
             format(type(cellsize), cellsize))
     self._indexing = ''.join(indexing)
     if self._indexing not in ('xy', 'ij'):
         raise ValueError("Invalid indexing: {!r}".format(self._indexing))
     self._transform = transform and affine.Affine(*transform)
Beispiel #12
0
 def rasterize_gsm(self, gsm, out_tif, cell_width=0.01):
     bounds = gsm.total_bounds
     width_cells = int(round((bounds[2] - bounds[0]) / cell_width))
     height_cells = int(round(((bounds[3] - bounds[1]) / cell_width)))
     cAffine = affine.Affine(cell_width, 0, bounds[0], 0, cell_width * -1,
                             bounds[3])
     cMeta = {
         'count': 1,
         'crs': gsm.crs,
         'dtype': 'uint8',
         'affine': cAffine,
         'driver': 'GTiff',
         'transform': cAffine,
         'height': height_cells,
         'width': width_cells
     }
     shapes = ((row.geometry, 1) for idx, row in gsm.iterrows())
     with rasterio.open(out_tif, 'w', **cMeta) as out:
         burned = features.rasterize(shapes=shapes,
                                     fill=0.,
                                     out_shape=(cMeta['height'],
                                                cMeta['width']),
                                     transform=out.transform)
         burned = burned.astype(cMeta['dtype'])
         out.write_band(1, burned)
Beispiel #13
0
def test_pad():
    arr = numpy.ones((10, 10))
    trans = affine.Affine(1.0, 0.0, 0.0, 0.0, -1.0, 10.0)
    arr2, trans2 = rasterio.pad(arr, trans, 2, 'edge')
    assert arr2.shape == (14, 14)
    assert trans2.xoff ==  -2.0
    assert trans2.yoff ==  12.0
Beispiel #14
0
def adjust_bounds_to_px_grid(bounds):
    """
    Adjust the bounds to fall exactly onto the pixel grid (center of pixels) of SRTM90.

    Args: 
        bounds: tuple (lon_min, lat_min, lon_max, lat_max) 

    Returns: 
        adjusted_bounds: tuple (lon_min, lat_min, lon_max, lat_max) 
                        The bounds are of adjusted on the center of the pixels. 
                        The last pixel (max) is included in the image.
        transform: affine.Affine px_is_area transform
        shape: tuple (height, width) shape of the output image.         

    """
    lon_min, lat_min, lon_max, lat_max = bounds

    # Adjust to pixel grid
    col_min = int(np.floor(lon_min / RES))
    col_max = int(np.ceil(lon_max / RES))
    row_min = int(np.floor(lat_min / RES))
    row_max = int(np.ceil(lat_max / RES))

    lon_min = RES * col_min
    lon_max = RES * col_max
    lat_min = RES * row_min
    lat_max = RES * row_max

    adjusted_bounds = (lon_min, lat_min, lon_max, lat_max)
    # Translate by half a pixel for the px_is_area transform( upper left corner)
    transform = affine.Affine(RES, 0, lon_min - RES / 2, 0, -RES,
                              lat_max + RES / 2)
    shape = (row_max - row_min + 1, col_max - col_min + 1)

    return adjusted_bounds, transform, shape
Beispiel #15
0
def _get_transform(projection_meta):
    coefs = (projection_meta[key] for key in [
        'colSpacing', 'orientationAngle', 'originX', 'orientationAngle',
        'rowSpacing', 'originY'
    ])
    a, b, c, d, e, f = coefs
    return affine.Affine(a, b, c, d, -e, f)
Beispiel #16
0
def _get_inputs():
    band_ids = [0, 1, 2]
    nbands = len(band_ids)
    ny, nx = 200, 100
    data = np.random.randint(0,
                             np.iinfo('uint16').max,
                             size=(nbands, ny, nx),
                             dtype='uint16')
    mtdFile = MTDFILES['WV02.imd']
    profile = dict(width=nx,
                   height=ny,
                   crs=rasterio.crs.CRS({'init': 'epsg:32640'}),
                   transform=affine.Affine(2.0, 0.0, 364466.0808031342, 0.0,
                                           -2.0, 2836767.9090107735),
                   nodata=0,
                   count=nbands)
    inputs = dict(data=data,
                  profile=profile,
                  sensor='WV2',
                  mtdFile=mtdFile,
                  sixs_params=dict(aeroProfile='Maritime',
                                   atm={
                                       'AOT': 0.35,
                                       'PWV': 1.0,
                                       'ozone': 0.15
                                   }),
                  band_ids=band_ids,
                  adjCorr=True,
                  aotMultiplier=1.0,
                  mtdFile_tile=None,
                  date=None,
                  use_modis=False,
                  modis_atm_dir=None,
                  earthdata_credentials={})
    return inputs
Beispiel #17
0
def test_warp_reproject_like(runner, tmpdir):
    likename = str(tmpdir.join('like.tif'))
    kwargs = {
        "crs": {
            'init': 'epsg:4326'
        },
        "transform": affine.Affine(0.001, 0, -106.523, 0, -0.001, 39.6395),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10,
        "nodata": 0
    }

    with rasterio.open(likename, 'w', **kwargs) as dst:
        data = np.zeros((10, 10), dtype=rasterio.uint8)
        dst.write(data, indexes=1)

    srcname = 'tests/data/shade.tif'
    outputname = str(tmpdir.join('test.tif'))
    result = runner.invoke(main_group,
                           ['warp', srcname, outputname, '--like', likename])
    assert result.exit_code == 0
    assert os.path.exists(outputname)

    with rasterio.open(outputname) as output:
        assert output.crs == {'init': 'epsg:4326'}
        assert np.allclose([0.001, 0.001],
                           [output.transform.a, -output.transform.e])
        assert output.width == 10
        assert output.height == 10
Beispiel #18
0
def rasterizeDataFrame(inD,
                       outFile,
                       idField='',
                       templateRaster='',
                       nCells=100):
    ''' Convert input geopandas dataframe into a raster file
        inD = gpd.read_file(r"C:\Temp\TFRecord\Data\Training Data\test3_training.shp")
        templateRaster=r"C:\Temp\TFRecord\Data\Training Data\test3.tif"
        idField = 'ID2'
        outFile = templateRaster.replace(".tif", "_labels.tif")

    INPUT VARIABLES
    inD [geopandas DataFrame]
    outFile [string] - path for creating output file
    OPTIONAL
    idField [string] - field to rasterize, sets everything to 1 otherwise
    templateRaster [string] - raster upon which to base raster creation
    nCells - resolution of output, if no template raster

    EXAMPLE
    templateRaster=r"C:\Temp\TFRecord\Data\Training Data\test3.tif"
    idField = 'ID2'
    inD = r"C:\Temp\TFRecord\Data\Training Data\test3_training.shp"
    rasterizeDataFrame(inD, templateRaster.replace(".tif", "_labels.tif"),
                        idField=idField, templateRaster=templateRaster)
    '''
    #Set VALUE field equal to idField
    inD['VALUE'] = 1
    if idField != '':
        inD['VALUE'] = inD[idField]

    if templateRaster != '':
        inR = rasterio.open(templateRaster)
        cMeta = inR.meta.copy()
        cMeta.update(count=1)
    else:
        bounds = inD.unary_union.bounds
        cellWidth = (bounds[2] - bounds[0]) / nCells
        cellHeight = ((bounds[3] - bounds[1]) / nCells) * -1
        cAffine = affine.Affine(bounds[0], cellWidth, 0, bounds, 0, cellHeight)
        nTransform = (bounds[0], cellWidth, 0, bounds[3], 0, cellHeight)
        cMeta = {
            'count': 1,
            'crs': inD.crs,
            'dtype': 'uint8',
            'affine': cAffine,
            'driver': 'GTiff',
            'transform': nTransform,
            'height': nCells,
            'width': nCells
        }
    shapes = ((row.geometry, row.VALUE) for idx, row in inD.iterrows())
    with rasterio.open(outFile, 'w', **cMeta) as out:
        burned = features.rasterize(shapes=shapes,
                                    fill=0,
                                    out_shape=(cMeta['height'],
                                               cMeta['width']),
                                    transform=out.transform)
        burned = burned.astype(cMeta['dtype'])
        out.write_band(1, burned)
    def __init__(self, input_size, hidden_size, output_size, weigth_init=0.01):
        self.param = {}  #对象应该带有哪些属性
        self.param['w1'] = weigth_init * np.random.randn(
            input_size, hidden_size)
        self.param['b1'] = np.zeros(hidden_size)
        self.param['w2'] = weigth_init * np.random.randn(
            hidden_size, output_size)
        self.param['b2'] = np.zeros(output_size)

        self.layers = OrderedDict()
        self.layers['Affine1'] = affine.Affine(self.param['w1'],
                                               self.param['b1'])
        self.layers['ReLu1'] = affine.ReLu()
        self.layers['Affine2'] = affine.Affine(self.param['w2'],
                                               self.param['b2'])
        self.lastLayer = affine.SoftmaxWithLoss()
Beispiel #20
0
def test_data_dir_2(tmpdir):
    kwargs = {
        "crs": {
            'init': 'epsg:4326'
        },
        "transform": affine.Affine(0.2, 0, -114, 0, -0.2, 46),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10
        # these files have undefined nodata.
    }

    with rasterio.open(str(tmpdir.join('b.tif')), 'w', **kwargs) as dst:
        data = np.zeros((10, 10), dtype=rasterio.uint8)
        data[0:6, 0:6] = 255
        dst.write(data, indexes=1)

    with rasterio.open(str(tmpdir.join('a.tif')), 'w', **kwargs) as dst:
        data = np.zeros((10, 10), dtype=rasterio.uint8)
        data[4:8, 4:8] = 254
        dst.write(data, indexes=1)

    return tmpdir
Beispiel #21
0
    def _create_path_multiband_no_colorinterp(count):
        # For GDAL 2.2.2 the first band can be 'undefined', but on older
        # versions it must be 'gray'.
        undefined_ci = [ColorInterp.gray]
        if count > 1:
            undefined_ci += [ColorInterp.undefined] * (count - 1)
        dst_path = str(tmpdir.join('4band-byte-no-ci.tif'))
        profile = {
            'height': 10,
            'width': 10,
            'count': count,
            'dtype': rasterio.ubyte,
            'transform': affine.Affine(1, 0.0, 0, 0.0, -1, 1),
            'driver': 'GTiff',
            'photometric': 'minisblack'
        }

        undefined_ci = tuple(undefined_ci)
        with rasterio.open(dst_path, 'w', **profile) as src:
            src.colorinterp = undefined_ci

        # Ensure override occurred.  Setting color interpretation on an
        # existing file is surrounded by traps and forceful GDAL assumptions,
        # especially on older versions.
        with rasterio.open(dst_path) as src:
            if src.colorinterp != undefined_ci:
                raise ValueError(
                    "Didn't properly set color interpretation.  GDAL can "
                    "forcefully make assumptions.")

        return dst_path
Beispiel #22
0
def test_data_dir_3(tmpdir):
    kwargs = {
        "crs": {
            'init': 'epsg:4326'
        },
        "transform": affine.Affine(0.2, 0, -114, 0, -0.2, 46),
        "count": 2,  # important: band count > 1
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10,
        "nodata": 1
    }

    with rasterio.open(str(tmpdir.join('b.tif')), 'w', **kwargs) as dst:
        data = np.ones((2, 10, 10), dtype=rasterio.uint8)
        data[:, 0:6, 0:6] = 255
        dst.write(data)

    with rasterio.open(str(tmpdir.join('a.tif')), 'w', **kwargs) as dst:
        data = np.ones((2, 10, 10), dtype=rasterio.uint8)
        data[:, 4:8, 4:8] = 254
        dst.write(data)

    return tmpdir
Beispiel #23
0
def test_write_crs_transform_affine(tmpdir):
    name = str(tmpdir.join("test_write_crs_transform.tif"))
    a = np.ones((100, 100), dtype=rasterio.ubyte) * 127
    transform = affine.Affine(300.0379266750948, 0.0, 101985.0, 0.0,
                              -300.041782729805, 2826915.0)
    with rasterio.open(name,
                       'w',
                       driver='GTiff',
                       width=100,
                       height=100,
                       count=1,
                       crs={
                           'units': 'm',
                           'no_defs': True,
                           'ellps': 'WGS84',
                           'proj': 'utm',
                           'zone': 18
                       },
                       transform=transform,
                       dtype=rasterio.ubyte) as s:
        s.write(a, indexes=1)

    assert s.crs.to_epsg() == 32618
    info = subprocess.check_output(["gdalinfo", name]).decode('utf-8')
    # make sure that pixel size is nearly the same as transform
    # (precision varies slightly by platform)
    assert re.search(r'Pixel Size = \(300.03792\d+,-300.04178\d+\)', info)
def test_warpedvrt_gcps__width_height(tmp_path):
    tiffname = tmp_path / "test.tif"
    src_gcps = [
        GroundControlPoint(row=0, col=0, x=156113, y=2818720, z=0),
        GroundControlPoint(row=0, col=800, x=338353, y=2785790, z=0),
        GroundControlPoint(row=800, col=800, x=297939, y=2618518, z=0),
        GroundControlPoint(row=800, col=0, x=115698, y=2651448, z=0),
    ]
    crs = CRS.from_epsg(32618)
    with rasterio.open(tiffname,
                       mode='w',
                       height=800,
                       width=800,
                       count=3,
                       dtype=numpy.uint8) as source:
        source.gcps = (src_gcps, crs)

    with rasterio.open(tiffname) as src:
        with WarpedVRT(src, width=10, height=10) as vrt:
            assert vrt.height == 10
            assert vrt.width == 10
            assert vrt.crs == crs
            assert vrt.dst_transform.almost_equals(
                affine.Affine(22271.389322449897, 0.0, 115698.25, 0.0,
                              -20016.05875815117, 2818720.0))
Beispiel #25
0
def test_mask_crop(runner, tmpdir, basic_feature, pixelated_image):
    """
    In order to test --crop option, we need to use a transform more similar to
    a normal raster, with a negative y pixel size.
    """

    image = pixelated_image
    outfilename = str(tmpdir.join('pixelated_image.tif'))
    kwargs = {
        "crs": CRS({'init': 'epsg:4326'}),
        "transform": affine.Affine(1, 0, 0, 0, -1, 0),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": image.shape[1],
        "height": image.shape[0],
        "nodata": 255
    }
    with rasterio.open(outfilename, 'w', **kwargs) as out:
        out.write(image, indexes=1)

    output = str(tmpdir.join('test.tif'))

    truth = np.zeros((3, 3))
    truth[0:2, 0:2] = 1

    result = runner.invoke(
        main_group,
        ['mask', outfilename, output, '--crop', '--geojson-mask', '-'],
        input=json.dumps(basic_feature))
    assert result.exit_code == 0
    assert os.path.exists(output)
    with rasterio.open(output) as out:
        assert np.array_equal(truth, out.read(1, masked=True).filled(0))
Beispiel #26
0
    def compress_image(self, image_path, save_directory, save=True):
        if not os.path.exists(save_directory):
            os.makedirs(save_directory)
        print("Compressing image ", self.IND)
        self.IND += 1
        meta = ""
        name = self.get_file_name(image_path)
        band = []
        if not os.path.exists(save_directory + "/" + name):
            dst_crs = rst.crs.CRS.from_epsg(
                4326)  # Coordinate system Hu Tzu Shan 1950
            dst_width = self.max_width
            dst_height = self.max_height

            xres = (self.maxRight - self.minLeft) / dst_width
            yres = (self.maxTop - self.minBottom) / dst_height
            dst_transform = affine.Affine(xres, 0.0, self.minLeft, 0.0, -yres,
                                          self.maxTop)
            vrt_options = {
                'resampling': rst.enums.Resampling.cubic,
                'crs': dst_crs,
                'transform': dst_transform,
                'height': dst_height,
                'width': dst_width
            }
            if (save):
                raster = rst.open(image_path)
                with WarpedVRT(raster, **vrt_options) as vrt:
                    rio_shutil.copy(vrt,
                                    save_directory + "/" + name,
                                    driver='GTiff')
                    raster.close()

        return name, band, meta
Beispiel #27
0
def plyflatten_from_plyfiles_list(clouds_list,
                                  resolution,
                                  radius=0,
                                  roi=None,
                                  sigma=None):
    """
    Projects a points cloud into the raster band(s) of a raster image (points clouds as files)

    Args:
        clouds_list: list of cloud.ply files
        resolution: resolution of the georeferenced output raster file
        roi: region of interest: (xoff, yoff, xsize, ysize), compute plyextrema if None

    Returns:
        raster: georeferenced raster
        profile: profile for rasterio
    """
    # read points clouds
    full_cloud = list()
    for cloud in clouds_list:
        cloud_data, _ = utils.read_3d_point_cloud_from_ply(cloud)
        full_cloud.append(cloud_data.astype(np.float64))

    full_cloud = np.concatenate(full_cloud)

    # region of interest (compute plyextrema if roi is None)
    if roi is not None:
        xoff, yoff, xsize, ysize = roi
    else:
        xx = full_cloud[:, 0]
        yy = full_cloud[:, 1]
        xmin = np.amin(xx)
        xmax = np.amax(xx)
        ymin = np.amin(yy)
        ymax = np.amax(yy)

        xsize = int(1 + np.floor((xmax - xmin) / resolution))
        ysize = int(1 + np.floor((ymax - ymin) / resolution))
        xoff = (xmax + xmin - resolution * xsize) / 2
        yoff = (ymax + ymin + resolution * ysize) / 2

    # The copy() method will reorder to C-contiguous order by default:
    full_cloud = full_cloud.copy()
    sigma = float("inf") if sigma is None else sigma
    raster = plyflatten(full_cloud, xoff, yoff, resolution, xsize, ysize,
                        radius, sigma)

    utm_zone = utils.utm_zone_from_ply(clouds_list[0])
    utm_proj = utils.utm_proj(utm_zone)

    # construct profile dict
    profile = dict()
    profile["tiled"] = True
    profile["nodata"] = float("nan")
    profile["crs"] = utm_proj.srs
    profile["transform"] = affine.Affine(resolution, 0.0, xoff, 0.0,
                                         -resolution, yoff)

    return raster, profile
Beispiel #28
0
def buffer_and_downsample(target_image,
                          reference_image,
                          outname="reprojected.tif",
                          outdir=None,
                          dst_nodata=0.0,
                          dst_dtype='uint16',
                          resample=Resampling.cubic):
    """
    Resample target_image to match resolution of reference_image. If reference_image is larger than target_image, then
    buffer target_image with no-data values so that it has the same width and height as reference_image.
    :param target_image: (str) file path of the image with the data to be resampled
    :param reference_image: (str) file path of the image with the desired resolution and extent
    :param dst_nodata: (numeric) no-data value for the output image
    :param dst_dtype: (str) data type for the output image
    :param outname: (str) name of output file
    :param outdir: (str) directory to save output image. If not given, image will be saved in working directory
    :param resample: rasterio resampling method, e.g. Resample.bilinear, Resample.cubic
    :return:
    """
    outpath = os.path.join(outdir, outname)

    with rasterio.open(target_image, 'r') as src:
        tgt_data = src.read()
        tgt_meta = src.profile
    tgt_resolution = tgt_meta['transform'][
        0]  # in meters. Assumes square pixels
    with rasterio.open(reference_image, 'r') as src:
        ref_meta = src.profile
    dst_res = ref_meta['transform'][
        0]  # destination resolution in meters. Assumes square pixels
    left = ref_meta['transform'][2]
    top = ref_meta['transform'][5]
    right = left + ref_meta['width'] * dst_res
    bottom = top + ref_meta['height'] * dst_res
    new_transform = affine.Affine(
        dst_res, 0., left, 0., -1. * dst_res,
        top)  # transformation for new image to be saved
    dst_raster = np.zeros(
        (ref_meta['count'], ref_meta['height'], ref_meta['width']),
        dtype=dst_dtype)  # store data
    # reproject the data from target_image into the array dst_raster using the new transformation and the resolution of
    # the reference_image
    rasterio.warp.reproject(tgt_data,
                            dst_raster,
                            src_transform=tgt_meta['transform'],
                            dst_transform=new_transform,
                            src_crs=tgt_meta['crs'],
                            dst_crs=ref_meta['crs'],
                            dst_nodata=dst_nodata,
                            src_nodata=tgt_meta['nodata'],
                            resampling=resample)
    # update the meta for the output file
    dst_profile = ref_meta
    dst_profile['transform'] = new_transform
    dst_profile['nodata'] = dst_nodata
    dst_profile['dtype'] = dst_dtype
    with rasterio.open(outpath, 'w', **dst_profile) as dst:
        dst.write(dst_raster)
    return outpath
Beispiel #29
0
def test_get_projected_extents():
    extent_rec = tiling.get_projected_extents(
        transform=affine.Affine(10, 0, 36000, 0, -10, 18000),
        height=100,
        width=200,
        src_crs={'init': 'epsg:4032'})
    assert extent_rec.shape == (100, 200)
    assert 'xmin' in extent_rec.dtype.names
Beispiel #30
0
def test_recarr_take_dict():
    a = tiling.get_projected_extents(
        transform=affine.Affine(10, 0, 36000, 0, -10, 18000),
        height=100,
        width=200,
        src_crs={'init': 'epsg:4032'})
    d = tiling.recarr_take_dict(a, 0, 0)
    assert type(d) == dict
    assert np.issubdtype(d['xmin'], np.floating)