-
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.
feat(back): update rallying_point_display function
- Loading branch information
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
84 changes: 84 additions & 0 deletions
84
back/src/Liane/Liane.Service/Scripts/15_clustered_points.sql
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,84 @@ | ||
DROP FUNCTION rallying_point_display; | ||
|
||
CREATE OR REPLACE FUNCTION rallying_point_display(z integer, x integer, y integer) RETURNS bytea | ||
immutable | ||
strict | ||
parallel safe | ||
language plpgsql | ||
AS | ||
$$ | ||
DECLARE | ||
mvt BYTEA; | ||
BEGIN | ||
|
||
IF z <= 12 THEN -- clustering only for zoom levels 0-12 | ||
WITH points AS ( | ||
SELECT | ||
cluster_id, | ||
st_centroid(st_union(geom)) AS geom, | ||
COUNT(id) AS point_count, | ||
ARRAY_AGG(id) AS ids | ||
FROM | ||
( | ||
SELECT | ||
ST_ClusterKMeans(location, 5) OVER() AS cluster_id, | ||
ST_Centroid(location) as geom, | ||
id | ||
FROM rallying_point | ||
WHERE location @ ST_Transform(ST_TileEnvelope(z, x, y), 4326) | ||
) tsub | ||
GROUP BY cluster_id | ||
), | ||
clusters AS ( | ||
SELECT ST_AsMVT(tile.*, 'rallying_point_display', 4096, 'geom') AS tile | ||
FROM ( | ||
SELECT ST_AsMVTGeom( | ||
ST_Transform(geom, 3857), | ||
ST_TileEnvelope(z, x, y), | ||
4096, 64, TRUE) AS geom, | ||
CASE point_count WHEN 1 THEN NULL ELSE point_count END AS point_count, | ||
ids, | ||
rp.id, | ||
rp.label, | ||
rp.type, | ||
rp.address, | ||
rp.zip_code, | ||
rp.city, | ||
rp.place_count, | ||
rp.is_active | ||
FROM points | ||
LEFT JOIN rallying_point rp ON points.point_count = 1 AND rp.id = ANY (points.ids) | ||
) AS tile | ||
) | ||
SELECT INTO mvt clusters.tile | ||
FROM clusters; | ||
|
||
RETURN mvt; | ||
ELSE | ||
WITH clusters AS ( | ||
SELECT ST_AsMVT(tile.*, 'rallying_point_display', 4096, 'geom') AS tile | ||
FROM ( | ||
SELECT ST_AsMVTGeom( | ||
ST_Transform(location, 3857), | ||
ST_TileEnvelope(z, x, y), | ||
4096, 64, TRUE) AS geom, | ||
rp.id, | ||
rp.label, | ||
rp.type, | ||
rp.address, | ||
rp.zip_code, | ||
rp.city, | ||
rp.place_count, | ||
rp.is_active | ||
FROM rallying_point rp | ||
WHERE location @ ST_Transform(ST_TileEnvelope(z, x, y), 4326) | ||
) AS tile | ||
) | ||
SELECT INTO mvt clusters.tile | ||
FROM clusters; | ||
|
||
RETURN mvt; | ||
END IF; | ||
|
||
END | ||
$$; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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