Summary
This post will demonstrate the usage of a new search feature within Redis - geospatial search with polygons. This search feature is part of the 7.2.0-M01 Redis Stack release. This initial release supports the WITHIN and CONTAINS query types for polygons, only. Additional geospatial search types will be forthcoming in future releases.
Architecture
Code Snippets
Point Generation
I use the Shapely module to generate the geometries for this demo. The code snippet below will generate a random point, optionally within a bounding box.
Polygon Generation
Random polygons can be generated using the random point function above. By passing a polygon as an input parameter, the generated polygon can be placed inside that input polygon.
Redis Polygon Search Index
The command below creates an index on the polygons with the new keyword 'GEOMETRY' for their associated WKT-formatted points. Note this code is sending a raw CLI command to Redis. The redis-py lib does not support the new geospatial command sets at the time of this writing.
Redis Polygon Load as JSON
The code below inserts 4 polygons into Redis as JSON objects. Those objects are indexed within Redis by the code above.
Redis Polygon Search
Redis Polygon search (contains or within) code below. Again, this is the raw CLI command.
Results
Plot
Results
Source
Copyright ©1993-2024 Joey E Whelan, All rights reserved.