Closed akhenakh closed 9 months ago
If the geometry is a MultiPolygon then you can use tg_geom_num_polys and tg_geom_poly_at to get the internal polygons.
For example:
int npolys = tg_geom_num_polys(geom);
for (int i = 0; i < npolys; i++) {
const struct tg_poly *poly = tg_geom_poly_at(geom, i);
if (tg_geom_intersects_xy(poly, point.x, point.y)) {
// hit!
}
}
For GeometryCollection use tg_geom_num_geometries and tg_geom_geometry_at.
Also I recommend using tg_geom_intersects
rather than tg_geom_contains
for PIP.
I'm trying to use tg in this case https://github.com/akhenakh/coord2country, I'm having a 250ish features in a FeatureCollection with mulltipolygons in them (countries, island and so on).
It fills unnatural to have to iterate over thousands of geometries to perform tg_geom_intersects_xy
on all of them?
Is there a way to use the index beforehand to reduce that number to only ones very close to them, (like s2 shape index is doing, first by filtering using the cells cover and then only performing PIP where it can't be sure yet).
I get that this is maybe not the usage you had in mind while designing tg, I assume the main goal is to have something prefiltering like an sqlite with a bbox, then loading the wkbs, then testing a few polygons, rather than a big live index with all the features in it?
I just added internal indexing for large collection types like GeometryCollection, FeatureCollection, MultiPolygon, etc. The "tg_geom_intersects" will now be very fast for those too.
I also added the tg_geom_search iterator. This should efficiently find all child geometries that have an external rectangle that intersects the provided rectangle.
struct pip_iter_ctx {
struct tg_point pip_point;
};
bool pip_iter(const struct tg_geom *child, int index, void *udata) {
struct pip_iter_ctx *ctx = udata;
if (tg_geom_intersects_xy(child, ctx->pip_point.x, ctx->pip_point.y)) {
// Point is in child geometry
}
return true;
}
int main()
struct tg_geom *big_geom = ...; // big geometry with lots of children
struct tg_point pip_point = ...; // point for PIP
struct pip_iter_ctx ctx = { .pip_point = pip_point };
tg_geom_search(big_geom, tg_point_rect(pip_point), pip_iter, &ctx);
}
I can do a PIP with a geom containing a lot of polygons against a point with
So we can tell if it's contained in something or not, but it's unclear how to get the information back about which polygon did contain the point?
The only iterator I can find in the API are the ones from the nearest queries (tg_ring_nearest_segment...).
Am I missing something, if it's not there yet are you planning on having it in the future?
Thanks!