Skip to content

Commit 9ac80e4

Browse files
author
stephanie
committed
handle polymorphic case when no inherited class for specified type
1 parent 8ef2822 commit 9ac80e4

File tree

3 files changed

+38
-182
lines changed

3 files changed

+38
-182
lines changed

Examples/Sample.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# session_factory = dbconnection.createConnection('mysql', 'localhost', 'odm2', 'ODM', 'odm')#mysql
2121
# session_factory= dbconnection.createConnection('mssql', "(local)", "LBRODM2", "ODM", "odm")#win MSSQL
2222
session_factory= dbconnection.createConnection('mssql', "arroyoodm2", "", "ODM", "odm")#mac/linux MSSQL
23+
# session_factory = dbconnection.createConnection('sqlite', '/Users/Stephanie/Desktop/TestODM2.db', 2.0)
2324

2425

2526

@@ -33,15 +34,15 @@
3334
# ------------------------------
3435
# Get all of the variables from the database and print their names to the console
3536
allVars = read.getVariables()
36-
print "\n-------- Information about Variables ---------"
37+
print ("\n-------- Information about Variables ---------")
3738
for x in allVars:
3839
print(x.VariableCode + ": " + x.VariableNameCV)
3940

4041

4142

4243
# Get all of the people from the database
4344
allPeople = read.getPeople()
44-
print "\n-------- Information about People ---------"
45+
print ("\n-------- Information about People ---------")
4546
for x in allPeople:
4647
print(x.PersonFirstName + " " + x.PersonLastName)
4748

@@ -55,12 +56,13 @@
5556

5657
# Get all of the SamplingFeatures from the database that are Sites
5758
try:
58-
print "\n-------- Information about Sites ---------"
59-
siteFeatures = read.getSamplingFeatures(type='Site')
59+
print ("\n-------- Information about Sites ---------")
60+
siteFeatures = read.getSamplingFeatures()
61+
# siteFeatures = read.getSamplingFeatures(type='Site')
6062
numSites = len(siteFeatures)
6163

6264
for x in siteFeatures:
63-
print(x.SamplingFeatureCode + ": " + x.SamplingFeatureName)
65+
print(x.SamplingFeatureCode + ": " + x.SamplingFeatureTypeCV )
6466
except Exception as e:
6567
print("Unable to demo getSamplingFeaturesByType", e)
6668

@@ -117,19 +119,19 @@
117119

118120

119121
# Now get a particular Result using a ResultID
120-
print("\n------- Example of Retrieving Attributes of a Time Series Result -------")
122+
print("\n------- Example of Retrieving Attributes of a Result -------")
121123
try:
122124
tsResult = read.getResults(ids = [1])[0]
123125
print (
124-
"The following are some of the attributes for the TimeSeriesResult retrieved using getTimeSeriesResultByResultID(): \n" +
126+
"The following are some of the attributes for the TimeSeriesResult retrieved using getResults(ids=[1]): \n" +
125127
"ResultTypeCV: " + tsResult.ResultTypeCV + "\n" +
126128
# Get the ProcessingLevel from the TimeSeriesResult's ProcessingLevel object
127129
"ProcessingLevel: " + tsResult.ProcessingLevelObj.Definition + "\n" +
128130
"SampledMedium: " + tsResult.SampledMediumCV + "\n" +
129131
# Get the variable information from the TimeSeriesResult's Variable object
130132
"Variable: " + tsResult.VariableObj.VariableCode + ": " + tsResult.VariableObj.VariableNameCV + "\n"
131-
"AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" +
132-
"Elevation_m: " + str(sf.Elevation_m) + "\n" +
133+
#"AggregationStatistic: " + tsResult.AggregationStatisticCV + "\n" +
134+
133135
# Get the site information by drilling down
134136
"SamplingFeature: " + tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureCode + " - " +
135137
tsResult.FeatureActionObj.SamplingFeatureObj.SamplingFeatureName)

Examples/Sample.py.orig

Lines changed: 0 additions & 165 deletions
This file was deleted.

odm2api/ODM2/models.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from sqlalchemy import BigInteger, Column, Date, DateTime, Float, ForeignKey, Integer, String, Boolean, BLOB
1+
from sqlalchemy import BigInteger, Column, Date, DateTime, Float, ForeignKey, Integer, String, Boolean, BLOB, case
22
from sqlalchemy.orm import relationship
33
from sqlalchemy.dialects import postgresql, mysql, sqlite
4-
# Should not be importing anything from a specific dialect
5-
# from sqlalchemy.dialects.mssql.base import BIT
4+
65

76
from geoalchemy import GeometryDDL, GeometryColumn
87
from geoalchemy.geometry import Geometry
@@ -280,11 +279,17 @@ class SamplingFeatures(Base):
280279
FeatureGeometryWKT = Column('featuregeometrywkt', String(50))
281280
# FeatureGeometry = Column('featuregeometry', BLOB) # custom geometry queries
282281
__mapper_args__ = {
282+
# 'polymorphic_on': SamplingFeatureTypeCV,
283+
"polymorphic_on":case([
284+
(SamplingFeatureTypeCV == "Specimen", "Specimen"),
285+
(SamplingFeatureTypeCV == "Site", "Site"),
286+
], else_="samplingfeatures"),
283287
'polymorphic_identity':'samplingfeatures',
284-
'polymorphic_on': SamplingFeatureTypeCV,
285-
'with_polymorphic':'*'
288+
# 'with_polymorphic':'*'
286289
}
287290

291+
292+
288293
def shape(self):
289294
"""
290295
Method name based on shapely shapely.geometry.shape() function.
@@ -466,9 +471,21 @@ class Results(Base):
466471
VariableObj = relationship(Variables)
467472

468473
__mapper_args__ = {
469-
'polymorphic_on':ResultTypeCV,
474+
# 'polymorphic_on':ResultTypeCV,
475+
"polymorphic_on":case([
476+
(ResultTypeCV == "Point coverage", "Point coverage"),
477+
(ResultTypeCV == "Profile Coverage", "Profile Coverage"),
478+
(ResultTypeCV == "Category coverage", "Category coverage"),
479+
(ResultTypeCV == "Transect Coverage", "Transect Coverage"),
480+
(ResultTypeCV == "Spectra coverage", "Spectra coverage"),
481+
(ResultTypeCV == "Time series coverage", "Time series coverage"),
482+
(ResultTypeCV == "Section coverage", "Section coverage"),
483+
(ResultTypeCV == "Profile Coverage", "Profile Coverage"),
484+
(ResultTypeCV == "Trajectory coverage", "Trajectory coverage"),
485+
(ResultTypeCV == "Measurement", "Measurement"),
486+
], else_="results"),
470487
'polymorphic_identity':'results',
471-
'with_polymorphic':'*'
488+
# 'with_polymorphic':'*'
472489
}
473490

474491
def __repr__(self):
@@ -738,7 +755,9 @@ class Specimens(SamplingFeatures):
738755
__mapper_args__ = {
739756
'polymorphic_identity':'Specimen',
740757
}
741-
758+
def __repr__(self):
759+
return "<Specimens('%s', '%s', '%s', '%s',)>" \
760+
% (self.SamplingFeatureID, self.SamplingFeatureCode, self.SpecimenTypeCV,self.SamplingFeatureCode)
742761

743762
class SpatialOffsets(Base):
744763
__tablename__ = u'spatialoffsets'

0 commit comments

Comments
 (0)