-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add distance to shore for each operation #42
- Loading branch information
1 parent
70eaa6c
commit 6e0b71c
Showing
6 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-- Delete rows where position has changed | ||
delete from operations_points | ||
where operation_id in ( | ||
select op.operation_id | ||
from operations_points op | ||
join operations o on o.operation_id = op.operation_id | ||
where op.latitude is distinct from o.latitude | ||
or op.longitude is distinct from o.longitude | ||
); | ||
|
||
-- Insert new rows that were not yet in the cache | ||
insert into operations_points (operation_id, "point", latitude, longitude) | ||
select | ||
operation_id, | ||
ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography, | ||
latitude, | ||
longitude | ||
from operations | ||
where operation_id not in (select operation_id from operations_points); | ||
|
||
-- Compute distances when it has not been computed yet | ||
update operations_points set | ||
distance_cote_metres = t.distance_cote_metres, | ||
distance_cote_milles_nautiques = t.distance_cote_milles_nautiques | ||
from ( | ||
select | ||
operation_id, | ||
distance_cote_metres, | ||
round((distance_cote_metres*0.000539957)::numeric, 2) distance_cote_milles_nautiques | ||
from ( | ||
select | ||
operation_id, | ||
round(dst / 100.0) * 100 distance_cote_metres | ||
from ( | ||
select | ||
o.operation_id, | ||
ST_Distance(fb.geography, o.point) dst | ||
from operations_points o | ||
join france_boundaries fb on true | ||
where o.point is not null | ||
and o.distance_cote_metres is null | ||
) _ | ||
) _ | ||
) t | ||
where t.operation_id = operations_points.operation_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
create table operations_points ( | ||
"operation_id" bigint primary key, | ||
"point" geography(point) | ||
); | ||
create index operations_points_point_idx on operations_points using gist(point); | ||
|
||
insert into operations_points | ||
select | ||
operation_id, | ||
ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography | ||
from operations; | ||
|
||
vacuum analyze operations_points; | ||
|
||
select | ||
o.operation_id, | ||
ST_Distance(b.geography, o.point)/1000 | ||
from operations_points o | ||
join france_boundaries b on true | ||
where o.point is not null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters