I've got regional data loaded into a PostGIS table and want to merge regions that share the same parent name (and ultimately output the data as KML.)
This is my query (I'm using php):
SELECT ST_AsTEXT(geom) FROM ST_UNION(ARRAY(SELECT geom FROM RegionsRaw WHERE (ParentName = '" . $parentName . "'))) AS geom;
When I run this script, I'm getting strange results:
It's pretty obvious that certain areas are a little messed up and I have a hunch that it's because some of the polygons I'm merging are next to each other (where I'm seeing success) while others are islands and separated from the rest (getting weird results).
Looking for some guidance from the community on this, thanks!
Best Answer
The most common reason to get results like this is that the original polygons are invalid. You can check with:
If you get any rows in return you have bad polygons.
But you can try to fix them. In PostGIS 2.0 ther will be a function called ST_MakeValid which will try to fix the polygons.
But if you are not running some beta-release of 2.0 you can try with a buffer with 0 as argument.
Another note is that there is no reason to make an array before unioning. ST_Union also works as an aggregate function, so try something like:
if you want the text output of the result.
Also note that if you want one unioned polygon for each "parentName" you can just group by parentName instead and get them all at once. One on each row
HTH
Nicklas