Self Hosting a Google Maps Alternative with OpenStreetMap
wcedmisten.fyi
wcedmisten.fyi
When writing this I found out that MapTiler[1] is maintaining MapLibre GL JS[2], a fork of a Mapbox project to do just that. It would be interesting to see the difference between self hosting raster and vector maps and compare pros and cons. You can even render raster tiles from vector tiles on the server if the client needs it[3].
[0] https://openmaptiles.org/docs/style/mapbox-gl-style-spec/
[1] https://www.maptiler.com/open-source/
More info from the author here: https://blog.nobugware.com/post/2019/self_hosted_world_maps/
The vector tiles are much smaller in filesize than pixel data and the rendering technology is much better. Almost all the styling is done on the client side so the server is basically just querying a spatial database.
1) Some of the most popular map library frontends like Leaflet support only raster tile layers out of the box.
2) Quality vector generalization (feature-dropping) is much more difficult than raster generalization.
3) A large fraction of use cases just need a "good enough" basemap without those extra features.
I at least would find it interesting to see the two compared by someone other than me ;).
https://github.com/protomaps/protomaps.js
TBH, the blocker is not demand for vector rendering but the technical difficulty and performance constraints of doing rasterization in the browser.
Do you think it's still too early to switch to vector map data for all clients then? Maybe render raster tiles on demand on the server to those clients that can't handle it?
There are a couple great public raster services like osm.org's default style and http://maps.stamen.com. These are 100% free to use, so they get used everywhere, but incur significant expense to the organizations running (paying) for them.
There aren't equivalent solutions in vector-land yet... I wrote a bit about this previously: https://protomaps.com/blog/free-tier-maps
Their stack is open sorce, described there: https://github.com/Qwant/qwantmaps#tile-server
[1]: https://github.com/SpatialServer/Leaflet.MapboxVectorTile
So lets optimize the browsers SVG rendering?
Things that are mandatory for map display, like:
- Label priority and positioning
- Display of internationalized, RTL, and Bidi text
- Geometry rasterization and styling
Are outside the scope of Leaflet's core design and can be implemented by plugins, one of which I have linked in a sibling comment.
I maintain a heavily used map of the US, and I found that storing data/serving in vector format seems to be the most efficient (on AWS s3 / DO spaces), and converting to raster in the browser or with a service call / lambda really opens the doors to a lot more use cases. I suspect parent can talk more about this than I can.
So raster tiles load much faster and OpenStreetMap retina tiles help against pixelation.
And you can - though not advisable for large areas - render raster tiles at much higher zoom levels, e.g. SomeOneElse renders his raster style up to level 28 for the UK.
I still think that the more proper solution would be to instead cap out the vector tiles on the other end, not going lower than say level 5.
I also don't intend to argue against a vector tile stack - both vector and raster have their use cases and pro's and con's. Vector tiles bring much more flexibility but less support in some use cases, raster normally loads/displays much faster (if served pre-rendered) and has universal support on the client side.
Unfortunately I'm not really aware of any FOSS dataset for that yet, but that may change in the coming decades if the price of mass to orbit goes down.
https://osmand.net/docs/user/map/vector-maps/
https://osmand.net/docs/technical/osmand-file-formats/osmand...
Amazon has donated $300,000 to MapLibre, Facebook - $80,000, TomTom - $10,000, and lots more companies have donated. Most of the code maintenance is done by various volunteers and companies, e.g. Amazon has hired a whole team of engineers to work on MapLibre native, etc.
P.S. I came up with the name MapLibre, called people to unite around this effort, and am on the board of the MapLibre organization. [2]
[1]: https://maplibre.org/charter/ [2]: https://twitter.com/nyuriks/status/1336493514646052864
Is it, because sth. like a full street network layer would degrade performance too quickly?
[0] For the explanation and minmal version see https://www.youtube.com/watch?v=t8eVmNwqh7M source code here https://github.com/pramsey/minimal-mvt. For a full fledged package see https://github.com/maplibre/martin or https://github.com/CrunchyData/pg_tileserv
[0]: https://github.com/onthegomap/planetiler [1]: https://github.com/maptiler/tileserver-gl
Here's a nice example of the maps in action (though the other links also have showcases): https://openmaptiles.org/docs/website/maplibre-gl-js/
The transitions seem really smooth and the user experience is pleasant... until I open them on an Android device that came out just 2 years ago (one of those rugged models), in Firefox.
I'm sure that the slowness has something to do with rendering vector data, because all of the sudden those maps become unusable - slow zooming, slow loading (possibly the data is fetched reasonably quickly, just slow to display), slow scrolling. In a word, laggy. When looking at an area of my choice, using the vector maps (even after letting them load fully) I get probably less than 10 frames per second so panning feels choppy.
On the other hand, the raster maps are basically what you'd get when panning across an image - the latter experience remains reasonably smooth. There, the only disadvantage is the typical swapping out of raster tiles as you zoom in or out, but that's a small nuisance at best, apart from the visual difference in quality.
Have a look at OpenStreetMap, which the first link uses for its data: https://www.openstreetmap.org
As it currently stands, if we all used only vector data, I feel like a lot of people on lower spec devices would be left out. I wonder whether any serious tests have been made on the battery usage or performance of both types, but even tests this simple show that there are certain problems with the current implementations of vector maps.
I actually did a blog post about this just now, with a video example of the two types of maps running side by side: https://blog.kronis.dev/everything%20is%20broken/vector-maps...
Note: this isn't meant to be a super accurate comparison, but rather a quick look into some of the immediately apparent practical differences in what the user experience is like.
If you need to support old underpowered devices you can serve raster tiles to them. I don't know how to automatically detect which type to send to a device, one way would be to just time how long it takes to render a frame of vector data and switch to raster maps if the device is too slow, or at least present the user with the option.
[0] https://www.mapbox.com/maps/streets (Press "Preview Style")
Google Maps (in Firefox) seem to be slow when loading data in, but decent when panning around. Though it does appear that they're using raster based tiles (I can quite literally see pixels when I zoom in, before the new tile loads and replaces the one for the previous zoom level).
Google Maps (Android app) seem to be really smooth when panning around, but also similarly slow when loading data in. Zooming is a bit sluggish, but not as bad as vector tiles when viewed through the browser.
I also remember using the HERE maps application a long time ago on an even slower device, somehow those performed way better than Google Maps application though.
My suspicions: a browser might have greater overhead, maybe not everything plays nicely with Firefox or my hardware. The slow data loading seems to be caused by the CPU/GPU instead of the network connection, because if new tiles are loaded in while I'm panning around, the movement also lags.
> If you need to support old underpowered devices you can serve raster tiles to them. I don't know how to automatically detect which type to send to a device, one way would be to just time how long it takes to render a frame of vector data and switch to raster maps if the device is too slow, or at least present the user with the option.
This is a good idea, provided that the people serving the maps would be interested in supporting both formats.
My daily driver is a Samsung Galaxy S4 (2014) with LineageOS and Firefox (Fennec F-Droid, no google services).
Your link works pretty well, it took a few seconds to load the map, but zooming and panning is quite smooth, it seems.
Edit: panning is much less smooth on openstreetmap. It's faster to bring in new features when zooming, but you have to endure blurry text and less details for half a second or so. Also, you can't rotate the view. IIRC google maps renders text locally to solve that.
This is exactly why I recorded a video and put one in that blog post - because sometimes people that are running even the same software will have vastly different experiences. At the end of the day "But it works/doesn't work on my machine." is just the reality that we need to deal with - which will vary for someone with a weaker or more powerful device, certain browser, certain drivers and so on.
> Panning is much less smooth on openstreetmap.
I'd say that from the best to worst for me it'd go a bit like this: Google Maps (application) > OpenStreetMap (web) > Google Maps (web) > MapLibre GL JS (web)
> It's faster to bring in new features when zooming, but you have to endure blurry text and less details for half a second or so.
Same here!
> Also, you can't rotate the view.
I can't rotate the view in Google Maps (web) either, only in the native app.
For me, the web maps also seem to use raster tiles, whereas the application seems to use vector data, added more info in another comment: https://news.ycombinator.com/item?id=33712150
Really if you aren't interacting with the data on the client I would stick with good old rasters - it's harder to get set up with them but they perform way better
It's less computationally expensive to just fetch a pre-rendered tile and drape it onto the map, which keeps rotating, panning an tilting very performant.
It doesn't look that nice, so there's the tradeoff. It can also be easier to just cache tiles on the end device and resort to them instead of constantly reassemble the vector layers.
bdon (author of PMTiles) already commented on this thread. I recommend taking a look at https://protomaps.com/docs - compared to this, raster tile servers sound like ancient technology.
Hiking trails and other more niche things, though. Compare the data quality there :)
I'll favorite this submission so I'll have something to reference if anyone balks at Google Maps ripping them off, and has a spare server around.
Everyone is also okay with driving combustion vehicles for the time being, even while realizing that it might not be the cleanest option. Supporting Google similarly feels dirty when necessary, but yes it's the norm.
For some things like traffic info, there also just isn't much of an alternative that works in most of the world with one simple thing to implement. There are things where OSM or TomTom or so just can't compete because it requires tracking half a billion people's locations in real time. Or showing up with your opening hours on Google's search monopoly by existing as a POI on TomTom or OSM will also just never happen, whereas existing as a POI on Google Maps is enough (using dominance in one market to influence another market). I know "everyone" is fine with using these products, but that's not why nerds get enthusiastic about seeing OSM in different places :)
Why? 20TB consumer storage is a thing. https://www.amazon.com/Western-Digital-20TB-Internal-Drive/d... ($420, heh)
What in the implementation makes the RAM usage explode?
It depends on the service, but we frequently find geo services are RAM limited up to a certain point (at which point there's enough cached to make it CPU limited).
I am wondering now if that's something that could be federated well.
note that this proprietary map data may be completely beating OSM or being vastly inferior, depending on your use case
here is the idea.
a ride sharing app uses OSM for routing and such. fine. The driver follows the map but some times there are new turns or changes to road that are not in the map, or there are some improvements.
my idea. the ride sharing app has the historical "ACTUAL" rides that have already taken place.
we build a tool that takes point A&B from the route given and plots its own normal route. the actual trip and "Expected" trip are overlaid and if there are deviations, the software (after some adjustments for errors) this can give us a high level idea of the general map to actual route. this can also tell mappers which routes to fix and stuff...
there is one tool that does manually but i would be like for large number of routes that would be matched 1:1.
https://wiki.openstreetmap.org/wiki/Organised_Editing/Activi...
The Valhalla pedestrian routing engine does include ferry routes, though it completely ignores ferry schedules. (Ferry use can be discouraged in the routing options... the interface for fine-tuning options for the different routing profiles is quite impressive. )
I am using something based on https://wiki.openstreetmap.org/wiki/ProxySimplePHP5 for a long time already and since I don't need to have up to date tiles every day, I set the TTL to a few weeks.
Together with leaflet this works beautifully.
os.makedirs(path, exist_ok=True)
Can you explain this in more detail? Slow? Is this slow for you too: https://graphhopper.com/maps/ ?
> I was just barely able to host tiles for and travelroute Sweden
Hosting tiles with graphhopper? Do you mean the vector tiles we serve only for debugging purposes?
> the travel time isochrones looked very jagged and not quite right
For public transit? The GTFS does not give much room to interpret this. Or do you mean road vehicles?
> Routing is the tricky part, especially public transit.
Exactly. And this is the reason your comment is tricky to interpret as the original post was about road routing and for isochrones you meant probably also road routing but in your case you included GTFS and then the initial setup is a lot more demanding. So not really comparing apples with apples ...
I used the docker image overv/openstreetmap-tile-server for the tiles.
The isochrones looked jagged for both public transit and road as far as i remember.
What’s tricky to interpret about my comment? I wanted to share that it’s possible to host google maps alternatives on a laptop with free software but it’s tricky to setup and not a smooth and fun experience, especially if you also want public transport routing, which is a hard problem.
Here is another excellent write-up from Stefan Erhardt who is the founder of OpenTopoMap [0][1] which gives step-by-step instructions [2] on how to set up a tile server with custom tile rendering, also based on OSM's data.
Also worth mentioning is GeoServer [3]
[1] https://github.com/der-stefan/OpenTopoMap
[2] https://github.com/der-stefan/OpenTopoMap/blob/master/mapnik...
Concerning hardware: I would not compare real hardware with cloud virtual servers. For a task like that I would rent a bare metal server. For example, Hetzner offers 6 cores AMD Ryzen 5 server with 128G RAM and 2T NVMe for only 90 EUR/month [1], but in Germany. I believe it is possible to find similar offer in US for comparable price.
[1] https://www.hetzner.com/dedicated-rootserver?ram_from=128&ra...
>Finally, to get external traffic from the internet connected to my computer (and specifically to Nginx), I had to set up port-forwarding on my router.
FYI: Most providers will not allow this and will disconnect your services for it.I have run a personal web server open to the Internet. But a good 15 years ago, I actually tried running a public website. It lasted for a few months, then they disconnected me until I called and agreed to turn it off.
I'm willing to bet your provider has something similar, enforcement may be lax, but it's not something you can count on.
Think I'll give this a go, always have wanted to run my own instance of OSM, to see how performant it is/how much traffic you can handle on a single server.
A watch having more than enough space for that is amazing in itself but really that's quite the compression for a seriously intense dataset.
Technically everyone could have that already pre-installed on their PC or phone these days, the problem of course that is a commercial dataset buut openstreetmap solves that part.
I wonder how much comoute and ram you need for the routing part.
When I worked on a maps stack (no routing and cloud) we would only provision a big compute instance for importing data and seeding (pre-caching) the new tiles. The server itself could be less powerful, though you might have to pay extra for more hard drive space.
https://github.com/Overv/openstreetmap-tile-server#enabling-...
I'm not sure about Nominatim or Valhalla's ability to auto-sync the latest changes, at least with the dockerized version, but it may be possible.
Including github repo.
I'm guessing that your router is available through the Internet directly, then? I wonder in how many places that's the case, because it isn't in my country (Latvia) and for my ISP (LMT, wireless connection in the countryside), because it seems like they're using CGNAT or something and any inbound traffic ended up getting dropped, at least the last time I tried: https://en.wikipedia.org/wiki/Carrier-grade_NAT
What I eventually did was rent a few cheap VPSes and set up WireGuard on them, so that my homelab servers could be accessed through the VPSes. I wrote a vague tutorial on my blog (though I should add the disclaimer that you probably don't want to forward almost all the ports if you're not lazy): https://blog.kronis.dev/tutorials/how-to-publicly-access-you...
Either way, this seems like a cool project!
> Even on this smaller extract, these services already use a large portion of my 1TB SSD (667 GB total). Assuming the usage scales proportionally, I would need around 3.7TB of storage for the entire planet. Not to mention the RAM requirements also scaling.
Though I can't help but to wonder why it wouldn't be possible to decrease the maximum level of detail that the map files contain, like the levels that you can zoom in or out. Even in the demo environment you get the lowest level of detail (land contours) for the whole world, even if you only have a part of it available in the higher fidelity.
> DigitalOcean Droplet (Memory Optimized): Total - $832.00 / mo
> AWS r6a.4xlarge EC2 Instance: Total - $735.42 / mo
> Azure D32as v5 VM Instance: Total - $1127.36 / mo
As for the cost aspect, someone suggested Hetzner, which does both cloud VPSes and dedicated ones: https://www.hetzner.com/
Example of Hetzner VPS with 32 vCPU cores, 128 GB of RAM (CCX51, dedicated vCPU): 353.31 Euros a month + storage costs (600 GB by default)
Example of Hetzner dedicated server with 16 CPU cores, 128 GB of RAM (from auction, not guaranteed): 46.05 Euros a month + storage costs (2 TB by default)
Then there's Contabo, which also has prices that are generally on the more affordable side: https://contabo.com/en/ Example of Contabo VDS with 12 CPU cores, 96 GB of RAM (Cloud VDS XXL): 149.99 Euros a month + storage costs (720 GB by default)
Example of Contabo dedicated server with 20 CPU cores, 256 GB of RAM (Intel Dual 10-Core): 149.44 Euros a month + storage costs (depends)
Apart from that, some people might also suggest that you look at https://lowendbox.com/ or similar sites for good deals, though typically the focus of such sites won't be on beefier specs.Depending on exactly how many resources you need, a smaller piece of hardware might be suitable. There can sometimes be regional providers, for example I use Time4VPS for most of my hosting: https://www.time4vps.com/?affid=5294 (affiliate link, feel free to remove affid; though they to have a sale going on now)
Apart from all that, I wonder why we don't have a "champion" project here that does pretty much everything in a single offering. Like for mail servers there's Mail-In-a-Box, for file storage there's Nextcloud (though some prefer Seafile). Such offerings are seldom ideal for all use cases, but are good for mass adoption.
Note that processing to reduce accuracy will require even more resources and what someone wants to preserve or drop depends on person.
And in the end - at least sometimes you will want to zoom in to the highest detail when using map data.
This is an excellent point, thanks!
In practice it probably depends on whether the party that's providing the map files for download (and prepares them) is okay with the bandwidth costs and/or the alternative costs of the downsampling (cutting out zoom levels).
But lately, I've also been instead using Tailscale, no need for VPS then, just having the client on each device that needs to have access to the machine (plus it can handle ssh now).
I realized I spend enough time on different projects on my home lab that the "open service to be accessible everywhere" part isn't worth my time or interest anymore. I'm glad for Tailscale and CloudFlare.
What problem do you have with this setup?
With my old setup: the fact that my public IP address wasn't "mine", it didn't lead to my router so I couldn't do port forwarding and expose anything publicly - instead it routed to my ISPs infrastructure and thus any inbound traffic that I wanted to reach my servers at home was dropped.
Thus I used WireGuard to make a tunnel between my local homelab server (outgoing connection) and a VPS that I rented, which could then forward any traffic it receives on port X to the same (or a different) port of my local server through the tunnel. Of course, the wording I use could use some work, networking isn't my forte.
I also use some dynamic DNS (ddclient is great) in places and Let's Encrypt for TLS certificate renewal, no complaints there.
Oh, my VPSes don't have data transfer costs as a dynamic component (Time4VPS, linked above). I just pay a fixed fee for a given amount of bandwidth and if I exceed it, then the speed is reduced for that VPS until the end of the month.
Here's the relevant bit from their FAQ:
> We reduce your VPS server’s port speed 10 times until the new month starts. No worries, we won’t charge any extra fees or suspend your services.
That said, they're definitely not the only platform that does something similar, many other VPS providers also have certain amount of data transfer included, Hetzner and Contabo included.
I actually have to say that Hetzner is perhaps the best billing wise, because if you just need a VPS for an hour or something, you can also order it for that amount of time, instead of a full month like with many other providers.
Either way, in my case I don't need to worry about the bandwidth too much, because nothing that I want to expose publicly is that popular, at least from my homelab nodes - mostly test environments and such to show to friends/colleagues and so on. Most of the stuff that can generate a bit more traffic (for example, my blog) I host in the data center that gives me all my other VPSes as well, just to not overwhelm my residential connection.