Ejemplo n.º 1
0
def fesomMeshQueryXY(dbcon, fesominfo, geoWKT):
    """Return fesom mesh grid as [lon, lat, depth]"""
    pnt=[]
    pnt_id=[]
    for entry in fesomMeshQuery(dbcon, fesominfo, geoWKT):
            tmp=shpextract(entry)
            xy = np.array([[tmp.x, tmp.y]])
            pnt_id.append(entry._row[1])
            tmp_id = np.array(entry._row[1])
            # tri_temp = 
            if len(pnt)==0:
                pnt=xy
                # pnt_id2= tmp_id
            else: 
                pnt = np.concatenate((pnt,xy)) 
                # pnt_id2 = np.concatenate((pnt_id2,tmp_id)) 
                # pnt_id2.extend(tmp_id)
            
    return pnt, pnt_id
Ejemplo n.º 2
0
def queryMonthlyArgo(dbcon, geoWKT, tstart, tend):
    """Query the database for lists of monthly Argo profiles within a certain polygon and time span"""

    out = {}

    for entry in argoQuery(dbcon,
                           geoWKT=geoWKT,
                           tspan=[tstart, tend],
                           tsort=True):
        epoch = (entry.tlocation.year, entry.tlocation.month)
        pnt = shpextract(entry)
        tmpdict = {
            "uri": entry.uri,
            "iprof": entry.iprof,
            "lonlat": (pnt.x, pnt.y)
        }
        if not epoch in out:
            out[epoch] = [tmpdict]
        else:
            out[epoch].append(tmpdict)

    return out
Ejemplo n.º 3
0
def fesomMeshQueryWKB(dbcon, fesominfo, geoWKT):
    """Query the database positions of OBP points"""
    
    return [(shpextract(x)) for x in fesomMeshQueryXY(dbcon, fesominfo, geoWKT)]
Ejemplo n.º 4
0
def awipiesWKB(dbcon, tspan, geoWKT=False):
    """Query the database positions of OBP points"""
    
    return [(shpextract(x)) for x in awipiesQuery(dbcon,tspan)]
Ejemplo n.º 5
0
def exportQuery(qryresult,
                outputfile,
                layer=None,
                driver="SQLITE",
                packFiles=False,
                striproot=None):
    """Export a query without a geometry column, and possibly pack corresponding files"""

    if "geom" in qryresult.keys():
        useGeoPandas = True
    else:
        useGeoPandas = False

    if useGeoPandas and driver not in ["GPKG"]:
        raise RunTimeError("no geometry found in the specified query")

    if useGeoPandas:
        df = gpd.GeoDataFrame()
        df["geometry"] = None
    else:
        df = pd.DataFrame()

    #initialize columns
    for ky in (ky for ky in qryresult.keys() if ky not in ["geom", "id"]):
        df[ky] = None

    if packFiles:
        #open a tgz archive
        farchive = os.path.splitext(outputfile)[0] + "_files.tgz"
        farchivetmp = os.path.splitext(outputfile)[0] + "_files.tar"
        if striproot:
            mmap = MirrorMap(striproot, farchive + ":/")

        #open/reopen archive
        if os.path.exists(farchive):
            #unzip to a temporary file
            farchivetmp = os.path.splitext(outputfile)[0] + "_files.tar"
            with gzip.open(farchive, 'rb') as gzid:
                with open(farchivetmp, 'wb') as fid:
                    shutil.copyfileobj(gzid, fid)

            tarar = tarfile.open(farchivetmp, mode='a')
        else:
            tarar = tarfile.open(farchive, mode='w:gz')

    else:
        tarar = None

    #add new rows
    for entry in qryresult:
        entrymod = {}
        for ky, val in entry.items():
            if useGeoPandas and ky == "geom":
                val = shpextract(entry)
                ky = "geometry"
            elif ky == "id":
                val = None
            elif ky == "data":
                #convert to json
                val = json.dumps(val)
            elif ky == "uri" and packFiles:
                #modify the uri and add file in the archive
                if not striproot:
                    #try stripping of everything before and including 'geoslurp' from the path
                    striproot = re.search("^.*/geoslurp/", val).group(0)
                    mmap = MirrorMap(striproot, farchive + ":/")
                uriorig = val
                val = mmap.apply(val)
                #create a new tarfile member if needed
                try:
                    basef = mmap.strip(uriorig)
                    tinfo = tarar.getmember(basef)
                except KeyError:
                    #create a new member
                    tarar.add(name=uriorig, arcname=basef)
            if val:
                entrymod[ky] = val

        df = df.append(entrymod, ignore_index=True)

    if packFiles:
        tarar.close()
        if os.path.exists(farchivetmp):
            #rezip the tar archive and clean up
            with gzip.open(farchive, 'wb') as gzid:
                with open(farchivetmp, 'rb') as fid:
                    shutil.copyfileobj(fid, gzid)
            os.remove(farchivetmp)
    #export to file
    if useGeoPandas:
        df.to_file(outputfile, layer=layer, driver=driver)
    else:
        if driver == "SQLITE":
            writeToSQLite(outputfile, df, layer)
        else:
            raise InputError("Don't know how to interpret output driver" %
                             driver)