The main reference about Geography datatype seems docs/using_postgis_dbmanagement:
Prior to PostGIS 2.2, the geography type only supported WGS 84 long lat (SRID:4326). For PostGIS 2.2 and above, any long/lat based spatial reference system defined in the spatial_ref_sys table can be used (…)
Regardless which spatial reference system you use, the units returned by the measurement (…) are in meters.
But it is not explicit: when I am "casting to another type" and when PostGIS is using internally the same "Geography calculations", without any casting need?
Other texts, like postgis-intro/geography, and other documents are also confuse: … is a Geometry with no projection a Geography?
The "standard no projection" is Geometry(4326)
, so, why the need for a Geography data type if PostGIS can detect it by SRID?
Of course, for PostgreSQL is important to change the data type, to do function overloading… But the guide not explains the equivalence schemas in the internal representation.
NOTES
There are some previous answers to earlier questions:
- this answer to Oracle equivalent to postgis EPSG 4326 "geometry" type, and
- this answer to Understanding geography, geometry, distance and wkt in PostGIS
but those questions are not duplicates of this one.
About comments, to avoid terminological confusion: a Geometry with SRID=4326
is not "only lat long". The so-called "WGS84" is a CRS (coordinate reference system), a synonym and human-readable abbreviation for urn:ogc:def:crs:EPSG::4326
. Any formal standard CRS is composed by ellipsoid parametrized model reference + parametrized datum reference, so, the formal definition is:
Best Answer
tl;dr: No. But see the update at the end.
GEOMETRY
andGEOGRAPHY
are different PostgreSQLTYPES
; exposed as higher level (SQL), user-defined composite types to the psql environment, but implemented as lower level (C) base types.These base types are defined on the C level with e.g. their own typemod constraints, TOAST support, data I/O functions, operator classes and casting behaviour. On top of that, both types have their own indexing mechanics.
On the SQL level, both types (optionally) accept a
TEXT
and aNUMERIC
parameter, which translate to geometry type, and spatial reference. Additionally, you can index both with the same command (USING GIST(<geom>)
), although internally those indexes differ, andCAST
between them almost seamlessly, with some caveats as seen in the example below.Under the hood, the core difference is the mathematical representation and calculation of and with the given coordinates, in two words: planar vs. spheroidal.
The
GEOMETRY
type is bound by definition to a 2D planar reference, and operates with Cartesian math regardless of the nature of the CRS; distance between two points is a straight line as e.g. per Pythagoras, even if your geometries are defined in a geodetic reference (e.g. EPSG:4326). TheGEOMETRY
type is meant to work best for projected CRS.The
GEOGRAPHY
type is inherently assuming that the given coordinates are geodetic coordinates, thus operating solely on great circle math (spherical) and/or it's more complicated application on spheroidal/ellipsoidal bodies. TheGEOGRAPHY
type is meant to work with geographic/geodetic RS (Longitudes/Latitudes) only.The WGS84 ellipsoid (EPSG:7030) is allegedly among the most usable representation of our planet on a global scale, and is widely supported in transformations to and from projections and other ellipsoids. Also, it is and has ever since been the base reference of most of our positioning systems.
It is de facto a global standard for spatial data storage and its interchangeability.
0
(which is bad to have...).Now, as a small example of what PostGIS does with wrong SRS definitions; you can do this (note that the coordinates should represent degrees...)
returning a valid
GEOMETRY
while this
returns a valid
GEOGRAPHY
along with the note
Btw. coercing here means sth. like
-90 + (1000 % 90) = -80
-180 + (1000 % 180) = -80
so it effectively walks around the globe until 1000 degrees are traversed.
Yet the
GEOGRAPHY
type does not care if you doexcept for the above note of coordinate range coercion, while it actually should (if it could; we're assigning a SRS on the created type, so it would be the function assigning it that needed to) inform you about the nature of the SRS (and, since we're at it, IMHO also the uselessness of EPSG:3857...).
However, a
CAST
toGEOGRAPHY
will indeed check the SRS and denies the cast for non-geodetic SRS, e.g.will error out with
CAST
toGEOGRAPHY
.Update as per comment request:
Because PostGIS defines the cast from
GEOMETRY
toGEOGRAPHY
(and only this way)AS IMPLICIT
, PostgreSQL is free to cast anyGEOMETRY
toGEOGRAPHY
whenever it is necessary, or possible, to solve ambuiguity. This affects parameters to functions in the following ways, where PostgreSQL willCAST
implicitly:if a distinct signature is unambiguously invoked that accepts only
GEOGRAPHY
types; an example would be theST_Area
signatures where, when explicitly used with a value foruse_spheroid
, PG casts aGEOMETRY
toGEOGRAPHY
an ambiguous signature for both types is used (i.e. a type overloaded function), and only one parameter is passed in as
GEOGRAPHY
, the other will get cast; an example isST_DWithin
where, when one parameter gets passed asGEOMETRY
and the other asGEOGRAPHY
, the former will get cast toGEOGRAPHY
One might say, since only the
CAST
toGEOGRAPHY
isIMPLICIT
, it has inherent precedence.In these cases, since the
CAST
is used, PostGIS will inform you (and deny the execution) if theGEOMETRY
in question does not use a geodetic SRS!CAST
.