cartodb-postgresql/scripts-available/CDB_GreatCircle.sql

27 lines
886 B
MySQL
Raw Normal View History

2015-11-24 00:07:24 +08:00
-- Great circle point-to-point routes, based on:
-- http://blog.cartodb.com/jets-and-datelines/
--
2019-05-31 21:29:28 +08:00
CREATE OR REPLACE FUNCTION @extschema@.CDB_GreatCircle(start_point @postgisschema@.geometry, end_point @postgisschema@.geometry, max_segment_length NUMERIC DEFAULT 100000)
RETURNS @postgisschema@.geometry AS $$
DECLARE
2019-05-31 21:29:28 +08:00
line @postgisschema@.geometry;
BEGIN
2019-05-31 21:29:28 +08:00
line = @postgisschema@.ST_Segmentize(
@postgisschema@.ST_Makeline(
2015-11-23 23:51:34 +08:00
start_point,
end_point
)::geography,
max_segment_length
2015-11-23 23:51:34 +08:00
)::geometry;
2019-05-31 21:29:28 +08:00
IF @postgisschema@.ST_XMax(line) - @postgisschema@.ST_XMin(line) > 180 THEN
line = @postgisschema@.ST_Difference(
@postgisschema@.ST_ShiftLongitude(line),
@postgisschema@.ST_Buffer(@postgisschema@.ST_GeomFromText('LINESTRING(180 90, 180 -90)', 4326), 0.00001)
2015-11-23 23:51:34 +08:00
);
END IF;
RETURN line;
END;
$$
2017-10-24 20:16:56 +08:00
LANGUAGE 'plpgsql' IMMUTABLE STRICT PARALLEL SAFE;