diff --git a/api/app/models/thing.py b/api/app/models/thing.py index bb746c76..2500bdce 100644 --- a/api/app/models/thing.py +++ b/api/app/models/thing.py @@ -13,6 +13,7 @@ # limitations under the License. from app.db.sqlalchemy_db import SCHEMA_NAME, Base +from geoalchemy2 import Geometry from sqlalchemy.dialects.postgresql.json import JSON from sqlalchemy.orm import relationship from sqlalchemy.sql.schema import Column, ForeignKey @@ -36,6 +37,7 @@ class Thing(Base): name = Column(String(255), nullable=False) description = Column(Text, nullable=False) properties = Column(JSON) + observed_area = Column("observedArea", Geometry) commit_id = Column(Integer, ForeignKey(f"{SCHEMA_NAME}.Commit.id")) location = relationship( "Location", secondary=Thing_Location, back_populates="thing" diff --git a/api/app/sta2rest/sta2rest.py b/api/app/sta2rest/sta2rest.py index 69029c94..9a6c64de 100644 --- a/api/app/sta2rest/sta2rest.py +++ b/api/app/sta2rest/sta2rest.py @@ -128,6 +128,7 @@ class STA2REST: "name", "description", "properties", + "observed_area", ], "ThingTravelTime": [ "id", @@ -138,6 +139,7 @@ class STA2REST: "name", "description", "properties", + "observed_area", "system_time_validity", ], "HistoricalLocation": [ diff --git a/database/istsos_schema.sql b/database/istsos_schema.sql index e69627f8..a9730a57 100644 --- a/database/istsos_schema.sql +++ b/database/istsos_schema.sql @@ -72,6 +72,17 @@ CREATE OR REPLACE FUNCTION "Datastreams@iot.navigationLink"(sensorthings."Thing" SELECT '/Things(' || $1.id || ')/Datastreams'; $$ LANGUAGE SQL; +CREATE OR REPLACE FUNCTION "observedArea"(sensorthings."Thing") RETURNS geometry AS $$ +BEGIN + RETURN ( + SELECT ST_ConvexHull(ST_Collect(d."observedArea")) + FROM sensorthings."Datastream" d + WHERE d."thing_id" = $1.id + AND d."observedArea" IS NOT NULL + ); +END; +$$ LANGUAGE plpgsql STABLE; + CREATE TABLE IF NOT EXISTS sensorthings."Thing_Location" ( "thing_id" BIGINT NOT NULL REFERENCES sensorthings."Thing"(id) ON DELETE CASCADE, "location_id" BIGINT NOT NULL REFERENCES sensorthings."Location"(id) ON DELETE CASCADE,