TransWikia.com

R and sf Binary Predicates

Geographic Information Systems Asked on December 31, 2020

Am trying to dial-down into the definitions and cannot resolve or understand the argument named, "prepared".

The definition of prepared is: "logical; prepare geometry for x, before looping over y? See Details. "

Say there is a point (pt) on the boundary of a polygon (p).
Experimenting and using the following two sf binary predicate functions:

st_within(pt3.sfg, g, prepared = TRUE)       # # Affirmative
st_within(g, pt3.sfg, prepared = TRUE)


st_contains(pt3.sfg, g, prepared = TRUE)
st_contains(g, pt3.sfg, prepared = TRUE)      # Affirmative

Running these lines of code returns:

Sparse geometry binary predicate list of length 1, where the predicate was `within'
 1: 1
Sparse geometry binary predicate list of length 1, where the predicate was `within'
 1: (empty)
Sparse geometry binary predicate list of length 1, where the predicate was `contains'
 1: (empty)
Sparse geometry binary predicate list of length 1, where the predicate was `contains'
 1: 1   

So the question is: What is the argument "prepared"? How would the meaning of this argument be used in a sentence? And how does TRUE or FALSE influence the prepared argument?

2 Answers

It's a binary tag telling you about the output of a geoprocess without actually doing the process. X ought be the first entry in the commannd (ie 'g' in 'st_contains(g, pt3.sfg, prepared = TRUE').

Language implies that st_contains is meant to be used in a loop, so that either the first or second items in 'st_contains(g, pt3.sfg, prepared = TRUE)' could be a list rather than a single item.

So sounds like it's a geometry check to make sure it's even possible for different geometry classes (point, line, polygon) to work with the chosen predicate (within/contains).

Answered by Mox on December 31, 2020

The prepared flag is essentially a hint to the underlying GEOS library that's doing the actual work behind the scenes.

If x is "prepared" it will be internally indexed so that repeated calls to the predicate (contains, intersects, etc.) will be faster. For example, if you want to test whether many points are within a single polygon, it's faster for GEOS to build a spatial index on the segments of the polygon, instead of scanning all segments for each point-in-polygon test. Building this index comes with a memory and CPU cost, so it's possible that you could get better performance with prepared = FALSE if you have few geometries to test, but I think this is unlikely.

As an aside, GEOS provides a "prepared" version of the "contains" predicate, but not the "within" predicate. This means that if you formulate your problem using st_contains instead of st_within, it will almost always be much faster.

Answered by dbaston on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP