Spatial Indexing aids Finding which Polygons contain a Point
Blog post from Tinybird
Geospatial operations, like identifying polygons that contain a specific point, can be accelerated using the geohash technique, which encodes Earth's surface into grid-shaped cells as short alphanumeric strings, with longer strings indicating greater precision. This approach is implemented using ClickHouse® functions for geohash encoding and indexing, which involve creating a materialized view that maps geohashes to polygon IDs. The process involves calculating the geohash of a point, retrieving candidate polygons from a geohash index table, and verifying the point's inclusion within these polygons using the pointInPolygon() function. Although geohash serves as a lossy filter, it significantly reduces the number of polygons that need to be tested, thus enhancing query efficiency. The introduction of experimental_geo_types in ClickHouse®, including Point, Ring, Polygon, and MultiPolygon, promises even faster future queries. Spatial indexing is highlighted as a method to further speed up queries by reducing the number of polygons tested, similar to how sorting keys optimize data source queries.