PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_bestsrid()

Datum geography_bestsrid ( PG_FUNCTION_ARGS  )

Definition at line 786 of file geography_measurement.c.

References GBOX::flags, GSERIALIZED::flags, gbox_angular_height(), gbox_angular_width(), gbox_centroid(), gbox_to_string(), gbox_union(), geography_project(), gserialized_get_gbox_p(), gserialized_is_empty(), LW_FAILURE, LW_FALSE, PG_FUNCTION_INFO_V1(), POINT2D::x, and POINT2D::y.

Referenced by geography_covers().

787 {
788  GBOX gbox, gbox1, gbox2;
789  GSERIALIZED *g1 = NULL;
790  GSERIALIZED *g2 = NULL;
791  int empty1 = LW_FALSE;
792  int empty2 = LW_FALSE;
793  double xwidth, ywidth;
794  POINT2D center;
795 
796  Datum d1 = PG_GETARG_DATUM(0);
797  Datum d2 = PG_GETARG_DATUM(1);
798 
799  /* Get our geometry objects loaded into memory. */
800  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(d1);
801  /* Synchronize our box types */
802  gbox1.flags = g1->flags;
803  /* Calculate if the geometry is empty. */
804  empty1 = gserialized_is_empty(g1);
805  /* Calculate a geocentric bounds for the objects */
806  if ( ! empty1 && gserialized_get_gbox_p(g1, &gbox1) == LW_FAILURE )
807  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g1, &gbox1)");
808 
809  POSTGIS_DEBUGF(4, "calculated gbox = %s", gbox_to_string(&gbox1));
810 
811  /* If we have a unique second argument, fill in all the necessary variables. */
812  if ( d1 != d2 )
813  {
814  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(d2);
815  gbox2.flags = g2->flags;
816  empty2 = gserialized_is_empty(g2);
817  if ( ! empty2 && gserialized_get_gbox_p(g2, &gbox2) == LW_FAILURE )
818  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g2, &gbox2)");
819  }
820  /*
821  ** If no unique second argument, copying the box for the first
822  ** argument will give us the right answer for all subsequent tests.
823  */
824  else
825  {
826  gbox = gbox2 = gbox1;
827  }
828 
829  /* Both empty? We don't have an answer. */
830  if ( empty1 && empty2 )
831  PG_RETURN_NULL();
832 
833  /* One empty? We can use the other argument values as infill. Otherwise merge the boxen */
834  if ( empty1 )
835  gbox = gbox2;
836  else if ( empty2 )
837  gbox = gbox1;
838  else
839  gbox_union(&gbox1, &gbox2, &gbox);
840 
841  gbox_centroid(&gbox, &center);
842 
843  /* Width and height in degrees */
844  xwidth = 180.0 * gbox_angular_width(&gbox) / M_PI;
845  ywidth = 180.0 * gbox_angular_height(&gbox) / M_PI;
846 
847  POSTGIS_DEBUGF(2, "xwidth %g", xwidth);
848  POSTGIS_DEBUGF(2, "ywidth %g", ywidth);
849  POSTGIS_DEBUGF(2, "center POINT(%g %g)", center.x, center.y);
850 
851  /* Are these data arctic? Lambert Azimuthal Equal Area North. */
852  if ( center.y > 70.0 && ywidth < 45.0 )
853  {
854  PG_RETURN_INT32(SRID_NORTH_LAMBERT);
855  }
856 
857  /* Are these data antarctic? Lambert Azimuthal Equal Area South. */
858  if ( center.y < -70.0 && ywidth < 45.0 )
859  {
860  PG_RETURN_INT32(SRID_SOUTH_LAMBERT);
861  }
862 
863  /*
864  ** Can we fit these data into one UTM zone?
865  ** We will assume we can push things as
866  ** far as a half zone past a zone boundary.
867  ** Note we have no handling for the date line in here.
868  */
869  if ( xwidth < 6.0 )
870  {
871  int zone = floor((center.x + 180.0) / 6.0);
872 
873  if ( zone > 59 ) zone = 59;
874 
875  /* Are these data below the equator? UTM South. */
876  if ( center.y < 0.0 )
877  {
878  PG_RETURN_INT32( SRID_SOUTH_UTM_START + zone );
879  }
880  /* Are these data above the equator? UTM North. */
881  else
882  {
883  PG_RETURN_INT32( SRID_NORTH_UTM_START + zone );
884  }
885  }
886 
887  /*
888  ** Can we fit into a custom LAEA area? (30 degrees high, variable width)
889  ** We will allow overlap into adjoining areas, but use a slightly narrower test (25) to try
890  ** and minimize the worst case.
891  ** Again, we are hoping the dateline doesn't trip us up much
892  */
893  if ( ywidth < 25.0 )
894  {
895  int xzone = -1;
896  int yzone = 3 + floor(center.y / 30.0); /* (range of 0-5) */
897 
898  /* Equatorial band, 12 zones, 30 degrees wide */
899  if ( (yzone == 2 || yzone == 3) && xwidth < 30.0 )
900  {
901  xzone = 6 + floor(center.x / 30.0);
902  }
903  /* Temperate band, 8 zones, 45 degrees wide */
904  else if ( (yzone == 1 || yzone == 4) && xwidth < 45.0 )
905  {
906  xzone = 4 + floor(center.x / 45.0);
907  }
908  /* Arctic band, 4 zones, 90 degrees wide */
909  else if ( (yzone == 0 || yzone == 5) && xwidth < 90.0 )
910  {
911  xzone = 2 + floor(center.x / 90.0);
912  }
913 
914  /* Did we fit into an appropriate xzone? */
915  if ( xzone != -1 )
916  {
917  PG_RETURN_INT32(SRID_LAEA_START + 20 * yzone + xzone);
918  }
919  }
920 
921  /*
922  ** Running out of options... fall-back to Mercator
923  ** and hope for the best.
924  */
925  PG_RETURN_INT32(SRID_WORLD_MERCATOR);
926 
927 }
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.
Definition: g_serialized.c:642
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:404
int gbox_centroid(const GBOX *gbox, POINT2D *out)
Computes the average(ish) center of the box and returns success.
Definition: lwgeodetic.c:267
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:179
#define LW_FAILURE
Definition: liblwgeom.h:79
double x
Definition: liblwgeom.h:328
#define LW_FALSE
Definition: liblwgeom.h:77
double y
Definition: liblwgeom.h:328
int gbox_union(const GBOX *g1, const GBOX *g2, GBOX *gout)
Update the output GBOX to be large enough to include both inputs.
Definition: g_box.c:146
uint8_t flags
Definition: liblwgeom.h:291
double gbox_angular_height(const GBOX *gbox)
GBOX utility functions to figure out coverage/location on the globe.
Definition: lwgeodetic.c:188
uint8_t flags
Definition: liblwgeom.h:383
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
Definition: lwgeodetic.c:215
Here is the call graph for this function:
Here is the caller graph for this function: