- add image import script for Pechgraben images
This commit is contained in:
parent
dc58b7235f
commit
d33e9d2b55
265
db/models.py
Normal file
265
db/models.py
Normal file
|
@ -0,0 +1,265 @@
|
|||
'''
|
||||
Tutorial link: https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
|
||||
Sqlalchemy version: 1.4.31
|
||||
Python version: 3.10
|
||||
'''
|
||||
#!/usr/bin/python# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import datetime
|
||||
# from config import db, ma
|
||||
|
||||
import os
|
||||
from sqlalchemy import (Column, Integer, Sequence,
|
||||
String, DateTime, ForeignKey, Numeric, SmallInteger, create_engine)
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import session, relationship, sessionmaker
|
||||
#from marshmallow import Schema
|
||||
from marshmallow_sqlalchemy import SQLAlchemySchema, SQLAlchemyAutoSchema
|
||||
from marshmallow import fields
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
# from db.pg_models import create_pg_session
|
||||
# import sqlalchemy.orm.session
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
def create_pg_session() -> sessionmaker:
|
||||
""" create postgres db session """
|
||||
load_dotenv(find_dotenv())
|
||||
dbschema = ''
|
||||
db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
db_password = os.environ.get("POSTGIS_DBPASSWORD")
|
||||
db_url = os.environ.get("POSTGIS_DBURL")
|
||||
engine = create_engine(
|
||||
"postgresql+psycopg2://" + db_user + ":" + db_password + "@" + db_url,
|
||||
connect_args={'options': f'-csearch_path={dbschema}'},
|
||||
isolation_level="READ UNCOMMITTED")
|
||||
session_maker = sessionmaker(bind=engine)
|
||||
_session = session_maker()
|
||||
|
||||
# Base.metadata.create_all(engine)
|
||||
return _session
|
||||
|
||||
class Platform(Base):
|
||||
""" Platform class """
|
||||
__tablename__ = 'platform'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
|
||||
id = Column('platform_id', Integer, primary_key=True)
|
||||
identifier = Column('identifier', String)
|
||||
sta_identifier = Column('sta_identifier', String)
|
||||
name = Column('name', String)
|
||||
# datasets = relationship('Dataset')
|
||||
datasets = relationship('Dataset', back_populates="platform", lazy=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'Platform {self.name}'
|
||||
|
||||
|
||||
class Phenomenon(Base):
|
||||
""" phenomenon class """
|
||||
__tablename__ = 'phenomenon'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
|
||||
id = Column('phenomenon_id', Integer, primary_key=True)
|
||||
name = Column('name', String)
|
||||
sta_identifier = Column('sta_identifier', String)
|
||||
# datasets = relationship('Dataset')
|
||||
datasets = relationship('Dataset', back_populates="phenomenon", lazy=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'Phenomenon {self.name}'
|
||||
|
||||
|
||||
class Procedure(Base):
|
||||
""" procedure class """
|
||||
__tablename__ = 'procedure'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
|
||||
id = Column('procedure_id', Integer, primary_key=True)
|
||||
name = Column('name', String)
|
||||
sta_identifier = Column('sta_identifier', String)
|
||||
# datasets = relationship('Dataset')
|
||||
datasets = relationship('Dataset', back_populates="procedure", lazy=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'Procedure {self.name}'
|
||||
|
||||
|
||||
class Dataset(Base):
|
||||
""" dataset class """
|
||||
__tablename__ = 'dataset'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
|
||||
id = Column('dataset_id', Integer, primary_key=True)
|
||||
name = Column('name', String)
|
||||
is_published = Column('is_published', SmallInteger)
|
||||
is_hidden = Column('is_hidden', SmallInteger)
|
||||
dataset_type = Column('dataset_type', String)
|
||||
observation_type = Column('observation_type', String)
|
||||
value_type = Column('value_type', String)
|
||||
|
||||
last_time = Column('last_time', DateTime)
|
||||
last_value = Column('last_value', Numeric(20, 10))
|
||||
fk_last_observation_id = Column(
|
||||
'fk_last_observation_id',
|
||||
Integer
|
||||
)
|
||||
# last_observation = relationship(
|
||||
# "Observation", foreign_keys=[fk_last_observation_id])
|
||||
|
||||
first_time = Column('first_time', DateTime)
|
||||
first_value = Column('first_value', Numeric(20, 10))
|
||||
fk_first_observation_id = Column(
|
||||
'fk_first_observation_id',
|
||||
Integer
|
||||
)
|
||||
# first_observation = relationship("Observation", foreign_keys=[
|
||||
# fk_first_observation_id])
|
||||
|
||||
observations = relationship(
|
||||
'Observation', back_populates='dataset', lazy=True)
|
||||
|
||||
fk_phenomenon_id = Column(
|
||||
'fk_phenomenon_id', Integer, ForeignKey('gba.phenomenon.phenomenon_id'), nullable=False)
|
||||
# phenomenon = relationship("Phenomenon", lazy="joined", foreign_keys=[fk_phenomenon_id])
|
||||
phenomenon = relationship(
|
||||
"Phenomenon", back_populates="datasets", lazy="joined")
|
||||
|
||||
fk_platform_id = Column('fk_platform_id', Integer, ForeignKey(
|
||||
'gba.platform.platform_id'), nullable=True)
|
||||
platform = relationship(
|
||||
"Platform", back_populates="datasets", lazy="joined")
|
||||
|
||||
fk_format_id = Column('fk_format_id', Integer, ForeignKey(
|
||||
'gba.format.format_id'), nullable=True)
|
||||
format = relationship(
|
||||
"Format", back_populates="datasets", lazy="joined")
|
||||
|
||||
fk_procedure_id = Column('fk_procedure_id', Integer, ForeignKey(
|
||||
'gba.procedure.procedure_id'), nullable=False)
|
||||
# procedure = relationship("Procedure", lazy="joined")
|
||||
procedure = relationship(
|
||||
"Procedure", back_populates="datasets", lazy="joined")
|
||||
|
||||
def new_id_factory():
|
||||
''' test '''
|
||||
dbschema = ''
|
||||
db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
db_password = os.environ.get("POSTGIS_DBPASSWORD")
|
||||
db_url = os.environ.get("POSTGIS_DBURL")
|
||||
engine = create_engine(
|
||||
"postgresql+psycopg2://" + db_user + ":" + db_password + "@" + db_url,
|
||||
connect_args={'options': f'-csearch_path={dbschema}'},
|
||||
isolation_level="READ UNCOMMITTED")
|
||||
result = engine.execute('SELECT MAX(observation_id) FROM gba.observation')
|
||||
mytable_max_id = result.first().max
|
||||
if mytable_max_id is None:
|
||||
mytable_max_id = 0
|
||||
mytable_max_id += 1
|
||||
return mytable_max_id
|
||||
|
||||
observation_seq = Sequence('observation_seq', schema="gba") # define sequence explicitly
|
||||
class Observation(Base):
|
||||
""" observation class """
|
||||
__tablename__ = 'observation'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
|
||||
# id = Column('observation_id', Integer, primary_key=True)
|
||||
id = Column('observation_id',
|
||||
Integer,
|
||||
observation_seq,
|
||||
primary_key=True,
|
||||
server_default=observation_seq.next_value())
|
||||
name = Column('name', String)
|
||||
value_type = Column('value_type', String, default="quantity")
|
||||
# pitch = Column('PITCH', String)
|
||||
# roll = Column('ROLL', String)
|
||||
sampling_time_start = Column('sampling_time_start', DateTime)
|
||||
sampling_time_end = Column('sampling_time_end', DateTime)
|
||||
result_time = Column('result_time', DateTime)
|
||||
sta_identifier = Column('sta_identifier', String)
|
||||
value_identifier = Column('value_identifier', String)
|
||||
value_quantity = Column('value_quantity', Numeric(20, 10))
|
||||
value_text = Column('value_text', String)
|
||||
|
||||
fk_dataset_id = Column(Integer, ForeignKey(
|
||||
'gba.dataset.dataset_id'), nullable=False)
|
||||
dataset = relationship("Dataset", back_populates="observations")
|
||||
|
||||
|
||||
class ObservationSchema(SQLAlchemySchema):
|
||||
""" Platform class """
|
||||
DateTime = fields.DateTime(attribute='result_time') # Or vice-versa
|
||||
# value_quantity = fields.Integer(attribute='Value')
|
||||
# id = fields.Integer(attribute='id')
|
||||
Value = fields.Integer(attribute='value_quantity')
|
||||
id = fields.Integer(attribute='value_identifier')
|
||||
# sta_identifier= fields.String(default=uuid.uuid4()),
|
||||
|
||||
class Meta:
|
||||
""" Platform class """
|
||||
model = Observation
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
#pg_session: session = create_pg_session()
|
||||
sqla_session: session = create_pg_session()
|
||||
|
||||
class Format(Base):
|
||||
""" Format class """
|
||||
__tablename__ = 'format'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
id = Column('format_id', Integer, primary_key=True)
|
||||
definition = Column('definition', String(255), index=True)
|
||||
|
||||
datasets = relationship('Dataset', back_populates="format", lazy=True)
|
||||
|
||||
class Person(Base):
|
||||
""" Platform class """
|
||||
__tablename__ = 'accounts'
|
||||
__table_args__ = {"schema": "gba"}
|
||||
person_id = Column('id', Integer, primary_key=True)
|
||||
lname = Column('last_name', String(255), index=True)
|
||||
fname = Column('first_name', String(255))
|
||||
login = Column(String(255))
|
||||
timestamp = Column('updated_at', DateTime, default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User(name='{self.login}', lastname='{self.lname}')>"
|
||||
|
||||
|
||||
class PersonSchema(SQLAlchemyAutoSchema):
|
||||
""" Platform class """
|
||||
class Meta:
|
||||
""" Platform class """
|
||||
model = Person
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
#pg_session: session = create_pg_session()
|
||||
sqla_session: session = create_pg_session()
|
||||
|
||||
|
||||
def create_db():
|
||||
# db_url = 'sqlite:///db.sqlite'
|
||||
# engine = create_engine(db_url, echo = True )
|
||||
# Base.metadata.drop_all(bind=engine)
|
||||
# Base.metadata.create_all(engine)
|
||||
""" create postgres db session """
|
||||
load_dotenv("D:\\Software\\geomon\\.env")
|
||||
dbschema = ''
|
||||
db_user = os.environ.get("POSTGIS_DBUSER")
|
||||
db_password = os.environ.get("POSTGIS_DBPASSWORD")
|
||||
db_url = os.environ.get("POSTGIS_DBURL")
|
||||
engine = create_engine(
|
||||
"postgresql+psycopg2://" + db_user + ":" + db_password + "@" + db_url,
|
||||
connect_args={'options': f'-csearch_path={dbschema}'},
|
||||
isolation_level="READ UNCOMMITTED", echo=True)
|
||||
# session_maker = sessionmaker(bind=engine)
|
||||
# session = session_maker()
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
# Base.metadata.create_all(engine)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_db()
|
|
@ -20,7 +20,7 @@ from dotenv import load_dotenv, find_dotenv
|
|||
from sqlalchemy.orm import session
|
||||
from sqlalchemy import func, asc, desc
|
||||
# from db.pg_models import Platform
|
||||
from gschliefgraben_glasfaser.models import (
|
||||
from db.models import (
|
||||
ObservationSchema, Person, PersonSchema, Observation,
|
||||
create_pg_session, Dataset, Procedure, Phenomenon, Platform)
|
||||
from gschliefgraben_glasfaser.my_api import MyApi
|
||||
|
@ -206,7 +206,7 @@ def create_observation(observation_json: ObservationSchema, db_session,
|
|||
schema = ObservationSchema()
|
||||
# deserialize to python object
|
||||
new_observation: Observation = schema.load(observation_json)
|
||||
new_observation.id = max_id
|
||||
# new_observation.id = max_id
|
||||
new_observation.sta_identifier = str(uuid.uuid4())
|
||||
new_observation.sampling_time_start = new_observation.result_time
|
||||
new_observation.sampling_time_end = new_observation.result_time
|
||||
|
@ -223,7 +223,7 @@ def create_observation(observation_json: ObservationSchema, db_session,
|
|||
return max_id
|
||||
# Otherwise, nope, person exists already
|
||||
else:
|
||||
print(409, f'Observation {ob_id} exists already')
|
||||
# print(409, f'Observation {ob_id} exists already')
|
||||
return max_id
|
||||
|
||||
|
||||
|
@ -269,6 +269,6 @@ def create(person_json: PersonSchema):
|
|||
|
||||
if __name__ == "__main__":
|
||||
load_dotenv(find_dotenv())
|
||||
print('sensors: {}'.format(os.environ.get(
|
||||
'GLASFASER_GSCHLIEFGRABEN_SENSORS', [])))
|
||||
sensor_list1 = os.environ.get('GLASFASER_GSCHLIEFGRABEN_SENSORS', [])
|
||||
print(f'sensors: {sensor_list1} .')
|
||||
main()
|
||||
|
|
|
@ -13,7 +13,7 @@ from dotenv import load_dotenv, find_dotenv
|
|||
from sqlalchemy.orm import session
|
||||
from sqlalchemy import func, asc, desc
|
||||
# from db.pg_models import Platform
|
||||
from gschliefgraben_glasfaser.models import (
|
||||
from db.models import (
|
||||
ObservationSchema, Observation, create_pg_session,
|
||||
Dataset, Procedure, Phenomenon, Platform)
|
||||
from gschliefgraben_glasfaser.my_api import MyApi
|
||||
|
@ -149,7 +149,7 @@ def create_observation(observation_json: ObservationSchema, db_session, max_id,
|
|||
schema = ObservationSchema()
|
||||
# deserialize to python object
|
||||
new_observation: Observation = schema.load(observation_json)
|
||||
new_observation.id = max_id
|
||||
# new_observation.id = max_id
|
||||
new_observation.sta_identifier = str(uuid.uuid4())
|
||||
new_observation.sampling_time_start = new_observation.result_time
|
||||
new_observation.sampling_time_end = new_observation.result_time
|
||||
|
@ -172,6 +172,6 @@ def create_observation(observation_json: ObservationSchema, db_session, max_id,
|
|||
|
||||
if __name__ == "__main__":
|
||||
load_dotenv(find_dotenv())
|
||||
print('sensors: {}'.format(os.environ.get(
|
||||
'GLASFASER_GSCHLIEFGRABEN_SENSORS', [])))
|
||||
sensor_list1 = os.environ.get('GLASFASER_GSCHLIEFGRABEN_SENSORS', [])
|
||||
print(f'sensors: {sensor_list1} .')
|
||||
main()
|
||||
|
|
17
notes.txt
17
notes.txt
|
@ -42,3 +42,20 @@ https://medium.com/dataexplorations/sqlalchemy-orm-a-more-pythonic-way-of-intera
|
|||
https://stackoverflow.com/questions/51737548/how-to-set-primary-key-auto-increment-in-sqlalchemy-orm
|
||||
|
||||
|
||||
<sml:output name=\"HumanVisualPerception\">
|
||||
<swe:Category definition=\"http://www.opengis.net/def/property/humanVisualPerception\">
|
||||
<swe:codeSpace xlink:href=\"NOT_DEFINED\"/>
|
||||
</swe:Category>
|
||||
</sml:output>
|
||||
|
||||
|
||||
|
||||
<sml:output name=\"Image\">
|
||||
<swe:DataRecord>
|
||||
<swe:field name=\"manuel_observation\">
|
||||
<swe:Text definition=\"manuel_observation\"/>
|
||||
</swe:field>
|
||||
</swe:DataRecord>
|
||||
</sml:output>
|
||||
|
||||
<swe:field name="wmo_weather_code"><swe:Category definition="https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM"><swe:codeSpace xlink:href="NOT_DEFINED"/></swe:Category></swe:field> -->
|
|
@ -10,6 +10,12 @@
|
|||
<gml:identifier codeSpace=\"uniqueID\">{procedure_identifier}</gml:identifier>
|
||||
<sml:identification>
|
||||
<sml:IdentifierList>
|
||||
<sml:identifier>
|
||||
<sml:Term definition=\"urn:ogc:def:identifier:OGC:1.0:longName\">
|
||||
<sml:label>longName</sml:label>
|
||||
<sml:value>{procedure_name}</sml:value>
|
||||
</sml:Term>
|
||||
</sml:identifier>
|
||||
<sml:identifier>
|
||||
<sml:Term definition=\"urn:ogc:def:identifier:OGC:1.0:shortName\">
|
||||
<sml:label>shortName</sml:label>
|
||||
|
@ -28,6 +34,20 @@
|
|||
</sml:capability>
|
||||
</sml:CapabilityList>
|
||||
</sml:capabilities>
|
||||
<sml:capabilities name=\"metadata\">
|
||||
<sml:CapabilityList> <!-- status indicates, whether sensor is insitu (true) or remote (false) -->
|
||||
<sml:capability name=\"insitu\">
|
||||
<swe:Boolean definition=\"insitu\">
|
||||
<swe:value>true</swe:value>
|
||||
</swe:Boolean>
|
||||
</sml:capability> <!-- status indicates, whether sensor is mobile (true) or fixed/stationary (false) -->
|
||||
<sml:capability name=\"mobile\">
|
||||
<swe:Boolean definition=\"mobile\">
|
||||
<swe:value>false</swe:value>
|
||||
</swe:Boolean>
|
||||
</sml:capability>
|
||||
</sml:CapabilityList>
|
||||
</sml:capabilities>
|
||||
<sml:featuresOfInterest>
|
||||
<sml:FeatureList definition=\"http://www.opengis.net/def/featureOfInterest/identifier\">
|
||||
<swe:label>featuresOfInterest</swe:label>
|
||||
|
@ -48,10 +68,10 @@
|
|||
</sml:featuresOfInterest>
|
||||
<sml:outputs>
|
||||
<sml:OutputList>
|
||||
<sml:output name=\"Image\">
|
||||
<sml:output name=\"HumanVisualPerception\">
|
||||
<swe:DataRecord>
|
||||
<swe:field name=\"manuel_observation\">
|
||||
<swe:Text definition=\"manuel_observation\"/>
|
||||
<swe:field name=\"HumanVisualPerception\">
|
||||
<swe:Text definition=\"HumanVisualPerception\"/>
|
||||
</swe:field>
|
||||
</swe:DataRecord>
|
||||
</sml:output>
|
||||
|
@ -61,19 +81,19 @@
|
|||
<swe:Vector referenceFrame=\"urn:ogc:def:crs:EPSG::4326\">
|
||||
<swe:coordinate name=\"easting\">
|
||||
<swe:Quantity axisID=\"x\">
|
||||
<swe:uom code=\"degree\" />
|
||||
<swe:uom code=\"degree\"/>
|
||||
<swe:value>{cord_x}</swe:value>
|
||||
</swe:Quantity>
|
||||
</swe:coordinate>
|
||||
<swe:coordinate name=\"northing\">
|
||||
<swe:Quantity axisID=\"y\">
|
||||
<swe:uom code=\"degree\" />
|
||||
<swe:uom code=\"degree\"/>
|
||||
<swe:value>{cord_y}</swe:value>
|
||||
</swe:Quantity>
|
||||
</swe:coordinate>
|
||||
<swe:coordinate name=\"altitude\">
|
||||
<swe:Quantity axisID=\"z\">
|
||||
<swe:uom code=\"m\" />
|
||||
<swe:uom code=\"m\"/>
|
||||
<swe:value>{height}</swe:value>
|
||||
</swe:Quantity>
|
||||
</swe:coordinate>
|
||||
|
|
|
@ -111,8 +111,8 @@
|
|||
<swes:observableProperty>http://www.opengis.net/def/property/humanVisualPerception</swes:observableProperty>
|
||||
<swes:metadata>
|
||||
<sos:SosInsertionMetadata>
|
||||
<sos:observationType>http://www.opengis.net/def/observationType/OGCOM/2.0/OM_CategoryObservation</sos:observationType>
|
||||
<sos:featureOfInterestType>http://www.opengis.net/def/samplingFeatureType/OGCOM/2.0/SF_SamplingPoint</sos:featureOfInterestType>
|
||||
<sos:observationType>http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_CategoryObservation</sos:observationType>
|
||||
<sos:featureOfInterestType>http://www.opengis.net/def/samplingFeatureType/OGC-OM/2.0/SF_SamplingPoint</sos:featureOfInterestType>
|
||||
</sos:SosInsertionMetadata>
|
||||
</swes:metadata>
|
||||
</swes:InsertSensor>
|
File diff suppressed because one or more lines are too long
165
pechgraben_images/import_image_observations.py
Normal file
165
pechgraben_images/import_image_observations.py
Normal file
|
@ -0,0 +1,165 @@
|
|||
'''
|
||||
Sqlalchemy version: 1.2.15
|
||||
Python version: 3.7
|
||||
'''
|
||||
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import session
|
||||
from sqlalchemy import asc, desc
|
||||
from exif import Image
|
||||
from db.models import (
|
||||
create_pg_session, Observation,
|
||||
Dataset, Procedure, Phenomenon, Platform, Format)
|
||||
|
||||
def main():
|
||||
''' main method '''
|
||||
pg_session: session = create_pg_session()
|
||||
platform_sta_identifier = "pechgraben_images"
|
||||
sensor = "camera2"
|
||||
|
||||
pg_query = pg_session.query(Dataset) \
|
||||
.join(Procedure) \
|
||||
.join(Phenomenon) \
|
||||
.filter(Procedure.sta_identifier == sensor.lower())
|
||||
visual_perception_dataset: Dataset = pg_query.filter(
|
||||
Phenomenon.sta_identifier == "HumanVisualPerception").first()
|
||||
if not visual_perception_dataset:
|
||||
print("Sensor " + sensor + " ist noch nicht angelegt!")
|
||||
exit()
|
||||
if not visual_perception_dataset.is_published:
|
||||
visual_perception_dataset.is_published = 1
|
||||
visual_perception_dataset.is_hidden = 0
|
||||
visual_perception_dataset.dataset_type = "timeseries"
|
||||
visual_perception_dataset.observation_type = "simple"
|
||||
visual_perception_dataset.value_type = "text"
|
||||
pg_session.commit()
|
||||
|
||||
platform_exists: bool = pg_session.query(Platform.id).filter_by(
|
||||
sta_identifier=platform_sta_identifier).scalar() is not None
|
||||
if platform_exists:
|
||||
sensor_platform = pg_session.query(Platform.id) \
|
||||
.filter(Platform.sta_identifier == platform_sta_identifier) \
|
||||
.first()
|
||||
visual_perception_dataset.fk_platform_id = sensor_platform.id
|
||||
|
||||
format_exists: bool = pg_session.query(Format.id).filter_by(
|
||||
definition="http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_TextObservation"
|
||||
).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_TextObservation") \
|
||||
.first()
|
||||
visual_perception_dataset.fk_format_id = sensor_format.id
|
||||
|
||||
# import all the images for the given sensor names
|
||||
import_images(visual_perception_dataset, pg_session)
|
||||
|
||||
# save first and last values of all the observations
|
||||
first_observation: Observation = pg_session.query(Observation) \
|
||||
.filter(Observation.fk_dataset_id == visual_perception_dataset.id) \
|
||||
.order_by(asc('sampling_time_start')) \
|
||||
.first()
|
||||
if first_observation is not None:
|
||||
visual_perception_dataset.first_time = first_observation.sampling_time_start
|
||||
# visual_perception_dataset.first_value = first_observation.value_quantity
|
||||
visual_perception_dataset.fk_first_observation_id = first_observation.id
|
||||
|
||||
last_observation: Observation = pg_session.query(Observation) \
|
||||
.filter(Observation.fk_dataset_id == visual_perception_dataset.id) \
|
||||
.order_by(desc('sampling_time_start')) \
|
||||
.first()
|
||||
if last_observation is not None:
|
||||
visual_perception_dataset.last_time = last_observation.sampling_time_start
|
||||
# visual_perception_dataset.last_value = last_observation.value_quantity
|
||||
visual_perception_dataset.fk_last_observation_id = last_observation.id
|
||||
|
||||
pg_session.commit()
|
||||
pg_session.close()
|
||||
|
||||
def import_images(dataset: Dataset, pg_session):
|
||||
''' main method '''
|
||||
folder_path = 'C:/Users/kaiarn/Documents/Fotos'
|
||||
# img_filename = '_DSC9548.JPG'
|
||||
# img_path = f'{folder_path}/{img_filename}'
|
||||
|
||||
# Get the list of image files in the directory that exifread supports
|
||||
directory = os.listdir(folder_path)
|
||||
for file_name in directory:
|
||||
if file_name.endswith(('jpg', 'JPG', 'png', 'PNG', 'tiff', 'TIFF')):
|
||||
file_path = os.path.join(folder_path, file_name)
|
||||
# print(file_path)
|
||||
img_file = open(file_path, 'rb')
|
||||
img: Image = Image(img_file)
|
||||
if img.has_exif:
|
||||
info = f" has the EXIF {img.exif_version}"
|
||||
else:
|
||||
info = "does not contain any EXIF information"
|
||||
# print(f"Image {img_file.name}: {info}")
|
||||
|
||||
# Original datetime that image was taken (photographed)
|
||||
# print(f'DateTime (Original): {img.get("datetime_original")}')
|
||||
datetime_original = img.get("datetime_original")
|
||||
# Grab the date
|
||||
date_obj = datetime.strptime(
|
||||
datetime_original, '%Y:%m:%d %H:%M:%S')
|
||||
# print(date_obj)
|
||||
create_observation(dataset, date_obj, file_name)
|
||||
|
||||
pg_session.commit()
|
||||
|
||||
def create_observation(dataset: Dataset, datetime_original, file_name):
|
||||
"""
|
||||
This function creates a new observation in the people structure
|
||||
based on the passed-in observation data
|
||||
:param observation: person to create in people structure
|
||||
:return: 201 on success, observation on person exists
|
||||
"""
|
||||
|
||||
# deserialize to python object
|
||||
new_observation: Observation = Observation()
|
||||
# new_observation.id = max_id
|
||||
new_observation.sta_identifier = str(uuid.uuid4())
|
||||
new_observation.result_time = datetime_original
|
||||
new_observation.sampling_time_start = new_observation.result_time
|
||||
new_observation.sampling_time_end = new_observation.result_time
|
||||
new_observation.value_type = "text"
|
||||
new_observation.value_text = "https://geomon.geologie.ac.at/images/" + file_name
|
||||
new_observation.fk_dataset_id = dataset.id
|
||||
|
||||
# Add the person to the database
|
||||
dataset.observations.append(new_observation)
|
||||
# db_session.commit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# load_dotenv(find_dotenv())
|
||||
# print('sensors: {}'.format(os.environ.get(
|
||||
# 'GLASFASER_GSCHLIEFGRABEN_SENSORS', [])))
|
||||
main()
|
||||
|
||||
# print(img.list_all())
|
||||
# print(img.has_exif)
|
||||
# # Make of device which captured image: NIKON CORPORATION
|
||||
# print(f'Make: {img.get("make")}')
|
||||
|
||||
# # Model of device: NIKON D7000
|
||||
# print(f'Model: {img.get("model")}')
|
||||
|
||||
# # Software involved in uploading and digitizing image: Ver.1.04
|
||||
# print(f'Software: {img.get("software")}')
|
||||
|
||||
# # Name of photographer who took the image: not defined
|
||||
# print(f'Artist: {img.get("artist")}')
|
||||
|
||||
# # Original datetime that image was taken (photographed)
|
||||
# print(f'DateTime (Original): {img.get("datetime_original")}')
|
||||
|
||||
# # Details of flash function
|
||||
# print(f'Flash Details: {img.get("flash")}')
|
||||
|
||||
# print(f"Coordinates - Image")
|
||||
# print("---------------------")
|
||||
# print(f"Latitude: {img.copyright} {img.get('gps_latitude_ref')}")
|
||||
# print(f"Longitude: {img.get('gps_longitude')} {img.get('gps_longitude_ref')}\n")
|
|
@ -1,78 +0,0 @@
|
|||
'''
|
||||
Sqlalchemy version: 1.2.15
|
||||
Python version: 3.7
|
||||
'''
|
||||
|
||||
import os
|
||||
from datetime import datetime
|
||||
from exif import Image
|
||||
|
||||
|
||||
def main():
|
||||
''' main method '''
|
||||
folder_path = 'C:/Users/kaiarn/Documents/Fotos'
|
||||
# img_filename = '_DSC9548.JPG'
|
||||
# img_path = f'{folder_path}/{img_filename}'
|
||||
|
||||
# Get the list of image files in the directory that exifread supports
|
||||
directory = os.listdir(folder_path)
|
||||
|
||||
for files in directory:
|
||||
if files.endswith(('jpg', 'JPG', 'png', 'PNG', 'tiff', 'TIFF')):
|
||||
file_path = os.path.join(folder_path, files)
|
||||
# print(file_path)
|
||||
img_file = open(file_path, 'rb')
|
||||
img: Image = Image(img_file)
|
||||
if img.has_exif:
|
||||
info = f" has the EXIF {img.exif_version}"
|
||||
else:
|
||||
info = "does not contain any EXIF information"
|
||||
print(f"Image {img_file.name}: {info}")
|
||||
|
||||
# Original datetime that image was taken (photographed)
|
||||
# print(f'DateTime (Original): {img.get("datetime_original")}')
|
||||
datetime_original = img.get("datetime_original")
|
||||
# print(datetime_original)
|
||||
# Grab the date
|
||||
date_obj = datetime.strptime(
|
||||
datetime_original, '%Y:%m:%d %H:%M:%S')
|
||||
print(date_obj)
|
||||
# print(f"Longitude: {img.get('gps_longitude')} {img.get('gps_longitude_ref')}\n")
|
||||
|
||||
# with open(img_path, 'rb') as img_file:
|
||||
# img = Image(img_file)
|
||||
# if img.has_exif:
|
||||
# info = f" has the EXIF {img.exif_version}"
|
||||
# else:
|
||||
# info = "does not contain any EXIF information"
|
||||
# print(f"Image {img_file.name}: {info}")
|
||||
|
||||
# print(img.list_all())
|
||||
# print(img.has_exif)
|
||||
# # Make of device which captured image: NIKON CORPORATION
|
||||
# print(f'Make: {img.get("make")}')
|
||||
|
||||
# # Model of device: NIKON D7000
|
||||
# print(f'Model: {img.get("model")}')
|
||||
|
||||
# # Software involved in uploading and digitizing image: Ver.1.04
|
||||
# print(f'Software: {img.get("software")}')
|
||||
|
||||
# # Name of photographer who took the image: not defined
|
||||
# print(f'Artist: {img.get("artist")}')
|
||||
|
||||
# # Original datetime that image was taken (photographed)
|
||||
# print(f'DateTime (Original): {img.get("datetime_original")}')
|
||||
|
||||
# # Details of flash function
|
||||
# print(f'Flash Details: {img.get("flash")}')
|
||||
|
||||
# print(f"Coordinates - Image")
|
||||
# print("---------------------")
|
||||
# print(f"Latitude: {img.copyright} {img.get('gps_latitude_ref')}")
|
||||
# print(f"Longitude: {img.get('gps_longitude')} {img.get('gps_longitude_ref')}\n")
|
||||
if __name__ == "__main__":
|
||||
# load_dotenv(find_dotenv())
|
||||
# print('sensors: {}'.format(os.environ.get(
|
||||
# 'GLASFASER_GSCHLIEFGRABEN_SENSORS', [])))
|
||||
main()
|
Loading…
Reference in New Issue
Block a user