def _merge_layers(rows,
                  cols,
                  geotransform,
                  spatialreference,
                  array1,
                  array2,
                  min1,
                  max1,
                  min2,
                  max2,
                  nodata1=None,
                  nodata2=None):
    path = create_tmp_filename('', ".tif")

    # find the indexes of the arrays
    index1 = (array1 > min1) & (array1 <= max1) & (array1 != nodata1)
    index2 = (array2 > min2) & (array2 <= max2) & (array2 != nodata2)

    # merge array indexes
    compound_index = index1 & index2
    del index1, index2

    # create a new raster
    output_raster = gdal.GetDriverByName('GTiff').Create(
        path, rows, cols, 1, gdal.GDT_Int16)  # Open the file
    output_raster.SetGeoTransform(geotransform)
    srs = SpatialReference(wkt=spatialreference)
    output_raster.SetProjection(srs.ExportToWkt())
    # create raster from the compound_index of the two rasters
    # TODO: the reshape slows the operation, use matrixes
    output_raster.GetRasterBand(1).WriteArray(
        compound_index.reshape(cols, rows))
    return path
Esempio n. 2
0
def gdal_translate(src_filename,
                   dst_filename,
                   dst_format="GTiff",
                   bands=None,
                   nodata=None,
                   projection=None,
                   options=None):
    """
    Convert a raster image with the specified arguments (as if running from commandline)
    :param argstring: command line arguments as string
    :return: Result of gdal_translate process (success or failure)
    """
    from osgeo import gdal
    from osr import SpatialReference

    if not options:
        options = []

    # Open existing dataset, subsetting bands if necessary
    if bands:
        tmp_file = src_filename + ".sub"
        gdal_band_subset(src_filename, bands, tmp_file)
        src_ds = gdal.Open(tmp_file)
    else:
        src_ds = gdal.Open(src_filename)
    try:
        #Open output format driver, see gdal_translate --formats for list
        driver = gdal.GetDriverByName(dst_format)

        #Output to new format
        dst_ds = driver.CreateCopy(dst_filename, src_ds, 0, options)

        if projection:
            srs = SpatialReference()
            srs.SetWellKnownGeogCS(projection)
            dst_ds.SetProjection(srs.ExportToWkt())

        if nodata is not None:
            band = dst_ds.GetRasterBand(1)
            band.SetNoDataValue(nodata)

    finally:
        #Properly close the datasets to flush to disk
        dst_ds = None
        src_ds = None
        band = None
        if bands and tmp_file:
            os.remove(tmp_file)
Esempio n. 3
0
    def calc_elevation_models(self):
        for item in range(len(self.data_lst)):
            file_name = self.data_lst[item].strip(".xyz")
            self.outfile = os.path.join(self.outfolder, file_name + ".tif")
            x, y, z = np.loadtxt(self.data_lst[item], skiprows=1, unpack=True)
            xmin, xmax, ymin, ymax = [min(x), max(x), min(y), max(y)]
            #size of the grid
            nx = (int(xmax - xmin + 1))
            ny = (int(ymax - ymin + 1))

            # Generate a regular grid to interpolate the data.
            xi = np.linspace(xmin, xmax, nx)
            yi = np.linspace(ymin, ymax, ny)
            xi, yi = np.meshgrid(xi, yi)
            # adding the z - values
            zi = il.griddata((x, y), z, (xi, yi),
                             method='nearest')  # linear, cubic, nearest

            #---------------  Write to GeoTIFF ------------------------
            nrows, ncols = np.shape(zi)
            xres = (xmax - xmin) / float(ncols)
            yres = (ymax - ymin) / float(nrows)
            geotransform = (xmin, xres, 0, ymin, 0, yres)

            self.outDS = gdal.GetDriverByName('GTiff').Create(
                self.outfile, ncols, nrows, 1, gdal.GDT_Float32,
                ['TFW=YES', 'COMPRESS=PACKBITS'])
            self.outDS.SetGeoTransform(geotransform)  # Specify its coordinates
            srs = SpatialReference()  # Establish its coordinate encoding
            srs.ImportFromEPSG(self.epsg)  # WGS 84 / UTM zone 32N
            self.outDS.SetProjection(
                srs.ExportToWkt())  # Exports the coordinate system to the file
            self.outDS.GetRasterBand(1).WriteArray(
                zi)  # Writes my array to the raster
            #self.outDS.FlushCache()
            self.data_out_lst.append(self.outfile)
            self.outDS = None
            zi = None
            x, y, z = None, None, None
            nrows, ncols = None, None
        return [self.message, self.data_out_lst]