From 2b0862495e916ab0c9e2c88066a0d29228bddfc6 Mon Sep 17 00:00:00 2001 From: Elijah West Date: Fri, 8 Dec 2017 13:38:43 -0700 Subject: [PATCH 1/2] Fixed getSamplingFeatureDatasets issue #130 --- odm2api/ODM2/services/readService.py | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index e6295f9..b7b8c09 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -74,9 +74,11 @@ def __init__(self, affiliation, person, org): class SamplingFeatureDataSet(): datasets={} - def __init__(self, samplingfeature, datasetresults): + related_features={} + def __init__(self, samplingfeature, datasetresults, relatedfeatures): sf = samplingfeature + self.SamplingFeature = sf self.SamplingFeatureID = sf.SamplingFeatureID self.SamplingFeatureUUID = sf.SamplingFeatureUUID self.SamplingFeatureTypeCV = sf.SamplingFeatureTypeCV @@ -88,11 +90,13 @@ def __init__(self, samplingfeature, datasetresults): self.ElevationDatumCV = sf.ElevationDatumCV self.FeatureGeometryWKT = sf.FeatureGeometryWKT self.assignDatasets(datasetresults) + self.assignRelatedFeatures(relatedfeatures) - print(self.datasets) + print(self.datasets) def assignDatasets(self, datasetresults): + self.datasets = {} for dsr in datasetresults: if dsr.DataSetObj not in self.datasets: #if the dataset is not in the dictionary, add it and the first result @@ -107,6 +111,14 @@ def assignDatasets(self, datasetresults): self.datasets[dsr.DataSetObj].append(res) + def assignRelatedFeatures(self, relatedfeatures): + self.related_features = {} + for related in relatedfeatures: + if related.SamplingFeatureTypeCV == 'Site': + self.related_features = related + + + class ReadODM2(serviceBase): @@ -875,7 +887,7 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None): return None - def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None): + def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, type=None): """ Retrieve a list of Datasets associated with the given sampling feature data. @@ -904,16 +916,20 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No # make sure one of the three arguments has been sent in - if all(v is None for v in [ids, codes, uuids]): - raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode argument') + # if all(v is None for v in [ids, codes, uuids, type]): + # raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType ' + # 'argument') sf_query = self._session.query(SamplingFeatures) + if type: + sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureTypeCV == type) if ids: sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureID.in_(ids)) if codes: sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureCode.in_(codes)) if uuids: sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureUUID.in_(uuids)) + sf_list = [] for sf in sf_query.all(): sf_list.append(sf) @@ -934,7 +950,9 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No vals = q.all() - sfds.append(SamplingFeatureDataSet(sf, vals)) + related = self.getRelatedSamplingFeatures(sf.SamplingFeatureID) + + sfds.append(SamplingFeatureDataSet(sf, vals, related)) except Exception as e: print('Error running Query: {}'.format(e)) return None From 9592471d1bb0dd35df48b9bb5806161f72620e0f Mon Sep 17 00:00:00 2001 From: lsetiawan Date: Wed, 13 Dec 2017 13:00:36 -0800 Subject: [PATCH 2/2] Fix getSamplingFeatureDataset --- odm2api/ODM2/services/readService.py | 49 +++++++++++++++------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index b7b8c09..168d20b 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -97,25 +97,27 @@ def __init__(self, samplingfeature, datasetresults, relatedfeatures): def assignDatasets(self, datasetresults): self.datasets = {} - for dsr in datasetresults: - if dsr.DataSetObj not in self.datasets: - #if the dataset is not in the dictionary, add it and the first result - self.datasets[dsr.DataSetObj]=[] - res = dsr.ResultObj - # res.FeatureActionObj = None - self.datasets[dsr.DataSetObj].append(res) - else: - #if the dataset is in the dictionary, append the result object to the list - res = dsr.ResultObj - # res.FeatureActionObj = None - self.datasets[dsr.DataSetObj].append(res) + if datasetresults: + for dsr in datasetresults: + if dsr.DataSetObj not in self.datasets: + #if the dataset is not in the dictionary, add it and the first result + self.datasets[dsr.DataSetObj]=[] + res = dsr.ResultObj + # res.FeatureActionObj = None + self.datasets[dsr.DataSetObj].append(res) + else: + #if the dataset is in the dictionary, append the result object to the list + res = dsr.ResultObj + # res.FeatureActionObj = None + self.datasets[dsr.DataSetObj].append(res) def assignRelatedFeatures(self, relatedfeatures): self.related_features = {} - for related in relatedfeatures: - if related.SamplingFeatureTypeCV == 'Site': - self.related_features = related + if relatedfeatures: + for related in relatedfeatures: + if related.SamplingFeatureTypeCV == 'Site': + self.related_features = related @@ -887,7 +889,7 @@ def getDataSetsValues(self, ids=None, codes=None, uuids=None, dstype=None): return None - def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, type=None): + def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=None, sftype=None): """ Retrieve a list of Datasets associated with the given sampling feature data. @@ -899,7 +901,8 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No uuids (list, optional): List of UUIDs string. dstype (str, optional): Type of Dataset from `controlled vocabulary name `_. - + sftype (str, optional): Type of SamplingFeature from + `controlled vocabulary name `_. Returns: list: List of DataSetsResults Objects associated with the given sampling feature @@ -911,18 +914,19 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No >>> READ.getSamplingFeatureDatasets(uuids=['a6f114f1-5416-4606-ae10-23be32dbc202', ... '5396fdf3-ceb3-46b6-aaf9-454a37278bb4']) >>> READ.getSamplingFeatureDatasets(dstype='singleTimeSeries') + >>> READ.getSamplingFeatureDatasets(sftype='Specimen') """ # make sure one of the three arguments has been sent in - # if all(v is None for v in [ids, codes, uuids, type]): - # raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType ' - # 'argument') + if all(v is None for v in [ids, codes, uuids, sftype]): + raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode OR samplingFeatureType ' + 'argument') sf_query = self._session.query(SamplingFeatures) - if type: - sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureTypeCV == type) + if sftype: + sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureTypeCV == sftype) if ids: sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureID.in_(ids)) if codes: @@ -934,7 +938,6 @@ def getSamplingFeatureDatasets(self, ids=None, codes=None, uuids=None, dstype=No for sf in sf_query.all(): sf_list.append(sf) - sfds = None try: sfds=[] for sf in sf_list: