Skip to content

Commit dd1ec35

Browse files
authored
Merge pull request #125 from ODM2/sfdataset
Sfdataset
2 parents 2d3e4ab + d84c252 commit dd1ec35

File tree

2 files changed

+86
-33
lines changed

2 files changed

+86
-33
lines changed

odm2api/ODM2/services/readService.py

+54-10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,43 @@ def __init__(self, affiliation, person, org):
7272
self.Organization = '(' + org.OrganizationCode + ') ' + org.OrganizationName
7373

7474

75+
class SamplingFeatureDataSet():
76+
datasets={}
77+
def __init__(self, samplingfeature, datasetresults):
78+
sf = samplingfeature
79+
80+
self.SamplingFeatureID = sf.SamplingFeatureID
81+
self.SamplingFeatureUUID = sf.SamplingFeatureUUID
82+
self.SamplingFeatureTypeCV = sf.SamplingFeatureTypeCV
83+
self.SamplingFeatureCode = sf.SamplingFeatureCode
84+
self.SamplingFeatureName = sf.SamplingFeatureName
85+
self.SamplingFeatureDescription = sf.SamplingFeatureDescription
86+
self.SamplingFeatureGeotypeCV = sf.SamplingFeatureGeotypeCV
87+
self.Elevation_m = sf.Elevation_m
88+
self.ElevationDatumCV = sf.ElevationDatumCV
89+
self.FeatureGeometryWKT = sf.FeatureGeometryWKT
90+
self.assignDatasets(datasetresults)
91+
92+
print(self.datasets)
93+
94+
95+
def assignDatasets(self, datasetresults):
96+
for dsr in datasetresults:
97+
if dsr.DataSetObj not in self.datasets:
98+
#if the dataset is not in the dictionary, add it and the first result
99+
self.datasets[dsr.DataSetObj]=[]
100+
res = dsr.ResultObj
101+
# res.FeatureActionObj = None
102+
self.datasets[dsr.DataSetObj].append(res)
103+
else:
104+
#if the dataset is in the dictionary, append the result object to the list
105+
res = dsr.ResultObj
106+
# res.FeatureActionObj = None
107+
self.datasets[dsr.DataSetObj].append(res)
108+
109+
110+
111+
75112
class ReadODM2(serviceBase):
76113
# Exists functions
77114
def resultExists(self, result):
@@ -871,7 +908,6 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
871908
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode argument')
872909

873910
sf_query = self._session.query(SamplingFeatures)
874-
875911
if ids:
876912
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureID.in_(ids))
877913
if codes:
@@ -880,21 +916,29 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
880916
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureUUID.in_(uuids))
881917
sf_list = []
882918
for sf in sf_query.all():
883-
sf_list.append(sf.SamplingFeatureID)
919+
sf_list.append(sf)
884920

885-
q = self._session.query(DataSetsResults)\
886-
.join(Results)\
887-
.join(FeatureActions)\
888-
.filter(FeatureActions.SamplingFeatureID.in_(sf_list))
921+
sfds = None
922+
try:
923+
sfds=[]
924+
for sf in sf_list:
889925

890-
if dstype:
891-
q = q.filter_by(DatasetTypeCV=dstype)
926+
q = self._session.query(DataSetsResults)\
927+
.join(Results)\
928+
.join(FeatureActions)\
929+
.filter(FeatureActions.SamplingFeatureID == sf.SamplingFeatureID)
892930

893-
try:
894-
return q.all()
931+
if dstype:
932+
q = q.filter_by(DatasetTypeCV=dstype)
933+
934+
935+
vals = q.all()
936+
937+
sfds.append(SamplingFeatureDataSet(sf, vals))
895938
except Exception as e:
896939
print('Error running Query: {}'.format(e))
897940
return None
941+
return sfds
898942

899943
# Data Quality
900944
def getDataQuality(self):

tests/test_odm2/test_readservice.py

+32-23
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,51 @@ def test_getDataSetsValues(self):
130130

131131

132132
def test_getSamplingFeatureDataSets(self):
133+
try:
134+
#find a sampling feature that is associated with a dataset
135+
sf = self.engine.execute(
136+
'SELECT * from SamplingFeatures as sf '
137+
'inner join FeatureActions as fa on fa.SamplingFeatureID == sf.SamplingFeatureID '
138+
'inner join Results as r on fa.FeatureActionID == r.FeatureActionID '
139+
'inner join DataSetsResults as ds on r.ResultID == ds.ResultID '
140+
).fetchone()
141+
assert len(sf) > 0
142+
143+
#get the dataset associated with the sampling feature
144+
ds = self.engine.execute(
145+
'SELECT * from DataSetsResults as ds '
146+
'inner join Results as r on r.ResultID == ds.ResultID '
147+
'inner join FeatureActions as fa on fa.FeatureActionID == r.FeatureActionID '
148+
'where fa.SamplingFeatureID = ' + str(sf[0])
149+
).fetchone()
150+
assert len(ds) > 0
133151

134-
#find a sampling feature that is associated with a dataset
135-
sf = self.engine.execute(
136-
'SELECT * from SamplingFeatures as sf '
137-
'inner join FeatureActions as fa on fa.SamplingFeatureID == sf.SamplingFeatureID '
138-
'inner join Results as r on fa.FeatureActionID == r.FeatureActionID '
139-
'inner join DataSetsResults as ds on r.ResultID == ds.ResultID '
140-
).fetchone()
141-
assert len(sf) > 0
152+
print (sf[0])
153+
# get the dataset associated with the sampling feature using hte api
154+
dsapi = self.reader.getSamplingFeatureDatasets(ids=[sf[0]])
142155

143-
#get the dataset associated with the sampling feature
144-
ds = self.engine.execute(
145-
'SELECT * from DataSetsResults as ds '
146-
'inner join Results as r on r.ResultID == ds.ResultID '
147-
'inner join FeatureActions as fa on fa.FeatureActionID == r.FeatureActionID '
148-
'where fa.SamplingFeatureID = ' + str(sf[0])
149-
).fetchone()
150-
assert len(ds) > 0
156+
assert dsapi is not None
157+
assert len(dsapi) > 0
158+
assert dsapi[0].datasets is not None
159+
assert dsapi[0].SamplingFeatureID == sf[0]
160+
# assert ds[0] == dsapi[0]
161+
except Exception as ex:
162+
assert False
163+
finally:
164+
self.reader._session.rollback()
151165

152-
print (sf[0])
153-
# get the dataset associated with the sampling feature using hte api
154-
dsapi = self.reader.getSamplingFeatureDatasets(ids=[sf[0]])
155-
156-
assert dsapi is not None
157-
assert len(dsapi) > 0
158-
assert ds[1] == dsapi[0].DataSetID
159166

160167
# Results
161168
def test_getAllResults(self):
169+
162170
# get all results from the database
163171
res = self.engine.execute('SELECT * FROM Results').fetchall()
164172
print(res)
165173
# get all results using the api
166174
resapi = self.reader.getResults()
167175
assert len(res) == len(resapi)
168176

177+
169178
def test_getResultsByID(self):
170179
# get a result from the database
171180
res = self.engine.execute('SELECT * FROM Results').fetchone()

0 commit comments

Comments
 (0)