Skip to content

Commit

Permalink
Siren works for all use cases now (Not tested enough yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
achirkin committed Feb 4, 2017
1 parent 0eadae6 commit 560e02d
Show file tree
Hide file tree
Showing 7 changed files with 3,383 additions and 3,359 deletions.
6,661 changes: 3,327 additions & 3,334 deletions apps/hs/qua-server/static/js/qua-view.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/hs/qua-view
4 changes: 4 additions & 0 deletions services/siren/sirenDBconfigure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
CREATE EXTENSION IF NOT EXISTS hstore;
GRANT SELECT, INSERT, REFERENCES ON TABLE spatial_ref_sys to siren;
16 changes: 16 additions & 0 deletions services/siren/sirenDBcreate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DROP DATABASE IF EXISTS sirendb;
DO
$body$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = 'siren') THEN
CREATE USER siren LOGIN PASSWORD 'sirenpass';
END IF;
END
$body$;
CREATE DATABASE sirendb;
GRANT ALL PRIVILEGES ON DATABASE sirendb to siren;


55 changes: 31 additions & 24 deletions services/siren/sql/create_scenario.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ BEGIN
scSRID := NULL;
END IF;

-- parse all features
CREATE TEMP TABLE features ON COMMIT DROP AS
( WITH fcs AS (SELECT jsonb_array_elements(fc -> 'features') AS data)
SELECT CAST(coalesce(nullif(fcs.data -> 'properties' ->> 'geomID',''),nullif(fcs.data ->> 'id','')) AS integer) as geomID
, ST_GeomFromGeoJSON(fcs.data ->> 'geometry') as geom
, (jsonb_strip_nulls(fcs.data -> 'properties') - 'geomID') AS props
FROM fcs
);
CREATE TEMP SEQUENCE geomID_seq;
SELECT setval('geomID_seq', max(geomID)) FROM features INTO geomID_max;

IF scSRID IS NOT NULL
AND EXISTS ( SELECT 1 FROM spatial_ref_sys WHERE srid = scSRID )
Expand All @@ -53,29 +63,37 @@ BEGIN
-- user-defined srids can be in range from 910000 and below 999000
SELECT (GREATEST(910000, (SELECT MAX(srid) FROM spatial_ref_sys))+1) INTO scSRID;

-- add a custom metric reference system with center at specified latitude and longitude
INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text)
VALUES ( scSRID
SELECT scSRID
, scName
, NULL
, 'PROJCS["' || scName || '"'
,('PROJCS["' || scName || '"'
|| ',GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]]'
|| ',PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]]'
|| ',UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]'
|| ',AUTHORITY["EPSG","4326"]]'
|| ',PROJECTION["Lambert_Azimuthal_Equal_Area"]'
|| ',PARAMETER["latitude_of_center",' || cast(coalesce(scLat, 0) as text) || ']'
|| ',PARAMETER["longitude_of_center",' || cast(coalesce(scLon, 0) as text) || ']'
|| ',PARAMETER["false_easting",0]'
|| ',PARAMETER["false_northing",0]'
|| ',PROJECTION["Transverse_Mercator"]'
|| ',PARAMETER["latitude_of_origin",' || cast(coalesce(scLat, 0) as text) || ']'
|| ',PARAMETER["central_meridian",' || cast(coalesce(scLon, 0) as text) || ']'
|| ',PARAMETER["scale_factor",1]'
|| ',PARAMETER["false_easting",' || cast(coalesce(ST_X(center), 0) as text) || ']'
|| ',PARAMETER["false_northing",' || cast(coalesce(ST_Y(center), 0) as text) || ']'
|| ',UNIT["metre",1,AUTHORITY["EPSG","9001"]]'
|| ']'
, '+proj=laea +lat_0=' || cast(coalesce(scLat, 0) as text)
|| ' +lon_0=' || cast(coalesce(scLon, 0) as text)
|| ' +ellps=WGS84 +units=m +no_defs'
);
|| ']')
,('+proj=tmerc +lat_0=' || cast(coalesce(scLat, 0) as text)
|| ' +lon_0=' || cast(coalesce(scLon, 0) as text)
|| ' +y_0=' || cast(coalesce(ST_Y(center), 0) as text)
|| ' +x_0=' || cast(coalesce(ST_X(center), 0) as text)
|| ' +ellps=WGS84 +k=1 +units=m +no_defs')
FROM (SELECT st_centroid(st_union(geom)) AS center
FROM features
) q;
END IF;

-- transform feature geometry to be in common reference system WGS84
UPDATE features
SET geom = ST_Force3D(ST_Transform(ST_SetSRID(geom, scSRID), 4326));

-- create a new scenario and keep its id in ScID variable
INSERT INTO scenario (name, lon, lat, alt, srid) VALUES (scName, scLon, scLat, scAlt, scSRID) RETURNING scenario.id INTO ScID;

Expand All @@ -88,17 +106,6 @@ BEGIN
FROM scenario_prop_history ph
WHERE ph.ts_update = curTime AND ph.scenario_id = ScID;

-- parse all features
CREATE TEMP TABLE features ON COMMIT DROP AS
( WITH fcs AS (SELECT jsonb_array_elements(fc -> 'features') AS data)
SELECT CAST(coalesce(nullif(fcs.data -> 'properties' ->> 'geomID',''),nullif(fcs.data ->> 'id','')) AS integer) as geomID
, ST_Force3D(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON(fcs.data ->> 'geometry'), scSRID), 4326)) as geom
, (jsonb_strip_nulls(fcs.data -> 'properties') - 'geomID') AS props
FROM fcs
);
CREATE TEMP SEQUENCE geomID_seq;
SELECT setval('geomID_seq', max(geomID)) FROM features INTO geomID_max;

-- fill in null geomIDs
UPDATE features
SET geomID = nextval('geomID_seq')
Expand Down
2 changes: 2 additions & 0 deletions services/siren/sql/get_last_sc_update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ BEGIN
geometry_output := jsonb_build_object
( 'format' , 'GeoJSON'
, 'name' , (SELECT name FROM scenario WHERE id = ScID)
, 'ScID' , ScID
, 'geometry' , features
, 'properties' , ( SELECT jsonb_object_agg(ph.name, ph.value)
FROM scenario_prop_history ph
Expand All @@ -59,6 +60,7 @@ BEGIN

RETURN jsonb_build_object
( 'geometry_output' , geometry_output
, 'ScID' , ScID
, 'created' , (SELECT round(EXTRACT(epoch FROM MIN(ts_update))) FROM sc_geometry_history WHERE scenario_id = ScID)
, 'lastmodified' , round(EXTRACT(epoch FROM lastTime))
);
Expand Down
2 changes: 2 additions & 0 deletions services/siren/sql/get_scenario.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ BEGIN
geometry_output := jsonb_build_object
( 'format' , 'GeoJSON'
, 'name' , (SELECT name FROM scenario WHERE id = ScID)
, 'ScID' , ScID
, 'geometry' , features
, 'properties' , ( SELECT jsonb_object_agg(ph.name, ph.value)
FROM scenario_prop p, scenario_prop_history ph
Expand All @@ -58,6 +59,7 @@ BEGIN

RETURN jsonb_build_object
( 'geometry_output' , geometry_output
, 'ScID' , ScID
, 'created' , (SELECT round(EXTRACT(epoch FROM MIN(ts_update))) FROM sc_geometry_history WHERE scenario_id = ScID)
, 'lastmodified' , (SELECT round(EXTRACT(epoch FROM MAX(ts_update))) FROM sc_geometry_history WHERE scenario_id = ScID)
);
Expand Down

0 comments on commit 560e02d

Please sign in to comment.