Skip to content

Fixed getSamplingFeatureDatasets issue #130 #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions odm2api/ODM2/services/readService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,23 +90,35 @@ 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):
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)
self.datasets = {}
if datasetresults:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this line to handle cases that there are no datasets associated.

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 = {}
if relatedfeatures:
Copy link
Member

@lsetiawan lsetiawan Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this line to handle cases that there are no relatedfeatures.

for related in relatedfeatures:
if related.SamplingFeatureTypeCV == 'Site':
self.related_features = related




Expand Down Expand Up @@ -875,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):
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.

Expand All @@ -887,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 <http://vocabulary.odm2.org/datasettype/>`_.

sftype (str, optional): Type of SamplingFeature from
`controlled vocabulary name <http://vocabulary.odm2.org/samplingfeaturetype/>`_.

Returns:
list: List of DataSetsResults Objects associated with the given sampling feature
Expand All @@ -899,26 +914,30 @@ 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]):
raise ValueError('Expected samplingFeatureID OR samplingFeatureUUID OR samplingFeatureCode 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 sftype:
sf_query = sf_query.filter(SamplingFeatures.SamplingFeatureTypeCV == sftype)
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)

sfds = None
try:
sfds=[]
for sf in sf_list:
Expand All @@ -934,7 +953,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
Expand Down