patternsqlMinor
MySQL equivalent of a spatial ST_Union aggregate function?
Viewed 0 times
equivalentfunctionmysqlst_unionspatialaggregate
Problem
I'm working on a DB-spacial project in Mysql and I was trying to perform a union operation for a group of shapes. Im looking for something similar to the SQL Server function
for example (In SQL SERVER):
ST_GeomFromWKB do the same job in Mysql.
in the second part and with the
How to perform a similar operation (aggregate+group by) in MySQL?
UnionAggregate, or the PostGIS Spatial Aggregate function ST_Unionfor example (In SQL SERVER):
SELECT
geometry::STGeomFromWKB(Shape,27582),
area_code
FROM area_table
WHERE area_code='xxxxxxx';ST_GeomFromWKB do the same job in Mysql.
in the second part and with the
UnionAggregate function + group by :select
dbo.UnionAggregate((geometry::STGeomFromWKB(Shape,27582)).MakeValid()),
area_code
FROM area_table
WHERE area_code='xxxxxxx'
GROUP BY area_code;How to perform a similar operation (aggregate+group by) in MySQL?
Solution
I found another alternative to perform the union aggregation by using ST_Union. I had some issues cause this Mysql function get only 2 parameters as inputs.
As a solution, I create my own function as follow :
This function enable an aggregate union over shapes for each "area code" which is what UnionAggregate do and what I need.
As a solution, I create my own function as follow :
CREATE FUNCTION `fct_create_geom_collection`(ac CHAR(8))
RETURNS GEOMETRY
BEGIN
DECLARE position INT;
DECLARE sortie BOOLEAN DEFAULT FALSE;
-- get all area_IDs
DECLARE curgeom CURSOR FOR
SELECT DISTINCT area_ID
FROM area_table
WHERE area_code= ac ORDER BY area_ID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET sortie = TRUE;
OPEN curgeom;
-- get the first area_id
FETCH curgeom INTO position ;
-- put the shape into @var_g_0 for this area_id
SELECT ST_GeomFromWKB(Shape,27582) INTO @var_g_0
FROM area_table
WHERE area_ID = position ;
LLD: LOOP
IF sortie THEN
LEAVE LLD;
END IF;
-- get the second area_id
FETCH curgeom INTO position ;
-- put the second shape into @var_g for this area_id
SELECT ST_GeomFromWKB(Shape,27582) INTO @var_g
FROM area_table
WHERE area_ID = position ;
-- performing a union operation between @var_g_0 and @var_g
SET @geom = ST_UNION(@var_g_0,@var_g); -- enclosed st_union
-- put the result into @var_g_0
SET @var_g_0 = @geom;
END LOOP;
CLOSE curgeom;
RETURN ST_GeomFromText(ST_ASTEXT(@geom));
-- RETURN ST_GeomFromWKB(@geom);
END$This function enable an aggregate union over shapes for each "area code" which is what UnionAggregate do and what I need.
Code Snippets
CREATE FUNCTION `fct_create_geom_collection`(ac CHAR(8))
RETURNS GEOMETRY
BEGIN
DECLARE position INT;
DECLARE sortie BOOLEAN DEFAULT FALSE;
-- get all area_IDs
DECLARE curgeom CURSOR FOR
SELECT DISTINCT area_ID
FROM area_table
WHERE area_code= ac ORDER BY area_ID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET sortie = TRUE;
OPEN curgeom;
-- get the first area_id
FETCH curgeom INTO position ;
-- put the shape into @var_g_0 for this area_id
SELECT ST_GeomFromWKB(Shape,27582) INTO @var_g_0
FROM area_table
WHERE area_ID = position ;
LLD: LOOP
IF sortie THEN
LEAVE LLD;
END IF;
-- get the second area_id
FETCH curgeom INTO position ;
-- put the second shape into @var_g for this area_id
SELECT ST_GeomFromWKB(Shape,27582) INTO @var_g
FROM area_table
WHERE area_ID = position ;
-- performing a union operation between @var_g_0 and @var_g
SET @geom = ST_UNION(@var_g_0,@var_g); -- enclosed st_union
-- put the result into @var_g_0
SET @var_g_0 = @geom;
END LOOP;
CLOSE curgeom;
RETURN ST_GeomFromText(ST_ASTEXT(@geom));
-- RETURN ST_GeomFromWKB(@geom);
END$$Context
StackExchange Database Administrators Q#165606, answer score: 3
Revisions (0)
No revisions yet.