Exemplo n.º 1
0
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
Exemplo n.º 2
0
def rasterize(feature, source_dir, target_dir):
    """ Rasterize streamline shape for a single tile into raster. """
    geo_transform = get_geo_transform(feature.geometry())
    name = feature[str('name')]
    partial_path = os.path.join(name[:3], name)

    # target path
    target_path = os.path.join(target_dir, partial_path) + '.tif'
    if os.path.exists(target_path):
        return

    # create directory
    try:
        os.makedirs(os.path.dirname(target_path))
    except OSError:
        pass  # no problem

    # open source
    source_path = os.path.join(source_dir, partial_path)
    data_source = ogr.Open(source_path)
    layer = data_source[0]

    # create target array
    kwargs = {'no_data_value': 0,
              'geo_transform': geo_transform,
              'array': np.zeros((1, 12500, 10000), 'u1'),
              'projection': osr.GetUserInputAsWKT(str('epsg:28992'))}

    with datasets.Dataset(**kwargs) as dataset:
        for value, attribute in enumerate([2, 3, 4, 4.7], 2):
            layer.SetAttributeFilter(str('class={}').format(attribute))
            gdal.RasterizeLayer(dataset, [1], layer, burn_values=[value])

        GTIF.CreateCopy(target_path, dataset, options=OPTIONS)
Exemplo n.º 3
0
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import
from __future__ import division

import argparse
import os

from raster_tools import gdal
from raster_tools import ogr
from raster_tools import osr

DRIVER_GDAL_GTIFF = gdal.GetDriverByName(b'gtiff')
DRIVER_GDAL_MEM = gdal.GetDriverByName(b'mem')

PROJECTION = osr.GetUserInputAsWKT(str('epsg:28992'))
NO_DATA_VALUE = 3.4028234663852886e+38
DATA_TYPE = gdal.GDT_Float32

OPTIONS = ['compress=deflate', 'tiled=yes']


def get_geo_transform(geometry, cellsize=(0.5, -0.5)):
    """ Return geotransform. """
    a, b, c, d = cellsize[0], 0.0, 0.0, cellsize[1]
    x1, x2, y1, y2 = geometry.GetEnvelope()
    return x1, a, b, y2, c, d


def constant(index_path, target_dir):
    """ Create tiles with a constant. """
Exemplo n.º 4
0
def command(gardens_path, aerial_image_path, target_path, min_green, max_green,
            check_rast, part, pk):
    # open input shapefile
    shape_gardens = ogr.Open(gardens_path)
    layer_gardens = shape_gardens[0]

    # check projection of input shapefile
    sr = layer_gardens.GetSpatialRef()
    check_sr = get_projection(sr)
    if check_sr is None:
        print('[!] ERROR : EPSG projection code missing from shape.')
        hint = '[*] INFO  : Use this command: gdalsrsinfo -o wkt EPSG:28992 > '
        print(hint, gardens_path.replace('.shp', '.prj'))
        return 1

    # check spaces in aerial_image_path
    if ' ' in aerial_image_path:
        hint = '[!] ERROR : There is a space in the aerial image path: '
        print(hint, aerial_image_path)
        return 1

    # check if raster exists
    if not os.path.isfile(aerial_image_path):
        hint = '[!] ERROR : The specified file could not be found: '
        print(hint, aerial_image_path)
        return 1

    # get raster info and check rasterformat
    rasterfmt = os.path.splitext(aerial_image_path)[1]
    if (rasterfmt == '.ecw' or rasterfmt == '.tif' or rasterfmt == '.vrt'):
        pixelsize, envelope_aerial_image = gdalinfo(aerial_image_path)
    else:
        hint = '[!] ERROR : No supported raster format selected: '
        print(hint, rasterfmt, ' (use .tif, .vrt or .ecw)')
        return 1

    # delete any existing target
    driver = ogr.GetDriverByName(str('ESRI Shapefile'))
    try:
        driver.DeleteDataSource(str(target_path))
    except RuntimeError:
        pass

    # prepare target dataset
    target_shape = driver.CreateDataSource(str(target_path))
    target_layer_name = os.path.basename(target_path)
    target_layer = target_shape.CreateLayer(target_layer_name, sr)
    wkt = osr.GetUserInputAsWKT(str('EPSG:28992'))
    open(target_path.replace('.shp', '.prj'), 'w').write(wkt)
    target_layer_defn = layer_gardens.GetLayerDefn()
    for i in range(target_layer_defn.GetFieldCount()):
        target_field_defn = target_layer_defn.GetFieldDefn(i)
        target_layer.CreateField(target_field_defn)
    target_field_defn = ogr.FieldDefn(str('green_perc'), ogr.OFTReal)
    target_field_defn.SetWidth(5)
    target_field_defn.SetPrecision(3)
    target_layer.CreateField(target_field_defn)
    target_field_defn = ogr.FieldDefn(str('green_area'), ogr.OFTReal)
    target_field_defn.SetWidth(10)
    target_field_defn.SetPrecision(2)
    target_layer.CreateField(target_field_defn)

    # create tmp_dir for files
    tmp_dirname = os.path.join(os.path.dirname(target_path), 'tmp_greenfactor')
    if not os.path.exists(tmp_dirname):
        os.makedirs(tmp_dirname)

    # initialise progress bar
    total = layer_gardens.GetFeatureCount()
    ogr.TermProgress_nocb(0)

    index = datasources.PartialDataSource(gardens_path)
    if part is not None:
        index = index.select(part)

    # main loop
    for count, feature_garden in enumerate(index):
        # get envelope and convert it to the projwin for ecw_gdal_translate
        geometry_garden = feature_garden.geometry()
        garden_name = feature_garden.GetFieldAsString(str(pk))
        envelope_garden = geometry_garden.GetEnvelope()
        skip_large_size = check_envelopes_input(garden_name, envelope_garden,
                                                envelope_aerial_image)
        if not skip_large_size == 1:
            x1, x2, y1, y2 = envelope_garden
            x1, y1 = np.floor(np.divide((x1, y1), pixelsize)) * pixelsize
            x2, y2 = np.ceil(np.divide((x2, y2), pixelsize)) * pixelsize
            envelope_garden_round = (x1, x2, y1, y2)
            projwin_garden = '%s %s %s %s' % (x1, y2, x2, y1)

            # create filename clipped aerial image geotiff
            tmp_aerial_tif = os.path.join(tmp_dirname, garden_name + '.tif')
            tmp_mask_tif = os.path.join(tmp_dirname, garden_name + '_mask.tif')
            tmp_green_tif = os.path.join(tmp_dirname,
                                         garden_name + '_green.tif')

            # prepare rgbt-array for green factor calculation
            rgbt, rasterdata = prepare_aerial_array(tmp_aerial_tif,
                                                    aerial_image_path,
                                                    projwin_garden, sr)

            # prepare mask-array for green factor calculation
            z = rgbt[:1, :, :] * 0
            rasterdata['fillvalue'] = 1
            array = create_filled_rasterarray(z,
                                              geometry_garden,
                                              tmp_mask_tif,
                                              rasterdata,
                                              print_rast=0)
            m = array[0].astype('b1')

            # Call function to determine the green factor
            result = determine_green_factor(rgbt, m, min_green, max_green)
            greenfactor, greengarden_percentage, greengarden_pixels = result
            greengarden_area = greengarden_pixels * pixelsize**2

            # Create raster to check green factor
            if check_rast == 1:
                rasterdata['fillvalue'] = rasterdata['no_data_value'] = -9999
                polygon_envelope_garden = get_envelope_polygon(
                    envelope_garden_round,
                    rasterdata['sr'],
                )
                outside = polygon_envelope_garden.Difference(geometry_garden)
                create_filled_rasterarray(greenfactor[np.newaxis],
                                          outside,
                                          tmp_green_tif,
                                          rasterdata,
                                          print_rast=1)

            # Create a new feature in shapefile
            attributes_garden = feature_garden.items()
            feature = ogr.Feature(target_layer.GetLayerDefn())
            feature.SetGeometry(geometry_garden)
            for key, value in attributes_garden.items():
                feature[str(key)] = value
            feature[str('green_perc')] = greengarden_percentage
            feature[str('green_area')] = greengarden_area
            target_layer.CreateFeature(feature)

            # remove raster
            os.remove(tmp_aerial_tif)

            # progress bar
            ogr.TermProgress_nocb((count + 1) / total)
    return 0
Exemplo n.º 5
0
import re

import numpy as np

from raster_tools import gdal
from raster_tools import ogr
from raster_tools import osr

from raster_tools import datasets
from raster_tools import datasources

DRIVER_OGR_MEMORY = ogr.GetDriverByName(str('Memory'))
DRIVER_GDAL_MEM = gdal.GetDriverByName(str('mem'))
DRIVER_GDAL_GTIFF = gdal.GetDriverByName(str('gtiff'))

WKT_RD = osr.GetUserInputAsWKT(str('epsg:28992'))
PROJ4_RD = osr.SpatialReference(WKT_RD).ExportToProj4().strip()


def command(gardens_path, aerial_image_path, target_path, min_green, max_green,
            check_rast, part, pk):
    # open input shapefile
    shape_gardens = ogr.Open(gardens_path)
    layer_gardens = shape_gardens[0]

    # check projection of input shapefile
    sr = layer_gardens.GetSpatialRef()
    check_sr = get_projection(sr)
    if check_sr is None:
        print('[!] ERROR : EPSG projection code missing from shape.')
        hint = '[*] INFO  : Use this command: gdalsrsinfo -o wkt EPSG:28992 > '
Exemplo n.º 6
0
def command(index_path, source_path, target_dir, attribute):
    """ Rasterize some postgis tables. """
    # investigate sources
    if source_path.lower().startswith('pg:'):
        source_data_source = PGDataSource(source_path)
    else:
        source_data_source = ogr.Open(source_path)
    if source_data_source.GetDriver().GetName() == 'ESRI Shapefile':
        # seems 1.9.1 does not sort, while 1.9.2 does
        ordered_source_data_source = sorted(source_data_source,
                                            key=lambda l: l.GetName())
    else:
        ordered_source_data_source = source_data_source

    source_field_names = []
    for source_layer in ordered_source_data_source:
        # check attribute for all source layers
        source_field_names.append(
            get_field_name(layer=source_layer, attribute=attribute)
        )
    ogr_type = get_ogr_type(
        data_source=ordered_source_data_source,
        field_names=source_field_names,
    )

    # Create indexes for shapefiles if necessary
    if source_data_source.GetDriver().GetName() == 'ESRI Shapefile':
        for source_layer in ordered_source_data_source:
            source_layer_name = source_layer.GetName()
            if os.path.isfile(source_path):
                source_layer_index_path = source_path[-4:] + '.qix'
            else:
                source_layer_index_path = os.path.join(
                    source_path, source_layer_name + '.qix',
                )
            if os.path.exists(source_layer_index_path):
                    continue
            print('Creating spatial index on {}.'.format(source_layer_name))
            source_data_source.ExecuteSQL(
                str('CREATE SPATIAL INDEX ON {}').format(source_layer_name),
            )

    # rasterize
    index_data_source = ogr.Open(index_path)
    index_layer = index_data_source[0]
    total = index_layer.GetFeatureCount()
    print('Starting rasterize.')
    gdal.TermProgress_nocb(0)
    for count, index_feature in enumerate(index_layer, 1):
        leaf_number = index_feature[str('BLADNR')]
        target_path = os.path.join(
            target_dir, leaf_number[0:3], leaf_number + '.tif',
        )
        if os.path.exists(target_path):
            gdal.TermProgress_nocb(count / total)
            continue

        index_geometry = index_feature.geometry()

        # prepare dataset
        data_type = DATA_TYPE[ogr_type]
        no_data_value = NO_DATA_VALUE[ogr_type]
        dataset = DRIVER_GDAL_MEM.Create('', 2000, 2500, 1, data_type)
        dataset.SetProjection(osr.GetUserInputAsWKT(str('epsg:28992')))
        dataset.SetGeoTransform(get_geotransform(index_geometry))
        band = dataset.GetRasterBand(1)
        band.SetNoDataValue(no_data_value)
        band.Fill(no_data_value)

        burned = False
        for i, source_layer in enumerate(ordered_source_data_source):
            source_field_name = source_field_names[i]
            source_layer.SetSpatialFilter(index_geometry)
            if not source_layer.GetFeatureCount():
                continue

            # create ogr layer if necessary
            if hasattr(source_layer, 'as_ogr_layer'):
                temp_data_source, source_layer = source_layer.as_ogr_layer(
                    name=source_field_name,
                    sr=index_layer.GetSpatialRef(),
                )

            # rasterize
            gdal.RasterizeLayer(
                dataset,
                [1],
                source_layer,
                options=['ATTRIBUTE={}'.format(source_field_name)]
            )
            burned = True

        if burned:
            leaf_number = index_feature[str('BLADNR')]
            array = (dataset.ReadAsArray() == 255).astype('u1')
            if array.any():
                # save no data tif for inspection
                ndv_target_path = os.path.join(target_dir,
                                               'no_data',
                                               leaf_number[0:3],
                                               leaf_number + '.tif')
                try:
                    os.makedirs(os.path.dirname(ndv_target_path))
                except OSError:
                    pass
                array.shape = 1, dataset.RasterYSize, dataset.RasterXSize
                kwargs = {
                    'array': array,
                    'no_data_value': 0,
                    'geo_transform': get_geotransform(index_geometry),
                    'projection': osr.GetUserInputAsWKT(str('epsg:28992')),
                }
                with datasets.Dataset(**kwargs) as ndv_dataset:
                    options = ['compress=deflate', 'tiled=yes']
                    DRIVER_GDAL_GTIFF.CreateCopy(ndv_target_path,
                                                 ndv_dataset,
                                                 options=options)

            # save
            try:
                os.makedirs(os.path.dirname(target_path))
            except OSError:
                pass
            DRIVER_GDAL_GTIFF.CreateCopy(
                target_path, dataset, options=['COMPRESS=DEFLATE'],
            )

        gdal.TermProgress_nocb(count / total)