Farr East
Farr East

Reputation: 67

Easy way to determine if a point is in a polygon using Google Cloud Functions

I have a collection of polygons (GeoJSON) within a Google Firestore collection.

For a given lat/lng coordinate how can I determine which polygons would contain it?

I have used Leaflet, and Mapbox to solve a similar problem for a front end solution but never for a backend. Other NoSQL databases can perform this search natively but I cannot determine if Firestore can as well.

Hopefully, there is a relatively lightweight way of doing this search.

Thanks for your input!

Upvotes: 3

Views: 673

Answers (1)

esafwan
esafwan

Reputation: 18029

As far as I know there is no native support for this within Firestore. Geofire for example have some implementation on Geo spatial data using firebase realtmedb. However, it too as per what I know doesn't work with Firestore.

Your best bet is to run this as a function in Firebase function. You can store your polygons in Firestore and then run the calculation from functions using a node module such as this : https://www.npmjs.com/package/point-in-polygon

Most basic implementation will require you to check against each of your polygons. However if you can reduce this using any additional metadatas or some strategy using shortest route etc would help if you have many polygons to check against.

Upvotes: 2

Related Questions