示例#1
0
def LinkSentineModisTiles():
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)
    #Open the db session for MODIS
    sessionModis = ManageMODIS()
    #Open the session for sentinel

    recs = sessionModis._SelectModisTileCoords({})
    sessionModis._Close()
    sessionSentinel = ManageSentinel()
    sentileD = GetSentinelTilesDict(sessionSentinel)
    for rec in recs:
        print('rec', rec)
        hvtile, h, v, minxsin, minysin, maxxsin, maxysin, ullat, ullon, lrlat, lrlon, urlat, urlon, lllat, lllon = rec
        llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                  lllat))
        modtilegeom = mj_gis.Geometry()
        modtilegeom.PointsToPolygonGeom(llptL)
        west, south, east, north = modtilegeom.shapelyGeom.bounds
        #Get all sentinel tiles falling inside the bounding box (nothing is omitted?)
        sentiles = sessionSentinel._SearchTilesFromWSEN(
            west, south, east, north)
        for sentile in sentiles:
            mgrs, tilewest, tilesouth, tileeast, tilenorth, ullon, ullat, urlon, urlat, lrlon, lrlat, lllon, lllat, minx, miny, maxx, maxy = sentile
            llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                      lllat))
            sentilegeom = mj_gis.Geometry()
            sentilegeom.PointsToPolygonGeom(llptL)

            if modtilegeom.shapelyGeom.intersects(sentilegeom.shapelyGeom):
                #print ('    overlap',productoverlap)
                #query = {'system':'system', 'regionid':regionid,'regiontype':'default', 'overwrite':False, 'delete':False, 'mgrs':mgrs,'utmzone':self.senTileD[mgrs]['utmzone'], 'mgrsid':self.senTileD[mgrs]['mgrsid']}
                #self.session._InsertSentinelRegionTile(query)
                print()
                queryD = {
                    'system': 'system',
                    'regionid': hvtile,
                    'regiontype': 'sintile',
                    'mgrs': mgrs,
                    'utmzone': sentileD[mgrs]['utmzone'],
                    'mgrsid': sentileD[mgrs]['mgrsid'],
                    'overwrite': False,
                    'delete': False
                }
                print('in')
                #query = {'system':'system', 'regionid':regionid,'regiontype':'default', 'overwrite':False, 'delete':False, 'mgrs':mgrs,'utmzone':self.senTileD[mgrs]['utmzone'], 'mgrsid':self.senTileD[mgrs]['mgrsid']}
                sessionSentinel._InsertSentinelMODISTile(queryD)
                #sessionSentinel._InsertSingleSentinelRegion(queryD)
                print('out')
    sessionSentinel._Close()
示例#2
0
def LinkSentinelLandsatTiles():
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)
    #Open the db session for MODIS
    sessionLandsat = ManageLandsat()
    #Open the session for sentinel
    sessionSentinel = ManageSentinel()
    #Select all landsat tiles
    recs = sessionLandsat._SelectLandsatTileCoords({})
    doneL = sessionSentinel._SelectSentineRegions({'regiontype': 'lsattile'})

    for rec in recs:
        wrs, dir, wrspath, wrsrow, minx, miny, maxx, maxy, ullat, ullon, lrlat, lrlon, urlat, urlon, lllat, lllon = rec
        prD = ConvertLandsatTilesToStr(wrspath, wrsrow)
        if prD['prstr'] in doneL:
            continue
        print('adding sentinel region:', prD['prstr'])
        llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                  lllat))
        landsattilegeom = mj_gis.Geometry()
        landsattilegeom.PointsToPolygonGeom(llptL)
        west, south, east, north = landsattilegeom.shapelyGeom.bounds
        #Get all sentinel tiles falling inside the bounding box (nothing is omitted?)
        sentiles = sessionSentinel._SearchTilesFromWSEN(
            west, south, east, north)
        print('adding sentinel region:', prD['prstr'], len(sentiles))
        for sentile in sentiles:
            mgrs, tilewest, tilesouth, tileeast, tilenorth, ullon, ullat, urlon, urlat, lrlon, lrlat, lllon, lllat, minx, miny, maxx, maxy = sentile
            llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                      lllat))
            tilegeom = mj_gis.Geometry()
            tilegeom.PointsToPolygonGeom(llptL)
            #Get the overlap
            overlapGeom = tilegeom.ShapelyIntersection(landsattilegeom)
            productoverlap = overlapGeom.area / tilegeom.shapelyGeom.area
            if productoverlap > 0:
                #Replace this with a COPY command, faster and assures that a tile is always complete
                queryD = {
                    'regionid': prD['prstr'],
                    'regiontype': 'lsattile',
                    'mgrs': mgrs,
                    'utm': int(mgrs[0:2]),
                    'mgrsid': mgrs[2:5]
                }
                sessionSentinel._InsertSingleSentinelRegion(queryD)
示例#3
0
def LinkLandsatModisTiles():
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)
    #Open the db session for MODIS
    sessionModis = ManageMODIS()
    #Open the session for sentinel
    sessionLandsat = ManageLandsat()
    recs = sessionModis._SelectModisTileCoords({})
    for rec in recs:
        hvtile, h, v, minxsin, minysin, maxxsin, maxysin, ullat, ullon, lrlat, lrlon, urlat, urlon, lllat, lllon = rec
        llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                  lllat))
        modtilegeom = mj_gis.Geometry()
        modtilegeom.PointsToPolygonGeom(llptL)
        west, south, east, north = modtilegeom.shapelyGeom.bounds
        #Get all sentinel tiles falling inside the bounding box (nothing is omitted?)
        landsattiles = sessionLandsat._SearchTilesFromWSEN(
            west, south, east, north)
        #loop
        for landsattile in landsattiles:
            wrs, ldir, p, r, tilewest, tilesouth, tileeast, tilenorth, ullon, ullat, urlon, urlat, lrlon, lrlat, lllon, lllat, minx, miny, maxx, maxy = landsattile
            llptL = ((ullon, ullat), (urlon, urlat), (lrlon, lrlat), (lllon,
                                                                      lllat))
            tilegeom = mj_gis.Geometry()
            tilegeom.PointsToPolygonGeom(llptL)
            prD = ConvertLandsatTilesToStr(p, r)
            overlapGeom = tilegeom.ShapelyIntersection(modtilegeom)
            productoverlap = overlapGeom.area / tilegeom.shapelyGeom.area
            if productoverlap > 0:
                queryD = {
                    'regionid': hvtile,
                    'regiontype': 'sintile',
                    'prstr': prD['prstr'],
                    'dir': ldir,
                    'wrs': wrs,
                    'path': p,
                    'row': r
                }
                sessionLandsat._InsertSingleLandsatRegion(queryD)
示例#4
0
def ReadXMLProcesses(projFPN,verbose):
    '''
    Run processes
    '''
    srcFP = path.join(path.split(projFPN)[0],'xml')
    #Read the textfile with links to the xml files defining schemas and tables
    print ('reading',projFPN)
    with open(projFPN) as f:
        xmlL = f.readlines()
    # Remove whitespace characters like `\n` at the end of each line
    xmlL = [path.join(srcFP, x.strip())  for x in xmlL if len(x) > 10 and x[0] != '#'] 

    #Get the fileformats required for all processing
    '''
    session = SelectFileFormats()
    gdalofD = session._SelectGDALof()
    session._Close()
    '''
    #Loop over all xml files and setup the processes
    procLL = []
    for xml in xmlL:
        procL = []
        if verbose:
            print ('    Reading xml',xml)
        with open(xml) as fd:
            doc = xmltodict.parse(fd.read())
        #Get the root and the content
        rootid = next(iter(doc.items()))[0]
        content = next(iter(doc.items()))[1]      
        #Get the userProj(ordered dictionary)
        userProjOD = content['userproj']

        userid = userProjOD['@userid']
        if '@userpswd' in userProjOD:
            userpswd = userProjOD['@userpswd']
        else:
            userpswd = ''

        session = SelectUser()
        userData = session._SelectUserCreds(userid,userpswd)
        session._Close()
        if userData == None:
            exit('No user found with given password')

        if userData:
            userId, userCat, userStratum, userProjs, userTracts, userSites = userData
            #userProj._SetCredentials(userCat, userStratum, userProjs=userProjs, userTracts=userTracts, userSites=userSites)
        else:
            exit('no user found')
        #print ('You are entering as user:'******'userproj', 'root', 'userproj')  

        userProj = UserProj(userProjOD,tagAttrL,xml)
        # set user credentials
        userProj._SetCredentials(userCat, userStratum, userProjs=userProjs, userTracts=userTracts, userSites=userSites)

        #Check that the user owns the project and the plot/site/tract requested
        projSetting = userProj._CheckUserProj()

        if not projSetting:
            warnstr = 'User does not own the project'
            print (warnstr)
            session._Close()
            continue
        #Get the default region for the tract/site
        projSetting = userProj._GetDefRegion(session)

        if not 'process' in content:
            exit('no process defined')
        processesDL = content['process']

        #Check if this is a single process, if so create a list with the single process
        if not type(processesDL) is list:
            processesDL = [processesDL]
    
        for processD in processesDL:
            processid = processD['@processid']
            #Instantiate Process
            proc = SetXMLProcess(userProj, processid, content, session, xml, verbose)
            #check that user has the right to this process
            permit = proc._CheckPermission(session)
            if not permit:
                NoPermitExit
                continue
            #Check the input parameters
            errorD = proc._CheckParams(processD, session)
            procL.append(proc)
        #all the data is read, process the data
        procLL.append(procL)
        session._Close()
    return procLL
示例#5
0
def ModisTileCoords():
    '''
    '''
    #Check  the karttur password (local .netrc file
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)

    #Open the db session for MODIS
    session = ManageRegion()
    SINproj = mj_gis.MjProj()
    SINproj.SetFromProj4(
        '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs'
    )
    LatLonproj = mj_gis.MjProj()
    LatLonproj.SetFromProj4(
        '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0')
    ptL = []
    for lon in range(360):
        ptL.append((lon - 180, 90))
    for lat in range(180):
        ptL.append((180, -1 * (lat - 90)))
    for lon in range(360):
        ptL.append((-1 * (lon - 180), -90))
    for lat in range(180):
        ptL.append((-180, lat - 90))
    #worldgeom = mj_gis.ShapelyPoygon('polygon',ptL)
    #print ('ptL',ptL)
    #worldgeom = mj_gis.Geometry()
    #worldgeom.PointsToPolygonGeom(ptL)
    #print (worldgeom.shapelyGeom)
    #SNULLEKOLLA
    worldgeom = mj_gis.ShapelyPolyGeom(ptL)

    worldgeom.ShapelyToOgrGeom()
    worldgeom.GeoTransform(LatLonproj, SINproj)
    worldgeom.OgrGeomToShapely()

    home = path.expanduser("~")
    tarShpFP = path.join(path.dirname(__file__), 'data')

    if not path.exists(tarShpFP):
        makedirs(tarShpFP)
    FN = 'modtiles-multi_karttur_global_epsg6842.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    fieldDefD = {
        'type': 'string',
        'transfer': 'constant',
        'source': 'globe',
        'width': 8
    }
    fieldDefL = [mj_gis.FieldDef('name', fieldDefD)]
    mj_gis.CreateESRIPolygonGeom(tarShpFPN, fieldDefL, worldgeom,
                                 SINproj.proj_cs, 'globe')
    # Create a shape file for all individual tiles in SIN proj
    FN = 'modtiles-single_karttur_global_epsg6842.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    tarDS, tarLayer = mj_gis.ESRICreateDSLayer(tarShpFPN, SINproj.proj_cs,
                                               'polygon', 'tiles', fieldDefL)
    # Create a shape file for all individual tiles in Geographic coordinates
    FN = 'modtiles_karttur_global_0.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    tarDSLonLat, tarLayerLonLat = mj_gis.ESRICreateDSLayer(
        tarShpFPN, LatLonproj.proj_cs, 'polygon', 'tiles', fieldDefL)

    #create a region with all tiles
    tlen = 20015109.3539999984204769
    tlen /= 18
    #regioncat = 'globe'
    #regionid = 'globe'
    for h in range(36):
        minx = tlen * (18 - 36) + h * tlen
        maxx = minx + tlen
        for v in range(18):
            maxy = tlen * (9 - 18) + (18 - v) * tlen
            miny = maxy - tlen
            ptL = [(minx, maxy), (maxx, maxy), (maxx, miny), (minx, miny)]
            #TGTODO MOVE TO mj_gis
            tilegeom = mj_gis.ShapelyMultiPointGeom(ptL)
            #convert to ogr
            tilegeom.ShapelyToOgrGeom()

            #write target feature

            tilegeom.GeoTransform(SINproj, LatLonproj)
            tilegeom.OgrGeomToShapely()
            coordL = []
            for point in [ptgeom for ptgeom in tilegeom.shapelyGeom]:
                coordL.extend(
                    [list(point.coords)[0][0],
                     list(point.coords)[0][1]])
            ullon, ullat, urlon, urlat, lrlon, lrlat, lllon, lllat = coordL
            tilepoly = mj_gis.ShapelyPolyGeom([(minx, maxy), (maxx, maxy),
                                               (maxx, miny), (minx, miny)])
            #Test if this tile is inside the globe

            if tilepoly.shapelyGeom.intersects(worldgeom.shapelyGeom):
                if h < 10:
                    htile = 'h0%s' % (h)
                else:
                    htile = 'h%s' % (h)
                if v < 10:
                    vtile = 'v0%s' % (v)
                else:
                    vtile = 'v%s' % (v)
                hvtile = '%s%s' % (htile, vtile)
                polytilegeom = mj_gis.ShapelyPolyGeom(ptL)
                polytilegeom.ShapelyToOgrGeom()
                fieldDefD = {
                    'type': 'string',
                    'transfer': 'constant',
                    'source': hvtile,
                    'width': 8
                }
                fieldDefL = [mj_gis.FieldDef('name', fieldDefD)]
                #create target feature
                tarFeat = mj_gis.ogrFeature(tarLayer)
                print(polytilegeom.shapelyGeom)
                tarFeat.CreateOgrFeature(polytilegeom.ogrGeom, fieldDefL)
                if h == 17:
                    pass
                else:
                    #to be correct 5 points are needed and also the lat must be fitted
                    if h < 18 and ullon > 0:
                        ullon = -180
                        #continue
                    if h < 18 and lllon > 0:
                        lllon = -180
                        #continue
                    if h < 18 and urlon > 0:
                        urlon = -180
                        #continue
                    if h < 18 and lrlon > 0:
                        lrlon = -180
                        #continue
                    if h > 18 and urlon < 0:
                        urlon = 180
                        #continue
                    if h > 18 and lrlon < 0:
                        lrlon = 180
                        #continue
                    if h > 18 and ullon < 0:
                        ullon = 180
                        #continue
                    if h > 18 and lllon < 0:
                        lllon = 180
                    if hvtile == 'h24v01':
                        urlon = 180
                    if hvtile == 'h24v16':
                        lrlon = 180
                    if hvtile == 'h11v01':
                        ullon = -180
                    if hvtile == 'h11v16':
                        lllon = -180

                if ullon > urlon:
                    print('ERROR', 'ullon > urlon', hvtile, ullon, urlon)

                if lllon > lrlon:
                    print('ERROR', 'lllon > lrlon', hvtile, lllon, lrlon)

                #
                polytilegeom = mj_gis.ShapelyPolyGeom([(ullon, ullat),
                                                       (urlon, urlat),
                                                       (lrlon, lrlat),
                                                       (lllon, lllat)])
                polytilegeom.ShapelyToOgrGeom()
                #polytilegeom.GeoTransform(SINproj,LatLonproj)
                #create target feature
                tarLonLatFeat = mj_gis.ogrFeature(tarLayerLonLat)
                tarLonLatFeat.CreateOgrFeature(polytilegeom.ogrGeom, fieldDefL)
                west, south, east, north = polytilegeom.shapelyGeom.bounds

                session._InsertModisTileCoord(hvtile, h, v, minx, maxy, maxx,
                                              miny, west, south, east, north,
                                              ullat, ullon, lrlon, lrlat,
                                              urlon, urlat, lllon, lllat)
                #print ('session.name',session.name)
                query = {
                    'system': 'system',
                    'table': 'regions',
                    'h': h,
                    'v': v,
                    'hvtile': hvtile,
                    'regionid': 'global',
                    'regioncat': 'global',
                    'regiontype': 'default',
                    'delete': False
                }
                session._InsertModisRegionTile(query)
    tarDS.CloseDS()
    tarDSLonLat.CloseDS()
    session._Close()
    print('Check the shaoe file', tarShpFPN)
    return (tarShpFPN)
示例#6
0
def DegreeTiles():
    '''Setup default region tiles covering 1deg by 1deg, this is like tile system for any data
    '''
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    session._Close()
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)

    headL = [
        'regioncat', 'regionid', 'regionname', 'parentid', 'title', 'label'
    ]
    headL = [
        'regioncat', 'regionid', 'regiontype', 'epsg', 'ullat', 'ullon',
        'urlat', 'urlon', 'lrlat', 'lrlon', 'lllat', 'lllon', 'monx', 'miny',
        'maxx', 'maxy'
    ]
    csvL = []
    regionL = []
    #the tile "region" naming is based on the lower left corner
    for lat in range(-90, 90):
        for lon in range(-180, 180):
            if lat < 0:
                latnr = abs(lat)
                latsign = 'S'
                if lon < 0:
                    lonnr = abs(lon)
                    lonsign = 'W'
                    #tileid = 'S%(lat)dW%(lon)d' %{'lat':abs(lat),'lon':abs(lon)}
                else:
                    lonnr = lon
                    lonsign = 'E'
                    #tileid = 'S%(lat)dE%(lon)d' %{'lat':lat,'lon':abs(lon)}
            else:
                latnr = lat
                latsign = 'N'
                if lon < 0:
                    lonnr = abs(lon)
                    lonsign = 'W'
                    #tileid = 'S%(lat)dW%(lon)d' %{'lat':abs(lat),'lon':abs(lon)}
                else:
                    lonnr = lon
                    lonsign = 'E'
                    #tileid = 'S%(lat)dE%(lon)d' %{'lat':lat,'lon':abs(lon)}
                '''
                if lon < 0:
                    tileid = 'N%(lat)dW%(lon)d' %{'lat':lat,'lon':abs(lon)}
                else:
                    tileid = 'N%(lat)dE%(lon)d' %{'lat':lat,'lon':lon}
                '''
            if latnr < 10:
                latstr = '0%(l)d' % {'l': latnr}
            else:
                latstr = '%(l)d' % {'l': latnr}
            if lonnr < 10:
                lonstr = '00%(l)d' % {'l': lonnr}
            elif lonnr < 100:
                lonstr = '0%(l)d' % {'l': lonnr}
            else:
                lonstr = '%(l)d' % {'l': lonnr}
            tileid = '%(latsign)s%(latstr)s%(lonsign)s%(lonstr)s' % {
                'latsign': latsign,
                'latstr': latstr,
                'lonsign': lonsign,
                'lonstr': lonstr
            }
            #if lonnr ==  180:
            #    print (lat,lon,tileid)
            #    queryD = {'regioncat':'global', 'regionid':tileid,'regionname':tileid,'parentid':'global',
            #              'title':'1 deg square global','label': '1 deg square global'}
            csvL.append([
                'global', tileid, tileid, 'global', '1deqsquare',
                '1 deg square global'
            ])
            regionL.append([
                'global', tileid, 'D', 4326, lat + 1.0, lon, lat + 1.0,
                lon + 1.0, lat, lon + 1.0, lat, lon, lon, lat, lon + 1.0,
                lat + 1.0
            ])
            [
                'regioncat', 'regionid', 'regiontype', 'epsg', 'ullat',
                'ullon', 'urlat', 'urlon', 'lrlat', 'lrlon', 'lllat', 'lllon',
                'minx', 'miny', 'maxx', 'maxy'
            ]

    home = path.expanduser("~")
    tmpFPN = path.join(home, '1degglobaltiles.csv')
    with open(tmpFPN, 'w') as csvfile:
        wr = csv.writer(csvfile)
        wr.writerows(csvL)
    regFPN = path.join(home, '1degglobalregions.csv')
    with open(regFPN, 'w') as csvfile:
        wr = csv.writer(csvfile)
        wr.writerows(regionL)
    #Open the db session for MODIS
    session = ManageRegion()

    #session._LoadBulkDefregions(tmpFPN)
    session._LoadBulkRegions(regFPN)
    session._Close()