PostGIS  2.2.7dev-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 137 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().

138 {
139  CircTreeGeomCache* tree_cache = NULL;
140 
141  int type1 = gserialized_get_type(g1);
142  int type2 = gserialized_get_type(g2);
143 
144  Assert(distance);
145 
146  /* Two points? Get outa here... */
147  if ( type1 == POINTTYPE && type2 == POINTTYPE )
148  return LW_FAILURE;
149 
150  /* Fetch/build our cache, if appropriate, etc... */
151  tree_cache = GetCircTreeGeomCache(fcinfo, g1, g2);
152 
153  /* OK, we have an index at the ready! Use it for the one tree argument and */
154  /* fill in the other tree argument */
155  if ( tree_cache && tree_cache->argnum && tree_cache->index )
156  {
157  CIRC_NODE* circtree_cached = tree_cache->index;
158  CIRC_NODE* circtree = NULL;
159  const GSERIALIZED* g_cached;
160  const GSERIALIZED* g;
161  LWGEOM* lwgeom = NULL;
162  int geomtype_cached;
163  int geomtype;
164  POINT4D p4d;
165 
166  /* We need to dynamically build a tree for the uncached side of the function call */
167  if ( tree_cache->argnum == 1 )
168  {
169  g_cached = g1;
170  g = g2;
171  geomtype_cached = type1;
172  geomtype = type2;
173  }
174  else if ( tree_cache->argnum == 2 )
175  {
176  g_cached = g2;
177  g = g1;
178  geomtype_cached = type2;
179  geomtype = type1;
180  }
181  else
182  {
183  lwpgerror("geography_distance_cache this cannot happen!");
184  return LW_FAILURE;
185  }
186 
187  lwgeom = lwgeom_from_gserialized(g);
188  if ( geomtype_cached == POLYGONTYPE || geomtype_cached == MULTIPOLYGONTYPE )
189  {
190  lwgeom_startpoint(lwgeom, &p4d);
191  if ( CircTreePIP(circtree_cached, g_cached, &p4d) )
192  {
193  *distance = 0.0;
194  lwgeom_free(lwgeom);
195  return LW_SUCCESS;
196  }
197  }
198 
199  circtree = lwgeom_calculate_circ_tree(lwgeom);
200  if ( geomtype == POLYGONTYPE || geomtype == MULTIPOLYGONTYPE )
201  {
202  POINT2D p2d;
203  circ_tree_get_point(circtree_cached, &p2d);
204  p4d.x = p2d.x;
205  p4d.y = p2d.y;
206  if ( CircTreePIP(circtree, g, &p4d) )
207  {
208  *distance = 0.0;
209  circ_tree_free(circtree);
210  lwgeom_free(lwgeom);
211  return LW_SUCCESS;
212  }
213  }
214 
215  *distance = circ_tree_distance_tree(circtree_cached, circtree, s, tolerance);
216  circ_tree_free(circtree);
217  lwgeom_free(lwgeom);
218  return LW_SUCCESS;
219  }
220  else
221  {
222  return LW_FAILURE;
223  }
224 }
double x
Definition: liblwgeom.h:336
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:55
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:1837
#define POLYGONTYPE
Definition: liblwgeom.h:72
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define LW_SUCCESS
Definition: liblwgeom.h:65
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:64
static int CircTreePIP(const CIRC_NODE *tree1, const GSERIALIZED *g1, const POINT4D *in_point)
double x
Definition: liblwgeom.h:312
double y
Definition: liblwgeom.h:312
Datum distance(PG_FUNCTION_ARGS)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
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:70
double y
Definition: liblwgeom.h:336

Here is the call graph for this function:

Here is the caller graph for this function: