Exemple #1
0
def create_geotiff(obj, data, outfile, type, work_dir):
    ''' creates a geo_tiff '''

    originX = float(obj.get_metadata()['X_FIRST'])
    pixelWidth = float(obj.get_metadata()['X_STEP'])
    originY = float(obj.get_metadata()['Y_FIRST'])
    pixelHeight = float(obj.get_metadata()['Y_STEP'])
    gt = [originX, pixelWidth, 0, originY, 0, pixelHeight]
    driver = gdal.GetDriverByName('GTiff')
    ds = driver.Create(
        outfile,
        data.shape[1],
        data.shape[0],
        1,
        gdal.GDT_Float32,
    )

    # this assumes the projection is Geographic lat/lon WGS 84
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    ds.SetProjection(srs.ExportToWkt())
    ds.SetGeoTransform(gt)

    # metadata consistent with Backscatter products:
    xmlfile = glob.glob(os.path.join(work_dir, pathObj.referencedir,
                                     '*.xml'))[0]
    attributes = putils.xmlread(xmlfile)
    Metadata = {
        'SAT':
        attributes['missionname'],
        'Mode':
        attributes['passdirection'],
        'Image_Type':
        'ortho_{}'.format(type),
        'Date':
        obj.get_metadata()['START_DATE'] + '-' + obj.get_metadata()['END_DATE']
    }
    ds.SetMetadata(Metadata)

    outband = ds.GetRasterBand(1)
    outband.WriteArray(data)
    outband.FlushCache()

    ds = None
    return
def main(iargs=None):
    """
    Crops SLC images from Isce merged/SLC directory and creates georectified and orthorectified products.
    """

    inps = cmd_line_parse(iargs, script='export_amplitude_tif')

    secondary_dir = os.path.join(inps.work_dir, pathObj.mergedslcdir)
    pic_dir = os.path.join(inps.work_dir, pathObj.tiffdir)

    if not os.path.exists(pic_dir):
        os.mkdir(pic_dir)

    if not iargs is None:
        input_arguments = iargs
    else:
        input_arguments = sys.argv[1::]

    message_rsmas.log(pic_dir, os.path.basename(__file__) + ' ' + ' '.join(input_arguments))

    os.chdir(secondary_dir)

    try:
        os.system('rm '+ inps.input_file + '/geo*')
    except:
        print('geocoding ...')

    slc = inps.input_file

    if inps.im_type == 'ortho':
        inps.geo_reference_dir = os.path.join(inps.work_dir, pathObj.georeferencedir)
    else:
        inps.geo_reference_dir = os.path.join(inps.work_dir, pathObj.geomlatlondir)

    os.chdir(os.path.join(secondary_dir, inps.input_file))

    geocode_file(inps)

    gfile = 'geo_' + slc + '.slc.ml'
    ds = gdal.Open(gfile + '.vrt', gdal.GA_ReadOnly)
    array = np.abs(ds.GetRasterBand(1).ReadAsArray())
    del ds

    ##
    array = np.where(array > 0, 10.0 * np.log10(pow(array, 2)) - 83.0, array)

    if inps.im_type == 'ortho':
        dst_file = 'orthorectified_' + slc + '_backscatter.tif'
    else:
        dst_file = 'georectified_' + slc + '_backscatter.tif'

    data = gdal.Open(gfile, gdal.GA_ReadOnly)
    transform = data.GetGeoTransform()

    ##
    xmlfile = glob.glob(os.path.join(inps.work_dir, pathObj.referencedir, '*.xml'))[0]
    attributes = xmlread(xmlfile)
    Metadata = {'SAT': attributes['missionname'], 'Mode': attributes['passdirection'],
                'Image_Type': '{}_BackScatter'.format(inps.im_type), 'Date': slc}

    raster2geotiff(dst_file, transform, array, Metadata)

    print('Find the output in {}'.format(pic_dir))

    os.system('mv *.tif {}'.format(pic_dir))
    os.system('rm geo*')

    return