Open alrasyidlathif opened 3 months ago
fetch data with order by nearest from a point
CREATE EXTENSION earthdistance CASCADE;
SELECT *, ROUND(earth_distance(ll_to_earth(42.1, 19.1), ll_to_earth(lat, lng))::NUMERIC, 2) AS distance
FROM
cities
WHERE
earth_box(ll_to_earth (42.1, 19.1), 10000) @> ll_to_earth (lat, lng)
AND earth_distance(ll_to_earth (42.1, 19.1), ll_to_earth (lat, lng)) < 10000
ORDER BY
distance;
source: https://postindustria.com/postgresql-geo-queries-made-easy/
solve TSP given a distance matrix with postgres
create extension postgis;
create extension pgrouting;
pgr_dijkstra(Edges SQL, start_vid, end_vid)
RETURNS SET OF (seq, path_seq, node, edge, cost, agg_cost) or EMPTY SET
example:
SELECT * FROM pgr_dijkstra(
'SELECT id, source, target, cost, reverse_cost FROM edge_table',
2, 3
);
result:
seq | path_seq | node | edge | cost | agg_cost
-----+----------+------+------+------+----------
1 | 1 | 2 | 4 | 1 | 0
2 | 2 | 5 | 8 | 1 | 1
3 | 3 | 6 | 9 | 1 | 2
4 | 4 | 9 | 16 | 1 | 3
5 | 5 | 4 | 3 | 1 | 4
6 | 6 | 3 | -1 | 0 | 5
(6 rows)
fetch data-data yang ada dalam radius tertentu dari suatu titik
source: https://medium.com/@sarvesh10n/efficient-technique-to-fetch-data-based-on-location-7d1caa261867