MariaDB spatial query to determine proportion of overlap - mariadb

Background: I have two tables containing geometries (polygons and multipolygons). One table contains large polygons that touch but do not overlap (they are actually administrative boundaries). The other contains smaller polygons (in this case, they are parcels of land).
In most cases, each smaller polygon from the second table will be wholly contained within one of the larger polygons from the first table. This is simple enough to determine with spatial queries (ST_WITHIN). Some, however, will overlap two (or, theoretically, more) admin area polygons.
The question is, for those smaller polygons which overlap two large ones, how can I tell which it is most in?
This image illustrates the situation. Land parcel A is wholly within Leftshire, and B is wholly within Rightshire. This is a simple ST_WITHIN spatial query. I can determine that C overlaps both Leftshire and Rightshire as ST_WITHIN will be false but ST_OVERLAP will be true for both of them. But how can I determine that C is more in Leftshire than it is in Rightshire?
For reference, I am using MariaDB 5.5.64. If the problem is more easily solved using a different version (or even using a different DB), that would be an acceptable fallback, but I would ideally like to solve it using the tools I have to hand if possible.
[Edit: One simple solution would be to check which larger area the centroid of the smaller area falls into. This would work perfectly for rectangles, as in the example. However, it will not be reliable where the shapes are more complex, and I need this to work even for complex multipolygons as neither administrative areas nor land parcels are reliably simple in real life!]

Related

Polygon comparison between layers

I'm trying to compare specific polygons between layers (different years), to see if there has been any change to the area. Eg[2021 & 2019]2019 & 2021 In this example (from left to right), A would decrease by the amount that is outside of the layer, as well as the amount that B is now taking instead. C would also increase in area.
The difference, symmetrical difference, union, and intersection tools all find areas outside the total bounding box of the layer, but cannot find changes to polygons within the layer.
My desired output is a change (either % or absolute) in area from each layer to a master layer (the latest year).
(Some polygons get split and/or renamed, however I suspect this is a small enough cohort that it can be addressed manually.)
Edit: Currently trying "Detect Dataset Changes", but the polygons don't exactly 100% overlap, the 0.00000001% changes in the border mess it up and mark every polygon as changed. Posted as new question: Is there a tolerance setting I can use with "detect dataset changes" tool?

Best data structure & packages to represent geometric units on a grid

I want to write a program with 'geometry automata'. I'd like it to be a companion to a book on artistic designs. There will be different units, like the 'four petal unit' and 'six petal unit' shown below, and users and choose rulesets to draw unique patterns onto the units:
I don't know what the best data structure to use for this project is. I also don't know if similar things have been done and if so, using what packages or languages. I'm willing to learn anything.
All I know right now is 2D arrays to represent a grid of units. I'm also having trouble mathematically partitioning the 'subunits'. I can see myself just overlapping a bunch of unit circle formulas and shrinking the x/y domains (cartesian system). I can also see myself representing the curve from one unit to another (radians).
Any help would be appreciated.
Thanks!!
I can't guarantee that this is the most efficient solution, but it is a solution so should get you started.
It seems that a graph (vertices with edges) is a natural way to encode this grid. Each node has 4 or 6 neighbours (the number of neighbours matches the number of petals). Each node has 8 or 12 edges, two for each neighbour.
Each vertex has an (x,y) co-ordinate, for example the first row in in the left image, starting from the left is at location (1,0), the next node to its right is (3,0). The first node on the second row is (0,1). This can let you make sure they get plotted correctly, but otherwise the co-ordinate doesn't have much to do with it.
The trouble comes from having two different edges to each neighbour, each aligned with a different circle. You could identify them with the centres of their circles, or you could just call one "upper" and the other "lower".
This structure lets you follow edges easily, and can be stored sparsely if necessary in a hash set (keyed by co-ordinate), or linked list.
Data structure:
The vertices can naturally be stored as a 2-dimensional array (row, column), with the special characteristic that every second column has a horizontal offset.
Each vertex has a set of possible connections to those vertices to its right (upper-right, right, or lower right). The set of possible connections depends on the grid. Whether a connection should be displayed as a thin or a thick line can be represented as a single bit, so all possible connections for the vertex could be packed into a single byte (more compact than a boolean array). For your 4-petal variant, only 4 bits need storing; for the 6-petal variant you need to store 6 bits.
That means your data structure should be a 2-dimensional array of bytes.
Package:
Anything you like that allows drawing and mouse/touch interaction. Drawing the connections is pretty straightforward; you could either draw arcs with SVG or you could even use a set of PNG sprites for different connection bit-patterns (the sprites having partial transparency so as not to obscure other connections).

Pathfinding on large, mostly walkable, non-grid based maps

I am developing a 2D space RTS game which involves spaceships navigating over large maps (think Homeworld but in 2D), which mostly consist of open space, but occasionally containing asteroid fields, space structures and other unwalkable terrain elements.
The problem I have is that using a grid-based solution with a good enough precision results in a very large amount of nodes, so pathfinding takes a long time, and considering that maps contain a lot of open space, huge walkable sections of map are represented by large amount of walkable nodes.
I have tried switching to a quad-tree representation of the map to reduce the amount of nodes in a navigation graph, but this approach yields wierd paths that involve ships going through the exact center of the square when in fact it has to just go through the square to the next one.
I have managed to implement a path optimization which removes nodes from the path when there is a straight path to a next point in the path, but this only partially resolved the problem, so the feeling I have now is that I am still using wrong representation for my navigation graph.
Looking at the way Unity does things, there is a way to represent navigation data using a mesh, but I haven't found any source code or even any more or less derailed description of its inner workings.
So which data structure/pathfinding algorithm is optimal for my case?

Determine minimum number of "invisible squares" to cover all markers on a map?

I have a map full of markers corresponding to GPS coordinates, represented as a PostgreSQL + PostGIS database table using "geography" type for the GPS column.
Imagine, if you will, one semi-transparent square on top of each of these points corresponding to 1x1 mile, based from the centre, often intersecting with each other.
I'm trying to determine the minimum number of such "squares" and their GPS coordinates, so that they "cover" all of the markers with a minimum of 25 meters to the nearest border.
If it makes it any easier, the positions of these "squares" don't have to match the positions of any of the markers.
The purpose of this is to attempt to cut down the number of API requests to a "houses for sale" service significantly, since most of the positions are close to each other and the API takes a 1x1 mile square "bounding box" as the input for each call. It would be insanely wasteful to call the API many times for basically the same area when maybe 1 or 2 times would do it if I can first figure out where these imaginary "squares" go.
I get the feeling that this is considered a "known, common and solved" problem, but so far, I've not been able to figure out how to do it.
Sorry, but it seems like you have no idea what you are doing and are just being rude, both here and at PostGIS irc-channel.
You give no information about your api.
What is creating your maps?
Is it a wms-service or what?
What most people would do is setting up a mapservice with a tile cache. Then the mapservice will pich the tiles needed for each house you want to show ( or multiple houses).
The tiles will be prepared o will be created on the fly. But they will be cached for next time.
So, I think you should read up on things like
MapServer
Mapnik
MapCache
Mapproxy
GeoServer
That is not a complete list, but might give you some ideas about what it is that you want

Point/circle in polygons search in Marklogic 7 using cts:circle-intersect

I have a list of around 1100 polygons to iterate through in $polygons (none of them overlapping each other) and I need to find to which polygon my point or circle with a 1 mile radius belongs/intersects. I used the function below and it takes about 1 second and a half, which is good, but I was wondering, is there is another better/faster approach to it?
I read about R/M-tree algorithms, but I don't have any rectangle hierarchies indexed inside the DB. I'm also trying cts:polygon-intersect to see if it is faster, but I doubt it.
cts:circle-intersects(cts:circle(1,cts:point(5.8864790,51.0006240)), $polygons)
You can use cts:bounding-boxes to get bounding boxes (of varying granularity in the case of the polygons) and check whether they overlap, and only go to the more expensive check if they do. Checking whether two boxes intersect is very quick.
So far, cts:circle-intersects is the fastest, iterates in 1.3 seconds between all the 1100+ polygons. I've tried cts:polygon-intersects and cts:region-intersects also. Since this is not a very critical task in order to apply some fancy and speedy algorithms I will leave it like this for the moment.

Resources