Best database for geolocation?

mudboy

Ars Legatus Legionis
17,501
Subscriptor
Working on a new project that will have at the center of its functionality geolocation functionality - i.e. querying for users that are within a set distance from an event location. I haven't done any coding in a while since I did a career change from web dev to teacher - probably 7 or 8 years since I did anything notable, and SQL was always a weak point for me. Of the major free DBMS's (MySQL, MS SQL, etc.) that are appropriate for web apps, which database makes geolocation querying the easiest?

Thanks,

Pete
 

hestermofet

Ars Legatus Legionis
25,464
Geolocation how? Using GPS directly via a smartphone API? HTML5? What is the SQL database for? Your own GIS data? Or just the user's location, and then you'll query taht against a GIS package, such as Google Maps or ESRI ArcGIS? Your project specifications are a bit fuzzy.

I've used Google Maps and MySQL to store user coordinates in the past. If you are planning massive scale, we have a postgres database at work that is close to a billion records.

Google Map's REST API is super easy to use.
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
Geo location based on "as the crow flies" or actual travel distance. Obviously you would use the former as a bounding box for working out the latter in any simple scheme, but you get the idea...

Both sql server and postgresql have some built in basic capabilities, but nothing like the power of a GIS solution. If you're dipping your toes in the free sounds good, otherwise you likely need someone with direct expertise. The link from bombcar seems like a great place to start.
 

AndrewZ

Ars Legatus Legionis
11,860
Is this a hobby project or for work? If you're going to use SQL, queried from web calls, then any database may be suitable. Top choices to get up and running quickly are PostGres (as referenced above), MySQL, and the free version of MS SQL Server. Probably not a big difference in performance until you scale up. The hardware comes into the picture.
 

mudboy

Ars Legatus Legionis
17,501
Subscriptor
Is this a hobby project or for work? If you're going to use SQL, queried from web calls, then any database may be suitable. Top choices to get up and running quickly are PostGres (as referenced above), MySQL, and the free version of MS SQL Server. Probably not a big difference in performance until you scale up. The hardware comes into the picture.

Personal project, as the crow flies distance. Thanks for the advice.
 
Django+PostgreSQL would probably be my recommendation right now if you're building a webapp and need to store the geodata in your own DB. GeoDjango uses the built-in Django ORM, which is pretty well documented, has lots of stack overflow posts, etc. - I think that would help a lot if you're SQL-fu is weak and ~decade old. PostGIS would need to be installed into postgres, but that's generally pretty easy (get a prebuilt docker container, or use the base brew packages for sanity's sake on a mac).

All that being said, if you can get away with keeping as much data in google maps as you can, do so. Seriously. Geoserver, Openlayers, Mapbox, etc. all suck to work with (to varying degrees) even when you're knowledgeable with their toolchains.
 
MySQL supports the gis extensions but I understand the word efficient is not used.
I used it's functionality in the past to create polygon based buckets to drop a set of postal codes in. It worked well enough for that because it was a one time thing. (Well, I need to refresh with a better postal code database because the first wasn't really accurate enough).

Postgres seemed to be the best free database choice and is used for stuff like OpenStreet Maps, so it's obviously decent.
 

thomahawk

Ars Tribunus Militum
2,147
Subscriptor++
PostGIS on PostgreSQL is a perfectly production-grade solution. Plenty of production GIS shops & geo-focused webapps use it as their prod backend.

That said, only bother with PostGIS if you’re doing “actual” geo work: handling polygon or line features, dealing with different coordinate systems and/or projected data, doing complex GIS queries (unions/intersects/buffers of geo features), and that kind of thing.

If you’re just doing a “within x km of point” search then almost every DB has a function for that by now. You might even just be able to store the point coordinates as a double in the DB and do your calculation client-side, either in google maps api or a library like turf.
 

dodexahedron

Ars Praefectus
3,368
Subscriptor++
PostGIS on PostgreSQL is a perfectly production-grade solution. Plenty of production GIS shops & geo-focused webapps use it as their prod backend.

That said, only bother with PostGIS if you’re doing “actual” geo work: handling polygon or line features, dealing with different coordinate systems and/or projected data, doing complex GIS queries (unions/intersects/buffers of geo features), and that kind of thing.

If you’re just doing a “within x km of point” search then almost every DB has a function for that by now. You might even just be able to store the point coordinates as a double in the DB and do your calculation client-side, either in google maps api or a library like turf.

Client-side? I don't see how you'd do that without literally barfing every single value down to the client.
I'd rather cache the results of a given lookup so I could return them instantly, later, since GIS searches tend to be pretty sargable (a term "kids these days" seem to not grok). At least if the search is "find X by distance from Y." If the search is "find X which is in the range of a user-supplied input Y through Z", then client-side almost makes sense, except that, at that point, you'd still just make that the parameter for your WHERE clause, and still only return a small result set to the client.

I'm willing to be educated, though, if there's a better, client-side, way to do things. Is there? Am I just a geezer, now?
 

thomahawk

Ars Tribunus Militum
2,147
Subscriptor++
PostGIS on PostgreSQL is a perfectly production-grade solution. Plenty of production GIS shops & geo-focused webapps use it as their prod backend.

That said, only bother with PostGIS if you’re doing “actual” geo work: handling polygon or line features, dealing with different coordinate systems and/or projected data, doing complex GIS queries (unions/intersects/buffers of geo features), and that kind of thing.

If you’re just doing a “within x km of point” search then almost every DB has a function for that by now. You might even just be able to store the point coordinates as a double in the DB and do your calculation client-side, either in google maps api or a library like turf.

Client-side? I don't see how you'd do that without literally barfing every single value down to the client.
I'd rather cache the results of a given lookup so I could return them instantly, later, since GIS searches tend to be pretty sargable (a term "kids these days" seem to not grok). At least if the search is "find X by distance from Y." If the search is "find X which is in the range of a user-supplied input Y through Z", then client-side almost makes sense, except that, at that point, you'd still just make that the parameter for your WHERE clause, and still only return a small result set to the client.

I'm willing to be educated, though, if there's a better, client-side, way to do things. Is there? Am I just a geezer, now?

Well, it depends on the data scale, but yes, it’s entirely plausible to send all the relevant data to the client in a geojson blob on page load. Obviously this isn’t going to be ideal if you have hundreds+ MB of data. But if you can do it, once it’s done, the in-browser queries/actions are instant, so it’s often better for interactive web apps.

So as always it depends on the use case. If your app is along the lines of “find me every street corner in America within 10 feet of a house with a slate roof that’s within a planning zone 3 polygon” then yeah, you want to do that server-side and PostGIS is the way to go. If you have a couple million geolocated tweets and want to find out how many were done within 100m of a Starbucks then you can probably do that query in whatever DB you’re using without a specialised geodatabase plugin (eg. elasticsearch has lat/lon data types and associated queries). If it’s more like “here are the few dozen public transport stops in my city, let me use a slider to adjust a buffer in real time so I can see which one my house is closest to” then you probably want to do that client-side so you’re not waiting on a server request every time you adjust the slider.

But yes, as bandwidth, user compute power, and tooling (like D3 and webGL) improve, you can do more and more client-side in-browser that you would have needed a full blown geodatabase and desktop workstation with QGIS/ESRI software for 5 years ago. Just look at Kepler https://eng.uber.com/keplergl/ or the like.