Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/drizzle/src/queries/parseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ export function parseParams({

if (typeof maxDistance === 'number' && !Number.isNaN(maxDistance)) {
geoConstraints.push(
sql`ST_DWithin(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857), ${maxDistance})`,
sql`ST_DWithin(${table[columnName]}::geography, ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326)::geography, ${maxDistance})`,
)
}

if (typeof minDistance === 'number' && !Number.isNaN(minDistance)) {
geoConstraints.push(
sql`ST_Distance(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857)) >= ${minDistance}`,
sql`ST_Distance(${table[columnName]}::geography, ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326)::geography) >= ${minDistance}`,
)
}
if (geoConstraints.length) {
Expand Down
43 changes: 43 additions & 0 deletions test/collections-rest/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,49 @@ describe('collections-rest', () => {
expect(result.docs).toHaveLength(0)
})

// https://github.com/payloadcms/payload/issues/14471 - ensure geospatial queries use true geodetic meters, not the distorted meters of EPSG:3857
it('should use true geodetic meters at high latitudes', async () => {
if (payload.db.name === 'sqlite') {
return
}

// A point ~10 km north of NYC: lat += 10000/111320 ≈ 0.0898°
const queryLng = -74.0059
const queryLat = 40.7128
const pointLat = queryLat + 0.0898 // ~10 km north
let createdId: number | string | undefined

try {
const created = await payload.create({
collection: pointSlug,
data: { point: [queryLng, pointLat] },
})

createdId = created.id

// Query with 12 km radius — the point at ~10 km should be within range.
// With the old EPSG:3857 approach, the effective radius at this latitude was
// only ~9 km, causing the point to be missed.
const response = await restClient.GET(`/${pointSlug}`, {
query: {
where: {
point: {
near: `${queryLng}, ${queryLat}, 12000`,
},
},
},
})
const result: any = await response.json()

expect(response.status).toEqual(200)
expect(result.docs.map((d: { id: number | string }) => d.id)).toContain(createdId)
} finally {
if (createdId !== undefined) {
await payload.delete({ collection: pointSlug, id: createdId })
}
}
})

it('should not return a point far away', async () => {
if (payload.db.name === 'sqlite') {
return
Expand Down
Loading