Skip to content

Commit 764b2ed

Browse files
authored
Merge pull request #7 from mn5hk/add-swords
Add swords
2 parents e78818c + a5554b8 commit 764b2ed

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

geoslurp_userplugins/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
from .sebs import *
3232
from .gleam import *
3333
from .imerg import *
34+
from .sword import *

geoslurp_userplugins/sword.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This file is part of geoslurp.
2+
# geoslurp is free software; you can redistribute it and/or
3+
# modify it under the terms of the GNU Lesser General Public
4+
# License as published by the Free Software Foundation; either
5+
# version 3 of the License, or (at your option) any later version.
6+
7+
# geoslurp is distributed in the hope that it will be useful,
8+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10+
# Lesser General Public License for more details.
11+
12+
# You should have received a copy of the GNU Lesser General Public
13+
# License along with geoslurp; if not, write to the Free Software
14+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15+
16+
# Author Roelof Rietbroek (r.rietbroek@utwente.nl), 2023
17+
# Author Amin Shakya (a.shakya@utwente.nl), 2023
18+
19+
from geoslurp.dataset.OGRBase import OGRBase
20+
from geoslurp.datapull.http import Uri as http
21+
from geoslurp.config.catalogue import geoslurpCatalogue
22+
from datetime import datetime
23+
import json
24+
from zipfile import ZipFile
25+
import os
26+
import tempfile
27+
import yaml
28+
29+
class SwordBase(OGRBase):
30+
"""
31+
Base class for SWORD: the SWOT River Database, Version 15. If you would like more information about SWORD, please see the paper published in Water Resources Research in 2021, led by Elizabeth Altenau. You can access it here: https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2021WR030054
32+
"""
33+
url='http://gaia.geosci.unc.edu/SWORD/SWORD_v15_gpkg.zip'
34+
path="SWORD_v15_gpkg.zip"
35+
scheme='globalgis'
36+
version=(0,0,0)
37+
cont=None
38+
swordtype=None
39+
40+
def __init__(self,dbconn):
41+
super().__init__(dbconn)
42+
self.ogrfile=os.path.join(self.cacheDir(),os.path.basename(self.path))
43+
self.gtype='GEOMETRY'
44+
self.setCacheDir(os.path.join(self.conf.getCacheDir(scheme=self.scheme),'SwordBase'))
45+
self.ogrfile=self.getogrfile()
46+
47+
48+
def pull(self):
49+
"""Pulls the geojson data from the url and unpacks it in the cache directory"""
50+
uri=http(self.url,lastmod=datetime(2023,6,28))
51+
uri.download(direc=self.cacheDir(),outfile=self.path,check=True)
52+
path_to_zip_file = os.path.join(self.cacheDir(), self.path)
53+
with ZipFile(path_to_zip_file, 'r') as zip_ref:
54+
zip_ref.extractall(self.cacheDir())
55+
56+
def getogrfile(self):
57+
return os.path.join(self.cacheDir("gpkg"),self.cont+"_sword_"+self.swordtype+"_v15.gpkg")
58+
59+
def SwordClassFactory(clsName,val):
60+
return type(clsName, (SwordBase,), {"cont":val["cont"], "swordtype":val["swordtype"]})
61+
62+
def getSwordDsets(conf):
63+
# Function does not initiate class instances, but only defines class types
64+
continents=["eu","af","sa","oc","as"]
65+
swordtypes = ["reaches", "nodes"]
66+
out=[]
67+
68+
#create a list of datasets
69+
for continent in continents:
70+
for swordtype in swordtypes:
71+
entry={"cont":continent,"swordtype":swordtype}
72+
clsname=continent + "_sword_" + swordtype
73+
out.append(SwordClassFactory(clsname,entry))
74+
75+
76+
return out
77+
78+
79+
geoslurpCatalogue.addDatasetFactory(getSwordDsets)

0 commit comments

Comments
 (0)