diff --git a/db/models.py b/db/models.py index 5054af4..d0f40ba 100644 --- a/db/models.py +++ b/db/models.py @@ -40,12 +40,18 @@ def create_pg_session() -> sessionmaker: # Base.metadata.create_all(engine) return _session +platform_seq = Sequence('platform_seq', schema="gba") # define sequence explicitly class Platform(Base): """ Platform class """ __tablename__ = 'platform' __table_args__ = {"schema": "gba"} - id = Column('platform_id', Integer, primary_key=True) + # id = Column('platform_id', Integer, primary_key=True) + id = Column('platform_id', + Integer, + platform_seq, + primary_key=True, + server_default=platform_seq.next_value()) identifier = Column('identifier', String) sta_identifier = Column('sta_identifier', String) name = Column('name', String) diff --git a/gschliefgraben_piezometer/InsertPechgraben.xml b/gschliefgraben_piezometer/InsertGschliefgrabenPiezometer.xml similarity index 91% rename from gschliefgraben_piezometer/InsertPechgraben.xml rename to gschliefgraben_piezometer/InsertGschliefgrabenPiezometer.xml index 8516902..dd58971 100644 --- a/gschliefgraben_piezometer/InsertPechgraben.xml +++ b/gschliefgraben_piezometer/InsertGschliefgrabenPiezometer.xml @@ -1,4 +1,4 @@ - - + - - - - - - + + + Elevation + + diff --git a/gschliefgraben_piezometer/import_feature_sensor.py b/gschliefgraben_piezometer/import_feature_sensor.py index 95e7cc9..cb8243c 100644 --- a/gschliefgraben_piezometer/import_feature_sensor.py +++ b/gschliefgraben_piezometer/import_feature_sensor.py @@ -37,62 +37,32 @@ def main(): """ sos_url = 'https://geomon.geologie.ac.at/52n-sos-webapp/service' - # Gschliefgraben Glasfaser - - # offering = Offering( - # "https://geomon.geologie.ac.at/52n-sos-webapp/api/offerings/", - # "inclino1_02", - # "Inklinometer inclino1_02, Gschliefgraben Glasfaser" - # ) - # procedure = Procedure( "inclino1_02","inclino1_02") - # foi = FoI("degree", "m", (13.774966, 47.910849, 0.0), - # "inclino1-glasfaser-gschliefgraben", - # "Glasfaser Untersuchungen am Gschliefgraben (Gmunden)") - - # offering = Offering( - # "https://geomon.geologie.ac.at/52n-sos-webapp/api/offerings/", - # "inclino1_05", - # "Inklinometer inclino1_05, Gschliefgraben Glasfaser" - # ) - # procedure = Procedure("inclino1_05", "inclino1_05") - # foi = FoI("degree", "m", (13.774966, 47.910849, 0.0), - # "inclino1-glasfaser-gschliefgraben", - # "Glasfaser Untersuchungen am Gschliefgraben (Gmunden)") - - # offering = Offering( - # "https://geomon.geologie.ac.at/52n-sos-webapp/api/offerings/", - # "inclino1_14", - # "Inklinometer inclino1_14, Gschliefgraben Glasfaser" - # ) - # procedure = Procedure("inclino1_14", "inclino1_14") - # foi = FoI("degree", "m", (13.774966, 47.910849, 0.0), - # "inclino1-glasfaser-gschliefgraben", - # "Glasfaser Untersuchungen am Gschliefgraben (Gmunden)") + # Gschliefgraben Piezometer offering = Offering( "https://geomon.geologie.ac.at/52n-sos-webapp/api/offerings/", - "inclino1_06", - "Inklinometer inclino1_06, Gschliefgraben Glasfaser" + "bohrloch", + "Bohrlöcher, Gschliefgraben Piezometer" ) - procedure = Procedure("inclino1_06", "inclino1_06") + procedure = Procedure("bohrloch1", "bohrloch1") foi = FoI("degree", "m", (13.774966, 47.910849, 0.0), - "inclino1-glasfaser-gschliefgraben", - "Glasfaser Untersuchungen am Gschliefgraben (Gmunden)") + "bohrloch1-glasfaser-gschliefgraben", + "Piezometer1 am Gschliefgraben") - sensor_type = SensorType("inclinometer") + sensor_type = SensorType("piezometer") post_data = insert_sensor(offering, procedure, foi, sensor_type) print(post_data) headers = {'Accept': 'application/json'} request = requests.post(sos_url, headers=headers, json=post_data) print(request.text) - # { - # "request" : "InsertSensor", - # "version" : "2.0.0", - # "service" : "SOS", - # "assignedProcedure" : "inclino1_14", - # "assignedOffering" : "inclino1_14" - # } +# { +# "request" : "InsertSensor", +# "version" : "2.0.0", +# "service" : "SOS", +# "assignedProcedure" : "bohrloch1", +# "assignedOffering" : "bohrloch" +# } def insert_sensor(offering, procedure, foi, sensor_type): @@ -109,7 +79,7 @@ def insert_sensor(offering, procedure, foi, sensor_type): # longName = 'Sibratsgfall test' # string # Offering values - off_name = '\"' + str(offering.name) + '\"' # Offering name, double quoted + gml_id = '\"' + str(procedure.id) + '\"' # Offering name, double quoted offering_name = offering.name offering_label = offering.label # offID = offering.fullId # URL format of full id @@ -165,16 +135,17 @@ def insert_sensor(offering, procedure, foi, sensor_type): "service": "SOS", "version": "2.0.0", "procedureDescriptionFormat": "http://www.opengis.net/sensorml/2.0", - "procedureDescription": f'{procedure_identifier}shortName{procedure_name}{offering_label}{offering_name}featuresOfInterest{feature_id}{feature_name}{coordinates}Slope{cord_x}{cord_y}{height}', + "procedureDescription": f'{procedure_identifier}longName{procedure_name}shortName{procedure_name}{offering_label}{offering_name}truefalsefeaturesOfInterest{feature_id}{feature_name}{coordinates}Elevation{cord_x}{cord_y}{height}', "observableProperty": [ - "Slope", + "Elevation", # "Roll", # "InSystemTemperature" ], "observationType": [ "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement" ], - "featureOfInterestType": "http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint" + "featureOfInterestType": + "http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint" } return body diff --git a/gschliefgraben_piezometer/import_piezometer_observations_hourly.py b/gschliefgraben_piezometer/import_piezometer_observations_hourly.py index 8059ca4..350f385 100644 --- a/gschliefgraben_piezometer/import_piezometer_observations_hourly.py +++ b/gschliefgraben_piezometer/import_piezometer_observations_hourly.py @@ -6,10 +6,11 @@ Python version: 3.7 import json import os import uuid +from datetime import datetime from sqlalchemy.orm import session +from sqlalchemy import asc, desc, func from dotenv import load_dotenv, find_dotenv import requests -from datetime import datetime from db.models import ( Observation, create_pg_session, @@ -26,9 +27,9 @@ def main(): pg_session: session = create_pg_session() platform_sta_identifier = "pechgraben_piezometer" # sensor = "bohrloch1" - # sensor_list = os.environ.get('GLASFASER_GSCHLIEFGRABEN_SENSORS', []) - sensor_list = json.loads(os.environ['GLASFASER_GSCHLIEFGRABEN_SENSORS']) - + # sensor_list = os.environ.get('PIEZOMETER_GSCHLIEFGRABEN_SENSORS', []) + sensor_list = json.loads(os.environ['PIEZOMETER_GSCHLIEFGRABEN_SENSORS']) + url = 'https://jaa5ixl2y0.execute-api.ap-southeast-2.amazonaws.com/v1/data' params = {} headers = {'content-type': 'application/json'} @@ -44,7 +45,7 @@ def main(): Phenomenon.sta_identifier == "Elevation").first() if not elevation_dataset: print("Sensor " + sensor + " ist noch nicht angelegt!") - exit() + continue if not elevation_dataset.is_published: elevation_dataset.is_published = 1 elevation_dataset.is_hidden = 0 @@ -55,7 +56,21 @@ def main(): platform_exists: bool = pg_session.query(Platform.id).filter_by( sta_identifier=platform_sta_identifier).scalar() is not None - if platform_exists: + # if platform_exists: + # sensor_platform = pg_session.query(Platform.id) \ + # .filter(Platform.sta_identifier == platform_sta_identifier) \ + # .first() + # elevation_dataset.fk_platform_id = sensor_platform.id + + if not platform_exists: + sensor_platform = Platform() + max_id = pg_session.query(func.max(Platform.id)).scalar() + # sensor_platform.id = max_id + 1 + sensor_platform.sta_identifier = platform_sta_identifier.lower() + sensor_platform.identifier = platform_sta_identifier.lower() + sensor_platform.name = platform_sta_identifier.lower() + elevation_dataset.platform = sensor_platform + else: sensor_platform = pg_session.query(Platform.id) \ .filter(Platform.sta_identifier == platform_sta_identifier) \ .first() @@ -66,16 +81,39 @@ def main(): ).scalar() is not None if format_exists: sensor_format = pg_session.query(Format.id) \ - .filter(Format.definition == "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement") \ + .filter(Format.definition == + "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement") \ .first() elevation_dataset.fk_format_id = sensor_format.id if sensor in data: create_observation(elevation_dataset, sensor, data, pg_session) + pg_session.commit() + + first_elevation_observation = pg_session.query(Observation) \ + .filter(Observation.fk_dataset_id == elevation_dataset.id) \ + .order_by(asc('sampling_time_start')) \ + .first() + if first_elevation_observation is not None: + elevation_dataset.first_time = first_elevation_observation.sampling_time_start + elevation_dataset.first_value = first_elevation_observation.value_quantity + elevation_dataset.fk_first_observation_id = first_elevation_observation.id + last_elevation_observation = pg_session.query(Observation) \ + .filter(Observation.fk_dataset_id == elevation_dataset.id) \ + .order_by(desc('sampling_time_start')) \ + .first() + if last_elevation_observation is not None: + elevation_dataset.last_time = last_elevation_observation.sampling_time_start + elevation_dataset.last_value = last_elevation_observation.value_quantity + elevation_dataset.fk_last_observation_id = last_elevation_observation.id + pg_session.commit() pg_session.close() -def create_observation(elevation_dataset: Dataset, sensor_key: str, data: json, db_session: session): +def create_observation(elevation_dataset: Dataset, + sensor_key: str, + data: json, + db_session: session): ''' create observation in db''' print("Sesnor key exist in JSON data") sensor_object = data[sensor_key] @@ -86,7 +124,8 @@ def create_observation(elevation_dataset: Dataset, sensor_key: str, data: json, existing_observation: bool = ( db_session.query(Observation) - .filter(Observation.result_time == date_obj, Observation.fk_dataset_id == elevation_dataset.id) + .filter(Observation.result_time == + date_obj, Observation.fk_dataset_id == elevation_dataset.id) .one_or_none() ) # Can we insert this observation? @@ -100,6 +139,7 @@ def create_observation(elevation_dataset: Dataset, sensor_key: str, data: json, new_observation.sampling_time_end = new_observation.result_time new_observation.value_type = "quantity" new_observation.value_quantity = abstich + new_observation.fk_dataset_id = elevation_dataset.id db_session.add(new_observation) @@ -135,6 +175,6 @@ def test(): if __name__ == "__main__": load_dotenv(find_dotenv()) - sensor_list1 = os.environ.get('GLASFASER_GSCHLIEFGRABEN_SENSORS', []) + sensor_list1 = os.environ.get('PIEZOMETER_GSCHLIEFGRABEN_SENSORS', []) print(f'sensors: {sensor_list1} .') main() diff --git a/insert_sensor/wrapper.py b/insert_sensor/wrapper.py index 87d90dc..c9274bd 100644 --- a/insert_sensor/wrapper.py +++ b/insert_sensor/wrapper.py @@ -71,10 +71,13 @@ class SensorType: ("Battery level", "m"), ("Temperature", "m"), ("Relative humidity", "m")]} elif type_ == "inclinometer": # INCLINOMETER self.pattern = {"name": "inclinometer", "type": 'fixed', "attributes": [ - ("Slope", "m"), ("Roll", "m")]} + ("Slope", "deg"), ("Roll", "deg")]} elif type_ == "camera": # INCLINOMETER self.pattern = {"name": "camera", "type": 'fixed', "attributes": [ ("Image", "xo")]} + elif type_ == "piezometer": # PIEZOMETER + self.pattern = {"name": "piezometer", "type": 'fixed', "attributes": [ + ("Elevation", "m")]} elif type_ == "noise": # NOISE self.pattern = {"name": "noise", "type": 'fixed', "attributes": [ ("Battery level", "m"), ("Noise", "m")]} diff --git a/pechgraben_images/import_feature_sensor.py b/pechgraben_images/import_feature_sensor.py index 792011c..fbd6863 100644 --- a/pechgraben_images/import_feature_sensor.py +++ b/pechgraben_images/import_feature_sensor.py @@ -73,7 +73,7 @@ def main(): # headers = {'Content-Type': 'application/soap+xml'} # set what your server accepts - # xml = get_xml(offering, procedure, foi, sensor_type) + # xml = get_xml(offering, procedure, foi, sensor_type) # request = requests.post(sos_url, data = xml, headers=headers) # print(request.text) @@ -229,7 +229,8 @@ def insert_sensor(offering, procedure, foi, sensor_type): "observationType": [ "http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_TextObservation" ], - "featureOfInterestType": "http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint" + "featureOfInterestType": + "http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint" } return body