PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum geography_bestsrid ( PG_FUNCTION_ARGS  )

Definition at line 679 of file geography_measurement.c.

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

680 {
681  GBOX gbox, gbox1, gbox2;
682  GSERIALIZED *g1 = NULL;
683  GSERIALIZED *g2 = NULL;
684  int empty1 = LW_FALSE;
685  int empty2 = LW_FALSE;
686  double xwidth, ywidth;
687  POINT2D center;
688 
689  Datum d1 = PG_GETARG_DATUM(0);
690  Datum d2 = PG_GETARG_DATUM(1);
691 
692  /* Get our geometry objects loaded into memory. */
693  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(d1);
694  /* Synchronize our box types */
695  gbox1.flags = g1->flags;
696  /* Calculate if the geometry is empty. */
697  empty1 = gserialized_is_empty(g1);
698  /* Calculate a geocentric bounds for the objects */
699  if ( ! empty1 && gserialized_get_gbox_p(g1, &gbox1) == LW_FAILURE )
700  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g1, &gbox1)");
701 
702  POSTGIS_DEBUGF(4, "calculated gbox = %s", gbox_to_string(&gbox1));
703 
704  /* If we have a unique second argument, fill in all the necessary variables. */
705  if ( d1 != d2 )
706  {
707  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(d2);
708  gbox2.flags = g2->flags;
709  empty2 = gserialized_is_empty(g2);
710  if ( ! empty2 && gserialized_get_gbox_p(g2, &gbox2) == LW_FAILURE )
711  elog(ERROR, "Error in geography_bestsrid calling gserialized_get_gbox_p(g2, &gbox2)");
712  }
713  /*
714  ** If no unique second argument, copying the box for the first
715  ** argument will give us the right answer for all subsequent tests.
716  */
717  else
718  {
719  gbox = gbox2 = gbox1;
720  }
721 
722  /* Both empty? We don't have an answer. */
723  if ( empty1 && empty2 )
724  PG_RETURN_NULL();
725 
726  /* One empty? We can use the other argument values as infill. Otherwise merge the boxen */
727  if ( empty1 )
728  gbox = gbox2;
729  else if ( empty2 )
730  gbox = gbox1;
731  else
732  gbox_union(&gbox1, &gbox2, &gbox);
733 
734  gbox_centroid(&gbox, &center);
735 
736  /* Width and height in degrees */
737  xwidth = 180.0 * gbox_angular_width(&gbox) / M_PI;
738  ywidth = 180.0 * gbox_angular_height(&gbox) / M_PI;
739 
740  POSTGIS_DEBUGF(2, "xwidth %g", xwidth);
741  POSTGIS_DEBUGF(2, "ywidth %g", ywidth);
742  POSTGIS_DEBUGF(2, "center POINT(%g %g)", center.x, center.y);
743 
744  /* Are these data arctic? Lambert Azimuthal Equal Area North. */
745  if ( center.y > 70.0 && ywidth < 45.0 )
746  {
747  PG_RETURN_INT32(SRID_NORTH_LAMBERT);
748  }
749 
750  /* Are these data antarctic? Lambert Azimuthal Equal Area South. */
751  if ( center.y < -70.0 && ywidth < 45.0 )
752  {
753  PG_RETURN_INT32(SRID_SOUTH_LAMBERT);
754  }
755 
756  /*
757  ** Can we fit these data into one UTM zone?
758  ** We will assume we can push things as
759  ** far as a half zone past a zone boundary.
760  ** Note we have no handling for the date line in here.
761  */
762  if ( xwidth < 6.0 )
763  {
764  int zone = floor((center.x + 180.0) / 6.0);
765 
766  if ( zone > 59 ) zone = 59;
767 
768  /* Are these data below the equator? UTM South. */
769  if ( center.y < 0.0 )
770  {
771  PG_RETURN_INT32( SRID_SOUTH_UTM_START + zone );
772  }
773  /* Are these data above the equator? UTM North. */
774  else
775  {
776  PG_RETURN_INT32( SRID_NORTH_UTM_START + zone );
777  }
778  }
779 
780  /*
781  ** Can we fit into a custom LAEA area? (30 degrees high, variable width)
782  ** We will allow overlap into adjoining areas, but use a slightly narrower test (25) to try
783  ** and minimize the worst case.
784  ** Again, we are hoping the dateline doesn't trip us up much
785  */
786  if ( ywidth < 25.0 )
787  {
788  int xzone = -1;
789  int yzone = 3 + floor(center.y / 30.0); /* (range of 0-5) */
790 
791  /* Equatorial band, 12 zones, 30 degrees wide */
792  if ( (yzone == 2 || yzone == 3) && xwidth < 30.0 )
793  {
794  xzone = 6 + floor(center.x / 30.0);
795  }
796  /* Temperate band, 8 zones, 45 degrees wide */
797  else if ( (yzone == 1 || yzone == 4) && xwidth < 45.0 )
798  {
799  xzone = 4 + floor(center.x / 45.0);
800  }
801  /* Arctic band, 4 zones, 90 degrees wide */
802  else if ( (yzone == 0 || yzone == 5) && xwidth < 90.0 )
803  {
804  xzone = 2 + floor(center.x / 90.0);
805  }
806 
807  /* Did we fit into an appropriate xzone? */
808  if ( xzone != -1 )
809  {
810  PG_RETURN_INT32(SRID_LAEA_START + 20 * yzone + xzone);
811  }
812  }
813 
814  /*
815  ** Running out of options... fall-back to Mercator
816  ** and hope for the best.
817  */
818  PG_RETURN_INT32(SRID_WORLD_MERCATOR);
819 
820 }
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:373
char * gbox_to_string(const GBOX *gbox)
Allocate a string representation of the GBOX, based on dimensionality of flags.
Definition: g_box.c:328
int gbox_centroid(const GBOX *gbox, POINT2D *out)
Computes the average(ish) center of the box and returns success.
Definition: lwgeodetic.c:244
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
#define LW_FAILURE
Definition: liblwgeom.h:54
double x
Definition: liblwgeom.h:284
#define LW_FALSE
Definition: liblwgeom.h:52
double y
Definition: liblwgeom.h:284
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:109
uint8_t flags
Definition: liblwgeom.h:247
double gbox_angular_height(const GBOX *gbox)
GBOX utility functions to figure out coverage/location on the globe.
Definition: lwgeodetic.c:165
uint8_t flags
Definition: liblwgeom.h:339
double gbox_angular_width(const GBOX *gbox)
Returns the angular width (longitudinal span) of the box in radians.
Definition: lwgeodetic.c:192

Here is the call graph for this function: