PostGIS  2.5.0dev-r@@SVN_REVISION@@
static int geography_distance_cache_tolerance ( FunctionCallInfoData *  fcinfo,
const GSERIALIZED g1,
const GSERIALIZED g2,
const SPHEROID s,
double  tolerance,
double *  distance 
)
static

Definition at line 161 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().

162 {
163  CircTreeGeomCache* tree_cache = NULL;
164 
165  int type1 = gserialized_get_type(g1);
166  int type2 = gserialized_get_type(g2);
167 
168  Assert(distance);
169 
170  /* Two points? Get outa here... */
171  if ( type1 == POINTTYPE && type2 == POINTTYPE )
172  return LW_FAILURE;
173 
174  /* Fetch/build our cache, if appropriate, etc... */
175  tree_cache = GetCircTreeGeomCache(fcinfo, g1, g2);
176 
177  /* OK, we have an index at the ready! Use it for the one tree argument and */
178  /* fill in the other tree argument */
179  if ( tree_cache && tree_cache->argnum && tree_cache->index )
180  {
181  CIRC_NODE* circtree_cached = tree_cache->index;
182  CIRC_NODE* circtree = NULL;
183  const GSERIALIZED* g_cached;
184  const GSERIALIZED* g;
185  LWGEOM* lwgeom = NULL;
186  int geomtype_cached;
187  int geomtype;
188  POINT4D p4d;
189 
190  /* We need to dynamically build a tree for the uncached side of the function call */
191  if ( tree_cache->argnum == 1 )
192  {
193  g_cached = g1;
194  g = g2;
195  geomtype_cached = type1;
196  geomtype = type2;
197  }
198  else if ( tree_cache->argnum == 2 )
199  {
200  g_cached = g2;
201  g = g1;
202  geomtype_cached = type2;
203  geomtype = type1;
204  }
205  else
206  {
207  lwpgerror("geography_distance_cache this cannot happen!");
208  return LW_FAILURE;
209  }
210 
211  lwgeom = lwgeom_from_gserialized(g);
212  if ( geomtype_cached == POLYGONTYPE || geomtype_cached == MULTIPOLYGONTYPE )
213  {
214  lwgeom_startpoint(lwgeom, &p4d);
215  if ( CircTreePIP(circtree_cached, g_cached, &p4d) )
216  {
217  *distance = 0.0;
218  lwgeom_free(lwgeom);
219  return LW_SUCCESS;
220  }
221  }
222 
223  circtree = lwgeom_calculate_circ_tree(lwgeom);
224  if ( geomtype == POLYGONTYPE || geomtype == MULTIPOLYGONTYPE )
225  {
226  POINT2D p2d;
227  circ_tree_get_point(circtree_cached, &p2d);
228  p4d.x = p2d.x;
229  p4d.y = p2d.y;
230  if ( CircTreePIP(circtree, g, &p4d) )
231  {
232  *distance = 0.0;
233  circ_tree_free(circtree);
234  lwgeom_free(lwgeom);
235  return LW_SUCCESS;
236  }
237  }
238 
239  *distance = circ_tree_distance_tree(circtree_cached, circtree, s, tolerance);
240  circ_tree_free(circtree);
241  lwgeom_free(lwgeom);
242  return LW_SUCCESS;
243  }
244  else
245  {
246  return LW_FAILURE;
247  }
248 }
double x
Definition: liblwgeom.h:351
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:2108
#define POLYGONTYPE
Definition: liblwgeom.h:86
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
#define LW_SUCCESS
Definition: liblwgeom.h:79
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)
static CircTreeGeomCache * GetCircTreeGeomCache(FunctionCallInfoData *fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2)
#define LW_FAILURE
Definition: liblwgeom.h:78
static int CircTreePIP(const CIRC_NODE *tree1, const GSERIALIZED *g1, const POINT4D *in_point)
double x
Definition: liblwgeom.h:327
double y
Definition: liblwgeom.h:327
Datum distance(PG_FUNCTION_ARGS)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
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:84
double y
Definition: liblwgeom.h:351

Here is the call graph for this function:

Here is the caller graph for this function: