28 #include "../postgis_config.h"
38 #include "lwgeom_pg.h"
41 #include "lwgeom_transform.h"
45 #define INVMINDIST 1.0e8
48 #define INVMINDIST 1.0e9
78 bool use_spheroid =
false;
82 g1 = PG_GETARG_GSERIALIZED_P(0);
83 g2 = PG_GETARG_GSERIALIZED_P(1);
100 PG_FREE_IF_COPY(g1, 0);
101 PG_FREE_IF_COPY(g2, 1);
111 POSTGIS_DEBUGF(2,
"[GIST] '%s' got distance %g", __func__,
distance);
116 PG_FREE_IF_COPY(g1, 0);
117 PG_FREE_IF_COPY(g2, 1);
141 bool use_spheroid =
true;
145 g1 = PG_GETARG_GSERIALIZED_P(0);
146 g2 = PG_GETARG_GSERIALIZED_P(1);
149 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
150 tolerance = PG_GETARG_FLOAT8(2);
153 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
154 use_spheroid = PG_GETARG_BOOL(3);
162 if ( ! use_spheroid )
163 s.a =
s.b =
s.radius;
171 PG_FREE_IF_COPY(g1, 0);
172 PG_FREE_IF_COPY(g2, 1);
182 POSTGIS_DEBUGF(2,
"[GIST] '%s' got distance %g", __func__,
distance);
187 PG_FREE_IF_COPY(g1, 0);
188 PG_FREE_IF_COPY(g2, 1);
210 bool use_spheroid =
true;
214 g1 = PG_GETARG_GSERIALIZED_P(0);
215 g2 = PG_GETARG_GSERIALIZED_P(1);
218 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
219 use_spheroid = PG_GETARG_BOOL(3);
227 if ( ! use_spheroid )
228 s.a =
s.b =
s.radius;
233 PG_FREE_IF_COPY(g1, 0);
234 PG_FREE_IF_COPY(g2, 1);
254 PG_FREE_IF_COPY(g1, 0);
255 PG_FREE_IF_COPY(g2, 1);
263 elog(ERROR,
"distance returned negative!");
280 double tolerance = 0.0;
282 bool use_spheroid =
true;
287 g1 = PG_GETARG_GSERIALIZED_P(0);
288 g2 = PG_GETARG_GSERIALIZED_P(1);
291 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
292 tolerance = PG_GETARG_FLOAT8(2);
295 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
296 use_spheroid = PG_GETARG_BOOL(3);
304 if ( ! use_spheroid )
305 s.a =
s.b =
s.radius;
310 PG_FREE_IF_COPY(g1, 0);
311 PG_FREE_IF_COPY(g2, 1);
312 PG_RETURN_BOOL(
false);
323 elog(ERROR,
"lwgeom_distance_spheroid returned negative!");
330 PG_FREE_IF_COPY(g1, 0);
331 PG_FREE_IF_COPY(g2, 1);
333 PG_RETURN_BOOL(dwithin);
346 double tolerance = 0.0;
348 bool use_spheroid =
true;
352 g1 = PG_GETARG_GSERIALIZED_P(0);
353 g2 = PG_GETARG_GSERIALIZED_P(1);
358 PG_FREE_IF_COPY(g1, 0);
359 PG_FREE_IF_COPY(g2, 1);
360 PG_RETURN_FLOAT8(0.0);
364 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
365 tolerance = PG_GETARG_FLOAT8(2);
368 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
369 use_spheroid = PG_GETARG_BOOL(3);
377 if ( ! use_spheroid )
378 s.a =
s.b =
s.radius;
382 elog(ERROR,
"geography_distance_tree failed!");
402 double tolerance = 0.0;
404 bool use_spheroid =
true;
408 g1 = PG_GETARG_GSERIALIZED_P(0);
409 g2 = PG_GETARG_GSERIALIZED_P(1);
412 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
413 tolerance = PG_GETARG_FLOAT8(2);
416 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
417 use_spheroid = PG_GETARG_BOOL(3);
425 if ( ! use_spheroid )
426 s.a =
s.b =
s.radius;
434 PG_RETURN_BOOL(
false);
442 PG_FREE_IF_COPY(g1, 0);
443 PG_FREE_IF_COPY(g2, 1);
448 elog(ERROR,
"lwgeom_distance_spheroid returned negative!");
449 PG_RETURN_BOOL(
false);
452 PG_RETURN_BOOL(
distance <= tolerance);
472 g = PG_GETARG_GSERIALIZED_P_COPY(0);
487 PG_RETURN_POINTER(g);
495 PG_RETURN_POINTER(g_out);
513 g = PG_GETARG_GSERIALIZED_P(0);
516 use_spheroid = PG_GETARG_BOOL(1);
527 PG_RETURN_FLOAT8(0.0);
531 gbox = *(lwgeom->
bbox);
543 if ( gbox.
zmax > 0.0 && gbox.
zmin < 0.0 )
549 if ( ! use_spheroid )
550 s.a =
s.b =
s.radius;
560 PG_FREE_IF_COPY(g, 0);
565 elog(ERROR,
"lwgeom_area_spher(oid) returned area < 0.0");
569 PG_RETURN_FLOAT8(
area);
587 g = PG_GETARG_GSERIALIZED_P(0);
593 PG_RETURN_FLOAT8(0.0);
602 PG_RETURN_FLOAT8(0.0);
606 use_spheroid = PG_GETARG_BOOL(1);
612 if ( ! use_spheroid )
613 s.a =
s.b =
s.radius;
621 elog(ERROR,
"lwgeom_length_spheroid returned length < 0.0");
628 PG_FREE_IF_COPY(g, 0);
629 PG_RETURN_FLOAT8(length);
646 g = PG_GETARG_GSERIALIZED_P(0);
653 PG_RETURN_FLOAT8(0.0);
657 use_spheroid = PG_GETARG_BOOL(1);
663 if ( ! use_spheroid )
664 s.a =
s.b =
s.radius;
672 elog(ERROR,
"lwgeom_length_spheroid returned length < 0.0");
679 PG_FREE_IF_COPY(g, 0);
680 PG_RETURN_FLOAT8(length);
698 g = PG_GETARG_GSERIALIZED_P(0);
703 POSTGIS_DEBUG(4,
"gserialized_get_gbox_p returned LW_FAILURE");
704 elog(ERROR,
"Error in gserialized_get_gbox_p calculation.");
718 SET_VARSIZE(g_out, g_out_size);
720 PG_FREE_IF_COPY(g, 0);
721 PG_RETURN_POINTER(g_out);
742 g1 = PG_GETARG_GSERIALIZED_P(0);
743 g2 = PG_GETARG_GSERIALIZED_P(1);
756 PG_FREE_IF_COPY(g1, 0);
757 PG_FREE_IF_COPY(g2, 1);
758 PG_RETURN_BOOL(
false);
767 PG_FREE_IF_COPY(g1, 0);
768 PG_FREE_IF_COPY(g2, 1);
770 PG_RETURN_BOOL(result);
784 GBOX gbox, gbox1, gbox2;
789 double xwidth, ywidth;
792 Datum d1 = PG_GETARG_DATUM(0);
793 Datum d2 = PG_GETARG_DATUM(1);
803 elog(ERROR,
"Error in geography_bestsrid calling gserialized_get_gbox_p(g1, &gbox1)");
805 POSTGIS_DEBUGF(4,
"calculated gbox = %s",
gbox_to_string(&gbox1));
814 elog(ERROR,
"Error in geography_bestsrid calling gserialized_get_gbox_p(g2, &gbox2)");
822 gbox = gbox2 = gbox1;
826 if ( empty1 && empty2 )
843 POSTGIS_DEBUGF(2,
"xwidth %g", xwidth);
844 POSTGIS_DEBUGF(2,
"ywidth %g", ywidth);
845 POSTGIS_DEBUGF(2,
"center POINT(%g %g)", center.
x, center.
y);
848 if ( center.
y > 70.0 && ywidth < 45.0 )
850 PG_RETURN_INT32(SRID_NORTH_LAMBERT);
854 if ( center.
y < -70.0 && ywidth < 45.0 )
856 PG_RETURN_INT32(SRID_SOUTH_LAMBERT);
867 int zone = floor((center.
x + 180.0) / 6.0);
869 if ( zone > 59 ) zone = 59;
872 if ( center.
y < 0.0 )
874 PG_RETURN_INT32( SRID_SOUTH_UTM_START + zone );
879 PG_RETURN_INT32( SRID_NORTH_UTM_START + zone );
892 int yzone = 3 + floor(center.
y / 30.0);
895 if ( (yzone == 2 || yzone == 3) && xwidth < 30.0 )
897 xzone = 6 + floor(center.
x / 30.0);
900 else if ( (yzone == 1 || yzone == 4) && xwidth < 45.0 )
902 xzone = 4 + floor(center.
x / 45.0);
905 else if ( (yzone == 0 || yzone == 5) && xwidth < 90.0 )
907 xzone = 2 + floor(center.
x / 90.0);
913 PG_RETURN_INT32(SRID_LAEA_START + 20 * yzone + xzone);
921 PG_RETURN_INT32(SRID_WORLD_MERCATOR);
937 double azimuth = 0.0;
943 if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
947 g = PG_GETARG_GSERIALIZED_P(0);
953 elog(ERROR,
"ST_Project(geography) is only valid for point inputs");
964 elog(ERROR,
"ST_Project(geography) cannot project from an empty start point");
968 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
969 azimuth = PG_GETARG_FLOAT8(2);
977 PG_RETURN_POINTER(g);
984 if ( lwp_projected == NULL )
986 elog(ERROR,
"lwgeom_project_spheroid returned null");
995 PG_FREE_IF_COPY(g, 0);
996 PG_RETURN_POINTER(g_out);
1017 g1 = PG_GETARG_GSERIALIZED_P(0);
1018 g2 = PG_GETARG_GSERIALIZED_P(1);
1025 elog(ERROR,
"ST_Azimuth(geography, geography) is only valid for point inputs");
1037 elog(ERROR,
"ST_Azimuth(geography, geography) cannot work with empty points");
1051 PG_FREE_IF_COPY(g1, 0);
1052 PG_FREE_IF_COPY(g2, 1);
1055 if( isnan(azimuth) )
1060 PG_RETURN_FLOAT8(azimuth);
1076 double max_seg_length;
1080 g1 = PG_GETARG_GSERIALIZED_P(0);
1088 PG_RETURN_POINTER(g1);
1106 g2 = geography_serialize(lwgeom2);
1111 PG_FREE_IF_COPY(g1, 0);
1113 PG_RETURN_POINTER(g2);
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.
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
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.
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *box)
Read the bounding box off a serialization and calculate one if it is not already there.
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
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_dwithin(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_dwithin_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, int *dwithin)
int geography_tree_distance(const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double tolerance, double *distance)
int geography_distance_cache(FunctionCallInfo fcinfo, const GSERIALIZED *g1, const GSERIALIZED *g2, const SPHEROID *s, double *distance)
void lwgeom_set_geodetic(LWGEOM *geom, int value)
Set the FLAGS geodetic bit on geometry an all sub-geometries and pointlists.
LWPOINT * lwpoint_make2d(int srid, double x, double y)
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.
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.
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
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.
void error_if_srid_mismatch(int srid1, int srid2)
#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.
double lwgeom_area_sphere(const LWGEOM *lwgeom, const SPHEROID *spheroid)
Calculate the geodetic area of a lwgeom on the sphere.
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.
Datum distance(PG_FUNCTION_ARGS)
Datum area(PG_FUNCTION_ARGS)