On Fri, 10 Jan 2025 19:16:27 -0800 (PST), Elijah Massey wrote:
> I am not sure whether SpatialIndex supports 3D at all. Are knn2 and
> SpatialIndex only 2D?
>
Hi Elijah,
Let's start by clarifying some basic notions.
SpatiaLite, just like PostGis and any other Spatial DBMS is designed
specifically for geographical applications.
The underlying spatial model is never true 3D, it's instead what many
define as 2.5D
That is, it's not exactly a flat surface (a true Euclidean plane),
it's rather a wavy and corrugated surface that follows the natural
relief of hills and mountains.
Z coordinates are simply used to represent sea level elevations,
they are never intended to describe solid objects in as in true 3D
The overall logic of the relationships between geometries always
remains purely 2D and is merely based on X and Y while Z is
systematically ignored.
I'm not at all sure that SpatiaLite can satisfactorily adapt
to the very specific needs of Minecraft; at first glance I
would say no.
> In that case, I could probably use one of them
> in an inner subquery to get approximate results, and then use
> something like ST_3DDistance to filter and sort by the actual
> distance
> in a much smaller set of data points.
>
In my opinion you can do better; SQLite's R*Tree, the real Spatial
Index engine, supports up to 5 dimensions.
Please read here for more details:
https://www.sqlite.org/rtree.html
If I understand correctly, you are simply interested in managing
3D POIs and 3D objects that can be simplified as cubes or
parallelepipeds or perhaps as spheres.
You shouldn't encounter too many difficulties in defining their
binary geometric representations in a BLOB, perhaps taking some
inspiration from what SpatiaLite does to adapt it to your needs.
Please see here:
https://www.gaia-gis.it/gaia-sins/BLOB-Geometry.html
At this point you would be able to directly exploit a SQLite
3D R*Tree for quickly pre-filtering objects in a small box.
For a more refined evaluation you simply need to define your
own function calculating 3D distances, which in the case of
simple points, parallelepipeds or spheres is particularly
simple and not at all complex.
Conclusions: building a small 3D geometry engine tailor-made
for Minecraft seems reasonably simple and shouldn't require
excessive development time.
You should also be able to solve the KNN problem without too
much effort, because ultimately it's just a matter of iteratively
executing direct queries on the 3D Spatial Index, gradually
enlarging the size of the search box until you find a sufficient
number of nearest candidates to be sorted by distance.
> Also, is an SRID of -1 the best for a Minecraft worlds? The world is
> flat and 384 blocks thick, and it extends -29,999,984 blocks in both
> horizontal directions from (0,0). I am using Z as Minecraft's Y axis,
> since Y is vertical in Minecraft. If not, which SRID should I use or
> should I create a new one?
>
Don't waste time with SRIDs, which are a concept that only makes
sense in the world of planets and other celestial bodies that have
a surface that is an ellipsoid.
It seems to me that the Minecraft reference model is something
completely different (and fortunately much simpler).
my 2 cents,
Sandro