Example #1
0
 def __buildvrt(archives, vrtfile, pattern, vsi, extent, nodata=None, srs=None):
     locals = [vsi + x for x in dissolve([finder(x, [pattern]) for x in archives])]
     if nodata is None:
         with Raster(locals[0]) as ras:
             nodata = ras.nodata
     opts = {'outputBounds': (extent['xmin'], extent['ymin'],
                              extent['xmax'], extent['ymax']),
             'srcNodata': nodata}
     if srs is not None:
         opts['outputSRS'] = crsConvert(srs, 'wkt')
     gdalbuildvrt(src=locals, dst=vrtfile,
                  options=opts)
Example #2
0
def dem_create(src,
               dst,
               t_srs=None,
               tr=None,
               resampling_method='bilinear',
               geoid_convert=False,
               geoid='EGM96'):
    """
    create a new DEM GeoTiff file and optionally convert heights from geoid to ellipsoid
    
    Parameters
    ----------
    src: str
        the input dataset, e.g. a VRT from function :func:`dem_autoload`
    dst: str
        the output dataset
    t_srs: None, int, str or osr.SpatialReference
        A target geographic reference system in WKT, EPSG, PROJ4 or OPENGIS format.
        See function :func:`spatialist.auxil.crsConvert()` for details.
        Default (None): use the crs of ``src``.
    tr: None or tuple
        the target resolution as (xres, yres)
    resampling_method: str
        the gdalwarp resampling method; See `here <https://gdal.org/programs/gdalwarp.html#cmdoption-gdalwarp-r>`_
        for options.
    geoid_convert: bool
        convert geoid heights?
    geoid: str
        the geoid model to be corrected, only used if ``geoid_convert == True``; current options:
         * 'EGM96'

    Returns
    -------

    """

    with Raster(src) as ras:
        nodata = ras.nodata
        epsg_in = ras.epsg

    if t_srs is None:
        epsg_out = epsg_in
    else:
        epsg_out = crsConvert(t_srs, 'epsg')

    gdalwarp_args = {
        'format': 'GTiff',
        'multithread': True,
        'srcNodata': nodata,
        'dstNodata': nodata,
        'srcSRS': 'EPSG:{}'.format(epsg_in),
        'dstSRS': 'EPSG:{}'.format(epsg_out),
        'resampleAlg': resampling_method
    }

    if tr is not None:
        gdalwarp_args.update({'xRes': tr[0], 'yRes': tr[1]})

    if geoid_convert:
        if gdal.__version__ < '2.2':
            raise RuntimeError('geoid conversion requires GDAL >= 2.2;'
                               'see documentation of gdalwarp')
        if geoid == 'EGM96':
            gdalwarp_args['srcSRS'] += '+5773'
        else:
            raise RuntimeError('geoid model not yet supported')

    try:
        message = 'creating mosaic'
        crs = gdalwarp_args['dstSRS']
        if crs != 'EPSG:4326':
            message += ' and reprojecting to {}'.format(crs)
        print(message)
        gdalwarp(src, dst, gdalwarp_args)
    except RuntimeError as e:
        if os.path.isfile(dst):
            os.remove(dst)
        errstr = str(e)
        if 'Cannot open egm96_15.gtx' in errstr:
            addition = '\nplease refer to the following site for instructions ' \
                       'on how to use the file egm96_15.gtx (requires proj.4 >= 5.0.0):\n' \
                       'https://gis.stackexchange.com/questions/258532/' \
                       'noaa-vdatum-gdal-variable-paths-for-linux-ubuntu'
            raise RuntimeError(errstr + addition)
        else:
            raise e