Generate geometries from each intersection and apply aggregation to row - postgresql

I'm trying to figure out how to use PostGIS (since I have my polygons stored there already) to generate a set of nonoverlapping polygons.
The source table has two columns (geometry and occurences), geometry is always a valid polygon, occurences is an integer.
The polygons in the table can overlap. For all the overlapping regions (there can generally be more than 2 intersecting polygons) I would like to generate a new polygon (as with ST_Intersection) AND apply an aggregation to the occurences column (MIN/MAX/AVG would be enough)
I can find pairs of overlaps by using ST_Intersects and handle the pairs, but I have trouble reducing multiple intersections.
In the following example, the 3 source rows would actually end up being 7 separate polygons (imagine the circle is actually a POLYGON geometry), the occurences then would be averaged from all interesecting polygons in each case.

Related

How do i find all the polygons within n radius from a point in QGIS?

I have two layers, the first has hundreds of polygons all with a unique id.
The second one has thousands of points all with a unique id
Each point has a "area of influence" or a radius of 2 miles.
I need a "list" of all the polygons touching (no need to fully cover) each point's area of influence.

Split geometry based on postgis function

I have a big geometry of a region or country which I want to split. The output should be 10 splitted geometries of one big geometry based on ST_MEMSIZE function of postgis.
For e.g. If threshold of geometry has MEMSIZE 100 and the threshold is 10 then the new table should have 10 splitted geometries. Not sure if a query would do it or if there is postgis function that could do it.
Table
New_Table
and so on.
The result of the below query doesn't show the complete geometry as shown in the image.
select st_subdivide(geom, 200) from regions where region = 'R';
A vertex is 16 bytes. You can then compute how many vertices you need per region and use st_subdivide. The number of regions will however depends on your data complexity and you may have some large region with just 5 vertices surrounded by small polygons with many vertices.
PS: replace 10000 by your threshold
select id,
st_subdivide(geom, floor(st_memsize(geom)/10000/16)::int)
from mylayer;

Intersection over union for rectangles with different orientation

I need to implement an algorithm in swift to find the intersection over union (IoU) between two rectangles with different orientations in 2-dimensional space. I could not find any tutorials or sample codes to teach how to implement such an algorithm.
Could someone provide relevant resources?
You can use O'Rourke algorithm for calculation of intersection of two convex polygons.
C and Java code is availaible on the page for book "Computational Geometry in C"
Algorithm traverses edges of polygons until it finds intersection (using
orientation test). After intersection it chooses "the most inner" edge from two possible next ones to build intersection core polygon (always convex).
When you have ordered list of vertices, you can calculate polygon area with shoelace formula.
To get area of union, we can calculate (thanks to Yves Daoust for hint)
Area(Union) = Area(P) + Area(Q) - Area(Intersection)
Alternatively to the very good solution of MBo/O'Rourke, you can use a sweep line approach.
For convenience, assume that one of the polygons is axis aligned. Then there are two "events" when the line hits the top and bottom sides of the aligned rectangle and four events when the line hits the vertices of the other (depending on the orientation, there are 8 possible permutations of the vertices).
The intersection of the rectangles occurs inside the vertical ranges defined by these events (you perform a merge of the two event sets) and there are up two six intersections to be computed between the oblique sides and the horizontals. For every event line, it is an easy matter to determine the X intervals spanned by both rectangles, and find their intersection or union. And the areas between two event lines are trapezoids.
To cope with rectangles in general position, you can
rotate both polygons to align one of them,
work with a counter-rotated coordinate system,
perform the sweep with an horizontal line anyway without moving the polygons; but then there are 8 events instead of 6 and up to 8 intersection computations.

QGIS - Mapping Ward Areas into Thiessen Polygons

I am designing a water supply distribution network. I have various pipes represented by Lines. Each start and end point of Line is a Junction (Node).
I have created Thiessen Polygons (Voronoi) using QGIS for each Junction (The Thiessen Polygon represents flow area for its corresponding Junction.)
I also have various Ward Boundaries (also represented as irregular Polygons), with each Ward having different Population.
My requirement : I want to calculate area of intersection between each Thiessen Polygon and each Ward and get a table, so that I can calculate the exact population within each Thiessen Polygon.
Example of Thiessen Polygons (The Dot within polygon represents corresponding Junction) :-
Example of Wards (also represented as Closed Polygons) :-
Final output desired :-
I have found the solution in ARCGIS -> Intersect Tool where it intersects Two Shapefiles (each having multiple polygons) and gives intersection results.
I think the same is not possible in QGIS as of now.

Storing areas and centers for polygons with postgresql

I have a table containing polygons that I will want to search by areas and centers. There are postgresql methods that calculate the area and centers of polygons, but as I intend to search on them, should I be writing the data to the table to prevent calculation and speed up searches, or would the data be redundant?