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

Definition at line 773 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.

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

Here is the call graph for this function: