When you specify a geometry without an SRID, it is actually 0
(or -1
for version < 2):
SELECT ST_SRID('POINT(-122.334172173172 46.602634395263560)'::geometry);
st_srid
---------
0
So when you use this geometry with another one with SRID=4326, it is mixing 0
and 4326
. This is usually a useful error, if the spatial references are truly different. With your case, the SRIDs are the same, but you didn't encode the SRID to the query point. So, to fix your query always specify the same SRID for your query point, and they will no longer be mixed.
As a side note, the geography
type has a default SRID of 4326 (WGS 84), as shown here:
SELECT ST_SRID('POINT(-122.334172173172 46.602634395263560)'::geography::geometry);
st_srid
---------
4326
So, if you use geography
types instead of geometry
types, the SRID does not need to be specified (unless you want a different SRID for an alternative ellipsoid for Mars or whatever).
As to why one query has an error, and the other does not, ST_Intersects
first does a &&
bounding box search, which is fast, and doesn't care about SRIDs. No mixed SRID error messages will be raised if the bounding boxes don't intersect. But if they do intersect, the second filter is _ST_Intersects
, which is more precise and checks the two SRIDs to make sure they match, and raises an error if they are mixed. For example:
WITH pad_meta AS (
SELECT 'SRID=4326;POLYGON((-124 50, -124 47, -121 50, -124 50))'::geometry AS area)
SELECT * FROM pad_meta where ST_Intersects(
'POINT(-122.334172173172 46.602634395263560)'::geometry, area::geometry
);
does not have any intersecting bounding boxes, and bypasses _ST_Intersects
. But POINT(-122.334172173172 47.602634395263560)
will raise the error because the bounding boxes do overlap (even though the geometries don't actually intersect).
However, with the same geometries and different filter:
WITH pad_meta AS (
SELECT 'SRID=4326;POLYGON((-124 50, -124 47, -121 50, -124 50))'::geometry AS area)
SELECT * FROM pad_meta where _ST_Intersects(
'POINT(-122.334172173172 46.602634395263560)'::geometry, area::geometry
);
throws a mixed SRID error, because bounding boxes are not considered.
You don't need constraints for PostGIS 2.x, just us typmods. For 2D geometries with SRID=4326, the typmod is geometry(Geometry,4326)
. Or for M-dimension geometries, it would be geometry(GeometryM,4326)
(you get the idea).
Example:
create temp table sometable(wkt geometry(Geometry,4326));
-- works
insert into sometable(wkt) values('SRID=4326;POINT(1 2)')
insert into sometable(wkt) values('SRID=4326;LINESTRING(1 2, 3 4)')
-- does not work
insert into sometable(wkt) values('POINT(1 2)')
-- ERROR: Geometry SRID (0) does not match column SRID (4326)
insert into sometable(wkt) values('SRID=4326;POINT M(1 2 3)')
-- ERROR: Geometry has M dimension but column does not
But as others have cautioned, make sure you can use the wkt
data in the end-user applications. Also, it isn't technically stored as well-known text—it is stored as well-known binary or WKB.
Best Answer
you can use the generic
geometry
type. Also make sure to spell the proper column name in theusing