Example #1
0
    def __init__(self, table, raster_path, output_path, floor, **kwargs):
        # postgis
        self.postgis_source = postgis.PostgisSource(**kwargs)
        self.table = table

        # raster
        if isdir(raster_path):
            raster_datasets = [gdal.Open(join(raster_path, path))
                               for path in sorted(os.listdir(raster_path))]
        else:
            raster_datasets = [gdal.Open(raster_path)]
        self.raster_group = groups.Group(*raster_datasets)

        # properties
        self.projection = self.raster_group.projection
        self.geo_transform = self.raster_group.geo_transform
        self.no_data_value = self.raster_group.no_data_value.item()

        self.kwargs = {
            'projection': self.projection,
            'no_data_value': self.no_data_value,
        }

        # output
        self.output_path = output_path
        self.floor = floor
Example #2
0
    def __init__(self, raster_path, output_path):
        # see class.__doc__
        azimuth = 216
        elevation = 57

        # put the input raster(s) in a group
        if os.path.isdir(raster_path):
            datasets = [
                gdal.Open(os.path.join(raster_path, path))
                for path in sorted(os.listdir(raster_path))
            ]
        else:
            datasets = [gdal.Open(raster_path)]
        self.group = groups.Group(*datasets)

        slope = math.tan(math.radians(elevation))
        m_per_px = self.group.geo_transform[1]

        dx = math.sin(math.radians(azimuth))
        dy = -math.cos(math.radians(azimuth))

        # calculate shift and corresponding elevation change
        self.ds = 1 / max(abs(dx), abs(dy))  # pixels
        self.dz = self.ds * slope * m_per_px  # meters
        self.dx = dx * self.ds  # pixels
        self.dy = dy * self.ds  # pixels

        # calculate margin for input data
        self.mz = 367  # gerbrandy tower, in meters
        ms = self.mz / slope / m_per_px  # pixels
        self.mx = int(math.copysign(math.ceil(abs(dx * ms)), dx))  # pixels
        self.my = int(math.copysign(math.ceil(abs(dy * ms)), dy))  # pixels

        self.output_path = output_path
Example #3
0
def line_up(source_path, raster_path, target_path, **kwargs):
    """
    Take linestrings from source and create target with height added.

    Source and target are both shapefiles.
    """
    # target
    if os.path.exists(target_path):
        if kwargs.pop('overwrite'):
            DRIVER.DeleteDataSource(str(target_path))
        else:
            logger.info('"%s" already exists. Use --overwrite.', target_path)
            return
    target = DRIVER.CreateDataSource(str(target_path))

    # rasters
    if os.path.isdir(raster_path):
        datasets = [
            gdal.Open(os.path.join(raster_path, path))
            for path in sorted(os.listdir(raster_path))
        ]
    else:
        datasets = [gdal.Open(raster_path)]
    raster = groups.Group(*datasets)

    # source
    source = datasources.PartialDataSource(source_path)

    process(raster=raster, source=source, target=target, **kwargs)
Example #4
0
    def __init__(self, direction_path, accumulation_path, target_path):
        # paths and source data
        self.direction_group = groups.Group(gdal.Open(direction_path))
        self.accumulation_group = groups.Group(gdal.Open(accumulation_path))
        self.target_path = target_path

        # geospatial reference
        self.geo_transform = self.direction_group.geo_transform
        self.projection = self.direction_group.projection
Example #5
0
    def __init__(self, output_path, raster_path, cover_path):
        # paths and source data
        self.output_path = output_path
        self.raster_group = groups.Group(gdal.Open(raster_path))
        self.cover_group = groups.Group(gdal.Open(cover_path))

        # geospatial reference
        self.geo_transform = self.raster_group.geo_transform
        self.projection = self.raster_group.projection
Example #6
0
    def __init__(self, source_path, target_path):
        """ Init group. """
        if os.path.isdir(source_path):
            raster_datasets = [gdal.Open(os.path.join(source_path, path))
                               for path in sorted(os.listdir(source_path))]
        else:
            raster_datasets = [gdal.Open(source_path)]

        self.group = groups.Group(*raster_datasets)
        self.projection = self.group.projection
        self.geo_transform = self.group.geo_transform
        self.no_data_value = self.group.no_data_value

        self.target_path = target_path
def prepare_aerial_array(tmp_aerial_tif, aerial_image_path, projwin_garden,
                         sr):

    # snap aerial image based on one building to geotiff
    ecw_gdal_translate(aerial_image_path, tmp_aerial_tif, projwin_garden)

    # inladen tmp aerial tiff for further calculation
    raster = gdal.Open(tmp_aerial_tif)
    rgbt = raster.ReadAsArray()
    geotransform = raster.GetGeoTransform()
    dtype = raster.GetRasterBand(1).DataType
    width = raster.RasterXSize
    height = raster.RasterYSize

    rasterdata = {
        'projection': osr.GetUserInputAsWKT(str(sr)),
        'geo_transform': geotransform,
        # 'no_data_value': 1,
        'data_type': dtype,
        'sr': sr,
        'width': width,
        'height': height
    }

    return rgbt, rasterdata
Example #8
0
    def __init__(self, output_path, raster_path, cover_path):
        # paths and source data
        self.output_path = output_path

        # rasters
        if os.path.isdir(raster_path):
            raster_datasets = [
                gdal.Open(os.path.join(raster_path, path))
                for path in sorted(os.listdir(raster_path))
            ]
        else:
            raster_datasets = [gdal.Open(raster_path)]
        self.raster_group = groups.Group(*raster_datasets)
        self.cover_group = groups.Group(gdal.Open(cover_path))

        # properties
        self.projection = self.raster_group.projection
        self.geo_transform = self.raster_group.geo_transform
        self.no_data_value = self.raster_group.no_data_value
Example #9
0
    def __init__(self, output_path, raster_path, iterations):
        # paths and source data
        self.iterations = iterations
        self.output_path = output_path

        # rasters
        if os.path.isdir(raster_path):
            datasets = [gdal.Open(os.path.join(raster_path, path))
                        for path in sorted(os.listdir(raster_path))]
        else:
            datasets = [gdal.Open(raster_path)]
        self.raster_group = groups.Group(*datasets)

        # geospatial reference
        self.geo_transform = self.raster_group.geo_transform
        self.projection = self.raster_group.projection

        # data settings
        self.no_data_value = self.raster_group.no_data_value
Example #10
0
 def __init__(self, raster_path, output_path):
     self.group = groups.Group(gdal.Open(raster_path))
     self.output_path = output_path
Example #11
0
def rebase(base_path, source_path, target_path):
    """ Rebase source on base and write it to target. """
    # skip existing
    if exists(target_path):
        print('{} skipped.'.format(target_path))
        return

    # skip when missing sources
    if not exists(source_path):
        print('{} not found.'.format(source_path))

    # prepare dirs
    try:
        os.makedirs(dirname(target_path))
    except OSError:
        pass

    # read source dataset
    source_dataset = gdal.Open(str(source_path))
    source_band = source_dataset.GetRasterBand(1)
    source_no_data_value = source_band.GetNoDataValue()
    source_array = source_band.ReadAsArray()

    # prepare target array
    target_projection = source_dataset.GetProjection()
    target_geo_transform = source_dataset.GetGeoTransform()
    target_no_data_value = np.finfo(source_array.dtype).max
    target_array = np.full_like(source_array, target_no_data_value)

    # copy active cells
    source_mask = (source_array != source_no_data_value)
    target_array[source_mask] = source_array[source_mask]

    # rebase
    if exists(base_path):
        base_dataset = gdal.Open(str(base_path))
        base_band = base_dataset.GetRasterBand(1)
        base_no_data_value = base_band.GetNoDataValue()
        base_array = base_band.ReadAsArray()

        # combined mask has active pixels from source and base that are equal
        mask = (base_array != base_no_data_value)
        equal = (source_array == base_array)
        blank = source_mask & mask & equal
        target_array[blank] = target_no_data_value

        method = 'rebase'
    else:
        method = 'copy'

    # write
    kwargs = {
        'projection': target_projection,
        'geo_transform': target_geo_transform,
        'no_data_value': target_no_data_value.item(),
    }

    # write
    with datasets.Dataset(target_array[np.newaxis, ...], **kwargs) as dataset:
        DRIVER_GDAL_GTIFF.CreateCopy(target_path, dataset, options=OPTIONS)
    print('{} created ({}).'.format(target_path, method))