PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_distance_cache_tolerance()

static int geography_distance_cache_tolerance ( FunctionCallInfo  fcinfo,
const GSERIALIZED g1,
const GSERIALIZED g2,
const SPHEROID s,
double  tolerance,
double *  distance 
)
static

Definition at line 162 of file geography_measurement_trees.c.

References CircTreeGeomCache::argnum, circ_tree_distance_tree(), circ_tree_free(), circ_tree_get_point(), CircTreePIP(), GetCircTreeGeomCache(), gserialized_get_type(), CircTreeGeomCache::index, LW_FAILURE, LW_SUCCESS, lwgeom_calculate_circ_tree(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_startpoint(), MULTIPOLYGONTYPE, POINTTYPE, POLYGONTYPE, POINT2D::x, POINT4D::x, POINT2D::y, and POINT4D::y.

Referenced by geography_distance_cache(), and geography_dwithin_cache().

168 {
169  CircTreeGeomCache* tree_cache = NULL;
170 
171  int type1 = gserialized_get_type(g1);
172  int type2 = gserialized_get_type(g2);
173 
174  Assert(distance);
175 
176  /* Two points? Get outa here... */
177  if ( type1 == POINTTYPE && type2 == POINTTYPE )
178  return LW_FAILURE;
179 
180  /* Fetch/build our cache, if appropriate, etc... */
181  tree_cache = GetCircTreeGeomCache(fcinfo, g1, g2);
182 
183  /* OK, we have an index at the ready! Use it for the one tree argument and */
184  /* fill in the other tree argument */
185  if ( tree_cache && tree_cache->argnum && tree_cache->index )
186  {
187  CIRC_NODE* circtree_cached = tree_cache->index;
188  CIRC_NODE* circtree = NULL;
189  const GSERIALIZED* g_cached;
190  const GSERIALIZED* g;
191  LWGEOM* lwgeom = NULL;
192  int geomtype_cached;
193  int geomtype;
194  POINT4D p4d;
195 
196  /* We need to dynamically build a tree for the uncached side of the function call */
197  if ( tree_cache->argnum == 1 )
198  {
199  g_cached = g1;
200  g = g2;
201  geomtype_cached = type1;
202  geomtype = type2;
203  }
204  else if ( tree_cache->argnum == 2 )
205  {
206  g_cached = g2;
207  g = g1;
208  geomtype_cached = type2;
209  geomtype = type1;
210  }
211  else
212  {
213  lwpgerror("geography_distance_cache this cannot happen!");
214  return LW_FAILURE;
215  }
216 
217  lwgeom = lwgeom_from_gserialized(g);
218  if ( geomtype_cached == POLYGONTYPE || geomtype_cached == MULTIPOLYGONTYPE )
219  {
220  lwgeom_startpoint(lwgeom, &p4d);
221  if ( CircTreePIP(circtree_cached, g_cached, &p4d) )
222  {
223  *distance = 0.0;
224  lwgeom_free(lwgeom);
225  return LW_SUCCESS;
226  }
227  }
228 
229  circtree = lwgeom_calculate_circ_tree(lwgeom);
230  if ( geomtype == POLYGONTYPE || geomtype == MULTIPOLYGONTYPE )
231  {
232  POINT2D p2d;
233  circ_tree_get_point(circtree_cached, &p2d);
234  p4d.x = p2d.x;
235  p4d.y = p2d.y;
236  if ( CircTreePIP(circtree, g, &p4d) )
237  {
238  *distance = 0.0;
239  circ_tree_free(circtree);
240  lwgeom_free(lwgeom);
241  return LW_SUCCESS;
242  }
243  }
244 
245  *distance = circ_tree_distance_tree(circtree_cached, circtree, s, tolerance);
246  circ_tree_free(circtree);
247  lwgeom_free(lwgeom);
248  return LW_SUCCESS;
249  }
250  else
251  {
252  return LW_FAILURE;
253  }
254 }
double x
Definition: liblwgeom.h:352
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
int lwgeom_startpoint(const LWGEOM *lwgeom, POINT4D *pt)
Definition: lwgeom.c:1881
#define POLYGONTYPE
Definition: liblwgeom.h:87
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define LW_SUCCESS
Definition: liblwgeom.h:80
double circ_tree_distance_tree(const CIRC_NODE *n1, const CIRC_NODE *n2, const SPHEROID *spheroid, double threshold)
CIRC_NODE * lwgeom_calculate_circ_tree(const LWGEOM *lwgeom)
#define LW_FAILURE
Definition: liblwgeom.h:79
static int CircTreePIP(const CIRC_NODE *tree1, const GSERIALIZED *g1, const POINT4D *in_point)
static CircTreeGeomCache * GetCircTreeGeomCache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2)
double x
Definition: liblwgeom.h:328
double y
Definition: liblwgeom.h:328
Datum distance(PG_FUNCTION_ARGS)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
void circ_tree_free(CIRC_NODE *node)
Recurse from top of node tree and free all children.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
double y
Definition: liblwgeom.h:352
Here is the call graph for this function:
Here is the caller graph for this function: