From dd233c0561f6fcde8433169fa32e5739171258f2 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 23 Mar 2016 10:29:39 -0600 Subject: [PATCH 1/9] new create functions --- odm2api/ODM2/services/createService.py | 1257 +++++++++++++----------- 1 file changed, 666 insertions(+), 591 deletions(-) diff --git a/odm2api/ODM2/services/createService.py b/odm2api/ODM2/services/createService.py index f1161c4..5ffa75b 100644 --- a/odm2api/ODM2/services/createService.py +++ b/odm2api/ODM2/services/createService.py @@ -17,468 +17,65 @@ def __init__(self, session): # Annotations # ################################################################################ - - -# ################################################################################ -# CV -# ################################################################################ - - - - -# ################################################################################ -# Core -# ################################################################################ - - def createVariable(self, code, name, vType, nodv, speciation=None, definition=None): - """ - - :param code: - :type String(50): - :param name: - :type String(255): - :param vType: - :type String(255): - :param nodv: - :type Float(53): - :param speciation: - :type String(255): - :param definition: - :type String(500): - :return: - """ - var = Variables() - var.VariableCode = code - var.VariableNameCV = name - var.VariableDefinition = definition - var.VariableTypeCV = vType - var.NoDataValue = nodv - var.SpeciationCV = speciation - + def createVariable(self, var): self._session.add(var) self._session.commit() - - return var - - def createMethod(self, code, name, vType, orgId=None, link=None, description=None): - """Create Method table for the database - - :param code: - :type String(50): - :param name: - :type String(255): - :param vType: - :type String(255): - :param orgId: - :type Integer: - :param link: - :type String(255): - :param description: - :type String(500): - :return: - """ - - meth = Methods() - meth.MethodCode = code - meth.MethodName = name - meth.MethodDescription = description - meth.MethodTypeCV = vType - meth.MethodLink = link - meth.OrganizationID = orgId - - self._session.add(meth) + def createMethod(self, method ): + self._session.add(method) self._session.commit() - - return meth - - def createProcessingLevel(self, code, definition=None, explanation=None): - """Create Processinglevel table for database - - :param code: - :type String(50): - :param definition: - :type String(500): - :param explanation: - :type String(500): - :return: - """ - pl = ProcessingLevels() - pl.ProcessingLevelCode = str(code) - pl.Definition = definition - pl.Explanation = explanation - - self._session.add(pl) + def createProcessingLevel(self, proclevel): + self._session.add(proclevel) self._session.commit() - - return pl - - def createSamplingFeature(self, uuid, code, vType, name=None, description=None, geoType=None, elevation=None, - elevationDatum=None, featureGeo=None): - """Create SamplingFeature table - - :param code: - :type String(50): - :param vType: - :type String(255): - :param name: - :type String(255): - :param description: - :type String(500): - :param geoType: - :type String(255): - :param evelation: - :type Float(53): - :param evelationDatum: - :type String(255): - :param featureGeo: - :type NullType: - :return: - """ - - sf = SamplingFeatures() - sf.SamplingFeatureUUID = uuid - sf.SamplingFeatureTypeCV = vType - sf.SamplingFeatureCode = code - sf.SamplingFeatureName = name - sf.SamplingFeatureDescription = description - sf.SamplingFeatureGeoTypeCV = geoType - sf.Elevation_m = elevation - sf.ElevationDatumCV = elevationDatum - sf.FeatureGeometry = featureGeo - - self._session.add(sf) + def createSamplingFeature(self , samplingfeature): + self._session.add(samplingfeature) self._session.commit() - - return sf - - - def createUnit(self, type, abbrev, name, link=None): - """Create Unit table - - :param code: - :type String(255): - :param abbrev: - :type String(50): - :param name: - :type String(255): - :return: - """ - unit = Units() - unit.UnitsTypeCV = type - unit.UnitsAbbreviation = abbrev - unit.UnitsName = name - unit.UnitsLink = link - + def createUnit(self, unit ): self._session.add(unit) self._session.commit() - - return unit - - def createOrganization(self, cvType, code, name, desc, link, parentOrgId): - """Create Organization table - - :param cvType: - :type String(255): - :param code: - :type String(50): - :param name: - :type String(255): - :param desc: - :type String(500): - :param link: - :type String(255): - :param parentOrgId: - :type Integer: - :return: - """ - - org = Organizations() - org.OrganizationTypeCV = cvType - org.OrganizationCode = code - org.OrganizationName = name - org.OrganizationDescription = desc - org.OrganizationLink = link - org.ParentOrganizationID = parentOrgId - + def createOrganization(self, org ): self._session.add(org) self._session.commit() - - return org - - def createPerson(self, firstName, lastName, middleName=""): - """Create Person Table - - :param firstName: - :type String(255): - :param lastName: - :type String(255): - :param middleName: - :type String(255): - :return: - """ - - p = People() - p.PersonFirstName = firstName - p.PersonMiddleName = middleName - p.PersonLastName = lastName - - self._session.add(p) + def createPerson(self, person ): + self._session.add(person) self._session.commit() - - return p - - # def createResult(self, uuid, featureActionId, vType, ): - - def createAffiliation(self, personid, organizationid, email, phone=None, address=None, link=None, - iscontact=False, affiliation_start=dt.datetime.today(), affiliation_end=None): - """ - - :param personid: id of the person record - :param organizationid: id of the organization record - :param email: primary email address - :param phone: primary phone number - :param address: primary mailing address - :param link: url pointing the web resource such as researchGate profile - :param iscontact: indicate if this person is the primary contact for the organization - :param affiliation_start: begin date of affiliation with organization - :param affiliation_end: end date of affiliation with organization - :return: ODM2.Affiliation - """ - - # create affiliation object - a = Affiliations() - a.PersonID = personid - a.OrganizationID = organizationid - a.PrimaryEmail = email - a.PrimaryPhone = phone - a.PrimaryAddress = address - a.PersonLink = link - a.IsPrimaryOrganizationContact = iscontact - a.AffiliationStartDate = affiliation_start - a.AffiliationEndDate = affiliation_end - - self._session.add(a) + def createAffiliation(self, affiliation ): + self._session.add(affiliation) self._session.commit() - #self._session.flush() - # self._session.refresh(a) - - print a.OrganizationID - - return a - - def createDataset(self, dstype, dscode, dstitle, dsabstract): - ds = DataSets() - - # create the dataset - ds.DataSetTypeCV = dstype - ds.DataSetCode = dscode - ds.DataSetTitle = dstitle - ds.DataSetAbstract = dsabstract - ds.DataSetUUID = uuid.uuid4().hex - - self._session.add(ds) + def createDataset(self, dataset ): + self._session.add(dataset) self._session.commit() - - return ds - - def createDatasetResults(self, dsid, resultid): - dsr = DataSetsResults() - - # link dataset to results - dsr.DatasetID = dsid - dsr.ResultID = resultid - - self._session.add(dsr) + def createDatasetResults(self, datasetresult ): + self._session.add(datasetresult) self._session.commit() - - return dsr - - def createAction(self, type, methodid, begindatetime, begindatetimeoffset, enddatetime=None, enddatetimeoffset=None, - description=None, filelink=None): - action = Actions() - action.ActionTypeCV = type - action.MethodID = methodid - action.BeginDateTime = begindatetime - action.BeginDateTimeUTCOffset = begindatetimeoffset - action.EndDateTime = enddatetime - action.EndDateTimeUTCOffset = enddatetimeoffset - action.ActionDescription = description - action.ActionFileLink = filelink - + def createAction(self, action, actionby): self._session.add(action) - self._session.commit() - - return action - - def createActionBy(self, actionid, affiliationid, isactionlead=True, roledescription=None): - actionby = ActionBy() - actionby.ActionID = actionid - actionby.AffiliationID = affiliationid - actionby.IsActionLead = isactionlead - actionby.RoleDescription = roledescription - self._session.add(actionby) self._session.commit() - - return actionby - - def createFeatureAction(self, samplingfeatureid, actionid): - featureaction = FeatureActions() - featureaction.SamplingFeatureID = samplingfeatureid - featureaction.ActionID = actionid - - self._session.add(featureaction) + def createRelatedAction(self, relatedaction ): + self._session.add(relatedaction) self._session.commit() - - return featureaction - - - def createResult(self, featureactionid, variableid, unitid, processinglevelid, valuecount, sampledmedium, - resulttypecv, - taxonomicclass=None, resultdatetime=None, resultdatetimeutcoffset=None, - validdatetime=None, validdatetimeutcoffset=None, statuscv=None): - result = Results() - #result.ResultUUID = uuid.uuid4().hex - result.ResultUUID = str(uuid.uuid4()) # Denver - result.FeatureActionID = featureactionid - result.ResultTypeCV = resulttypecv - result.VariableID = variableid - result.UnitsID = unitid - result.ProcessingLevelID = processinglevelid - result.ValueCount = valuecount - result.SampledMediumCV = sampledmedium - result.TaxonomicClassifierID = taxonomicclass - result.ResultDateTime = resultdatetime - result.ResultDateTimeUTCOffset = resultdatetimeutcoffset - result.ValidDateTime = validdatetime - result.ValidDateTimeUTCOffset = validdatetimeutcoffset - result.StatusCV = statuscv - + def createResult(self, result ): self._session.add(result) self._session.commit() - - return result - - -# ################################################################################ -# Data Quality -# ################################################################################ - - - -# ################################################################################ -# Equipment -# ################################################################################ - - - - -# ################################################################################ -# ExtensionProperties -# ################################################################################ - - - - - -# ################################################################################ -# External Identifiers -# ################################################################################ - - - - -# ################################################################################ -# Lab Analyses -# ################################################################################ - - - - -# ################################################################################ -# Provenance -# ################################################################################ - - - - -# ################################################################################ -# Results -# ################################################################################ - - - - def createTimeSeriesResult(self, result, aggregationstatistic, xloc=None, xloc_unitid=None, yloc=None, - yloc_unitid=None, zloc=None, zloc_unitid=None, - srsID=None, timespacing=None, timespacing_unitid=None): - - tsr = TimeSeriesResults() - - tsr.ResultID = result.ResultID - #tsr.ResultUUID = result.ResultUUID - - - tsr.XLocation = xloc - tsr.XLocationUnitsID = xloc_unitid - tsr.YLocation = yloc - tsr.YLocationUnitsID = yloc_unitid - tsr.ZLocation = zloc - tsr.ZLocationUnitsID = zloc_unitid - tsr.SpatialReferenceID = srsID - tsr.IntendedTimeSpacing = timespacing - tsr.IntendedTimeSpacingUnitsID = timespacing_unitid - tsr.AggregationStatisticCV = aggregationstatistic - - - #tsr.ResultID = result.ResultID - # tsr.ResultUUID = result.ResultUUID - # tsr.FeatureActionID = result.FeatureActionID - # tsr.VariableID = result.VariableID - # tsr.UnitsID = result.UnitsID - # tsr.ProcessingLevelID = result.ProcessingLevelID - # tsr.ValueCount = result.ValueCount - # tsr.SampledMediumCV = result.SampledMediumCV - # tsr.ResultTypeCV = result.ResultTypeCV - - self._session.add(tsr) + def createResultValues(self, values ): + self._session.add(values) self._session.commit() - return tsr - ''' - def createTimeSeriesResultValues(self, resultid, datavalues, datetimes, datetimeoffsets, censorcodecv, - qualitycodecv, - timeaggregationinterval, timeaggregationunit): - - - try: - values = TimeSeriesResultValues() - for i in range(len(datavalues)): - values.ResultID = resultid - values.CensorCodeCV = censorcodecv - values.QualityCodeCV = qualitycodecv - values.TimeAggregationInterval = timeaggregationinterval - values.TimeAggregationIntervalUnitsID = timeaggregationunit - values.DataValue = datavalues[i] - values.ValueDateTime = datetimes[i] - values.ValueDateTimeUTCOffset = datetimeoffsets[i] - self._session.add(values) - self._session.commit() - return values - except Exception, e: - print e - return None - ''' - + def createSpatialReference(self , spatialref): + self._session.add(spatialref) + self._session.commit() + def createModel(self, model ): + self._session.add(model) + self._session.commit() + def createRelatedModel(self, relatedmodel ): + self._session.add(relatedmodel) + self._session.commit() + def createSimulation(self , simulation): + self._session.add(simulation) + self._session.commit() def createTimeSeriesResultValues(self, datavalues): try: - #using Pandas built-in --slow - #changing way values sent --unknown error on insert - #cols = datavalues.columns.tolist() - #['ValueDateTime', 'DataValue', 'TimeAggregationInterval', 'TimeAggregationIntervalUnitsID', 'QualityCodeCV', 'CensorCodeCV', 'ResultID', 'ValueDateTimeUTCOffset'] - #cols = ['ResultID','DataValue','ValueDateTime','ValueDateTimeUTCOffset','CensorCodeCV','QualityCodeCV','TimeAggregationInterval','TimeAggregationIntervalUnitsID'] - #datavalues = datavalues[cols] - #print datavalues - #datavalues.to_sql(name=TimeSeriesResultValues.__tablename__, + datavalues.to_sql(name="TimeSeriesResultValues", schema=TimeSeriesResultValues.__table_args__['schema'], if_exists='append', @@ -487,172 +84,650 @@ def createTimeSeriesResultValues(self, datavalues): index=False) self._session.commit() - - #using sqlalchemy core --sending empty parameters - # data = datavalues.to_dict('records') - # self._session.execute(TimeSeriesResultValues.__table__.insert(data)) - - #using cursor and StringIO --not all cursors have the copy_from function - # print "using cursor" - # import cStringIO - # #stream the data using 'to_csv' and StringIO(); then use sql's 'copy_from' function - # output = cStringIO.StringIO() - # #ignore the index - # datavalues.to_csv(output, sep='\t', header=False, index=False) - # #jump to start of stream - # output.seek(0) - # contents = output.getvalue() - # connection = self._session_factory.engine.raw_connection() - # cur = connection.cursor() - # #null values become '' - # cur.copy_from(output, 'ODM2.TimeSeriesResultValues', null="") - # connection.commit() - # cur.close() - - #using Bulk Insert * user must have permissions --file created locally code running remote - # datavalues.to_csv('C:\\Users\\Stephanie\\temp.csv') - # sql = """ - # BULK INSERT ODM2.TimeSeriesResultValues - # FROM 'C:\\Users\\Stephanie\\temp.csv' WITH ( - # FIELDTERMINATOR=',', - # ROWTERMINATOR='\\n'); - # """ - # self._session.execute(sql) - - - - return datavalues except Exception, e: print e return None -# ################################################################################ -# Sampling Features -# ################################################################################ - - - def createSite(self, sfId, spatialRefId, vType, latitude, longitude): - """Create Site table - - :param vType: - :type String(255): - :param latitude: - :type Float(53): - :param longitude: - :type Float(53): - :return: - """ - - s = Sites() - s.SamplingFeatureID = sfId - s.SpatialReferenceID = spatialRefId - s.SiteTypeCV = vType - s.Latitude = latitude - s.Longitude = longitude - - self._session.add(s) - self._session.commit() - - return s - - - def createSpatialReference(self, srsCode, srsName, srsDescription=None): - spatialreference = SpatialReferences() - spatialreference.SRSCode = srsCode - spatialreference.SRSName = srsName - spatialreference.SRSDescription = srsDescription - - self._session.add(spatialreference) - self._session.commit() - - return spatialreference # ################################################################################ -# Sensors +# CV # ################################################################################ - def createDeploymentAction(self, actionId, cvType, desc, configActionId, calibActionId, spatialOffSet, - deploymentSchematicLink, **kwargs): - """Create DeploymentAction Object - - :param **kwargs: - :param actionId: - :type Integer: - :param cvType: - :type String(255): - :param desc: - :type String(500): - :param configActionId: - :type Integer: - :param calibActionId: - :type Integer: - :param spatialOffSet: - :type Integer: - :param deploymentSchematicLink: - :type String(255): - :return: - """ - da = DeploymentActions() - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) - da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) # ################################################################################ -# Simulation +# Core # ################################################################################ - - def createModel(self, code, name, description=None): - model = Models() - model.ModelCode = code - model.ModelName = name - model.ModelDescription = description - - self._session.add(model) - self._session.commit() - - return model - - - def createRelatedModel(self, modelid, relatedModelID, relationshipType): - related = RelatedModels() - related.ModelID = modelid - related.RelationshipTypeCV = relationshipType - related.RelatedModelID = relatedModelID - - self._session.add(related) - self._session.commit() - - return related - - - def createSimulation(self, actionid, modelID, simulationName, simulationDescription, simulationStartDateTime, - simulationStartOffset, - simulationEndDateTime, simulationEndOffset, timeStepValue, timeStepUnitID, - inputDatasetID=None): - sim = Simulations() - sim.ActionID = actionid - sim.ModelID = modelID - sim.SimulationName = simulationName - sim.SimulationDescription = simulationDescription - sim.SimulationStartDateTime = simulationStartDateTime - sim.SimulationStartDateTimeUTCOffset = simulationStartOffset - sim.SimulationEndDateTime = simulationEndDateTime - sim.SimulationEndDateTimeUTCOffset = simulationEndOffset - sim.TimeStepValue = timeStepValue - sim.TimeStepUnitsID = timeStepUnitID - sim.InputDatasetID = inputDatasetID - - self._session.add(sim) - self._session.commit() - - return sim - +# def createVariable(self, code, name, vType, nodv, speciation=None, definition=None): +# """ +# +# :param code: +# :type String(50): +# :param name: +# :type String(255): +# :param vType: +# :type String(255): +# :param nodv: +# :type Float(53): +# :param speciation: +# :type String(255): +# :param definition: +# :type String(500): +# :return: +# """ +# var = Variables() +# var.VariableCode = code +# var.VariableNameCV = name +# var.VariableDefinition = definition +# var.VariableTypeCV = vType +# var.NoDataValue = nodv +# var.SpeciationCV = speciation +# +# self._session.add(var) +# self._session.commit() +# +# return var +# +# def createMethod(self, code, name, vType, orgId=None, link=None, description=None): +# """Create Method table for the database +# +# :param code: +# :type String(50): +# :param name: +# :type String(255): +# :param vType: +# :type String(255): +# :param orgId: +# :type Integer: +# :param link: +# :type String(255): +# :param description: +# :type String(500): +# :return: +# """ +# +# meth = Methods() +# meth.MethodCode = code +# meth.MethodName = name +# meth.MethodDescription = description +# meth.MethodTypeCV = vType +# meth.MethodLink = link +# meth.OrganizationID = orgId +# +# self._session.add(meth) +# self._session.commit() +# +# return meth +# +# def createProcessingLevel(self, code, definition=None, explanation=None): +# """Create Processinglevel table for database +# +# :param code: +# :type String(50): +# :param definition: +# :type String(500): +# :param explanation: +# :type String(500): +# :return: +# """ +# pl = ProcessingLevels() +# pl.ProcessingLevelCode = str(code) +# pl.Definition = definition +# pl.Explanation = explanation +# +# self._session.add(pl) +# self._session.commit() +# +# return pl +# +# def createSamplingFeature(self, uuid, code, vType, name=None, description=None, geoType=None, elevation=None, +# elevationDatum=None, featureGeo=None): +# """Create SamplingFeature table +# +# :param code: +# :type String(50): +# :param vType: +# :type String(255): +# :param name: +# :type String(255): +# :param description: +# :type String(500): +# :param geoType: +# :type String(255): +# :param evelation: +# :type Float(53): +# :param evelationDatum: +# :type String(255): +# :param featureGeo: +# :type NullType: +# :return: +# """ +# +# sf = SamplingFeatures() +# sf.SamplingFeatureUUID = uuid +# sf.SamplingFeatureTypeCV = vType +# sf.SamplingFeatureCode = code +# sf.SamplingFeatureName = name +# sf.SamplingFeatureDescription = description +# sf.SamplingFeatureGeoTypeCV = geoType +# sf.Elevation_m = elevation +# sf.ElevationDatumCV = elevationDatum +# sf.FeatureGeometry = featureGeo +# +# self._session.add(sf) +# self._session.commit() +# +# return sf +# +# +# def createUnit(self, type, abbrev, name, link=None): +# """Create Unit table +# +# :param code: +# :type String(255): +# :param abbrev: +# :type String(50): +# :param name: +# :type String(255): +# :return: +# """ +# unit = Units() +# unit.UnitsTypeCV = type +# unit.UnitsAbbreviation = abbrev +# unit.UnitsName = name +# unit.UnitsLink = link +# +# self._session.add(unit) +# self._session.commit() +# +# return unit +# +# def createOrganization(self, cvType, code, name, desc, link, parentOrgId): +# """Create Organization table +# +# :param cvType: +# :type String(255): +# :param code: +# :type String(50): +# :param name: +# :type String(255): +# :param desc: +# :type String(500): +# :param link: +# :type String(255): +# :param parentOrgId: +# :type Integer: +# :return: +# """ +# +# org = Organizations() +# org.OrganizationTypeCV = cvType +# org.OrganizationCode = code +# org.OrganizationName = name +# org.OrganizationDescription = desc +# org.OrganizationLink = link +# org.ParentOrganizationID = parentOrgId +# +# self._session.add(org) +# self._session.commit() +# +# return org +# +# def createPerson(self, firstName, lastName, middleName=""): +# """Create Person Table +# +# :param firstName: +# :type String(255): +# :param lastName: +# :type String(255): +# :param middleName: +# :type String(255): +# :return: +# """ +# +# p = People() +# p.PersonFirstName = firstName +# p.PersonMiddleName = middleName +# p.PersonLastName = lastName +# +# +# +# self._session.add(p) +# self._session.commit() +# +# return p +# +# # def createResult(self, uuid, featureActionId, vType, ): +# +# def createAffiliation(self, personid, organizationid, email, phone=None, address=None, link=None, +# iscontact=False, affiliation_start=dt.datetime.today(), affiliation_end=None): +# """ +# +# :param personid: id of the person record +# :param organizationid: id of the organization record +# :param email: primary email address +# :param phone: primary phone number +# :param address: primary mailing address +# :param link: url pointing the web resource such as researchGate profile +# :param iscontact: indicate if this person is the primary contact for the organization +# :param affiliation_start: begin date of affiliation with organization +# :param affiliation_end: end date of affiliation with organization +# :return: ODM2.Affiliation +# """ +# +# # create affiliation object +# a = Affiliations() +# a.PersonID = personid +# a.OrganizationID = organizationid +# a.PrimaryEmail = email +# a.PrimaryPhone = phone +# a.PrimaryAddress = address +# a.PersonLink = link +# a.IsPrimaryOrganizationContact = iscontact +# a.AffiliationStartDate = affiliation_start +# a.AffiliationEndDate = affiliation_end +# +# self._session.add(a) +# self._session.commit() +# #self._session.flush() +# # self._session.refresh(a) +# +# print a.OrganizationID +# +# return a +# +# def createDataset(self, dstype, dscode, dstitle, dsabstract): +# ds = DataSets() +# +# # create the dataset +# ds.DataSetTypeCV = dstype +# ds.DataSetCode = dscode +# ds.DataSetTitle = dstitle +# ds.DataSetAbstract = dsabstract +# ds.DataSetUUID = uuid.uuid4().hex +# +# self._session.add(ds) +# self._session.commit() +# +# return ds +# +# def createDatasetResults(self, dsid, resultid): +# dsr = DataSetsResults() +# +# # link dataset to results +# dsr.DatasetID = dsid +# dsr.ResultID = resultid +# +# self._session.add(dsr) +# self._session.commit() +# +# return dsr +# +# def createAction(self, type, methodid, begindatetime, begindatetimeoffset, enddatetime=None, enddatetimeoffset=None, +# description=None, filelink=None): +# action = Actions() +# action.ActionTypeCV = type +# action.MethodID = methodid +# action.BeginDateTime = begindatetime +# action.BeginDateTimeUTCOffset = begindatetimeoffset +# action.EndDateTime = enddatetime +# action.EndDateTimeUTCOffset = enddatetimeoffset +# action.ActionDescription = description +# action.ActionFileLink = filelink +# +# self._session.add(action) +# self._session.commit() +# +# return action +# +# def createActionBy(self, actionid, affiliationid, isactionlead=True, roledescription=None): +# actionby = ActionBy() +# actionby.ActionID = actionid +# actionby.AffiliationID = affiliationid +# actionby.IsActionLead = isactionlead +# actionby.RoleDescription = roledescription +# +# self._session.add(actionby) +# self._session.commit() +# +# return actionby +# +# def createFeatureAction(self, samplingfeatureid, actionid): +# featureaction = FeatureActions() +# featureaction.SamplingFeatureID = samplingfeatureid +# featureaction.ActionID = actionid +# +# self._session.add(featureaction) +# self._session.commit() +# +# return featureaction +# +# +# def createResult(self, featureactionid, variableid, unitid, processinglevelid, valuecount, sampledmedium, +# resulttypecv, +# taxonomicclass=None, resultdatetime=None, resultdatetimeutcoffset=None, +# validdatetime=None, validdatetimeutcoffset=None, statuscv=None): +# result = Results() +# #result.ResultUUID = uuid.uuid4().hex +# result.ResultUUID = str(uuid.uuid4()) # Denver +# result.FeatureActionID = featureactionid +# result.ResultTypeCV = resulttypecv +# result.VariableID = variableid +# result.UnitsID = unitid +# result.ProcessingLevelID = processinglevelid +# result.ValueCount = valuecount +# result.SampledMediumCV = sampledmedium +# result.TaxonomicClassifierID = taxonomicclass +# result.ResultDateTime = resultdatetime +# result.ResultDateTimeUTCOffset = resultdatetimeutcoffset +# result.ValidDateTime = validdatetime +# result.ValidDateTimeUTCOffset = validdatetimeutcoffset +# result.StatusCV = statuscv +# +# self._session.add(result) +# self._session.commit() +# +# return result +# +# +# # ################################################################################ +# # Data Quality +# # ################################################################################ +# +# +# +# # ################################################################################ +# # Equipment +# # ################################################################################ +# +# +# +# +# # ################################################################################ +# # ExtensionProperties +# # ################################################################################ +# +# +# +# +# +# # ################################################################################ +# # External Identifiers +# # ################################################################################ +# +# +# +# +# # ################################################################################ +# # Lab Analyses +# # ################################################################################ +# +# +# +# +# # ################################################################################ +# # Provenance +# # ################################################################################ +# +# +# +# +# # ################################################################################ +# # Results +# # ################################################################################ +# +# +# +# def createTimeSeriesResult(self, result, aggregationstatistic, xloc=None, xloc_unitid=None, yloc=None, +# yloc_unitid=None, zloc=None, zloc_unitid=None, +# srsID=None, timespacing=None, timespacing_unitid=None): +# +# tsr = TimeSeriesResults() +# +# tsr.ResultID = result.ResultID +# #tsr.ResultUUID = result.ResultUUID +# +# +# tsr.XLocation = xloc +# tsr.XLocationUnitsID = xloc_unitid +# tsr.YLocation = yloc +# tsr.YLocationUnitsID = yloc_unitid +# tsr.ZLocation = zloc +# tsr.ZLocationUnitsID = zloc_unitid +# tsr.SpatialReferenceID = srsID +# tsr.IntendedTimeSpacing = timespacing +# tsr.IntendedTimeSpacingUnitsID = timespacing_unitid +# tsr.AggregationStatisticCV = aggregationstatistic +# +# +# #tsr.ResultID = result.ResultID +# # tsr.ResultUUID = result.ResultUUID +# # tsr.FeatureActionID = result.FeatureActionID +# # tsr.VariableID = result.VariableID +# # tsr.UnitsID = result.UnitsID +# # tsr.ProcessingLevelID = result.ProcessingLevelID +# # tsr.ValueCount = result.ValueCount +# # tsr.SampledMediumCV = result.SampledMediumCV +# # tsr.ResultTypeCV = result.ResultTypeCV +# +# self._session.add(tsr) +# self._session.commit() +# +# return tsr +# ''' +# def createTimeSeriesResultValues(self, resultid, datavalues, datetimes, datetimeoffsets, censorcodecv, +# qualitycodecv, +# timeaggregationinterval, timeaggregationunit): +# +# +# try: +# values = TimeSeriesResultValues() +# for i in range(len(datavalues)): +# values.ResultID = resultid +# values.CensorCodeCV = censorcodecv +# values.QualityCodeCV = qualitycodecv +# values.TimeAggregationInterval = timeaggregationinterval +# values.TimeAggregationIntervalUnitsID = timeaggregationunit +# values.DataValue = datavalues[i] +# values.ValueDateTime = datetimes[i] +# values.ValueDateTimeUTCOffset = datetimeoffsets[i] +# self._session.add(values) +# self._session.commit() +# return values +# except Exception, e: +# print e +# return None +# ''' +# +# def createTimeSeriesResultValues(self, datavalues): +# try: +# #using Pandas built-in --slow +# #changing way values sent --unknown error on insert +# #cols = datavalues.columns.tolist() +# #['ValueDateTime', 'DataValue', 'TimeAggregationInterval', 'TimeAggregationIntervalUnitsID', 'QualityCodeCV', 'CensorCodeCV', 'ResultID', 'ValueDateTimeUTCOffset'] +# #cols = ['ResultID','DataValue','ValueDateTime','ValueDateTimeUTCOffset','CensorCodeCV','QualityCodeCV','TimeAggregationInterval','TimeAggregationIntervalUnitsID'] +# #datavalues = datavalues[cols] +# #print datavalues +# #datavalues.to_sql(name=TimeSeriesResultValues.__tablename__, +# datavalues.to_sql(name="TimeSeriesResultValues", +# schema=TimeSeriesResultValues.__table_args__['schema'], +# if_exists='append', +# chunksize= 1000, +# con=self._session_factory.engine, +# index=False) +# self._session.commit() +# +# +# #using sqlalchemy core --sending empty parameters +# # data = datavalues.to_dict('records') +# # self._session.execute(TimeSeriesResultValues.__table__.insert(data)) +# +# #using cursor and StringIO --not all cursors have the copy_from function +# # print "using cursor" +# # import cStringIO +# # #stream the data using 'to_csv' and StringIO(); then use sql's 'copy_from' function +# # output = cStringIO.StringIO() +# # #ignore the index +# # datavalues.to_csv(output, sep='\t', header=False, index=False) +# # #jump to start of stream +# # output.seek(0) +# # contents = output.getvalue() +# # connection = self._session_factory.engine.raw_connection() +# # cur = connection.cursor() +# # #null values become '' +# # cur.copy_from(output, 'ODM2.TimeSeriesResultValues', null="") +# # connection.commit() +# # cur.close() +# +# #using Bulk Insert * user must have permissions --file created locally code running remote +# # datavalues.to_csv('C:\\Users\\Stephanie\\temp.csv') +# # sql = """ +# # BULK INSERT ODM2.TimeSeriesResultValues +# # FROM 'C:\\Users\\Stephanie\\temp.csv' WITH ( +# # FIELDTERMINATOR=',', +# # ROWTERMINATOR='\\n'); +# # """ +# # self._session.execute(sql) +# +# +# +# +# return datavalues +# except Exception, e: +# print e +# return None +# +# +# # ################################################################################ +# # Sampling Features +# # ################################################################################ +# +# +# def createSite(self, sfId, spatialRefId, vType, latitude, longitude): +# """Create Site table +# +# :param vType: +# :type String(255): +# :param latitude: +# :type Float(53): +# :param longitude: +# :type Float(53): +# :return: +# """ +# +# s = Sites() +# s.SamplingFeatureID = sfId +# s.SpatialReferenceID = spatialRefId +# s.SiteTypeCV = vType +# s.Latitude = latitude +# s.Longitude = longitude +# +# self._session.add(s) +# self._session.commit() +# +# return s +# +# +# def createSpatialReference(self, srsCode, srsName, srsDescription=None): +# spatialreference = SpatialReferences() +# spatialreference.SRSCode = srsCode +# spatialreference.SRSName = srsName +# spatialreference.SRSDescription = srsDescription +# +# self._session.add(spatialreference) +# self._session.commit() +# +# return spatialreference +# +# +# # ################################################################################ +# # Sensors +# # ################################################################################ +# +# def createDeploymentAction(self, actionId, cvType, desc, configActionId, calibActionId, spatialOffSet, +# deploymentSchematicLink, **kwargs): +# """Create DeploymentAction Object +# +# :param **kwargs: +# :param actionId: +# :type Integer: +# :param cvType: +# :type String(255): +# :param desc: +# :type String(500): +# :param configActionId: +# :type Integer: +# :param calibActionId: +# :type Integer: +# :param spatialOffSet: +# :type Integer: +# :param deploymentSchematicLink: +# :type String(255): +# :return: +# """ +# +# da = DeploymentActions() +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# da.ActionID = (kwargs['actionId'] if kwargs['actionId'] else None) +# +# +# # ################################################################################ +# # Simulation +# # ################################################################################ +# +# +# def createModel(self, code, name, description=None): +# model = Models() +# model.ModelCode = code +# model.ModelName = name +# model.ModelDescription = description +# +# self._session.add(model) +# self._session.commit() +# +# return model +# +# +# def createRelatedModel(self, modelid, relatedModelID, relationshipType): +# related = RelatedModels() +# related.ModelID = modelid +# related.RelationshipTypeCV = relationshipType +# related.RelatedModelID = relatedModelID +# +# self._session.add(related) +# self._session.commit() +# +# return related +# +# +# def createSimulation(self, actionid, modelID, simulationName, simulationDescription, simulationStartDateTime, +# simulationStartOffset, +# simulationEndDateTime, simulationEndOffset, timeStepValue, timeStepUnitID, +# inputDatasetID=None): +# sim = Simulations() +# sim.ActionID = actionid +# sim.ModelID = modelID +# sim.SimulationName = simulationName +# sim.SimulationDescription = simulationDescription +# sim.SimulationStartDateTime = simulationStartDateTime +# sim.SimulationStartDateTimeUTCOffset = simulationStartOffset +# sim.SimulationEndDateTime = simulationEndDateTime +# sim.SimulationEndDateTimeUTCOffset = simulationEndOffset +# sim.TimeStepValue = timeStepValue +# sim.TimeStepUnitsID = timeStepUnitID +# sim.InputDatasetID = inputDatasetID +# +# self._session.add(sim) +# self._session.commit() +# +# return sim +# From 0c73b8169981233f2dd51ffeb6e6d01d2bce2367 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 23 Mar 2016 10:40:47 -0600 Subject: [PATCH 2/9] reformat createservices --- odm2api/ODM2/services/createService.py | 64 ++++++++++++++++---------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/odm2api/ODM2/services/createService.py b/odm2api/ODM2/services/createService.py index 5ffa75b..166e190 100644 --- a/odm2api/ODM2/services/createService.py +++ b/odm2api/ODM2/services/createService.py @@ -3,83 +3,100 @@ import datetime as dt import uuid -#from src.api.ODM2.LikeODM1.model import Site +# from src.api.ODM2.LikeODM1.model import Site from odm2api.ODM2.models import * from odm2api.ODM2 import serviceBase -class CreateODM2( serviceBase): +class CreateODM2(serviceBase): ''' def __init__(self, session): self._session = session ''' -# ################################################################################ -# Annotations -# ################################################################################ + # ################################################################################ + # Annotations + # ################################################################################ def createVariable(self, var): self._session.add(var) self._session.commit() - def createMethod(self, method ): + + def createMethod(self, method): self._session.add(method) self._session.commit() + def createProcessingLevel(self, proclevel): self._session.add(proclevel) self._session.commit() - def createSamplingFeature(self , samplingfeature): + + def createSamplingFeature(self, samplingfeature): self._session.add(samplingfeature) self._session.commit() - def createUnit(self, unit ): + + def createUnit(self, unit): self._session.add(unit) self._session.commit() - def createOrganization(self, org ): + + def createOrganization(self, org): self._session.add(org) self._session.commit() - def createPerson(self, person ): + + def createPerson(self, person): self._session.add(person) self._session.commit() - def createAffiliation(self, affiliation ): + + def createAffiliation(self, affiliation): self._session.add(affiliation) self._session.commit() - def createDataset(self, dataset ): + + def createDataset(self, dataset): self._session.add(dataset) self._session.commit() - def createDatasetResults(self, datasetresult ): + + def createDatasetResults(self, datasetresult): self._session.add(datasetresult) self._session.commit() - def createAction(self, action, actionby): + + def createAction(self, action, actionby): self._session.add(action) self._session.add(actionby) self._session.commit() - def createRelatedAction(self, relatedaction ): + + def createRelatedAction(self, relatedaction): self._session.add(relatedaction) self._session.commit() - def createResult(self, result ): + + def createResult(self, result): self._session.add(result) self._session.commit() - def createResultValues(self, values ): + + def createResultValues(self, values): self._session.add(values) self._session.commit() - def createSpatialReference(self , spatialref): + def createSpatialReference(self, spatialref): self._session.add(spatialref) self._session.commit() - def createModel(self, model ): + + def createModel(self, model): self._session.add(model) self._session.commit() - def createRelatedModel(self, relatedmodel ): + + def createRelatedModel(self, relatedmodel): self._session.add(relatedmodel) self._session.commit() - def createSimulation(self , simulation): + + def createSimulation(self, simulation): self._session.add(simulation) self._session.commit() + def createTimeSeriesResultValues(self, datavalues): try: datavalues.to_sql(name="TimeSeriesResultValues", schema=TimeSeriesResultValues.__table_args__['schema'], if_exists='append', - chunksize= 1000, + chunksize=1000, con=self._session_factory.engine, index=False) self._session.commit() @@ -89,9 +106,6 @@ def createTimeSeriesResultValues(self, datavalues): print e return None - - - # ################################################################################ # CV # ################################################################################ From dfcb7e378fb53d36520ec3f11a7f21175e0b08e5 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 23 Mar 2016 11:24:50 -0600 Subject: [PATCH 3/9] update sample function calls, add inheritance in results, work on get functions with inheritance --- Examples/Sample.py | 7 +- odm2api/ODM2/models.py | 48 ++-- odm2api/ODM2/services/readService.py | 400 ++++++++++++++++----------- 3 files changed, 267 insertions(+), 188 deletions(-) diff --git a/Examples/Sample.py b/Examples/Sample.py index eea771f..dfaf75e 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -74,7 +74,7 @@ sf = read.getSamplingFeatures(code='USU-LBR-Mendon') print sf print "\n-------- Information about an individual SamplingFeature ---------" - print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeatureByCode(): \n" + print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeature(code = x): \n" print "SamplingFeatureCode: " + sf.SamplingFeatureCode print "SamplingFeatureName: " + sf.SamplingFeatureName print "SamplingFeatureDescription: %s" % sf.SamplingFeatureDescription @@ -127,7 +127,7 @@ # Now get a particular Result using a ResultID print "\n------- Example of Retrieving Attributes of a Time Series Result -------" try: - tsResult = read.getTimeSeriesResultByResultId(1) + tsResult = read.getResults(id = 1) print ( "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" + "ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" + @@ -147,8 +147,7 @@ # Get the values for a particular TimeSeriesResult print "\n-------- Example of Retrieving Time Series Result Values ---------" -tsValues = read.getTimeSeriesResultValuesByResultId(1) # Return type is a pandas dataframe -tsValues = read.getResultValues(type = 'timeseries', id = 1) +tsValues = read.getResultValues(resultid = 1) # Return type is a pandas datafram # Print a few Time Series Values to the console # tsValues.set_index('ValueDateTime', inplace=True) diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index b497930..8d7fe86 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -1009,7 +1009,7 @@ def __repr__(self): % (self.SpatialReferenceID, self.SRSCode, self.SRSName, self.SRSDescription, self.SRSLink) -class Specimens(Base): +class Specimens(SamplingFeatures): __tablename__ = u'specimens' __table_args__ = {u'schema': 'odm2'} @@ -1019,7 +1019,7 @@ class Specimens(Base): SpecimenMediumCV = Column('specimenmediumcv', ForeignKey(CVMediumType.Name), nullable=False, index=True) IsFieldSpecimen = Column('isfieldspecimen', Boolean, nullable=False) - SamplingFeatureObj = relationship(SamplingFeatures) + # SamplingFeatureObj = relationship(SamplingFeatures) class SpatialOffsets(Base): @@ -1041,7 +1041,7 @@ class SpatialOffsets(Base): Offset3UnitObj = relationship(Units, primaryjoin='SpatialOffsets.Offset3UnitID == Units.UnitsID') -class Sites(Base): +class Sites(SamplingFeatures): __tablename__ = u'sites' __table_args__ = {u'schema': 'odm2'} @@ -1054,7 +1054,7 @@ class Sites(Base): Longitude = Column('longitude', Float(53), nullable=False) SpatialReferenceObj = relationship(SpatialReferences) - SamplingFeatureObj = relationship(SamplingFeatures) + # SamplingFeatureObj = relationship(SamplingFeatures) def __repr__(self): return "" \ @@ -1758,7 +1758,7 @@ class RelatedResults(Base): # ################################################################################ -class PointCoverageResults(Base): +class PointCoverageResults(Results): __tablename__ = u'pointcoverageresults' __table_args__ = {u'schema': 'odm2'} @@ -1779,10 +1779,10 @@ class PointCoverageResults(Base): YUnitObj = relationship(Units, primaryjoin='PointCoverageResults.IntendedYSpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) ZUnitObj = relationship(Units, primaryjoin='PointCoverageResults.ZLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='PointCoverageResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='PointCoverageResults.ResultID == Results.ResultID') -class ProfileResults(Base): +class ProfileResults(Results): __tablename__ = u'profileresults' __table_args__ = {u'schema': 'odm2'} @@ -1804,10 +1804,10 @@ class ProfileResults(Base): SpatialReferenceObj = relationship(SpatialReferences) XUnitObj = relationship(Units, primaryjoin='ProfileResults.XLocationUnitsID == Units.UnitsID') YUnitObj = relationship(Units, primaryjoin='ProfileResults.YLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='ProfileResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='ProfileResults.ResultID == Results.ResultID') -class CategoricalResults(Base): +class CategoricalResults(Results): __tablename__ = u'categoricalresults' __table_args__ = {u'schema': 'odm2'} @@ -1822,10 +1822,10 @@ class CategoricalResults(Base): QualityCodeCV = Column('qualitycodecv', ForeignKey(CVQualityCode.Name), nullable=False, index=True) SpatialReferenceObj = relationship(SpatialReferences) - ResultObj = relationship(Results, primaryjoin='CategoricalResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='CategoricalResults.ResultID == Results.ResultID') -class TransectResults(Base): +class TransectResults(Results): __tablename__ = u'transectresults' __table_args__ = {u'schema': 'odm2'} @@ -1844,10 +1844,10 @@ class TransectResults(Base): TransectUnitObj = relationship(Units, primaryjoin='TransectResults.IntendedTransectSpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) ZUnitObj = relationship(Units, primaryjoin='TransectResults.ZLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='TransectResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='TransectResults.ResultID == Results.ResultID') -class SpectraResults(Base): +class SpectraResults(Results): __tablename__ = u'spectraresults' __table_args__ = {u'schema': 'odm2'} @@ -1869,10 +1869,10 @@ class SpectraResults(Base): XUnitObj = relationship(Units, primaryjoin='SpectraResults.XLocationUnitsID == Units.UnitsID') YUnitObj = relationship(Units, primaryjoin='SpectraResults.YLocationUnitsID == Units.UnitsID') ZUnitObj = relationship(Units, primaryjoin='SpectraResults.ZLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='SpectraResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='SpectraResults.ResultID == Results.ResultID') -class TimeSeriesResults(Base): +class TimeSeriesResults(Results): __tablename__ = u'timeseriesresults' __table_args__ = {u'schema': 'odm2'} @@ -1889,14 +1889,14 @@ class TimeSeriesResults(Base): AggregationStatisticCV = Column('aggregationstatisticcv', ForeignKey(CVAggregationStatistic.Name), nullable=False, index=True) - ResultObj = relationship(Results) + # ResultObj = relationship(Results) IntendedTimeSpacingUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.IntendedTimeSpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) XLocationUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.XLocationUnitsID == Units.UnitsID') YLocationUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.YLocationUnitsID == Units.UnitsID') ZLocationUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.ZLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='TimeSeriesResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='TimeSeriesResults.ResultID == Results.ResultID') def __repr__(self): return "" % \ @@ -1905,7 +1905,7 @@ def __repr__(self): self.IntendedTimeSpacing, self.AggregationStatisticCV) -class SectionResults(Base): +class SectionResults(Results): __tablename__ = u'sectionresults' __table_args__ = {u'schema': 'odm2'} @@ -1927,10 +1927,10 @@ class SectionResults(Base): ZUnitObj = relationship(Units, primaryjoin='SectionResults.IntendedZSpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) YUnitObj = relationship(Units, primaryjoin='SectionResults.YLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='SectionResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='SectionResults.ResultID == Results.ResultID') -class TrajectoryResults(Base): +class TrajectoryResults(Results): __tablename__ = u'trajectoryresults' __table_args__ = {u'schema': 'odm2'} @@ -1947,10 +1947,10 @@ class TrajectoryResults(Base): TrajectoryUnitObj = relationship(Units, primaryjoin='TrajectoryResults.IntendedTrajectorySpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) - ResultObj = relationship(Results, primaryjoin='TrajectoryResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='TrajectoryResults.ResultID == Results.ResultID') -class MeasurementResults(Base): +class MeasurementResults(Results): __tablename__ = u'measurementresults' __table_args__ = {u'schema': 'odm2'} @@ -1975,12 +1975,12 @@ class MeasurementResults(Base): XLocationUnitsObj = relationship(Units, primaryjoin='MeasurementResults.XLocationUnitsID == Units.UnitsID') YLocationUnitsObj = relationship(Units, primaryjoin='MeasurementResults.YLocationUnitsID == Units.UnitsID') ZLocationUnitsObj = relationship(Units, primaryjoin='MeasurementResults.ZLocationUnitsID == Units.UnitsID') - ResultObj = relationship(Results, primaryjoin='MeasurementResults.ResultID == Results.ResultID') + # ResultObj = relationship(Results, primaryjoin='MeasurementResults.ResultID == Results.ResultID') def __repr__(self): return "" % \ (self.ResultID, self.XLocation, self.YLocation, self.XLocation, - self.ResultObj, self.XLocationUnitsObj, self.SpatialReferenceObj, + self.ResultTypeCV, self.XLocationUnitsObj, self.SpatialReferenceObj, self.AggregationStatisticCV) diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 705456f..837cbd6 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -52,28 +52,41 @@ def __init__(self, session): def getAnnotations(self, type=None): - #TODO What keywords do I use for type + # TODO What keywords do I use for type a = Annotations - if type =="action": a=ActionAnnotations - elif type =="categoricalresultvalue": a=CategoricalResultValueAnnotations - elif type =="equipmentannotation": a=EquipmentAnnotations - elif type =="measurementresultvalue": a=MeasurementResultValueAnnotations - elif type =="method": a=MethodAnnotations - elif type =="pointcoverageresultvalue": a=PointCoverageResultValueAnnotations - elif type =="profileresultvalue": a= ProfileResultValueAnnotations - elif type =="result": a = ResultAnnotations - elif type =="samplingfeature": a=SamplingFeatureAnnotations - elif type =="sectionresultvalue": a=SectionResultValueAnnotations - elif type =="spectraresultvalue": a=SpectraResultValueAnnotations - elif type =="timeSeriesresultvalue": a=TimeSeriesResultValueAnnotations - elif type =="trajectoryresultvalue": a= TrajectoryResultValueAnnotations - elif type =="transectresultvalue": a= TransectResultValueAnnotations + if type == "action": + a = ActionAnnotations + elif type == "categoricalresultvalue": + a = CategoricalResultValueAnnotations + elif type == "equipmentannotation": + a = EquipmentAnnotations + elif type == "measurementresultvalue": + a = MeasurementResultValueAnnotations + elif type == "method": + a = MethodAnnotations + elif type == "pointcoverageresultvalue": + a = PointCoverageResultValueAnnotations + elif type == "profileresultvalue": + a = ProfileResultValueAnnotations + elif type == "result": + a = ResultAnnotations + elif type == "samplingfeature": + a = SamplingFeatureAnnotations + elif type == "sectionresultvalue": + a = SectionResultValueAnnotations + elif type == "spectraresultvalue": + a = SpectraResultValueAnnotations + elif type == "timeSeriesresultvalue": + a = TimeSeriesResultValueAnnotations + elif type == "trajectoryresultvalue": + a = TrajectoryResultValueAnnotations + elif type == "transectresultvalue": + a = TransectResultValueAnnotations try: return self._session.query(a).all() except: return None - # ################################################################################ # CV # ############################################################################## @@ -81,37 +94,64 @@ def getAnnotations(self, type=None): def getCVs(self, type): CV = CVActionType - if type == "actiontype": CV = CVActionType - elif type == "aggregationstatistic": CV = CVAggregationStatistic - elif type == "annotationtype": CV = CVAnnotationType - elif type == "censorcode": CV = CVCensorCode - elif type == "dataqualitytype": CV = CVDataQualityType - elif type == "dataset type": CV = CVDataSetType - elif type == "Directive Type": CV = CVDirectiveType - elif type == "Elevation Datum": CV = CVElevationDatum - elif type == "Equipment Type": CV = CVEquipmentType - elif type == "Medium": CV = CVMediumType - elif type == "Method Type": CV = CVMethodType - elif type == "Organization Type": CV = CVOrganizationType - elif type == "Property Data Type": CV = CVPropertyDataType - elif type == "Quality Code": CV = CVQualityCode - elif type == "Relationship Type": CV = CVRelationshipType - elif type == "Result Type": CV = CVResultType - elif type == "Sampling Feature Geo-type": CV = CVSamplingFeatureGeoType - elif type == "Sampling Feature Type": CV = CVSamplingFeatureType - elif type == "Site Type": CV = CVSiteType - elif type == "Spatial Offset Type": CV = CVSpatialOffsetType - elif type == "Speciation": CV = CVSpeciation - elif type == "Specimen Type": CV = CVSpecimenType - elif type == "Status": CV = CVStatus - elif type == "Taxonomic Classifier Type": CV = CVTaxonomicClassifierType - elif type == "Units Type": CV = CVUnitsType - elif type == "Variable Name": CV = CVVariableName - elif type == "Variable Type": CV = CVVariableType - else: return None + if type == "actiontype": + CV = CVActionType + elif type == "aggregationstatistic": + CV = CVAggregationStatistic + elif type == "annotationtype": + CV = CVAnnotationType + elif type == "censorcode": + CV = CVCensorCode + elif type == "dataqualitytype": + CV = CVDataQualityType + elif type == "dataset type": + CV = CVDataSetType + elif type == "Directive Type": + CV = CVDirectiveType + elif type == "Elevation Datum": + CV = CVElevationDatum + elif type == "Equipment Type": + CV = CVEquipmentType + elif type == "Medium": + CV = CVMediumType + elif type == "Method Type": + CV = CVMethodType + elif type == "Organization Type": + CV = CVOrganizationType + elif type == "Property Data Type": + CV = CVPropertyDataType + elif type == "Quality Code": + CV = CVQualityCode + elif type == "Relationship Type": + CV = CVRelationshipType + elif type == "Result Type": + CV = CVResultType + elif type == "Sampling Feature Geo-type": + CV = CVSamplingFeatureGeoType + elif type == "Sampling Feature Type": + CV = CVSamplingFeatureType + elif type == "Site Type": + CV = CVSiteType + elif type == "Spatial Offset Type": + CV = CVSpatialOffsetType + elif type == "Speciation": + CV = CVSpeciation + elif type == "Specimen Type": + CV = CVSpecimenType + elif type == "Status": + CV = CVStatus + elif type == "Taxonomic Classifier Type": + CV = CVTaxonomicClassifierType + elif type == "Units Type": + CV = CVUnitsType + elif type == "Variable Name": + CV = CVVariableName + elif type == "Variable Type": + CV = CVVariableType + else: + return None return self._session.query(CV).all() - # ################################################################################ # Core # ################################################################################ @@ -209,7 +249,7 @@ def getProcessingLevels(self, id=None, code=None): Sampling Feature """ - def getSamplingFeatures(self, id=None, code=None, type=None, wkt=None): + def getSamplingFeatures(self, id=None, code=None, uuid=None, type=None, wkt=None): """ * Pass nothing - returns a list of all sampling feature objects with each object of type specific to that sampling feature * Pass a SamplingFeatureID - returns a single sampling feature object @@ -218,9 +258,15 @@ def getSamplingFeatures(self, id=None, code=None, type=None, wkt=None): * Pass a SamplingFeatureGeometry(TYPE????) - return a list of sampling feature objects """ - q = self._session.query(SamplingFeatures) + s = SamplingFeatures + if type.lower() == 'site': + s = Sites + elif type.lower() == 'specimen': + s = Specimens - if type: q = q.filter_by(SamplingFeatureTypeCV=type) + q = self._session.query(s) + + # if type: q = q.filter_by(SamplingFeatureTypeCV=type) if id: q = q.filter_by(SamplingFeatureID=id) if code: q = q.filter_by(SamplingFeatureCode=code) if wkt: q = q.filter_by(FeatureGeometryWKT=wkt) @@ -237,25 +283,28 @@ def getRelatedSamplingFeatures(self, id): * Pass a SamplingFeatureID - get a list of sampling feature objects related to the input sampling feature along with the relationship type """ - sf= self._session.query(SamplingFeatures).select_from(RelatedFeatures).join(RelatedFeatures.RelatedFeatureObj) - if id: sf= sf.filter(RelatedFeatures.RelatedFeatureID == id) + sf = self._session.query(SamplingFeatures).select_from(RelatedFeatures).join(RelatedFeatures.RelatedFeatureObj) + if id: sf = sf.filter(RelatedFeatures.RelatedFeatureID == id) return sf.all() """ Action """ - def getActions(self, id = None, type=None, sfid=None): - a=Actions - if type =="equipment": a=EquipmentActions - elif type =="calibration": a=CalibrationActions - elif type =="maintenance": a=MaintenanceActions - - q= self._session.query(Actions) - if id: q= q.filter_by(ActionID=id) + def getActions(self, id=None, type=None, sfid=None): + a = Actions + if type == "equipment": + a = EquipmentActions + elif type == "calibration": + a = CalibrationActions + elif type == "maintenance": + a = MaintenanceActions + + q = self._session.query(Actions) + if id: q = q.filter_by(ActionID=id) # if type: q=q.filter_by(Actions.ActionTypeCV.ilike(type)) if sfid: - q=q.join(FeatureActions).filter(FeatureActions.SamplingFeatureID == sfid) + q = q.join(FeatureActions).filter(FeatureActions.SamplingFeatureID == sfid) # return self._session.query(Results) \ # .join(FeatureActions) \ # .join(Actions) \ @@ -273,8 +322,8 @@ def getRelatedActions(self, actionid=None): * Pass an ActionID - get a list of Action objects related to the input action along with the relatinship type """ - q= self._session.query(Actions).select_from(RelatedActions).join(RelatedActions.RelatedActionObj) - if actionid: q= q.filter(RelatedActions.ActionID ==actionid) + q = self._session.query(Actions).select_from(RelatedActions).join(RelatedActions.RelatedActionObj) + if actionid: q = q.filter(RelatedActions.ActionID == actionid) return q.all() @@ -290,7 +339,6 @@ def getUnits(self, id=None, name=None, type=None): * Pass UnitsName - returns a single units object """ - q = self._session.query(Units) if id: q = q.filter_by(UnitsID=id) if name: q = q.filter(Units.UnitsName.ilike(name)) @@ -300,13 +348,11 @@ def getUnits(self, id=None, name=None, type=None): except: return None - - """ Organization """ - def getOrganizations(self, id =None, code =None): + def getOrganizations(self, id=None, code=None): """Select all on Organization :return Organization Objects: @@ -314,19 +360,17 @@ def getOrganizations(self, id =None, code =None): """ q = self._session.query(Organizations) if id: q = q.filter_by(OrganizationID=id) - if code:q = q.filter_by(OrganizationCode=code) + if code: q = q.filter_by(OrganizationCode=code) try: return q.all() except: return None - - """ Person """ - def getPeople(self, id =None, firstname=None, lastname=None): + def getPeople(self, id=None, firstname=None, lastname=None): """Select all on Person :return Person Objects: @@ -341,8 +385,7 @@ def getPeople(self, id =None, firstname=None, lastname=None): except: return None - - def getAffiliations(self, id, personfirst=None, personlast=None, orgcode=None): + def getAffiliations(self, id, personfirst=None, personlast=None, orgcode=None): """ Select all affiliation of person :param personfirst: first name of person @@ -350,8 +393,8 @@ def getAffiliations(self, id, personfirst=None, personlast=None, orgcode=None): :param orgcode: organization code (e.g. uwrl) :return: ODM2.Affiliation """ - q=self._session.query(Affiliations) - if id: q=q.filter(AffiliationID = id) + q = self._session.query(Affiliations) + if id: q = q.filter(AffiliationID=id) if orgcode: q = q.filter(Organizations.OrganizationCode.ilike(orgcode)) if personfirst: q = q.filter(People.PersonFirstName.ilike(personfirst)) if personlast: q = q.filter(People.PersonLastName.ilike(personlast)).first() @@ -364,9 +407,9 @@ def getAffiliations(self, id, personfirst=None, personlast=None, orgcode=None): Results """ - def getResults(self, id=None, actionid = None, type=None): + def getResults(self, id=None, actionid=None, type=None): - #TODO what if user sends in both type and actionid vs just actionid + # TODO what if user sends in both type and actionid vs just actionid """Select by variableId :param id: @@ -376,20 +419,27 @@ def getResults(self, id=None, actionid = None, type=None): """ R = Results if type is not None: - if type == "categorical": R = CategoricalResults - # elif "countObservation": R= - elif type == "measurement": R = MeasurementResults - elif type == "pointcoverage":R = PointCoverageResults - elif type == "profile": R = ProfileResults - elif type == "section": R = SectionResults - elif type == "spectra": R = SpectraResults - elif type == "timeseries": R = TimeSeriesResults - elif type == "trajectory": R = TrajectoryResults - elif type == "transect": R = TransectResults - # elif "truthObservation": R= + if type == "categorical": + R = CategoricalResults + elif type == "measurement": + R = MeasurementResults + elif type == "pointcoverage": + R = PointCoverageResults + elif type == "profile": + R = ProfileResults + elif type == "section": + R = SectionResults + elif type == "spectra": + R = SpectraResults + elif type == "timeseries": + R = TimeSeriesResults + elif type == "trajectory": + R = TrajectoryResults + elif type == "transect": + R = TransectResults query = self._session.query(R) - if actionid: query = query.join(FeatureActions).filter_by(ActionID = actionid) + if actionid: query = query.join(FeatureActions).filter_by(ActionID=actionid) if id: query = query.filter_by(ResultID=id) # if type: query=query.filter_by(ResultTypeCV=type) @@ -397,6 +447,7 @@ def getResults(self, id=None, actionid = None, type=None): return query.all() except: return None + # # # def getResultValidDateTime(self, resultId): @@ -437,7 +488,6 @@ def getDataSets(self, code=None): except: return None - # ################################################################################ # Data Quality # ################################################################################ @@ -449,13 +499,17 @@ def getDataQuality(self): :type list: """ return self._session.query(DataQuality).all() - #TODO DataQuality Schema Queries + + # TODO DataQuality Schema Queries def getReferenceMaterials(self): return self._session.query(ReferenceMaterials).all() + def getReferenceMaterialValues(self): return self._session.query(ReferenceMaterialValues).all() + def getResultNormalizationValues(self): return self._session.query(ResultNormalizationValues).all() + def getResultsDataQuality(self): return self._session.query(ResultsDataQuality).all() @@ -464,41 +518,47 @@ def getResultsDataQuality(self): # ################################################################################ - #TODO Equipment Schema Queries - def getEquipment(self, code=None, type = None , sfid=None, actionid=None): + # TODO Equipment Schema Queries + def getEquipment(self, code=None, type=None, sfid=None, actionid=None): e = self._session.query(Equipment) - if sfid: e= e.join(EquipmentUsed)\ - .join(Actions)\ - .join(FeatureActions)\ + if sfid: e = e.join(EquipmentUsed) \ + .join(Actions) \ + .join(FeatureActions) \ .filter(FeatureActions.SamplingFeatureID == sfid) - if actionid: e=e.join(EquipmentUsed).join(Actions)\ + if actionid: e = e.join(EquipmentUsed).join(Actions) \ .filter(Actions.ActionID == actionid) return e.all() def CalibrationReferenceEquipment(self): return self._session.query(CalibrationReferenceEquipment).all() + def CalibrationStandards(self): return self._session.query(CalibrationStandards).all() + def DataloggerFileColumns(self): return self._session.query(DataLoggerFileColumns).all() + def DataLoggerFiles(self): return self._session.query(DataLoggerFiles).all() + def DataloggerProgramFiles(self): return self._session.query(DataLoggerProgramFiles).all() + def EquipmentModels(self): return self._session.query(EquipmentModels).all() + def EquipmentUsed(self): return self._session.query(EquipmentUsed).all() + def InstrumentOutputVariables(self, modelid=None, variableid=None): - i=self._session.query(InstrumentOutputVariables) - if modelid: i=i.filter_by(ModelID=modelid) - if variableid: i=i.filter_by(VariableID= variableid) + i = self._session.query(InstrumentOutputVariables) + if modelid: i = i.filter_by(ModelID=modelid) + if variableid: i = i.filter_by(VariableID=variableid) return i.all() - def RelatedEquipment(self, code=None): - r=self._session.query(RelatedEquipment) - if code: r=r.filter_by(EquipmentCode=code) + r = self._session.query(RelatedEquipment) + if code: r = r.filter_by(EquipmentCode=code) return r.all() # ################################################################################ @@ -506,76 +566,94 @@ def RelatedEquipment(self, code=None): # ################################################################################ def getExtensionProperties(self, type=None): - #Todo what values to use for extensionproperties type + # Todo what values to use for extensionproperties type e = ExtensionProperties - if type =="action": e = ActionExtensionPropertyValues - elif type =="citation": e= CitationExtensionPropertyValues - elif type =="method": e= MethodExtensionPropertyValues - elif type =="result":e=ResultExtensionPropertyValues - elif type =="samplingfeature": e= SamplingFeatureExtensionPropertyValues - elif type =="variable": e=VariableExtensionPropertyValues + if type == "action": + e = ActionExtensionPropertyValues + elif type == "citation": + e = CitationExtensionPropertyValues + elif type == "method": + e = MethodExtensionPropertyValues + elif type == "result": + e = ResultExtensionPropertyValues + elif type == "samplingfeature": + e = SamplingFeatureExtensionPropertyValues + elif type == "variable": + e = VariableExtensionPropertyValues try: return self._session.query(e).all() except: return None - # ################################################################################ # External Identifiers # ################################################################################ def getExternalIdentifiers(self, type=None): e = ExternalIdentifierSystems - if type.lowercase =="citation": e = CitationExternalIdentifiers - elif type =="method": e = MethodExternalIdentifiers - elif type =="person": e = PersonExternalIdentifiers - elif type =="referencematerial": e = ReferenceMaterialExternalIdentifiers - elif type =="samplingfeature": e = SamplingFeatureExternalIdentifiers - elif type =="spatialreference": e = SpatialReferenceExternalIdentifiers - elif type =="taxonomicclassifier": e = TaxonomicClassifierExternalIdentifiers - elif type =="variable": e = VariableExternalIdentifiers + if type.lowercase == "citation": + e = CitationExternalIdentifiers + elif type == "method": + e = MethodExternalIdentifiers + elif type == "person": + e = PersonExternalIdentifiers + elif type == "referencematerial": + e = ReferenceMaterialExternalIdentifiers + elif type == "samplingfeature": + e = SamplingFeatureExternalIdentifiers + elif type == "spatialreference": + e = SpatialReferenceExternalIdentifiers + elif type == "taxonomicclassifier": + e = TaxonomicClassifierExternalIdentifiers + elif type == "variable": + e = VariableExternalIdentifiers try: return self._session.query(e).all() except: return None - # ################################################################################ # Lab Analyses # ################################################################################ - #TODO functions for Lab Analyses + # TODO functions for Lab Analyses def getDirectives(self): return self._session.query(Directives).all() + def getActionDirectives(self): return self._session.query(ActionDirectives).all() + def getSpecimenBatchPositions(self): return self._session.query(SpecimenBatchPositions).all() - - # ################################################################################ # Provenance # ################################################################################ - #TODO functions for Provenance + # TODO functions for Provenance def getAuthorLists(self): self._session.query(AuthorLists).all() def getDatasetCitations(self): self._session.query(DataSetCitations).all() + def getDerivationEquations(self): self._session.query(DerivationEquations).all() + def getMethodCitations(self): self._session.query(MethodCitations).all() def getRelatedAnnotations(self): - #q= read._session.query(Actions).select_from(RelatedActions).join(RelatedActions.RelatedActionObj) + # q= read._session.query(Actions).select_from(RelatedActions).join(RelatedActions.RelatedActionObj) self._session.query(RelatedAnnotations).all() + def getRelatedCitations(self): self._session.query(RelatedCitations).all() + def getRelatedDatasets(self): self._session.query(RelatedDataSets).all() + def getRelatedResults(self): self._session.query(RelatedResults).all() + def getResultDerivationEquations(self): self._session.query(ResultDerivationEquations).all() @@ -588,9 +666,7 @@ def getResultDerivationEquations(self): ResultValues """ - - - def getResultValues(self, resultid= None, type=None, starttime=None, endtime=None): + def getResultValues(self, resultid=None, starttime=None, endtime=None): """Select all on TimeSeriesResults getResultValues() @@ -599,34 +675,42 @@ def getResultValues(self, resultid= None, type=None, starttime=None, endtime=Non NOTE: Another option here would be to put a flag on getResults that specifies whether values should be returned :return TimeSeriesResultsValue Objects: :type list: + + """ - Result=TimeSeriesResults - - if type == "categorical": Result = CategoricalResultValues - elif type == "measurement": Result = MeasurementResultValues - elif type == "pointsoverage": Result = PointCoverageResultValues - elif type == "profile": Result = ProfileResultValues - elif type == "section": Result = SectionResults - elif type == "spectra": Result = SpectraResultValues - elif type == "timeseries": Result = TimeSeriesResultValues - elif type == "trajectory": Result = TrajectoryResultValues - elif type == "transect": Result = TransectResultValues - - - q = self._session.query(Result) - if resultid: q = q.filter_by(ResultID=resultid) - if starttime: q= q.filter(Result.ValueDateTime >= starttime) - if endtime: q=q.filter(Result.ValueDateTime <= endtime) + type = self._session.query(Results).filter_by(ResultID=resultid).first().ResultTypeCV + Result = TimeSeriesResults + + if "categorical" in type.lower(): + Result = CategoricalResultValues + elif "measurement" in type.lower(): + Result = MeasurementResultValues + elif "coverage" in type.lower(): + Result = PointCoverageResultValues + elif "profile" in type.lower(): + Result = ProfileResultValues + elif "section" in type.lower(): + Result = SectionResults + elif "spectra" in type.lower(): + Result = SpectraResultValues + elif "time" in type.lower(): + Result = TimeSeriesResultValues + elif "trajectory" in type.lower(): + Result = TrajectoryResultValues + elif "transect" in type.lower(): + Result = TransectResultValues + + q = self._session.query(Result).filter_by(ResultID=id) + if starttime: q = q.filter(Result.ValueDateTime >= starttime) + if endtime: q = q.filter(Result.ValueDateTime <= endtime) try: - q=q.order_by(Result.ValueDateTime).all() + q = q.order_by(Result.ValueDateTime).all() df = pd.DataFrame([dv.list_repr() for dv in q.all()]) df.columns = q[0].get_columns() return df except: return None - - # ################################################################################ # SamplingFeatures # ################################################################################ @@ -634,7 +718,7 @@ def getResultValues(self, resultid= None, type=None, starttime=None, endtime=Non """ Site """ - #ToDo function for get sampling features + # ToDo function for get sampling features def getSites(self): """Select all on Sites @@ -700,13 +784,14 @@ def getSimulations(self, name=None, actionid=None): :return: :rtype: """ - s=self._session.query(Simulations) + s = self._session.query(Simulations) if name: s = s.filter(Simulations.SimulationName.ilike(name)) - if actionid: s= s.filter_by(ActionID=actionid) + if actionid: s = s.filter_by(ActionID=actionid) try: return s.all() except: return None + # # def getResultsBySimulationID(self, simulationid): # try: @@ -719,16 +804,15 @@ def getSimulations(self, name=None, actionid=None): # print e # return None - def getModels(self, code= None): - m= self._session.query(Models) + def getModels(self, code=None): + m = self._session.query(Models) if code: m = m.filter(Models.ModelCode.ilike(code)) try: return m.all() except: return None - - def getRelatedModels(self, id = None, code = None): + def getRelatedModels(self, id=None, code=None): """ getRelatedModels() * Pass a ModelID - get a list of model objects related to the model having ModelID @@ -741,10 +825,9 @@ def getRelatedModels(self, id = None, code = None): :rtype: """ - - m=self._session.query(Models).select_from(RelatedModels).join(RelatedModels.RelatedModelObj) - if id: m= m.filter(RelatedModels.ModelID==id) - if code: m= m.filter(RelatedModels.ModelCode == code) + m = self._session.query(Models).select_from(RelatedModels).join(RelatedModels.RelatedModelObj) + if id: m = m.filter(RelatedModels.ModelID == id) + if code: m = m.filter(RelatedModels.ModelCode == code) try: return m.all() @@ -753,9 +836,6 @@ def getRelatedModels(self, id = None, code = None): return None - - - # ################################################################################ # ODM2 # ################################################################################ From d14b3cf7478d52e6cf0d3b0cda00565f5fc53327 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 23 Mar 2016 12:20:05 -0600 Subject: [PATCH 4/9] implement polymorphic identity for SF and Results --- odm2api/ODM2/models.py | 33 ++++++++++++++++++++++--- odm2api/ODM2/services/readService.py | 36 ++++++---------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index 8d7fe86..034c5f6 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -570,6 +570,11 @@ class SamplingFeatures(Base): FeatureGeometry = Column('featuregeometry', Geometry) # String(50))# # FeatureGeometryWKT = Column('featuregeometrywkt', String(50)) # FeatureGeometry = Column('featuregeometry', BLOB) # custom geometry queries + __mapper_args__ = { + 'polymorphic_identity':'samplingfeatures', + 'polymorphic_on': SamplingFeatureTypeCV, + 'with_polymorphic':'*' + } def shape(self): """ @@ -750,6 +755,12 @@ class Results(Base): UnitsObj = relationship(Units) VariableObj = relationship(Variables) + __mapper_args__ = { + 'polymorphic_on':ResultTypeCV, + 'polymorphic_identity':'results', + 'with_polymorphic':'*' + } + def __repr__(self): return "" % ( self.ResultID, self.ResultUUID, self.ResultTypeCV, self.ProcessingLevelID, self.ValueCount) @@ -1020,6 +1031,9 @@ class Specimens(SamplingFeatures): IsFieldSpecimen = Column('isfieldspecimen', Boolean, nullable=False) # SamplingFeatureObj = relationship(SamplingFeatures) + __mapper_args__ = { + 'polymorphic_identity':'Specimen', + } class SpatialOffsets(Base): @@ -1056,10 +1070,13 @@ class Sites(SamplingFeatures): SpatialReferenceObj = relationship(SpatialReferences) # SamplingFeatureObj = relationship(SamplingFeatures) + __mapper_args__ = { + 'polymorphic_identity':'Site', + } def __repr__(self): return "" \ % (self.SamplingFeatureID, self.SpatialReferenceID, self.SiteTypeCV, self.Latitude, self.Longitude, - self.SpatialReferenceObj, self.SamplingFeatureObj) + self.SpatialReferenceObj, self.SamplingFeatureCode) class RelatedFeatures(Base): @@ -1780,6 +1797,8 @@ class PointCoverageResults(Results): SpatialReferenceObj = relationship(SpatialReferences) ZUnitObj = relationship(Units, primaryjoin='PointCoverageResults.ZLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='PointCoverageResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Point coverage'} + class ProfileResults(Results): @@ -1805,6 +1824,7 @@ class ProfileResults(Results): XUnitObj = relationship(Units, primaryjoin='ProfileResults.XLocationUnitsID == Units.UnitsID') YUnitObj = relationship(Units, primaryjoin='ProfileResults.YLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='ProfileResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Profile Coverage'} class CategoricalResults(Results): @@ -1823,6 +1843,7 @@ class CategoricalResults(Results): SpatialReferenceObj = relationship(SpatialReferences) # ResultObj = relationship(Results, primaryjoin='CategoricalResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Category coverage'} class TransectResults(Results): @@ -1845,6 +1866,7 @@ class TransectResults(Results): SpatialReferenceObj = relationship(SpatialReferences) ZUnitObj = relationship(Units, primaryjoin='TransectResults.ZLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='TransectResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Transect Coverage'} class SpectraResults(Results): @@ -1870,6 +1892,7 @@ class SpectraResults(Results): YUnitObj = relationship(Units, primaryjoin='SpectraResults.YLocationUnitsID == Units.UnitsID') ZUnitObj = relationship(Units, primaryjoin='SpectraResults.ZLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='SpectraResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Spectra coverage'} class TimeSeriesResults(Results): @@ -1897,11 +1920,12 @@ class TimeSeriesResults(Results): YLocationUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.YLocationUnitsID == Units.UnitsID') ZLocationUnitsObj = relationship(Units, primaryjoin='TimeSeriesResults.ZLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='TimeSeriesResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Time series coverage'} def __repr__(self): return "" % \ (self.ResultID, self.XLocation, self.YLocation, self.XLocation, - self.ResultObj, self.XLocationUnitsObj, self.SpatialReferenceObj, + self.ResultTypeCV, self.XLocationUnitsObj, self.SpatialReferenceObj, self.IntendedTimeSpacing, self.AggregationStatisticCV) @@ -1914,7 +1938,7 @@ class SectionResults(Results): YLocationUnitsID = Column('ylocationunitsid', ForeignKey(Units.UnitsID)) SpatialReferenceID = Column('spatialreferenceid', ForeignKey(SpatialReferences.SpatialReferenceID)) IntendedXSpacing = Column('intendedxspacing', Float(53)) - IntendedXSpacingUnitsID = Column('intendedxpacingunitsid', ForeignKey(Units.UnitsID)) + IntendedXSpacingUnitsID = Column('intendedxspacingunitsid', ForeignKey(Units.UnitsID)) IntendedZSpacing = Column('intendedzspacing', Float(53)) IntendedZSpacingUnitsID = Column('intendedzspacingunitsid', ForeignKey(Units.UnitsID)) IntendedTimeSpacing = Column('intendedtimespacing', Float(53)) @@ -1928,6 +1952,7 @@ class SectionResults(Results): SpatialReferenceObj = relationship(SpatialReferences) YUnitObj = relationship(Units, primaryjoin='SectionResults.YLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='SectionResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Section coverage'} class TrajectoryResults(Results): @@ -1948,6 +1973,7 @@ class TrajectoryResults(Results): primaryjoin='TrajectoryResults.IntendedTrajectorySpacingUnitsID == Units.UnitsID') SpatialReferenceObj = relationship(SpatialReferences) # ResultObj = relationship(Results, primaryjoin='TrajectoryResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Trajectory coverage'} class MeasurementResults(Results): @@ -1976,6 +2002,7 @@ class MeasurementResults(Results): YLocationUnitsObj = relationship(Units, primaryjoin='MeasurementResults.YLocationUnitsID == Units.UnitsID') ZLocationUnitsObj = relationship(Units, primaryjoin='MeasurementResults.ZLocationUnitsID == Units.UnitsID') # ResultObj = relationship(Results, primaryjoin='MeasurementResults.ResultID == Results.ResultID') + __mapper_args__ = {'polymorphic_identity':'Measurement'} def __repr__(self): return "" % \ diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 837cbd6..38b1350 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -258,15 +258,11 @@ def getSamplingFeatures(self, id=None, code=None, uuid=None, type=None, wkt=None * Pass a SamplingFeatureGeometry(TYPE????) - return a list of sampling feature objects """ - s = SamplingFeatures - if type.lower() == 'site': - s = Sites - elif type.lower() == 'specimen': - s = Specimens - q = self._session.query(s) - # if type: q = q.filter_by(SamplingFeatureTypeCV=type) + q = self._session.query(SamplingFeatures) + + if type: q = q.filter_by(SamplingFeatureTypeCV=type) if id: q = q.filter_by(SamplingFeatureID=id) if code: q = q.filter_by(SamplingFeatureCode=code) if wkt: q = q.filter_by(FeatureGeometryWKT=wkt) @@ -417,28 +413,10 @@ def getResults(self, id=None, actionid=None, type=None): :return Return matching Variable object filtered by variableId: :type Variable: """ - R = Results - if type is not None: - if type == "categorical": - R = CategoricalResults - elif type == "measurement": - R = MeasurementResults - elif type == "pointcoverage": - R = PointCoverageResults - elif type == "profile": - R = ProfileResults - elif type == "section": - R = SectionResults - elif type == "spectra": - R = SpectraResults - elif type == "timeseries": - R = TimeSeriesResults - elif type == "trajectory": - R = TrajectoryResults - elif type == "transect": - R = TransectResults - - query = self._session.query(R) + + + query = self._session.query(Results) + if type: query = query.filter_by(ResultTypeCV=type) if actionid: query = query.join(FeatureActions).filter_by(ActionID=actionid) if id: query = query.filter_by(ResultID=id) From d01a8f0709f0a5a41acbdafcc404f04e531a5de8 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 23 Mar 2016 14:26:32 -0600 Subject: [PATCH 5/9] create CV parent class --- Examples/Sample.py | 14 +- odm2api/ODM2/models.py | 371 +++++------------------------------------ odm2api/base.py | 54 +++--- 3 files changed, 68 insertions(+), 371 deletions(-) diff --git a/Examples/Sample.py b/Examples/Sample.py index dfaf75e..0d2821c 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -21,10 +21,10 @@ #connect to database #createconnection (dbtype, servername, dbname, username, password) -#session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm') -#session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0) -# session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm") -session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "LBRODM2", "ODM", "odm") +# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql +#session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite +# session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm")#win MSSQL +session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL @@ -64,14 +64,14 @@ numSites = len(siteFeatures) for x in siteFeatures: - print x.SamplingFeatureCode + ": " + x.SamplingFeatureName + print x.SamplingFeatureCode + ": " + x.SamplingFeatureName + ", " + x.SamplingFeatureTypeCV except Exception as e: print "Unable to demo getSamplingFeaturesByType", e # Now get the SamplingFeature object for a SamplingFeature code try: - sf = read.getSamplingFeatures(code='USU-LBR-Mendon') + sf = read.getSamplingFeatures(code='USU-LBR-Mendon')[0] print sf print "\n-------- Information about an individual SamplingFeature ---------" print "The following are some of the attributes of a SamplingFeature retrieved using getSamplingFeature(code = x): \n" @@ -79,7 +79,7 @@ print "SamplingFeatureName: " + sf.SamplingFeatureName print "SamplingFeatureDescription: %s" % sf.SamplingFeatureDescription print "SamplingFeatureGeotypeCV: %s" % sf.SamplingFeatureGeotypeCV - print "SamplingFeatureGeometry: %s" % sf.FeatureGeometry + print "SamplingFeatureGeometry: %s" % sf.FeatureGeometryWKT print "Elevation_m: %s" % str(sf.Elevation_m) except Exception as e: print "Unable to demo getSamplingFeatureByCode: ", e diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index 034c5f6..3bfdebd 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -30,10 +30,7 @@ def is_hex(s): ################################################################################ # CV ################################################################################ - - -class CVActionType(Base): - __tablename__ = 'cv_actiontype' +class CV (object): __table_args__ = {u'schema': 'odm2'} Term = Column('term', String(255), nullable=False) @@ -45,406 +42,124 @@ class CVActionType(Base): def __repr__(self): return "" % (self.Term, self.Name, self.Definition, self.Category) +class CVActionType(Base, CV): + __tablename__ = 'cv_actiontype' -class CVAggregationStatistic(Base): - __tablename__ = 'cv_aggregationstatistic' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % ( - self.Term, self.Name, self.Definition, self.Category) +class CVAggregationStatistic(Base, CV): + __tablename__ = 'cv_aggregationstatistic' -class CVAnnotationType(Base): +class CVAnnotationType(Base, CV): __tablename__ = 'cv_annotationtype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVCensorCode(Base): +class CVCensorCode(Base, CV): __tablename__ = 'cv_censorcode' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVDataQualityType(Base): +class CVDataQualityType(Base, CV): __tablename__ = 'cv_dataqualitytype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVDataSetType(Base): +class CVDataSetType(Base, CV): __tablename__ = 'cv_datasettypecv' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVDeploymentType(Base): +class CVDeploymentType(Base, CV): __tablename__ = 'cv_deploymenttype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVDirectiveType(Base): +class CVDirectiveType(Base, CV): __tablename__ = 'cv_directivetype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVElevationDatum(Base): +class CVElevationDatum(Base, CV): __tablename__ = 'cv_elevationdatum' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVEquipmentType(Base): +class CVEquipmentType(Base, CV): __tablename__ = 'cv_equipmenttype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - -class CVMediumType(Base): +class CVMediumType(Base, CV): __tablename__ = 'cv_medium' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyUri = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - -class CVMethodType(Base): +class CVMethodType(Base, CV): __tablename__ = 'cv_methodtype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVOrganizationType(Base): +class CVOrganizationType(Base, CV): __tablename__ = 'cv_organizationtype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVPropertyDataType(Base): +class CVPropertyDataType(Base, CV): __tablename__ = 'cv_propertydatatype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVQualityCode(Base): +class CVQualityCode(Base, CV): __tablename__ = 'cv_qualitycode' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVResultType(Base): +class CVResultType(Base, CV): __tablename__ = 'cv_resulttype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVRelationshipType(Base): +class CVRelationshipType(Base, CV): __tablename__ = 'cv_relationshiptype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - -class CVSamplingFeatureGeoType(Base): +class CVSamplingFeatureGeoType(Base, CV): __tablename__ = 'cv_samplingfeaturegeotype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - -class CVSamplingFeatureType(Base): +class CVSamplingFeatureType(Base, CV): __tablename__ = 'cv_samplingfeaturetype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVSpatialOffsetType(Base): +class CVSpatialOffsetType(Base, CV): __tablename__ = 'cv_spatialoffsettype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVSpeciation(Base): +class CVSpeciation(Base, CV): __tablename__ = 'cv_speciation' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVSpecimenType(Base): +class CVSpecimenType(Base, CV): __tablename__ = 'cv_specimentype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVSiteType(Base): +class CVSiteType(Base, CV): __tablename__ = 'cv_sitetype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVStatus(Base): +class CVStatus(Base, CV): __tablename__ = 'cv_status' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - -class CVTaxonomicClassifierType(Base): +class CVTaxonomicClassifierType(Base, CV): __tablename__ = 'cv_taxonomicclassifiertype' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVUnitsType(Base): +class CVUnitsType(Base, CV): __tablename__ = 'cv_unitstype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVVariableName(Base): +class CVVariableName(Base, CV): __tablename__ = 'cv_variablename' - __table_args__ = {u'schema': 'odm2'} - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) -class CVVariableType(Base): +class CVVariableType(Base, CV): __tablename__ = 'cv_variabletype' - __table_args__ = {u'schema': 'odm2'} - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) - - -class CVReferenceMaterialMedium(Base): +class CVReferenceMaterialMedium(Base, CV): __tablename__ = 'cv_referencematerialmedium' - __table_args__ = {u'schema': 'odm2'} # - - Term = Column('term', String(255), nullable=False) - Name = Column('name', String(255), primary_key=True) - Definition = Column('definition', String(1000)) - Category = Column('category', String(255)) - SourceVocabularyURI = Column('sourcevocabularyuri', String(255)) - - def __repr__(self): - return "" % (self.Term, self.Name, self.Definition, self.Category) # ################################################################################ # Core # ################################################################################ class People(Base): - __tablename__ = u'people' - __table_args__ = {u'schema': 'odm2'} + PersonID = Column('personid', Integer, primary_key=True, nullable=False) PersonFirstName = Column('personfirstname', String(255), nullable=False) @@ -457,8 +172,7 @@ def __repr__(self): class Organizations(Base): - __tablename__ = u'organizations' - __table_args__ = {u'schema': 'odm2'} + OrganizationID = Column('organizationid', Integer, primary_key=True, nullable=False) OrganizationTypeCV = Column('organizationtypecv', ForeignKey(CVOrganizationType.Name), nullable=False, @@ -479,8 +193,7 @@ def __repr__(self): class Affiliations(Base): - __tablename__ = 'affiliations' - __table_args__ = {u'schema': 'odm2'} + AffiliationID = Column('affiliationid', Integer, primary_key=True, nullable=False) PersonID = Column('personid', ForeignKey(People.PersonID), nullable=False) @@ -498,8 +211,7 @@ class Affiliations(Base): class Methods(Base): - __tablename__ = 'methods' - __table_args__ = {u'schema': 'odm2'} + MethodID = Column('methodid', Integer, primary_key=True, nullable=False) MethodTypeCV = Column('methodtypecv', ForeignKey(CVMethodType.Name), nullable=False, index=True) @@ -518,8 +230,7 @@ def __repr__(self): class Actions(Base): - __tablename__ = u'actions' - __table_args__ = {u'schema': 'odm2'} + ActionID = Column('actionid', Integer, primary_key=True, nullable=False) ActionTypeCV = Column('actiontypecv', ForeignKey(CVActionType.Name), nullable=False, index=True) @@ -539,8 +250,7 @@ def __repr__(self): class ActionBy(Base): - __tablename__ = u'actionby' - __table_args__ = {u'schema': 'odm2'} + BridgeID = Column('bridgeid', Integer, primary_key=True, nullable=False) ActionID = Column('actionid', Integer, ForeignKey(Actions.ActionID), nullable=False) @@ -553,8 +263,7 @@ class ActionBy(Base): class SamplingFeatures(Base): - __tablename__ = u'samplingfeatures' - __table_args__ = {u'schema': 'odm2'} + SamplingFeatureID = Column('samplingfeatureid', Integer, primary_key=True, nullable=False) SamplingFeatureUUID = Column('samplingfeatureuuid', String(36), nullable=False) @@ -568,7 +277,7 @@ class SamplingFeatures(Base): Elevation_m = Column('elevation_m', Float(53)) ElevationDatumCV = Column('elevationdatumcv', ForeignKey(CVElevationDatum.Name), index=True) FeatureGeometry = Column('featuregeometry', Geometry) # String(50))# - # FeatureGeometryWKT = Column('featuregeometrywkt', String(50)) + FeatureGeometryWKT = Column('featuregeometrywkt', String(50)) # FeatureGeometry = Column('featuregeometry', BLOB) # custom geometry queries __mapper_args__ = { 'polymorphic_identity':'samplingfeatures', diff --git a/odm2api/base.py b/odm2api/base.py index d650720..f76ce2f 100644 --- a/odm2api/base.py +++ b/odm2api/base.py @@ -2,32 +2,6 @@ -from sqlalchemy.ext.declarative import declarative_base -# #from sqlalchemy import MetaData -# from .ODMconnection import SessionFactory - -#only one copy of class at a time -class Singleton(type): - _instances = {} - - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - #print "Singleton", cls._instances[cls] - return cls._instances[cls] - -#only one copy of class with a particular connection -class SingletonByConn(type): - _instances= {} - - def __call__(cls, *args, **kwargs): - conn= args[0].engine - if (cls, conn) not in cls._instances: - - cls._instances[(cls, conn)] = super(SingletonByConn, cls).__call__(*args, **kwargs) - #print "Singleton", cls._instances[cls] - return cls._instances[(cls, conn)] - class serviceBase(object): #__metaclass__ = SingletonByConn @@ -57,17 +31,31 @@ def getSession(self): return self._session + + + +from sqlalchemy.ext.declarative import declared_attr +class Base(object): + @declared_attr + def __tablename__(cls): + return cls.__name__.lower() + + __table_args__ = {u'schema': 'odm2'} + + def __init__(self, *args, **kwargs): + for name, value in kwargs.items(): setattr(self, name, value) + +from sqlalchemy.ext.declarative import declarative_base + + + class modelBase(): - Base = declarative_base() + Base = declarative_base(cls=Base) metadata = Base.metadata - ''' - metadata =MetaData(schema='odm2') - print "Schema:", metadata.schema - def __init__(self, schema): - self.metadata =MetaData(schema='odm2') - ''' + + From 3eb9b869721b6dfc6737086865f6312d3435d09f Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Wed, 30 Mar 2016 11:23:31 -0600 Subject: [PATCH 6/9] update sample to work with inheritance --- .gitignore | 1 + Examples/Sample.py | 16 ++++++------- odm2api/ODM2/services/readService.py | 36 +++++++--------------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 59f128f..deb5922 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ log .idea .DS_Store +.ipynb_checkpoints diff --git a/Examples/Sample.py b/Examples/Sample.py index 0d2821c..31e6fd9 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -64,7 +64,7 @@ numSites = len(siteFeatures) for x in siteFeatures: - print x.SamplingFeatureCode + ": " + x.SamplingFeatureName + ", " + x.SamplingFeatureTypeCV + print x.SamplingFeatureCode + ": " + str(x.SamplingFeatureName) + ", " + x.SamplingFeatureTypeCV except Exception as e: print "Unable to demo getSamplingFeaturesByType", e @@ -127,20 +127,20 @@ # Now get a particular Result using a ResultID print "\n------- Example of Retrieving Attributes of a Time Series Result -------" try: - tsResult = read.getResults(id = 1) + tsResult = read.getResults(id = 1)[0] print ( "The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" + - "ResultTypeCV: " + tsResult.ResultObj.ResultTypeCV + "\n" + + "ResultTypeCV: " + tsResult.ResultTypeCV + "\n" + # Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object - "ProcessingLevel: " + tsResult.ResultObj.ProcessingLevelObj.Definition + "\n" + - "SampledMedium: " + tsResult.ResultObj.SampledMediumCV + "\n" + + "ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" + + "SampledMedium: " + tsResult.SampledMediumCV + "\n" + # Get the variable information from the TimeSeriesResult's Variable object - "Variable: " + tsResult.ResultObj.VariableObj.VariableCode + ": " + tsResult.ResultObj.VariableObj.VariableNameCV + "\n" + "Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n" "AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" + "Elevation_m: " + str(sf.Elevation_m) + "\n" + # Get the site information by drilling down - "SamplingFeature: " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + - tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) + "SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " + + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName) except Exception as e: print "Unable to demo Example of retrieving Attributes of a time Series Result: ", e diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 38b1350..697bd58 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -381,7 +381,7 @@ def getPeople(self, id=None, firstname=None, lastname=None): except: return None - def getAffiliations(self, id, personfirst=None, personlast=None, orgcode=None): + def getAffiliations(self, id=None, personfirst=None, personlast=None, orgcode=None): """ Select all affiliation of person :param personfirst: first name of person @@ -644,7 +644,7 @@ def getResultDerivationEquations(self): ResultValues """ - def getResultValues(self, resultid=None, starttime=None, endtime=None): + def getResultValues(self, resultid, starttime=None, endtime=None): """Select all on TimeSeriesResults getResultValues() @@ -656,33 +656,13 @@ def getResultValues(self, resultid=None, starttime=None, endtime=None): """ - type = self._session.query(Results).filter_by(ResultID=resultid).first().ResultTypeCV - Result = TimeSeriesResults - - if "categorical" in type.lower(): - Result = CategoricalResultValues - elif "measurement" in type.lower(): - Result = MeasurementResultValues - elif "coverage" in type.lower(): - Result = PointCoverageResultValues - elif "profile" in type.lower(): - Result = ProfileResultValues - elif "section" in type.lower(): - Result = SectionResults - elif "spectra" in type.lower(): - Result = SpectraResultValues - elif "time" in type.lower(): - Result = TimeSeriesResultValues - elif "trajectory" in type.lower(): - Result = TrajectoryResultValues - elif "transect" in type.lower(): - Result = TransectResultValues - - q = self._session.query(Result).filter_by(ResultID=id) - if starttime: q = q.filter(Result.ValueDateTime >= starttime) - if endtime: q = q.filter(Result.ValueDateTime <= endtime) + + + q = self._session.query(Results).filter_by(ResultID=id) + if starttime: q = q.filter(Results.ValueDateTime >= starttime) + if endtime: q = q.filter(Results.ValueDateTime <= endtime) try: - q = q.order_by(Result.ValueDateTime).all() + q = q.order_by(Results.ValueDateTime).all() df = pd.DataFrame([dv.list_repr() for dv in q.all()]) df.columns = q[0].get_columns() return df From 13b664b576d796fb2e4f6f6f0ba3e2729ef9e5f9 Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Mon, 4 Apr 2016 12:19:20 -0600 Subject: [PATCH 7/9] fix sample file so that it is not throwing an error when plotting --- Examples/Sample.py | 29 +++++++--------------------- odm2api/ODM2/models.py | 6 ------ odm2api/ODM2/services/readService.py | 27 ++++++++++++++++++-------- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/Examples/Sample.py b/Examples/Sample.py index 31e6fd9..2dd808d 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -1,17 +1,11 @@ __author__ = 'stephanie' -import sys -import os + import matplotlib.pyplot as plt from matplotlib import dates -#this will be removed when we can installthe api -# this_file = os.path.realpath(__file__) -# directory = os.path.dirname(os.path.dirname(this_file)) -# print directory -# sys.path.insert(0, directory) from odm2api.ODMconnection import dbconnection from odm2api.ODM2.services.readService import * @@ -20,16 +14,15 @@ #connect to database -#createconnection (dbtype, servername, dbname, username, password) +# createconnection (dbtype, servername, dbname, username, password) +# session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite # session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql -#session_factory = dbconnection.createConnection('connection type: sqlite|mysql|mssql|postgresql', '/your/path/to/db/goes/here', 2.0)#sqlite # session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm")#win MSSQL session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL #_session = session_factory.getSession() - read = ReadODM2(session_factory) @@ -152,6 +145,7 @@ # Print a few Time Series Values to the console # tsValues.set_index('ValueDateTime', inplace=True) try: + print "tsValues " print tsValues.head() except Exception as e: print e @@ -159,18 +153,9 @@ # Plot the time series try: - fig = plt.figure() - ax = fig.add_subplot(111) - tsValues.plot(x='ValueDateTime', y='DataValue', kind='line', - title=tsResult.ResultObj.VariableObj.VariableNameCV + " at " + tsResult.ResultObj.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName, - ax=ax) - ax.set_ylabel(tsResult.ResultObj.VariableObj.VariableNameCV + " (" + tsResult.ResultObj.UnitsObj.UnitsAbbreviation + ")") - ax.set_xlabel("Date/Time") - ax.xaxis.set_minor_locator(dates.MonthLocator()) - ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) - ax.xaxis.set_major_locator(dates.YearLocator()) - ax.xaxis.set_major_formatter(dates.DateFormatter('\n%Y')) - ax.grid(True) + plt.figure() + ax=tsValues.plot(x='ValueDateTime', y='DataValue') + plt.show() except Exception as e: print "Unable to demo plotting of tsValues: ", e diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index 3bfdebd..6d406cc 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -480,10 +480,6 @@ def __repr__(self): # ################################################################################ - - - - class DataLoggerProgramFiles(Base): __tablename__ = u'dataloggerprogramfiles' __table_args__ = {u'schema': 'odm2'} @@ -510,8 +506,6 @@ class DataLoggerFiles(Base): ProgramObj = relationship(DataLoggerProgramFiles) - - class EquipmentModels(Base): __tablename__ = u'equipmentmodels' __table_args__ = {u'schema': 'odm2'} diff --git a/odm2api/ODM2/services/readService.py b/odm2api/ODM2/services/readService.py index 697bd58..daf6f40 100644 --- a/odm2api/ODM2/services/readService.py +++ b/odm2api/ODM2/services/readService.py @@ -656,15 +656,26 @@ def getResultValues(self, resultid, starttime=None, endtime=None): """ - - - q = self._session.query(Results).filter_by(ResultID=id) - if starttime: q = q.filter(Results.ValueDateTime >= starttime) - if endtime: q = q.filter(Results.ValueDateTime <= endtime) + type= self._session.query(Results).filter_by(ResultID=resultid).first().ResultTypeCV + Result = TimeSeriesResults + if "categorical" in type.lower():Result = CategoricalResultValues + elif "measurement" in type.lower():Result = MeasurementResultValues + elif "point" in type.lower():Result = PointCoverageResultValues + elif "profile" in type.lower():Result = ProfileResultValues + elif "section" in type.lower():Result = SectionResults + elif "spectra" in type.lower():Result = SpectraResultValues + elif "time" in type.lower():Result = TimeSeriesResultValues + elif "trajectory" in type.lower():Result = TrajectoryResultValues + elif "transect" in type.lower():Result = TransectResultValues + + + q = self._session.query(Result).filter_by(ResultID=resultid) + if starttime: q = q.filter(Result.ValueDateTime >= starttime) + if endtime: q = q.filter(Result.ValueDateTime <= endtime) try: - q = q.order_by(Results.ValueDateTime).all() - df = pd.DataFrame([dv.list_repr() for dv in q.all()]) - df.columns = q[0].get_columns() + vals = q.order_by(Result.ValueDateTime) + df = pd.DataFrame([dv.list_repr() for dv in vals.all()]) + df.columns = vals[0].get_columns() return df except: return None From e304133a631d0db7c7f4ff9875e3c240cdb4b5ea Mon Sep 17 00:00:00 2001 From: Stephanie Reeder Date: Mon, 4 Apr 2016 15:15:40 -0600 Subject: [PATCH 8/9] fix sample code to work with creating an inherited object --- Examples/Sample.py | 20 +++++++++----------- odm2api/ODM2/models.py | 13 +++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Examples/Sample.py b/Examples/Sample.py index 2dd808d..38c3592 100644 --- a/Examples/Sample.py +++ b/Examples/Sample.py @@ -9,6 +9,7 @@ from odm2api.ODMconnection import dbconnection from odm2api.ODM2.services.readService import * +from odm2api.ODM2.services import CreateODM2 # Create a connection to the ODM2 database # ---------------------------------------- @@ -24,6 +25,7 @@ #_session = session_factory.getSession() read = ReadODM2(session_factory) +create =CreateODM2(session_factory) @@ -80,18 +82,14 @@ #add sampling feature print "\n------------ Create Sampling Feature --------- \n", try: - from odm2api.ODM2.models import SamplingFeatures - newsf = SamplingFeatures() + # from odm2api.ODM2.models import SamplingFeatures session = session_factory.getSession() - newsf.FeatureGeometry = "POINT(-111.946 41.718)" - newsf.Elevation_m=100 - newsf.ElevationDatumCV=sf.ElevationDatumCV - newsf.SamplingFeatureCode= "TestSF" - newsf.SamplingFeatureDescription = "this is a test to add Feature Geomotry" - newsf.SamplingFeatureGeotypeCV= "Point" - newsf.SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV - newsf.SamplingFeatureUUID= sf.SamplingFeatureUUID+"2" - session.add(newsf) + newsf = Sites(FeatureGeometryWKT = "POINT(-111.946 41.718)", Elevation_m=100, ElevationDatumCV=sf.ElevationDatumCV, + SamplingFeatureCode= "TestSF",SamplingFeatureDescription = "this is a test to add Feature Geomotry", + SamplingFeatureGeotypeCV= "Point", SamplingFeatureTypeCV=sf.SamplingFeatureTypeCV,SamplingFeatureUUID= sf.SamplingFeatureUUID+"2", + SiteTypeCV="cave", Latitude= "100", Longitude= "-100", SpatialReferenceID= 0) + + create.createSamplingFeature(newsf) #session.commit() print "new sampling feature added to database", newsf diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index 6d406cc..9f3d86a 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -306,15 +306,16 @@ def shape(self): return geomshape def __repr__(self): - geom = self.shape() - if geom is not None: - geomkt = geom.wkt - else: - geomkt = None + # geom = self.shape() + # if geom is not None: + # geomkt = geom.wkt + # else: + # geomkt = None return "" % ( self.SamplingFeatureCode, self.SamplingFeatureName, self.SamplingFeatureDescription, - self.Elevation_m, geomkt) + # self.Elevation_m, geomkt) + self.Elevation_m, self.FeatureGeometry) GeometryDDL(SamplingFeatures.__table__) # Geoalchemy1 From 8179e10d8f96469cec8ce5b128eaf3a35d68b553 Mon Sep 17 00:00:00 2001 From: Emilio Mayorga Date: Thu, 7 Apr 2016 16:47:12 -0700 Subject: [PATCH 9/9] Update models.py SamplingFeatures.FeatureGeometry won't be used by default; FeatureGeometryWKT will be used instead. So, the __repr__ method should return FeatureGeometryWKT, not FeatureGeometry --- odm2api/ODM2/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/odm2api/ODM2/models.py b/odm2api/ODM2/models.py index 9f3d86a..6c444b3 100644 --- a/odm2api/ODM2/models.py +++ b/odm2api/ODM2/models.py @@ -315,7 +315,7 @@ def __repr__(self): return "" % ( self.SamplingFeatureCode, self.SamplingFeatureName, self.SamplingFeatureDescription, # self.Elevation_m, geomkt) - self.Elevation_m, self.FeatureGeometry) + self.Elevation_m, self.FeatureGeometryWKT) GeometryDDL(SamplingFeatures.__table__) # Geoalchemy1