PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ geography_bestsrid()

Datum geography_bestsrid ( PG_FUNCTION_ARGS  )

Definition at line 797 of file geography_measurement.c.

798 {
799  GBOX gbox, gbox1, gbox2;
800  GSERIALIZED *g1 = NULL;
801  GSERIALIZED *g2 = NULL;
802  int empty1 = LW_FALSE;
803  int empty2 = LW_FALSE;
804  double xwidth, ywidth;
805  POINT2D center;
806  LWGEOM *lwgeom;
807 
808  /* Get our geometry objects loaded into memory. */
809  g1 = PG_GETARG_GSERIALIZED_P(0);
810  /* Synchronize our box types */
811  gbox1.flags = gserialized_get_lwflags(g1);
812  /* Calculate if the geometry is empty. */
813  empty1 = gserialized_is_empty(g1);
814 
815  /* Convert g1 to LWGEOM type */
816  lwgeom = lwgeom_from_gserialized(g1);
817 
818  /* Calculate a geocentric bounds for the objects */
819  if ( ! empty1 && gserialized_get_gbox_p(g1, &gbox1) == LW_FAILURE )
820  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g1, &gbox1)");
821 
822  POSTGIS_DEBUGF(4, "calculated gbox = %s", gbox_to_string(&gbox1));
823 
824  if ( !lwgeom_isfinite(lwgeom) ) {
825  elog(ERROR, "Error in geography_bestsrid calling with infinite coordinate geographies");
826  }
827  lwgeom_free(lwgeom);
828 
829  /* If we have a unique second argument, fill in all the necessary variables. */
830  if (PG_NARGS() > 1)
831  {
832  g2 = PG_GETARG_GSERIALIZED_P(1);
833  gbox2.flags = gserialized_get_lwflags(g2);
834  empty2 = gserialized_is_empty(g2);
835  if ( ! empty2 && gserialized_get_gbox_p(g2, &gbox2) == LW_FAILURE )
836  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g2, &gbox2)");
837 
838  /* Convert g2 to LWGEOM type */
839  lwgeom = lwgeom_from_gserialized(g2);
840 
841  if ( !lwgeom_isfinite(lwgeom) ) {
842  elog(ERROR, "Error in geography_bestsrid calling with second arg infinite coordinate geographies");
843  }
844  lwgeom_free(lwgeom);
845  }
846  /*
847  ** If no unique second argument, copying the box for the first
848  ** argument will give us the right answer for all subsequent tests.
849  */
850  else
851  {
852  gbox = gbox2 = gbox1;
853  }
854 
855  /* Both empty? We don't have an answer. */
856  if ( empty1 && empty2 )
857  PG_RETURN_NULL();
858 
859  /* One empty? We can use the other argument values as infill. Otherwise merge the boxen */
860  if ( empty1 )
861  gbox = gbox2;
862  else if ( empty2 )
863  gbox = gbox1;
864  else
865  gbox_union(&gbox1, &gbox2, &gbox);
866 
867  gbox_centroid(&gbox, &center);
868 
869  /* Width and height in degrees */
870  xwidth = 180.0 * gbox_angular_width(&gbox) / M_PI;
871  ywidth = 180.0 * gbox_angular_height(&gbox) / M_PI;
872 
873  POSTGIS_DEBUGF(2, "xwidth %g", xwidth);
874  POSTGIS_DEBUGF(2, "ywidth %g", ywidth);
875  POSTGIS_DEBUGF(2, "center POINT(%g %g)", center.x, center.y);
876 
877  /* Are these data arctic? Lambert Azimuthal Equal Area North. */
878  if ( center.y > 70.0 && ywidth < 45.0 )
879  {
880  PG_RETURN_INT32(SRID_NORTH_LAMBERT);
881  }
882 
883  /* Are these data antarctic? Lambert Azimuthal Equal Area South. */
884  if ( center.y < -70.0 && ywidth < 45.0 )
885  {
886  PG_RETURN_INT32(SRID_SOUTH_LAMBERT);
887  }
888 
889  /*
890  ** Can we fit these data into one UTM zone?
891  ** We will assume we can push things as
892  ** far as a half zone past a zone boundary.
893  ** Note we have no handling for the date line in here.
894  */
895  if ( xwidth < 6.0 )
896  {
897  int zone = floor((center.x + 180.0) / 6.0);
898 
899  if ( zone > 59 ) zone = 59;
900 
901  /* Are these data below the equator? UTM South. */
902  if ( center.y < 0.0 )
903  {
904  PG_RETURN_INT32( SRID_SOUTH_UTM_START + zone );
905  }
906  /* Are these data above the equator? UTM North. */
907  else
908  {
909  PG_RETURN_INT32( SRID_NORTH_UTM_START + zone );
910  }
911  }
912 
913  /*
914  ** Can we fit into a custom LAEA area? (30 degrees high, variable width)
915  ** We will allow overlap into adjoining areas, but use a slightly narrower test (25) to try
916  ** and minimize the worst case.
917  ** Again, we are hoping the dateline doesn't trip us up much
918  */
919  if ( ywidth < 25.0 )
920  {
921  int xzone = -1;
922  int yzone = 3 + floor(center.y / 30.0); /* (range of 0-5) */
923 
924  /* Equatorial band, 12 zones, 30 degrees wide */
925  if ( (yzone == 2 || yzone == 3) && xwidth < 30.0 )
926  {
927  xzone = 6 + floor(center.x / 30.0);
928  }
929  /* Temperate band, 8 zones, 45 degrees wide */
930  else if ( (yzone == 1 || yzone == 4) && xwidth < 45.0 )
931  {
932  xzone = 4 + floor(center.x / 45.0);
933  }
934  /* Arctic band, 4 zones, 90 degrees wide */
935  else if ( (yzone == 0 || yzone == 5) && xwidth < 90.0 )
936  {
937  xzone = 2 + floor(center.x / 90.0);
938  }
939 
940  /* Did we fit into an appropriate xzone? */
941  if ( xzone != -1 )
942  {
943  PG_RETURN_INT32(SRID_LAEA_START + 20 * yzone + xzone);
944  }
945  }
946 
947  /*
948  ** Running out of options... fall-back to Mercator
949  ** and hope for the best.
950  */
951  PG_RETURN_INT32(SRID_WORLD_MERCATOR);
952 
953 }
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
Definition: gbox.c:135
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: gbox.c:392
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
Definition: gserialized.c:65
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
lwflags_t gserialized_get_lwflags(const GSERIALIZED *g)
Read the flags from a GSERIALIZED and return a standard lwflag integer.
Definition: gserialized.c:18
#define LW_FALSE
Definition: liblwgeom.h:94
#define LW_FAILURE
Definition: liblwgeom.h:96
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2681
double gbox_angular_height(const GBOX *gbox)
GBOX utility functions to figure out coverage/location on the globe.
Definition: lwgeodetic.c:188
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
Definition: lwgeodetic.c:215
int gbox_centroid(const GBOX *gbox, POINT2D *out)
Computes the average(ish) center of the box and returns success.
Definition: lwgeodetic.c:267
lwflags_t flags
Definition: liblwgeom.h:353
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References GBOX::flags, gbox_angular_height(), gbox_angular_width(), gbox_centroid(), gbox_to_string(), gbox_union(), gserialized_get_gbox_p(), gserialized_get_lwflags(), gserialized_is_empty(), LW_FAILURE, LW_FALSE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_isfinite(), POINT2D::x, and POINT2D::y.

Here is the call graph for this function: