Skip to content

docs(sqlalchemy): note SRID caveat for DWITHIN/BEYOND distances - #168

Closed
cpruijsen wants to merge 2 commits into
geopython:mainfrom
cpruijsen:fix/issue-164
Closed

cpruijsen wants to merge 2 commits into
geopython:mainfrom
cpruijsen:fix/issue-164

Conversation

@cpruijsen

Copy link
Copy Markdown

The SQLAlchemy backend converts DWITHIN and BEYOND distances the wrong way. In
pygeofilter/backends/sqlalchemy/filters.py, kilometers are divided by 1000 and miles by 1609, where
both should multiply to reach meters. A 5 kilometer query therefore searches 0.005 meters, so it
matches nothing, and the failure is silent: an empty result set rather than an error.

This multiplies instead, and uses 1609.34 for miles rather than 1609, which is the statute mile to
three decimal places rather than truncated.

The docstring gains a note that the value is converted to meters, and that meters is what ST_DWithin
measures in only for projected SRIDs or geography columns. On a geographic SRID with a geometry
column it measures in degrees, so the conversion is right for the cases this backend supports and
worth stating rather than leaving for someone to rediscover.

Tests cover both units against the compiled SQL.

Fixes #164

The distance is converted to meters, which is only what ST_DWithin
measures in for projected SRIDs or geography columns; on a
geographic-SRID geometry column it is interpreted in degrees.

Refs geopython#164
@cpruijsen

Copy link
Copy Markdown
Author

Closing this. Not something I'm going to keep current.

@cpruijsen cpruijsen closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlalchemy backend: DWITHIN/BEYOND unit conversion divides instead of multiplying

1 participant