27 #include "catalog/pg_type.h"
29 #include "../postgis_config.h"
38 #include "lwgeom_pg.h"
41 #include "lwgeom_transform.h"
45 #define INVMINDIST 1.0e8
48 #define INVMINDIST 1.0e7
79 bool use_spheroid =
false;
83 g1 = PG_GETARG_GSERIALIZED_P(0);
84 g2 = PG_GETARG_GSERIALIZED_P(1);
101 PG_FREE_IF_COPY(g1, 0);
102 PG_FREE_IF_COPY(g2, 1);
112 POSTGIS_DEBUGF(2,
"[GIST] '%s' got distance %g", __func__,
distance);
117 PG_FREE_IF_COPY(g1, 0);
118 PG_FREE_IF_COPY(g2, 1);
142 bool use_spheroid =
true;
146 g1 = PG_GETARG_GSERIALIZED_P(0);
147 g2 = PG_GETARG_GSERIALIZED_P(1);
150 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
151 tolerance = PG_GETARG_FLOAT8(2);
154 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
155 use_spheroid = PG_GETARG_BOOL(3);
163 if ( ! use_spheroid )
164 s.a =
s.b =
s.radius;
172 PG_FREE_IF_COPY(g1, 0);
173 PG_FREE_IF_COPY(g2, 1);
183 POSTGIS_DEBUGF(2,
"[GIST] '%s' got distance %g", __func__,
distance);
188 PG_FREE_IF_COPY(g1, 0);
189 PG_FREE_IF_COPY(g2, 1);
208 SHARED_GSERIALIZED *shared_geom1 = ToastCacheGetGeometry(fcinfo, 0);
209 SHARED_GSERIALIZED *shared_geom2 = ToastCacheGetGeometry(fcinfo, 1);
210 const GSERIALIZED *g1 = shared_gserialized_get(shared_geom1);
211 const GSERIALIZED *g2 = shared_gserialized_get(shared_geom2);
213 bool use_spheroid =
true;
218 use_spheroid = PG_GETARG_BOOL(2);
226 if ( ! use_spheroid )
227 s.a =
s.b =
s.radius;
256 elog(ERROR,
"distance returned negative!");
270 SHARED_GSERIALIZED *shared_geom1 = ToastCacheGetGeometry(fcinfo, 0);
271 SHARED_GSERIALIZED *shared_geom2 = ToastCacheGetGeometry(fcinfo, 1);
272 const GSERIALIZED *g1 = shared_gserialized_get(shared_geom1);
273 const GSERIALIZED *g2 = shared_gserialized_get(shared_geom2);
276 bool use_spheroid =
true;
283 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
284 tolerance = PG_GETARG_FLOAT8(2);
287 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
288 use_spheroid = PG_GETARG_BOOL(3);
294 if ( ! use_spheroid )
295 s.a =
s.b =
s.radius;
299 PG_RETURN_BOOL(
false);
309 elog(ERROR,
"lwgeom_distance_spheroid returned negative!");
315 PG_RETURN_BOOL(dwithin);
321 PG_RETURN_BOOL(CallerFInfoFunctionCall2(
323 PG_GETARG_DATUM(0), PG_GETARG_DATUM(1)));
335 double tolerance = 0.0;
337 bool use_spheroid =
true;
341 g1 = PG_GETARG_GSERIALIZED_P(0);
342 g2 = PG_GETARG_GSERIALIZED_P(1);
349 PG_FREE_IF_COPY(g1, 0);
350 PG_FREE_IF_COPY(g2, 1);
351 PG_RETURN_FLOAT8(0.0);
355 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
356 tolerance = PG_GETARG_FLOAT8(2);
359 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
360 use_spheroid = PG_GETARG_BOOL(3);
366 if ( ! use_spheroid )
367 s.a =
s.b =
s.radius;
371 elog(ERROR,
"geography_distance_tree failed!");
393 double tolerance = 0.0;
395 bool use_spheroid =
true;
399 g1 = PG_GETARG_GSERIALIZED_P(0);
400 g2 = PG_GETARG_GSERIALIZED_P(1);
404 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
405 tolerance = PG_GETARG_FLOAT8(2);
408 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
409 use_spheroid = PG_GETARG_BOOL(3);
415 if ( ! use_spheroid )
416 s.a =
s.b =
s.radius;
424 PG_RETURN_BOOL(
false);
432 PG_FREE_IF_COPY(g1, 0);
433 PG_FREE_IF_COPY(g2, 1);
438 elog(ERROR,
"lwgeom_distance_spheroid returned negative!");
439 PG_RETURN_BOOL(
false);
442 PG_RETURN_BOOL(
distance <= tolerance);
462 g = PG_GETARG_GSERIALIZED_P_COPY(0);
477 PG_RETURN_POINTER(g);
485 PG_RETURN_POINTER(g_out);
503 g = PG_GETARG_GSERIALIZED_P(0);
506 use_spheroid = PG_GETARG_BOOL(1);
517 PG_RETURN_FLOAT8(0.0);
521 gbox = *(lwgeom->
bbox);
525 #ifndef PROJ_GEODESIC
533 if ( gbox.
zmax > 0.0 && gbox.
zmin < 0.0 )
539 if ( ! use_spheroid )
540 s.a =
s.b =
s.radius;
547 PG_FREE_IF_COPY(g, 0);
552 elog(ERROR,
"lwgeom_area_spher(oid) returned area < 0.0");
556 PG_RETURN_FLOAT8(area);
574 g = PG_GETARG_GSERIALIZED_P(0);
580 PG_RETURN_FLOAT8(0.0);
589 PG_RETURN_FLOAT8(0.0);
593 use_spheroid = PG_GETARG_BOOL(1);
599 if ( ! use_spheroid )
600 s.a =
s.b =
s.radius;
608 elog(ERROR,
"lwgeom_length_spheroid returned length < 0.0");
615 PG_FREE_IF_COPY(g, 0);
616 PG_RETURN_FLOAT8(length);
633 g = PG_GETARG_GSERIALIZED_P(0);
640 PG_RETURN_FLOAT8(0.0);
644 use_spheroid = PG_GETARG_BOOL(1);
650 if ( ! use_spheroid )
651 s.a =
s.b =
s.radius;
659 elog(ERROR,
"lwgeom_length_spheroid returned length < 0.0");
666 PG_FREE_IF_COPY(g, 0);
667 PG_RETURN_FLOAT8(length);
684 POSTGIS_DEBUG(4,
"gserialized_datum_get_gbox_p returned LW_FAILURE");
685 elog(ERROR,
"Error in gserialized_datum_get_gbox_p calculation.");
695 PG_RETURN_POINTER(geography_serialize(lwpoint));
715 g1 = PG_GETARG_GSERIALIZED_P(0);
716 g2 = PG_GETARG_GSERIALIZED_P(1);
728 PG_FREE_IF_COPY(g1, 0);
729 PG_FREE_IF_COPY(g2, 1);
730 PG_RETURN_BOOL(
false);
739 PG_FREE_IF_COPY(g1, 0);
740 PG_FREE_IF_COPY(g2, 1);
756 g1 = PG_GETARG_GSERIALIZED_P(1);
757 g2 = PG_GETARG_GSERIALIZED_P(0);
769 PG_FREE_IF_COPY(g1, 1);
770 PG_FREE_IF_COPY(g2, 0);
771 PG_RETURN_BOOL(
false);
780 PG_FREE_IF_COPY(g1, 1);
781 PG_FREE_IF_COPY(g2, 0);
797 GBOX gbox, gbox1, gbox2;
802 double xwidth, ywidth;
807 g1 = PG_GETARG_GSERIALIZED_P(0);
818 elog(ERROR,
"Error in geography_bestsrid calling gserialized_get_gbox_p(g1, &gbox1)");
820 POSTGIS_DEBUGF(4,
"calculated gbox = %s",
gbox_to_string(&gbox1));
823 elog(ERROR,
"Error in geography_bestsrid calling with infinite coordinate geographies");
830 g2 = PG_GETARG_GSERIALIZED_P(1);
834 elog(ERROR,
"Error in geography_bestsrid calling gserialized_get_gbox_p(g2, &gbox2)");
840 elog(ERROR,
"Error in geography_bestsrid calling with second arg infinite coordinate geographies");
850 gbox = gbox2 = gbox1;
854 if ( empty1 && empty2 )
871 POSTGIS_DEBUGF(2,
"xwidth %g", xwidth);
872 POSTGIS_DEBUGF(2,
"ywidth %g", ywidth);
873 POSTGIS_DEBUGF(2,
"center POINT(%g %g)", center.
x, center.
y);
876 if ( center.
y > 70.0 && ywidth < 45.0 )
878 PG_RETURN_INT32(SRID_NORTH_LAMBERT);
882 if ( center.
y < -70.0 && ywidth < 45.0 )
884 PG_RETURN_INT32(SRID_SOUTH_LAMBERT);
895 int zone = floor((center.
x + 180.0) / 6.0);
897 if ( zone > 59 ) zone = 59;
900 if ( center.
y < 0.0 )
902 PG_RETURN_INT32( SRID_SOUTH_UTM_START + zone );
907 PG_RETURN_INT32( SRID_NORTH_UTM_START + zone );
920 int yzone = 3 + floor(center.
y / 30.0);
923 if ( (yzone == 2 || yzone == 3) && xwidth < 30.0 )
925 xzone = 6 + floor(center.
x / 30.0);
928 else if ( (yzone == 1 || yzone == 4) && xwidth < 45.0 )
930 xzone = 4 + floor(center.
x / 45.0);
933 else if ( (yzone == 0 || yzone == 5) && xwidth < 90.0 )
935 xzone = 2 + floor(center.
x / 90.0);
941 PG_RETURN_INT32(SRID_LAEA_START + 20 * yzone + xzone);
949 PG_RETURN_INT32(SRID_WORLD_MERCATOR);
965 double azimuth = 0.0;
971 if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
975 g = PG_GETARG_GSERIALIZED_P(0);
981 elog(ERROR,
"ST_Project(geography) is only valid for point inputs");
992 elog(ERROR,
"ST_Project(geography) cannot project from an empty start point");
996 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
997 azimuth = PG_GETARG_FLOAT8(2);
1005 PG_RETURN_POINTER(g);
1012 if ( lwp_projected == NULL )
1014 elog(ERROR,
"lwgeom_project_spheroid returned null");
1023 PG_FREE_IF_COPY(g, 0);
1024 PG_RETURN_POINTER(g_out);
1042 uint32_t type1, type2;
1045 g1 = PG_GETARG_GSERIALIZED_P(0);
1046 g2 = PG_GETARG_GSERIALIZED_P(1);
1053 elog(ERROR,
"ST_Azimuth(geography, geography) is only valid for point inputs");
1065 elog(ERROR,
"ST_Azimuth(geography, geography) cannot work with empty points");
1079 PG_FREE_IF_COPY(g1, 0);
1080 PG_FREE_IF_COPY(g2, 1);
1083 if( isnan(azimuth) )
1088 PG_RETURN_FLOAT8(azimuth);
1104 double max_seg_length;
1108 g1 = PG_GETARG_GSERIALIZED_P(0);
1116 PG_RETURN_POINTER(g1);
1134 g2 = geography_serialize(lwgeom2);
1139 PG_FREE_IF_COPY(g1, 0);
1141 PG_RETURN_POINTER(g2);
char result[OUT_DOUBLE_BUFFER_SIZE]
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
GSERIALIZED * gserialized_expand(GSERIALIZED *g, double distance)
Return a GSERIALIZED with an expanded bounding box.
Datum geography_length(PG_FUNCTION_ARGS)
Datum geography_area(PG_FUNCTION_ARGS)
Datum geography_expand(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(geography_distance_knn)
Datum geography_azimuth(PG_FUNCTION_ARGS)
Datum geography_distance_tree(PG_FUNCTION_ARGS)
Datum geography_point_outside(PG_FUNCTION_ARGS)
Datum geography_distance_uncached(PG_FUNCTION_ARGS)
Datum geography_distance(PG_FUNCTION_ARGS)
Datum geography_segmentize(PG_FUNCTION_ARGS)
Datum geography_covers(PG_FUNCTION_ARGS)
Datum geography_intersects(PG_FUNCTION_ARGS)
Datum geography_dwithin(PG_FUNCTION_ARGS)
Datum geography_coveredby(PG_FUNCTION_ARGS)
Datum geography_perimeter(PG_FUNCTION_ARGS)
Datum geography_bestsrid(PG_FUNCTION_ARGS)
Datum geography_dwithin_uncached(PG_FUNCTION_ARGS)
Datum geography_project(PG_FUNCTION_ARGS)
Datum geography_distance_knn(PG_FUNCTION_ARGS)
int geography_tree_distance(const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
int geography_dwithin_cache(FunctionCallInfo fcinfo, SHARED_GSERIALIZED *g1, SHARED_GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
int geography_distance_cache(FunctionCallInfo fcinfo, SHARED_GSERIALIZED *g1, SHARED_GSERIALIZED *g2, const SPHEROID *s, double *distance)
void gserialized_error_if_srid_mismatch(const GSERIALIZED *g1, const GSERIALIZED *g2, const char *funcname)
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
lwflags_t gserialized_get_lwflags(const GSERIALIZED *g)
Read the flags from a GSERIALIZED and return a standard lwflag integer.
int gserialized_datum_get_gbox_p(Datum gsdatum, GBOX *gbox)
Given a GSERIALIZED datum, as quickly as possible (peaking into the top of the memory) return the gbo...
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
LWPOINT * lwgeom_project_spheroid(const LWPOINT *r, const SPHEROID *spheroid, double distance, double azimuth)
Calculate the location of a point on a spheroid, give a start point, bearing and distance.
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
LWGEOM * lwgeom_segmentize_sphere(const LWGEOM *lwg_in, double max_seg_length)
Derive a new geometry with vertices added to ensure no vertex is more than max_seg_length (in radians...
void lwpoint_free(LWPOINT *pt)
double lwgeom_distance_spheroid(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2, const SPHEROID *spheroid, double tolerance)
Calculate the geodetic distance from lwgeom1 to lwgeom2 on the spheroid.
void lwgeom_free(LWGEOM *geom)
int gbox_pt_outside(const GBOX *gbox, POINT2D *pt_outside)
Calculate a spherical point that falls outside the geocentric gbox.
int lwgeom_calculate_gbox_geodetic(const LWGEOM *geom, GBOX *gbox)
Calculate the geodetic bounding box for an LWGEOM.
double lwgeom_area_spheroid(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the spheroid.
int lwgeom_covers_lwgeom_sphere(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
Calculate covers predicate for two lwgeoms on the sphere.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
void lwgeom_add_bbox_deep(LWGEOM *lwgeom, GBOX *gbox)
Compute a box for geom and all sub-geometries, if not already computed.
double lwgeom_length_spheroid(const LWGEOM *geom, const SPHEROID *s)
Calculate the geodetic length of a lwgeom on the unit sphere.
#define LW_TRUE
Return types for functions with status returns.
double lwgeom_azumith_spheroid(const LWPOINT *r, const LWPOINT *s, const SPHEROID *spheroid)
Calculate the bearing between two points on a spheroid.
This library is the generic geometry handling section of PostGIS.
double gbox_angular_height(const GBOX *gbox)
GBOX utility functions to figure out coverage/location on the globe.
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
#define FP_TOLERANCE
Floating point comparators.
int gbox_centroid(const GBOX *gbox, POINT2D *out)
Computes the average(ish) center of the box and returns success.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
static double distance(double x1, double y1, double x2, double y2)