{"id":49,"date":"2018-03-19T12:10:01","date_gmt":"2018-03-19T03:10:01","guid":{"rendered":"http:\/\/blog.geofictician.net\/?p=49"},"modified":"2018-03-19T12:10:01","modified_gmt":"2018-03-19T03:10:01","slug":"a-more-technical-summary-of-how-i-built-my-tileserver-part-1","status":"publish","type":"post","link":"https:\/\/blog.geofictician.net\/index.php\/2018\/03\/19\/a-more-technical-summary-of-how-i-built-my-tileserver-part-1\/","title":{"rendered":"A more technical summary of how I built my tileserver &#8211; part 1"},"content":{"rendered":"<p>I thought I should put a discussion of how I did this, with much more detail, as I am sure there are other people out there in the world who might want to do something similar.<\/p>\n<p>This is part 1. I&#8217;ll post part 2 later.<\/p>\n<h3>Background<\/h3>\n<p>I wanted to be able to serve Openstreetmap-style map tiles of my own fictional planet, in the same way that the site OpenGeofiction does, but using my own data set.<\/p>\n<p>This process of building a tileserver is separate from the job of setting up an Openstreetmap-style apidb database to be able to edit the data set using tools such as iD, Potlatch, or JOSM. I&#8217;m still working on that.<\/p>\n<h3>Platform and Preliminaries<\/h3>\n<p>I deliberately set up my server on Ubuntu 16.04 (a flavor of Debian Linux) because I knew that OpenGeofiction runs in this environment. I&#8217;m not actually sure, but I assume Openstreetmap does too, though, given its scale, that may not be exactly the case, anymore &#8211; more likely it&#8217;s got a kind of customized, clustered Linux fork that has some genetic relationship to Ubuntu.<\/p>\n<p>I thought it would therefore be easier to replicate the OpenGeofiction application stack.<\/p>\n<p>Before starting this work, I had already installed MySQL and Apache and Mediawiki &#8211; except for Apache, however, these are not relevant to setting up a tileserver.<\/p>\n<p>I had also already set up PostgreSQL (the preferred Openstreetmap database server), so the preliminary mentions of setting up this application were skipped.<\/p>\n<p>Finally, using Apache&#8217;s <em>sites-available<\/em> config files and DNS, I had set up a subdomain on my server, tile.geofictician.net, to be the &#8220;outside address&#8221; for my tileserver. This will hopefully mean that if I ever decide to separate my tileserver from other things running on the same server, it will be somewhat easier to do.<\/p>\n<h3>S2OMBaTS with Deviations<\/h3>\n<p>Starting out, I mostly followed the steps and documentation at switch2osm.org&#8217;s detailed tutorial, <a href=\"https:\/\/switch2osm.org\/manually-building-a-tile-server-16-04-2-lts\/\">here<\/a>. Below, I refer to this page as S2OMBaTS (&#8220;switch2osm manually building a tile server&#8221;).<\/p>\n<p>So I don&#8217;t see any need to repeat everything it says there. I just followed the steps given on that webpage exactly and religiously. What I&#8217;ll document are only the spots where I had to do something differently. These are my &#8220;deviations.&#8221;<\/p>\n<ol>\n<li>Where S2OMBaTS suggests creating a &#8216;renderaccount&#8217; on the server to own all the tileserver-related directories and tools, I used my non-root regular username. I&#8217;m not sure this is good practice, and if I were setting something up as a &#8220;production&#8221; environment, I&#8217;d be more careful to segregate ownership of this collection of files, applications and services.<\/li>\n<li>There are some problems with authenticating a non-root user for PostgresSQL (&#8216;root&#8217; being the infamous &#8216;postgres&#8217; superuser). I had to edit the&nbsp;\/etc\/postgresql\/9.5\/main\/pg_hba.conf file so that the authentication method was &#8220;trust&#8221;[css]<br \/>\n# Database administrative login by Unix domain socket<br \/>\nlocal   all             postgres                                trust<\/p>\n<p># TYPE  DATABASE        USER            ADDRESS                 METHOD<\/p>\n<p># &#8220;local&#8221; is for Unix domain socket connections only<br \/>\nlocal   all             all                                     trust<br \/>\n[\/css]<\/p>\n<p>I think this might be a bad solution from a security standpoint, but it&#8217;s the only one I could find that I understood and could get to work. PostgreSQL security is weird, to me. My DBA experience was entirely with SQLServer and Oracle, back in the day, and those databases&#8217; security are integrated to OS security more tightly, I think. Similarly, MySQL seems to assume linkages between system users and database users, so security for the matched pairs of users are linked. But it seems like PostgreSQL doesn&#8217;t work that way.<\/li>\n<li>Where S2OMBaTS suggests using the URI=\/hot\/ in the&nbsp;\/usr\/local\/etc\/renderd.conf file (which seems intended to hijack other applications&#8217; support for the already-existing &#8220;HOT&#8221; &#8211; Humanitarian Openstreetmap Team &#8211; layer). I used URI=\/h\/ instead, which was entirely arbitrary and I could just as easily have used something more meaningful, as at OpenGeofiction, with e.g. URI=\/osmcarto\/.<\/li>\n<li>To test my installation, of course, I had to load some test data.&nbsp;S2OMBaTS uses a geofabrik snapshot of Azerbaijan. I decided just for the sake of familiarity, to use a snapshot of South Korea. I had to spend quite a bit of time researching and tweaking the individual osm2pgsql options (parameters) to get it to run on my itty-bitty server, even for a fairly small dataset like South Korea&#8217;s OSM snapshot, so&nbsp;here&#8217;s the osm2pgsql invokation I used to load the data (YMMV).\n<pre>osm2pgsql --database gis --create --slim  --multi-geometry --keep-coastlines --hstore --verbose --tag-transform-script ~\/src\/openstreetmap-carto\/openstreetmap-carto.lua --cache 2500 --cache-strategy optimized --number-processes 1 --style ~\/src\/openstreetmap-carto\/openstreetmap-carto.style ~\/data\/south-korea-latest.osm.pbf\n<\/pre>\n<\/li>\n<\/ol>\n<p>At this point, I reached the end of the&nbsp;S2OMBaTS tutorial.<\/p>\n<h3>Loading my own planet<\/h3>\n<p>I then had to customize things to load my own planet instead of a largely &#8220;naked earth&#8221; with South Korea well-mapped. The first step was easy enough. I just replaced the South Korea pbf extract with a pbf of my own planet, and re-ran the osm2pgsql step. I got the pbf extract of my planet by working with some kludges and with JOSM on my desktop machine. It was a &#8220;simplified&#8221; planet &#8211; just the continent outlines, a few cities, two countries with their admin_level=2 boundaries, and one tiny outlying island with lots of detail, which I borrowed from my city-state T\u00e1rrases at OpenGeofiction. It was composed as a kind of &#8220;test-planet&#8221; to keep things simple but hopefully test most of what I wanted to achieve in my tileserver.<\/p>\n<p>Here&#8217;s the load script for that (essentially the same as used for South Korea, above).<\/p>\n<pre>osm2pgsql --database gis --create --slim  --multi-geometry --keep-coastlines --hstore --verbose --tag-transform-script ~\/src\/openstreetmap-carto\/openstreetmap-carto.lua --cache 2500 --cache-strategy optimized --number-processes 1 --style ~\/src\/openstreetmap-carto\/openstreetmap-carto.style ~\/data\/gf-planet.osm.pbf\n<\/pre>\n<p>The problem, of course, is that if you run the render at this point, you get all the features of the new planet, but the continent outlines and the land-water distinction is &#8220;inherited&#8221; from earth. That&#8217;s because the mapnik style being used is referencing the shapefiles produced by and downloaded from Openstreetmap. The creators of the Openstreetmap software, including the OSM carto style, didn&#8217;t take into account the possibility that someone would try to use their software to show a map of somewhere that wasn&#8217;t planet Earth, and consequently, these need for these shapefiles is &#8220;hardcoded.&#8221; So the &#8220;Earth&#8221; shapefiles have to be substituted by alternate shapefiles extracted from the alternate planet dataset.<\/p>\n<h3>Customizing Coastlines and Shapefile Hell<\/h3>\n<p>This was the hardest part for me. It took me more than a week to figure it all out. I&#8217;m not experienced with shapefiles, and don&#8217;t really understand them, and the process by which shapefiles get extracted from the OSM global dataset in a format that can be used by the openstreetmap-carto mapnik style is very poorly documented, online. So it was a lot of google-fu and experimentation, and downloading QGIS and teaching myself a bit about shapefiles, before I could get things working. It&#8217;s not clear to me that I really did it the right way. All I can say is that it seems to work.<\/p>\n<p>The first steps I took were to try to simplify my task. I did this by chasing down the shapefile dependencies in the mapnik style sheet, and manually removing the ones that seemed less important. I did this mostly through trial and error.<\/p>\n<p>The only file that needs to be edited to accomplish this simplification is the main mapnik xml file:&nbsp;<strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/mapnik.xml<\/strong>. Bear in mind, though, that this file is the <em>output<\/em> of the carto engine (or whatever it&#8217;s called). By editing it, I have &#8220;broken&#8221; it &#8211; I won&#8217;t be able to upgrade my OSM carto style easily. But this is just a test run, right? I just wanted to get it to work.<\/p>\n<p>So I edited the&nbsp;<strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/mapnik.xml<\/strong> file and deleted some stuff. You have to be comfortable just going in and hacking around the giant xml file &#8211; just remember to only delete things at the same branch level of the tree structure so you don&#8217;t end up breaking the tree.<\/p>\n<p>I removed the&nbsp;&lt;Style&gt;&lt;\/Style&gt; sections that mentioned Antarctic icesheets &#8211; there were two. As things stand, my planet has no Antarctic icesheets, so why try to incorporate shapefiles supporting them?<\/p>\n<p>Then, I eliminated the&lt;Style&gt;&lt;\/Style&gt; section mentioning the <strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/data\/ne_110m_admin_0_boundary_lines_land directory<\/strong>, since these are land-boundaries for Earthly nations. I figured if I couldn&#8217;t see land-boundaries for my planet&#8217;s nations at low zooms, it would be no big deal. It&#8217;s not clear to me that this has been implemented on OpenGeofiction, either.<\/p>\n<p>I also discovered that in fact, this file doesn&#8217;t even point to the&nbsp;<strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/data\/world_boundaries<\/strong> directory. So there was no need to worry about that one.<\/p>\n<p>So that left me with two shapefiles I had to recreate for my own planet&#8217;s data:<br \/>\n<strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/data\/land-polygons-split-3857\/land_polygons.shp<\/strong> and&nbsp;<strong>&lt;YOUR-PATH&gt;\/openstreetmap-carto\/data\/simplified-land-polygons-complete-3857\/simplified_land_polygons.shp<\/strong>.<\/p>\n<p>Let&#8217;s just summarize by saying that this is what took so long. I had to figure out how to create shapefiles that the mapnik style would know what to do with, so that my continents would appear on the render. It took a lot of trial and error, but I&#8217;ll document what&#8217;s working for me, so far.<\/p>\n<p>*** To be continued ***<\/p>\n<p>[Update 20180923: Continues <a href=\"http:\/\/blog.geofictician.net\/?m=20180324\">here<\/a>]<\/p>\n<p>Music to map by:&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=faoTW6e_eNk\">H\u00e9ctor Acosta, &#8220;Tu Veneno.&#8221;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought I should put a discussion of how I did this, with much more detail, as I am sure there are other people out there in the world who might want to do something similar. This is part 1. I&#8217;ll post part 2 later. Background I wanted to be able to serve Openstreetmap-style map &hellip; <a href=\"https:\/\/blog.geofictician.net\/index.php\/2018\/03\/19\/a-more-technical-summary-of-how-i-built-my-tileserver-part-1\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A more technical summary of how I built my tileserver &#8211; part 1<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-hrate"],"_links":{"self":[{"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":0,"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.geofictician.net\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}