IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Geoqueries on boxes #100

Open RWOverdijk opened 4 years ago

RWOverdijk commented 4 years ago

I have venues with delivery areas that I store inside of boxes. My initial plan was to find all venues that have a box I'm inside of, but unfortunately firestore doesn't allow multi-field range queries.

I want to find all the venues that have a delivery area (box) that the user is inside of. Geohashing works with points, so I'm not quite sure how to solve this.

Do you maybe have any pointers?

IjzerenHein commented 4 years ago

A geohash in itself is not a point but also a box. I guess you could perhaps create a field in each venue doc that holds an array of geohashes. E.g. area: ['rd56s', 'rd673', ..]. And then for instance let each geohash have a resolution of about (1000?) meters. And then calculate the geohash (with that same 1000m resolution) at your current location and use an array-in query to get all venues.

RWOverdijk commented 4 years ago

@IjzerenHein Ah, I see. I actually meant the other way around. The user's location is a point, and I want to check inside of which boxes it belongs.

So the venue has let's say 4 polygons, but I make it a box that contains all of those polygons (using the lowest and highest lat/lon of all polygons).

image

This should make it easier to do a "fast" initial filter because traditionally I would be able to do:

const within = xLat <= lat && xLon <= lon && yLat >= lat && yLon >= lon;

And then I can filter inside of those matches (which is going to be a much smaller data set, in which I can then testing the polygons).

Which is what I do right now on the client. I just fetch everything and filter client-side. But as you can imagine that's not very scalable. I suppose it would help if I could filter this out before it gets observed but even then, there's a limit.

But because (afaik) geohash boxes are fixed in position/grid I can't really do that, can I? Or did I misunderstand what you said?

Why can't firestore just let me do multiple range clauses on multiple fields. 😄