Postgres
PostGIS adds geography columns and spatial indexes to Postgres.
Set up the table
CREATE EXTENSION IF NOT EXISTS postgis | Turns on PostGIS. Only works in schema.sql |
col geography(Point, 4326) | A longitude/latitude column. Distances in meters |
CREATE INDEX name ON t USING gist (col) | GiST index, for spatial columns |
Each run fills drivers by column: id, lat, lng, updated_at, and any geography column.
Find the nearest drivers
ORDER BY location <-> point LIMIT 10 | The 10 drivers nearest a point, nearest first, read from the GiST index |
It returns rows like this, with dist in meters. The 10 nearest can be outside the radius, so drop any row whose dist is over radius_km * 1000:
[{"id": 8812, "lat": 37.7751, "lng": -122.4191, "dist": 412.7}, ...]Redis
driver_locations is a geo set of every driver, with the driver's id as the member.
await redis.geosearch(key, ...) | Members within a radius of a point |
It returns each driver's id and (lng, lat), nearest first:
[["8812", (-122.4191, 37.7751)], ["301", (-122.42, 37.776)], ...]Python · 65 linesStarter code · Ctrl/⌘ + Enter to run
SystemBroke
No runs yet.
Run the simulation to send this kata's traffic to your code. Hidden checks grade what it does.