PostGIS ordering overlapping polygons - geospatial

Is there a way in PostGIS to order polygons in layers such that when they overlap at a particular x,y point, where I’m looking for intersecting polygons, I can find the one that’s on the top? I am not looking to flatten / cut this data as being able to identify all intersections at a given point is also valuable.

Related

How to choose a certain edge in Geometry Node (Blender 3.4.0 alpha)

two questions about Geometry Nodes.
To retopologize certain mesh, I'd like to subdivide a certain edge that both neighbors are triangle.
Questions:(https://i.stack.imgur.com/kG3Us.png)
1.How to find the edge with Geometry Node?
2.Is there a general strategy to find a specific elements(index of vertex, edge, and face)?
History:
I want to retopo the shape(=A) of being after Convex Hull Node because this is messy.
To do so, I chose a way that is of shrinking a simple shape onto A.
Bounding Box > Subdivide > Set Position is the order of the Nodes, but large areas still remain.
To fit the shape more precisely, I am trying to subdivide adittionaly only on the areas and then finally Set Position Node again to fit to the original messy A.
After I have tried some ideas (bellow), now I am trying to do a way of extruding the face, scaling the top selection to zero, merge the face and the set these new 'vertex' to the messy A.
And I find the edge between the face remain.🤣
This is my question above. How to fit these edges on to A?
Ideas I have tried:
Separate the large areas>Subdivide>Join Geometry>Set Position makes holes.
Separate the large areas>Subdivide>Convex Hull>Boolean Mesh makes messy topology
The way of not subdividing the large area, such as scaling the bounding box up enough to disappear the large area, will result overstretch to other mesh, which looks more difficult to solve, so I prefer to solve large flatten area If I can.
(https://i.stack.imgur.com/QzHKa.jpg)
I want to do retopology. I want to fit a new shape that has a clean topology onto the original messy shape.

Polygon searching

Which of these (k-d tree, r-tree) would be suitable for searching and indexing polygon.
My usecase is that i have been given some lat-long points (min 3 for a valid polygon) and from these points i need to find the polygon which is the smallest one.
By smallest i mean that if there is a polygon inside another polygon, then the inside polygon should be returned.
And if polygon overlap, they should not be chosen.
I think finding the location and then the area might be tried but i am not sure.
I would also like get some idea about which data structure would be useful. I think postgis uses R-tree indexing.
R-tree is good for polygons, because it works on bounding boxes, including all the points for a particular polygon. You can easily find candidate polygons and then do a fine-grain check for overlap, area, etc.
Kd-tree works on points, so polygons would be hard to index.
R-tree also supports adding and removing data items. As I recall a kd-tree, once built, is hard to update for new or changing data.

Determining which polygons a point is within from a large set of polygons

Say I have a 2D plane, covered with polygons (identified as an array of vertexes), analogous to:
Lets say I also have a point with coordinates on this plane, what is the easiest method to return which of the polygons the point is present in?
Although this example lists 4 polygons, it would be simple to run a check on each polygon to see if the point is within it, but I am building a system that presently has about 150 polygons, and could extend up to thousands, so doing it that way could become very slow.
So, are there any solutions to doing this that do not incur iterating through all available polygons, and checking if the point is present?
You can use a kd-tree or a r-tree. It can reduces the search space. You can also look for a quadtree. You can choose the quad size to fit the polygons and to minimize overlapping bounding boxes.

Detecting arbitrary shapes

Greetings,
We have a set of points which represent an intersection of a 3d body and a horizontal plane. We would like to detect the 2D shapes that represent the cross sections of the body. There can be one or more such shapes. We found articles that discuss how to operate on images using Hough Transform, but we may have thousands of such points, so converting to an image is very wasteful. Is there a simpler way to do this?
Thank you
In converting your 3D model to a set of points, you have thrown away the information required to find the intersection shapes. Walk the edge-face connectivity graph of your 3D model to find the edge-plane intersection points in order.
Assuming you have, or can construct, the 3d model topography (some number of vertices, edges between vertices, faces bound by edges):
Iterate through the edge list until you find one that intersects the test plane, add it to a list
Pick one of the faces that share this edge
Iterate through the other edges of that face to find the next intersection, add it to the list
Repeat for the other face that shares that edge until you arrive back at the starting edge
You've built an ordered list of edges that intersect the plane - it's trivial to linearly interpolate each edge to find the intersection points, in order, that form the intersection shape. Note that this process assumes that the face polygons are convex, which in your case they are.
If your volume is concave you'll have multiple discrete intersection shapes, and so you need to repeat this process until all edges have been examined.
There's some java code that does this here
The algorithm / code from the accepted answer does not work for complex special cases, when the plane intersects some vertices of a concave surface. In this case "walking" the edge-face connectivity graph greedily could close some of the polygons before time.
What happens is, that because the plane intersects a vertex, at one point when walking the graph there are two possibilities for the next edge, and it does matter which one is chosen.
A possible solution is to implement a graph traversal algorithm (for instance depth-first search), and choose the longest loop which contains the starting edge.
It looks like you wanted to combine intersection points back into connected figures using some detection or Hough Transform.
Much simpler and more robust way is to immediately get not just intersection points, but contours of 3D body, where the plane cuts it.
To construct contours on the body given by triangular mesh, define the value in each mesh vertex equal to signed distance from the plane (positive on one side of the plane and negative on the other side). The marching squares algorithm for isovalue=0 can be then applied to extract the segments of the contours:
This algorithm works well even when the plane passes through a vertex or an edge of the mesh.
To better understand what is the result of plane section, please take a look at this short video. Following the links there, one can find the implementation as well.

Searching a database of coordinate-bound data for an arbitrary polygonal area

I have a relational database where each entry is marked as a dot with latitude/longitude coordinates. I give the user the ability to mark an arbitrary polygon on a map, and want to return all entries that are within the polygonal shape.
What would be the best way to achieve this?
Also, it might be worth to point out that small errors are ok (ie. if there is an effective way to turn the polygon into a set of rectangles, then that is fine).
Use spatial extensions, most databases have this.
In MySql you can only use them with MyISAM tables which are not transactional.
http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html
One way to quickly cut down on the number of points to consider is to compute the bounding rectangle for the polygon (i.e. just min-x, min-y, max-x, max-y of the points in the polygon), and then select for points within the bounding rectangle (i.e. where x is between min-x and max-x and same for y).
Of course not all these points are necessarily inside the polygon, but now you can hone it with code.
An old hack:
Count the number of times a line connecting <point far away> to <point in question> crosses any of the bounding segments of the polygon.
Even numbers mean the point is outside the polygon
Odd numbers mean it is inside the polygon

Resources