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're going to want to replace this construction
st_intersects(proj_shape, st_buffer(cary_proj_point, 5280))
with this one
st_dwithin(proj_shape, cary_proj_point, 5280)
(because otherwise you'll have a terrible scalability problem in the future) and it will won't give you the answer you want unless proj_shape and cary_proj_point both have SRID = 2264. Do they?
Best Answer
The point that is created has no SRID, i.e.
0
, which is different than4326
.That being said,
st_distance
uses the unit of the CRS, so degrees in this case.One solution is to use
geography
instead ofgeometry
, as the unit is meters, and to usest_dwithin()
instead ofst_distance
as it makes use of spatial index. Eventually, you will want to add an index on the geography transformation.