hotosm / fmtm

Field Mapping Tasking Manager - coordinated field mapping.
https://fmtm.hotosm.org/
GNU Affero General Public License v3.0
46 stars 46 forks source link

Embed simple reverse geocoder in PostGIS database #1827

Open spwoodcock opened 5 days ago

spwoodcock commented 5 days ago

Problem

Solution

The final query could be something like (untested):

SELECT 
    g.city, 
    g.country
FROM 
    geocoding g
WHERE 
    ST_Contains(g.voronoi, ST_GeomFromText('POINT(-72.48496 -13.92862)', 4326));

Additional Considerations

Sujanadh commented 4 days ago

I think it would be best if we create our own package or separate repo for the geocoding so it will be efficient to use , scale it and benefits of using it in other projects too like Tasking Manager.

spwoodcock commented 4 days ago

Good point, this would be a great pypi package!

Taking a quick browse there is already a similar implementation! https://github.com/thampiman/reverse-geocoder

Perhaps we could take this code and update it (or it might even work as it is, despite being so old).

Looks like they bundle a small C++ util? Not sure about this approach. Would love to leverage PostGIS.

spwoodcock commented 4 days ago

Obviously this is a quite common issue to solve.

I love it when I write out an idea and somebody on the internet has already solved the task, to your exact spec 😂 https://github.com/richardpenman/reverse_geocode

We should probably just use that package 👍

spwoodcock commented 4 days ago

On second thought perhaps we just use that as inspiration - I'm not too keen on the implementation!

The package downloads the JSON data and loads it into memory for the query - far from ideal.

We could write an alternative backed by PostGIS and consider a similar k-d tree index like this? (needs more research):

CREATE INDEX [indexname] ON [tablename] USING SPGIST ( [geometryfield] );

https://www.crunchydata.com/blog/the-many-spatial-indexes-of-postgis