Find the buildings with the most nearby taxi pickups#
Which buildings have the most taxi pickups nearby? You’ll join buildings to pickups by distance, then check the ranked counts against the sample’s expected rows.
cuGeo aims to help GIS practitioners and developers run vector analysis, raster processing, and coordinate transformations on NVIDIA GPUs at larger scales. Here you’ll use familiar spatial SQL through Geometry’s Python interface.
Open the sample layers#
Start in cugeo-geometry/ with the Python bindings and native backend available. The sample has 113 trip rows with pickup points in t_pickuploc and 105 building rows with polygons in b_boundary.
from pathlib import Path
from collections import Counter
import json
import cugeo.geometry as geo
root = Path('crates/execution/tests/fixtures/spatialbench-q8-local-files')
tables = {
name: geo.table_from_file(root / f'{name}.parquet')
for name in ('trip', 'building')
}
Count nearby pickups#
The saved query joins each pickup to buildings within 0.0045 of it, groups the pairs by building key and name, and returns the first 100 groups in rank order.
SELECT b.b_buildingkey, b.b_name, COUNT(*) AS nearby_pickup_count
FROM trip t JOIN building b
ON ST_DWithin(ST_GeomFromWKB(t.t_pickuploc), ST_GeomFromWKB(b.b_boundary), 0.0045)
GROUP BY b.b_buildingkey, b.b_name
ORDER BY nearby_pickup_count DESC, b.b_buildingkey ASC
LIMIT 100
The ST_DWithin threshold is in the sample’s coordinate units. Both files declare a null CRS, so you cannot infer a distance in metres. Each qualifying pickup–building pair contributes one occurrence to its building key and name count.
Execute the saved query.
result = geo.sql((root / 'q8.sql').read_text(), tables=tables)
Read the ranking#
Print the columns and row count, then sort a preview for repeatable display.
print(result.columns)
print(len(result.rows))
for row in sorted(
result.rows,
key=lambda r: (-r[2], r[0], r[1] is not None, r[1] or ''),
)[:5]:
print(row)
The verified display follows.
('b_buildingkey', 'b_name', 'nearby_pickup_count')
100
(5000000000, None, 4)
(5000000000, 'Annex', 3)
(5000000000, 'Café 東京', 3)
(3000000000, 'shared-name', 1)
(3000000001, 'shared-name', 1)
The leading group has four pickups and a missing name. Annex and Café 東京 share a key and each has three pickups, but grouping by key and name keeps their counts separate. Alphabetical ordering of those two names is a display choice.
Check all 100 rows#
Compare every row occurrence with the expected data, then check the ordering promised by the query. The ordering check accepts either order for peers tied on count and key.
records = json.loads((root / 'q8-expected.json').read_text())['rows']
expected = [tuple(record[c] for c in result.columns) for record in records]
assert Counter(result.rows) == Counter(expected)
keys = [(-r[2], r[0]) for r in result.rows]
assert keys == sorted(keys)
print('All expected rows match')
All expected rows match