|
| 1 | +struct STRtree{T} |
| 2 | + ptr::Ptr{GEOSSTRtree} # void pointer to the tree |
| 3 | + items::T # any geometry for which an envelope can be derived |
| 4 | +end |
| 5 | + |
| 6 | +""" |
| 7 | + STRtree(items; nodecapacity=10, context::GEOSContext=_context) |
| 8 | +
|
| 9 | +Create a Sort Tile Recursive tree for fast intersection queries of objects for which an |
| 10 | +envelope is defined. The tree cannot be changed after its creation. |
| 11 | +
|
| 12 | +## Arguments |
| 13 | +- `items`: The items to store in the tree. |
| 14 | +- `nodecapacity`: The maximum number of items that can be stored per tree node (default 10). |
| 15 | +- `context`: The GEOS context in which the tree should be created in. Defaults to the global |
| 16 | + LibGEOS context. |
| 17 | +""" |
| 18 | +function STRtree(items; nodecapacity = 10, context::GEOSContext = _context) |
| 19 | + tree = LibGEOS.GEOSSTRtree_create_r(context.ptr, nodecapacity) |
| 20 | + for item in items |
| 21 | + envptr = envelope(item).ptr |
| 22 | + GEOSSTRtree_insert_r(context.ptr, tree, envptr, pointer_from_objref(item)) |
| 23 | + end |
| 24 | + return STRtree(tree, items) |
| 25 | +end |
| 26 | + |
| 27 | +""" |
| 28 | + query(tree::STRtree, geometry; context::GEOSContext=_context) |
| 29 | +
|
| 30 | +Returns the objects within `tree`, whose envelope intersects the envelope of `geometry`. |
| 31 | +
|
| 32 | +## Arguments |
| 33 | +- `tree`: The STRtree to query |
| 34 | +- `geometry`: The LibGEOS geometry (e.g. Polygon) to run the query for |
| 35 | +- `context`: The GEOS context. Defaults to the global LibGEOS context. |
| 36 | +""" |
| 37 | +function query( |
| 38 | + tree::STRtree{T}, |
| 39 | + geometry::Geometry; |
| 40 | + context::GEOSContext = _context, |
| 41 | +) where {T} |
| 42 | + matches = eltype(T)[] |
| 43 | + |
| 44 | + # called for each overlapping item in the tree, used to push the item to matches |
| 45 | + function callback(item, userdata)::Ptr{Cvoid} |
| 46 | + item_ = unsafe_pointer_to_objref(item) |
| 47 | + userdata_ = unsafe_pointer_to_objref(userdata) |
| 48 | + push!(userdata_, item_) |
| 49 | + return C_NULL |
| 50 | + end |
| 51 | + |
| 52 | + cfun_callback = @cfunction($callback, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid})) |
| 53 | + GEOSSTRtree_query_r( |
| 54 | + context.ptr, |
| 55 | + tree.ptr, |
| 56 | + geometry.ptr, |
| 57 | + cfun_callback, |
| 58 | + pointer_from_objref(matches), |
| 59 | + ) |
| 60 | + return matches |
| 61 | +end |
0 commit comments