cartodb-postgresql/scripts-available/CDB_GreatCircle.sql

27 lines
652 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/
--
CREATE OR REPLACE FUNCTION CDB_GreatCircle(start_point geometry, end_point geometry, max_segment_length NUMERIC DEFAULT 100000)
2015-11-23 23:51:34 +08:00
RETURNS geometry AS $$
DECLARE
2015-11-23 23:51:34 +08:00
line geometry;
BEGIN
2015-11-23 23:51:34 +08:00
line = ST_Segmentize(
ST_Makeline(
start_point,
end_point
)::geography,
max_segment_length
2015-11-23 23:51:34 +08:00
)::geometry;
2015-11-23 23:51:34 +08:00
IF ST_XMax(line) - ST_XMin(line) > 180 THEN
line = ST_Difference(
ST_Shift_Longitude(line),
ST_Buffer(ST_GeomFromText('LINESTRING(180 90, 180 -90)', 4326), 0.00001)
);
END IF;
RETURN line;
END;
$$
2015-11-23 23:40:42 +08:00
LANGUAGE 'plpgsql';