Пример #1
0
 def __init__(me, snwe, hiresonly=False):
     me.geocode_bbox = snwe
     me.insar = INSAR(snwe)
     me.demStitcher = createManager('dem1', 'iscestitcher')
     # True indicates, to only download from high res server.
     # False indicates, download high res dem if available,
     # otherwise download from the low res server.
     me.useHighResolutionDemOnly = hiresonly
Пример #2
0
def download_waterMask(inps):

    sw = createManager('wbd')
    sw.configure()
    inps.waterBodyGeo = sw.defaultName(inps.bbox)
    sw._noFilling = False
    #sw._fillingValue = -1.0
    sw._fillingValue = 0.0
    sw.stitch(inps.bbox[0:2],inps.bbox[2:])

    return inps
Пример #3
0
def download_waterMask(bbox, dem_file, fill_value=-1):
    out_dir = os.getcwd()
    # update out_dir and/or bbox if dem_file is input
    if dem_file:
        out_dir = os.path.dirname(dem_file)
        if not bbox:
            bbox = dem2bbox(dem_file)

    sw = createManager('wbd')
    sw.configure()
    #inps.waterBodyGeo = sw.defaultName(inps.bbox)
    sw.outputFile = os.path.join(out_dir, sw.defaultName(bbox))
    sw._noFilling = False
    sw._fillingValue = fill_value
    sw.stitch(bbox[0:2], bbox[2:])
    return sw.outputFile
Пример #4
0
def download_waterMask(bbox, dem_file, fill_value=-1):
    out_dir = os.getcwd()
    # update out_dir and/or bbox if dem_file is input
    if dem_file:
        out_dir = os.path.dirname(dem_file)
        if not bbox:
            bbox = dem2bbox(dem_file)
    print('bounding box in (S, N, W, E): {}'.format(bbox))

    sw = createManager('wbd')
    sw.configure()
    #inps.waterBodyGeo = sw.defaultName(inps.bbox)
    sw.outputFile = os.path.join(out_dir, sw.defaultName(bbox))
    if os.path.isfile(sw.outputFile):
        print('wbd file already exists at: {}'.format(sw.outputFile))
        print('skip re-downloading and continue')
    else:
        sw._noFilling = False
        sw._fillingValue = fill_value
        sw.stitch(bbox[0:2], bbox[2:])
    return sw.outputFile
Пример #5
0
def download_wbd(s, n, w, e):
    '''
    download water body
    water body. (0) --- land; (-1) --- water; (-2) --- no data.

    set no-value pixel inside of latitude [-56, 60] to -1
    set no-value pixel outside of latitidue [-56, 60] to -2

    look at this figure for SRTM coverage:
    https://www2.jpl.nasa.gov/srtm/images/SRTM_2-24-2016.gif
    '''
    import os
    import numpy as np
    import isceobj
    from iscesys.DataManager import createManager

    latMin = np.floor(s)
    latMax = np.ceil(n)
    lonMin = np.floor(w)
    lonMax = np.ceil(e)

    ############################################################
    #1. download and stitch wbd
    ############################################################
    sw = createManager('wbd')
    sw.configure()

    outputFile = sw.defaultName([latMin, latMax, lonMin, lonMax])
    if os.path.exists(outputFile) and os.path.exists(outputFile + '.xml'):
        print('water body file: {}'.format(outputFile))
        print('exists, do not download and correct')
        return outputFile

    #download and stitch the SWBD tiles
    sw.noFilling = False
    sw._fillingValue = -1
    sw.stitch([latMin, latMax], [lonMin, lonMax])

    ############################################################
    #2. replace 'areas with SRTM but no SWBD' with zeros (land)
    ############################################################
    print('post-process water body file')

    print('get SRTM tiles')
    srtmListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'srtm_tiles.txt')
    with open(srtmListFile) as f:
        srtmList = f.readlines()
    srtmList = [x[0:7] for x in srtmList]

    #get tiles that have SRTM DEM, but no SWBD, these are mostly tiles that do not have water body
    print('get tiles with SRTM and without SWBD')
    noSwbdListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                  'srtm_no_swbd_tiles.txt')
    with open(noSwbdListFile) as f:
        noSwbdList = f.readlines()
    noSwbdList = [x[0:7] for x in noSwbdList]

    print('get SWBD tiles')
    swbdListFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                'swbd_tiles.txt')
    with open(swbdListFile) as f:
        swbdList = f.readlines()
    swbdList = [x[0:7] for x in swbdList]

    #read resulting mosaicked water body
    wbdImage = isceobj.createDemImage()
    wbdImage.load(outputFile + '.xml')
    #using memmap instead, which should be faster, since we only have a few pixels to change
    wbd = np.memmap(outputFile,
                    dtype=np.int8,
                    mode='r+',
                    shape=(wbdImage.length, wbdImage.width))

    #replace 'areas with SRTM but no SWBD' with zeros (land)
    names, nlats, nlons = sw.createNameListFromBounds([latMin, latMax],
                                                      [lonMin, lonMax])
    sign = {'S': -1, 'N': 1, 'W': -1, 'E': 1}
    for tile in names:
        print('checking tile: {}'.format(tile))
        firstLatitude = sign[tile[0].upper()] * int(tile[1:3]) + 1
        firstLongitude = sign[tile[3].upper()] * int(tile[4:7])
        lineOffset = np.int32((firstLatitude - wbdImage.firstLatitude) /
                              wbdImage.deltaLatitude + 0.5)
        sampleOffset = np.int32((firstLongitude - wbdImage.firstLongitude) /
                                wbdImage.deltaLongitude + 0.5)

        #first line/sample of mosaicked SWBD is integer lat/lon, but it does not include last integer lat/lon line/sample
        #so here the size is 3600*3600 instead of 3601*3601

        #assuming areas without swbd are water
        if tile[0:7] not in swbdList:
            wbd[0 + lineOffset:3600 + lineOffset,
                0 + sampleOffset:3600 + sampleOffset] = -1
        #assuming areas with srtm and without swbd are land
        if tile[0:7] in noSwbdList:
            wbd[0 + lineOffset:3600 + lineOffset,
                0 + sampleOffset:3600 + sampleOffset] = 0

    ############################################################
    #3. set values outside of lat[-56, 60] to -2 (no data)
    ############################################################
    print('check water body file')
    print('set areas outside of lat[-56, 60] to -2 (no data)')
    for i in range(wbdImage.length):
        lat = wbdImage.firstLatitude + wbdImage.deltaLatitude * i
        if lat > 60.0 or lat < -56.0:
            wbd[i, :] = -2
    del wbd, wbdImage

    return outputFile
Пример #6
0
def runCreateWbdMask(self, info):
    if self.insar.applyWaterMask:
        sw = createManager('wbd')
        sw.configure()
        ####If the user has requested a bounding box
        if self.geocode_bbox:
            latMax = math.ceil(self.geocode_bbox[1])
            latMin = math.floor(self.geocode_bbox[0])
            lonMin = math.floor(self.geocode_bbox[2])
            lonMax = math.ceil(self.geocode_bbox[3])
        else:
            extremes = info.getExtremes(.2)
            latMax = extremes[1]
            latMin = extremes[0]
            lonMax = extremes[3]
            lonMin = extremes[2]

        #get the name of the swbd image
        name = sw.defaultName([latMin, latMax, lonMin, lonMax])
        #form the name of the corresponding xml file
        nameXml = name + '.xml'

        #Check if the swbd file exists on disk to load from
        #either in the local directory
        if os.path.exists(nameXml) and os.path.exists(name):
            from isceobj import createImage
            image = createImage()
            image.load(nameXml)
            image.metadatalocation = nameXml

        #or in the DEMDB directory
        elif ("DEMDB" in os.environ
              and os.path.isfile(os.path.join(os.environ["DEMDB"], nameXml))):
            from isceobj import createImage
            image = createImage()
            image.load(os.path.join(os.environ["DEMDB"], nameXml))
            image.metadatalocation = os.path.join(os.environ["DEMDB"], nameXml)

        #or finally, have the stitcher download and stitch a new one.
        else:
            sw.noFilling = False
            sw.stitch([latMin, latMax], [lonMin, lonMax])
            image = sw.image

            #if there is a global store, move the swbd files to it
            if "DEMDB" in os.environ and os.path.exists(os.environ["DEMDB"]):
                #modify the filename in the meta data to include
                #path to the global store
                from isceobj import createImage
                image = createImage()
                image.load(nameXml)
                image.filename = os.path.join(os.environ["DEMDB"],
                                              image.filename)
                image._extraFilename = os.path.join(os.environ["DEMDB"],
                                                    image._extraFilename)
                image.metadatalocation = os.path.join(os.environ["DEMDB"],
                                                      nameXml)
                image.dump(nameXml)

                #remove the swbdLat*.vrt file from the local directory because
                #a side effect of the demImage.dump() above was to create the
                #vrt in the location indicated by the path in the xml file.
                os.remove(nameXml.replace('.xml', '.vrt'))

                #make list of swbdLat file names to be moved to the global store
                import glob
                dwlist = glob.glob(name + "*")
                import shutil
                #move the dem files to the global store
                for dwfile in dwlist:
                    shutil.move(dwfile, os.environ["DEMDB"])

        #put the wbdImage in the InsarProc object
        self.insar.wbdImage = image
Пример #7
0
def createDem(usnwe, info, insar, demStitcher, useHighResOnly=False, useZeroTiles=False):
    """
    Create a dem object given a user specified snwe lat, lon bounding box (usnwe),
    a frame information object (info),
    an insar container object (insar),
    a configured demStitcher object,
    an option useHighResOnly (default False) to accept only high resolution dem with zero fill
    and option useZeroTiles (default False) to proceed with zero filled dem tiles if unavailable
    The insar object contains a configured demStitcher,
    """

    #get the south, north latitude and west, east longitude extremes (snwe) from the frame
    #information with additional padding of 0.2 degrees in each direction
    snwe = info.getExtremes(0.2)
    #take the larger bounding region from these frame snwe values and the user's specified usnwe,
    #if given
    if usnwe:
        op1 = (min, max)
        snwe = [op1[i%2](usnwe[i], snwe[i]) for i in range(4)]
    #round outwards (relative to bounding box) to nearest integer latitude, longitudes
    import math
    op2 = (math.floor, math.ceil)
    snwe = [op2[i%2](snwe[i]) for i in range(4)]

    #Record the user's (or default) preference for using zeroed out tiles when the DEM is not
    #available (should really be done before coming here)
    demStitcher.proceedIfZeroDem = useZeroTiles

    #get the name of the wgs84 dem and its metadata file
    demName = demStitcher.defaultName(snwe)
    demNameXml = demName + '.xml'
    wgs84demName = demName + '.wgs84'
    wgs84demNameXml = wgs84demName + '.xml'

    #save the name just in case
    insar.demInitFile = wgs84demNameXml

    #check to see if the demStitcher has a valid DEM image instance we can use
    demImage = demStitcher.image
    if demImage:
        #get the wgs84 version
        wgs84dem = get_wgs84dem(demStitcher, demImage)
        insar.demImage = wgs84dem
        return

    #If not, check if there is already one in the local directory to load from
    #wgs84 version?
    if os.path.isfile(wgs84demNameXml):
        wgs84demImage  = createDemImage()
        wgs84demImage.load(wgs84demNameXml)
        insar.demImage = wgs84demImage
        return

    #or create one from the non-wgs84 version
    if os.path.isfile(demNameXml):
        demImage  = createDemImage()
        demImage.load(demNameXml)
        wgs84demImage = get_wgs84dem(demStitcher, demImage)
        insar.demImage = wgs84demImage
        return

    #or in the DEMDB directory
    #the wgs84dem
    if "DEMDB" in os.environ and os.path.isfile(os.path.join(os.environ["DEMDB"], wgs84demNameXml)):
        dbwgs84dem = os.path.join(os.environ["DEMDB"], wgs84demNameXml)
        wgs84demImage  = createDemImage()
        wgs84demImage.load(dbwgs84dem)
        insar.demImage = wgs84demImage
        return

    #or from the non-wgs84 version
    if "DEMDB" in os.environ and os.path.isfile(os.path.join(os.environ["DEMDB"], demNameXml)):
        dbdem = os.path.join(os.environ["DEMDB"], demNameXml)
        demImage  = createDemImage()
        demImage.load(dbdem)
        wgs84demImage = get_wgs84dem(demStitcher, demImage)
        insar.demImage = wgs84demImage
        return

    #or finally, have the demStitcher download and stitch a new one.
    #stitch
    if useHighResOnly:
        #use the high res DEM. Fill the missing tiles
        demStitcher.noFilling = False
        stitchOk = demStitcher.stitch(snwe[0:2], snwe[2:4])
    else:
        #try to use the demStitcher (high resolution DEM by default)
        #and do not allow missing tiles
        demStitcher.noFilling = True
        stitchOk = demStitcher.stitch(snwe[0:2], snwe[2:4])
        #check if high resolution stitching was not OK
        if not stitchOk:
            #then maybe try the lower resolution DEM
            newDemStitcher = createManager('dem3')
            #and do not allow missing tiles
            newDemStitcher.noFilling = True
            #set the preference for proceeding if the server does not return a tile
            newDemStitcher.proceedIfNoServer = useZeroTiles
            #try again only if it's not already a low res instance
            if type(newDemStitcher) != type(demStitcher):
                stitchOk = newDemStitcher.stitch(snwe[0:2], snwe[2:4])
                if stitchOk:
                    #if low res was ok change the stitcher to dem3
                    demStitcher = newDemStitcher

            #if cannot form a full dem with either high and low res
            #then use what ever with have with high res
            if not stitchOk:
                 demStitcher.noFilling = False
                 stitchOk = demStitcher.stitch(snwe[0:2], snwe[2:4])

    #check if stitching worked
    if stitchOk:
        #get the image
        demImage = demStitcher.image
        #set the metadatalocation and _extraFilename attributes
        demImage.metadatalocation = demImage.filename + ".xml"
        demImage._extraFilename = demImage.metadatalocation.replace(".xml", ".vrt")

        #get the wgs84 dem
        wgs84demImage = get_wgs84dem(demStitcher, demImage)

        #if there is a global store, move the dem files to it
        if "DEMDB" in os.environ and os.path.exists(os.environ["DEMDB"]):
            #modify filename in the meta data to include
            #path to the global store

            #the demImage
            demImage.filename = os.path.join(os.environ["DEMDB"],
                demImage.filename)
            demImage.metadatalocation = os.path.join(os.environ["DEMDB"],
                demImage.metadatalocation)
            demImage._extraFilename = os.path.join(os.environ["DEMDB"],
                demImage._extraFilename)
            demImage.dump(demNameXml)

            #the wgs84demImage
            wgs84demImage.load(wgs84demNameXml)
            wgs84demImage.filename = os.path.join(os.environ["DEMDB"],
                wgs84demImage.filename)
            wgs84demImage.metadatalocation = os.path.join(os.environ["DEMDB"],
                wgs84demImage.metadatalocation)
            wgs84demImage._extraFilename = os.path.join(os.environ["DEMDB"],
                wgs84demImage._extraFilename)
            wgs84demImage.dump(wgs84demNameXml)

            #remove the demLat*.vrt file from the local directory because
            #a side effect of the demImage.dump() above was to create the
            #vrt in the location indicated by the path in the xml file.
            os.remove(demNameXml.replace('.xml','.vrt'))
            os.remove(wgs84demNameXml.replace('.xml','.vrt'))

            #move the demfiles to the global store
            #make list of dem file names to be moved to the global store
            import glob
            dwlist = glob.glob(demName+"*")
            import shutil
            #move the dem files to the global store
            for dwfile in dwlist:
                shutil.move(dwfile,os.environ["DEMDB"])

        #put the wgs84demImage in the InsarProc object
        insar.demImage = wgs84demImage
        #that's all
        return

    #exhausted all options; ask the user for help
    else:
        logger.error(
            "Cannot form the DEM for the region of interest. "+
            "If you have one, set the appropriate DEM "+
            "component in the input file.")
        raise Exception

    return