Skip to content

Commit 4acdf62

Browse files
authored
Merge pull request #131 from ODM2/samplingfeaturedataset_dev
Fixed getSamplingFeatureDatasets issue #130
2 parents 5bf911c + 9592471 commit 4acdf62

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ def __init__(self, affiliation, person, org):
7474

7575
class SamplingFeatureDataSet():
7676
datasets={}
77-
def __init__(self, samplingfeature, datasetresults):
77+
related_features={}
78+
def __init__(self, samplingfeature, datasetresults, relatedfeatures):
7879
sf = samplingfeature
7980

81+
self.SamplingFeature = sf
8082
self.SamplingFeatureID = sf.SamplingFeatureID
8183
self.SamplingFeatureUUID = sf.SamplingFeatureUUID
8284
self.SamplingFeatureTypeCV = sf.SamplingFeatureTypeCV
@@ -88,23 +90,35 @@ def __init__(self, samplingfeature, datasetresults):
8890
self.ElevationDatumCV = sf.ElevationDatumCV
8991
self.FeatureGeometryWKT = sf.FeatureGeometryWKT
9092
self.assignDatasets(datasetresults)
93+
self.assignRelatedFeatures(relatedfeatures)
9194

92-
print(self.datasets)
9395

96+
print(self.datasets)
9497

9598
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)
99+
self.datasets = {}
100+
if datasetresults:
101+
for dsr in datasetresults:
102+
if dsr.DataSetObj not in self.datasets:
103+
#if the dataset is not in the dictionary, add it and the first result
104+
self.datasets[dsr.DataSetObj]=[]
105+
res = dsr.ResultObj
106+
# res.FeatureActionObj = None
107+
self.datasets[dsr.DataSetObj].append(res)
108+
else:
109+
#if the dataset is in the dictionary, append the result object to the list
110+
res = dsr.ResultObj
111+
# res.FeatureActionObj = None
112+
self.datasets[dsr.DataSetObj].append(res)
113+
114+
115+
def assignRelatedFeatures(self, relatedfeatures):
116+
self.related_features = {}
117+
if relatedfeatures:
118+
for related in relatedfeatures:
119+
if related.SamplingFeatureTypeCV == 'Site':
120+
self.related_features = related
121+
108122

109123

110124

@@ -888,7 +902,7 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None):
888902
return None
889903

890904

891-
def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None):
905+
def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, sftype=None):
892906
"""
893907
Retrieve a list of Datasets associated with the given sampling feature data.
894908
@@ -900,7 +914,8 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
900914
uuids (list, optional): List of UUIDs string.
901915
dstype (str, optional): Type of Dataset from
902916
`controlled vocabulary name <http://vocabulary.odm2.org/datasettype/>`_.
903-
917+
sftype (str, optional): Type of SamplingFeature from
918+
`controlled vocabulary name <http://vocabulary.odm2.org/samplingfeaturetype/>`_.
904919
905920
Returns:
906921
list: List of DataSetsResults Objects associated with the given sampling feature
@@ -912,26 +927,30 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
912927
>>> READ.getSamplingFeatureDatasets(uuids=['a6f114f1-5416-4606-ae10-23be32dbc202',
913928
... '5396fdf3-ceb3-46b6-aaf9-454a37278bb4'])
914929
>>> READ.getSamplingFeatureDatasets(dstype='singleTimeSeries')
930+
>>> READ.getSamplingFeatureDatasets(sftype='Specimen')
915931
916932
"""
917933

918934

919935
# make sure one of the three arguments has been sent in
920-
if all(v is None for v in [ids, codes, uuids]):
921-
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode argument')
936+
if all(v is None for v in [ids, codes, uuids, sftype]):
937+
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType '
938+
'argument')
922939

923940
sf_query = self._session.query(SamplingFeatures)
941+
if sftype:
942+
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureTypeCV == sftype)
924943
if ids:
925944
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureID.in_(ids))
926945
if codes:
927946
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureCode.in_(codes))
928947
if uuids:
929948
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureUUID.in_(uuids))
949+
930950
sf_list = []
931951
for sf in sf_query.all():
932952
sf_list.append(sf)
933953

934-
sfds = None
935954
try:
936955
sfds=[]
937956
for sf in sf_list:
@@ -947,7 +966,9 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No
947966

948967
vals = q.all()
949968

950-
sfds.append(SamplingFeatureDataSet(sf, vals))
969+
related = self.getRelatedSamplingFeatures(sf.SamplingFeatureID)
970+
971+
sfds.append(SamplingFeatureDataSet(sf, vals, related))
951972
except Exception as e:
952973
print('Error running Query: {}'.format(e))
953974
return None

0 commit comments

Comments
 (0)