PostGIS "union" like in ArcGIS - postgis

Is there a solution in postgis that will allow me to "union" two shapes / sets of shapes, where the output is unmerged? That is to say, it should flatten the inputs, but maintain any lines / splits / polygon boundaries in the output. Essentially I'd like it to work like the union tool in ArcMap. The true union in postgis merges the outputs. Any thoughts appreciated. Thanks!

Try ST_Intersection:
SELECT distinct ST_Intersection(t1.geom,t2.geom) as geom
FROM test_polygon AS t1 CROSS JOIN test_polygon AS t2;

Related

Flink:How to get the value after union other datastream?

eg.
I have two DataStream<Tuple4<String, String, Date, String>> named ds1 and ds2, DataStream ds3 = ds1.union(ds2).Then I want to know How can I get the value of ds1.f2 and ds2.f2 from ds3.
Thanks.
Stream union in Flink is the same as the union operation on multisets -- you just get a bigger stream will all of the elements from the two input streams.
So, in other words, a Union is not a Join. ds3.f2 is a value that previously was either ds1.f2 or ds2.f2 for some Tuple in one of those streams.
Depending on what you are trying to accomplish, you could add a fifth element to each Tuple so you would know its origin. Or you might rather use some sort of Join operation to combine the two streams. See the documentation for window joins, table joins, sql joins, and low-level joins.

TSQL - Geography: Which polygon?

Using SQL Server, when I get a result of 1 for the expression #multipolygon.STIntersects(#points), indicating that the point is within one of the polygons comprising the multi-polygon
is there a way of finding out which polygon inside the many within the multi-polygon actually contains the point?
I've used something like this before:
select *
from dbo.Numbers as n
where #point.STIntersects(#multipolygon.STGeometryN(n.Number)) = 1
and n.Number <= #multipolygon.STNumGeometries();
Where dbo.Numbers is a tally table. This query will return a 1-based index of which polygon(s) matched. If you want the polygons themselves as well, add STGeometry(n.Number) to the select list.
Try splitting the single multi-polygon row into many, single-polygon rows in a query and then doing the intersect, that will return only matching rows.
I haven't done anything like that myself, but this link might help https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d99cef8e-d345-44ee-87e1-f9d4df851c35/multipolygon-results-split-into-polygons?forum=sqlspatial

How to populate a CTE with a list of values in Sqlite

I am working with SQLite and straight C. I have a C array of ids of length N that I am compiling into a string with the following format:
'id1', 'id2', 'id3', ... 'id[N]'
I need to build queries to do several operations that contain comparisons to this list of ids, an example of which might be...
SELECT id FROM tableX WHERE id NOT IN (%s);
... where %s is replaced by the string representation of my array of ids. For complicated queries and high values of N, this obviously produces some very ungainly queries, and I would like to clean them up using common-table-expressions. I have tried the following:
WITH id_list(id) AS
(VALUES(%s))
SELECT * FROM id_list;
This doesn't work because SQLite expects a column for for each value in my string. My other failed attempt was
WITH id_list(id) AS
(SELECT (%s))
SELECT * FROM id_list;
but this throws a syntax error at the comma. Does SQLite syntax exist to accomplish what I'm trying to do?
SQLite supports VALUES clauses with multiple rows, so you can write:
WITH id_list(id) AS (VALUES ('id1'), ('id2'), ('id3'), ...
However, this is not any more efficient than just listing the IDs in IN.
You could write all the IDs into a temporary table, but this would not make sense unless you have measured the performance improvement.
One solution I have found is to reformat my string as follows:
VALUES('id1') UNION VALUES('id2') ... UNION VALUES('id[N]')
Then, the following query achieves the desired result:
WITH id_list(id) AS
(%s)
SELECT * FROM id_list;
However, I am not totally satisfied with this solution. It seems inefficient.

How to make a polygon with a hole

I'm using postgresSQL with postgis and I'm trying to create a polygon with a hole. I read a lot of users asking for something similar, but I can't do it for myself. My code is
insert into distritos(nombre,geom) values ('Distrito6',ST_MakePolygon(ST_GeomFromText('LINESTRING((-15.66486 27.91996, -15.60610 27.91820, -15.60359 27.97169, -15.66586 27.97144,-15.66486 27.91996),(-15.65753 27.95894, -15.61610 27.95995, -15.61459 27.93157, -15.65477 27.27.93007,-15.65753 27.95894))',4258)));
but it doesn't work. What do I have to do?
A Polygon has a double parenthesis at the start and end, and inner rings are delineated by using single pairs of parenthesis, between commas, ie, ),( whereas a Linestring only has one set of parenthesis at the beginning and end. You are actually using the syntax for a MultiLinestring, which is probably where your issue is. You can fix you query like this:
insert into distritos(nombre, geom) values
('Distrito6', ST_GeomFromText('POLYGON((-15.66486 27.91996,
-15.60610 27.91820, -15.60359 27.97169, -15.66586 27.97144,-15.66486 27.91996),
(-15.65753 27.95894, -15.61610 27.95995, -15.61459 27.93157,
-15.65477 27.93007,-15.65753 27.95894))',4258));
Also, you do not need to use ST_MakePolygon, as if you are using ST_GeomFromText, you can create the polygon directly by using POLYGON instead of LINESTRING in your example above. ST_MakePolygon is probably more useful when you have arrays of Linestrings representing inner rings.
The Wikipedia WKT article details this very clearly, with WKT and accompanying graphics.
NOTE: You also have an error in you linestring, you have a repeated 27. in 27.27.93007.

PostGIS point attributes to polygons, where poligon contain point

I have a polygon database (bdus) and a point database (bdps) under the same schema in PostGIS. These databases were imported from shapefiles with the Shapefile and DBF loader. What I want to do is to join the point attributes on the polygon layer based on the contain criteria.
So for every polygon that contain one to n points, to add the columns of points to polygons. If there are more than one point a good approach would be to average column values.
Can someone guide me? I am new to PostGreSQL and PostGIS but I managed to run this query
SELECT * FROM bdps
JOIN
bdus
ON
ST_Contains(bdus.the_geom, bdps.the_geom);
which return a table with bdps joined with the corresponding bdus, but I want the reverse.
Thanks in advance for any help!
Did you meant, that you want to create a new polygon with the polygon and the points that satisfy the ST_Contains(polys, points) criteria?
SELECT ST_Union(bdus.the_geom, bdps.the_geom) FROM bdus,bdps WHERE
ST_Contains(bdus.the_geom, bdps.the_geom);

Resources