openstreetmap-tile-server-ubuntu-16-04
在Ubuntu16-04版本上搭建離線免費地圖,具體的步驟如下,也可以直接下載【我的資源】里面的pdf,下載地址為:
http://download.csdn.net/detail/emily201314/9645880
或者直接查看有道筆記分享鏈接:http://note.youdao.com/noteshare?id=0b23b59ad6615a60eb45ef67670626cd
來源地址:https://www.linuxbabe.com/linux-server/openstreetmap-tile-server-ubuntu-16-04
Step 1: Upgrade Software
sudo apt update sudo apt upgradeStep 2: Install?PostgreSQL Database Server with PostGIS
We will use PostgreSQL to store map data. PostGIS is a geospatial extenstion to PostgreSQL. Run the following commands to install them.
sudo apt install postgresql postgresql-contrib postgis postgresql-9.5-postgis-2.2 修改為(一個一個安裝): sudo apt install postgresqlsudo apt install postgresql-contribsudo apt install postgissudo apt install postgresql-9.5-postgis-2.2A user named?postgres?will be created during the installation process. This allows the use of peer authentication.?Let’s switch to the?postgres?user:
sudo -u postgres -iCreate a PostgreSQL database user?osm.
createuser osmCreate a database named?gis?and at the same time make?osm?as the owner of the database.?-E UTF8?specifies the character encoding scheme to be used in the database is UTF8.
createdb -E UTF8 -O osm gisCreate hstore and postgis extension.
psql -c "CREATE EXTENSION hstore;" -d gispsql -c "CREATE EXTENSION postgis;" -d gisExit from the?postgres?user.
exitCreate?osm?user on your operating system so the tile server can run as?osm?user.
sudo adduser osmStep 3: Download Map Stylesheet and Map Data
First switch to?osm?user
su - osmDownload the latest CartoCSS map stylesheets to the?osm?user’s home directory.
wget https://github.com/gravitystorm/openstreetmap-carto/archive/v2.41.0.tar.gzExtract it.
tar xvf v2.41.0.tar.gzNext, download map data to the osm user’s home directory. Use the below command to download the map data of the whole planet (32G).
wget -c http://planet.openstreetmap.org/pbf/planet-latest.osm.pbfIf you want a map of individual country or state, go to?http://download.geofabrik.de. Also,?BBBike.org?provides?extracts of more than 200 cities and regions world-wide in different formats.
For example, download the map data of Great Britain (847M).
wget -c http://download.geofabrik.de/europe/great-britain-latest.osm.pbf
可以下載中國地圖或者是北京地圖
wget?-c?http://download.bbbike.org/osm/bbbike/Beijing/Beijing.osm.pbf 或者是 wget?-c?http://download.geofabrik.de/asia/china-latest.osm.pbfNow exit from the?osm?user.
exitRecommendations before Importing Map Data
Importing map data takes a lot of RAM. If your physical memory is small, you can easily add a swap file. First we usefallocate?command to create a file. For example, create a file named swapfile with 2G capacity in root file system:
sudo fallocate -l 3G /swapfileThen make sure only root can read and write to it.
sudo chmod 600 /swapfileFormat it to swap:
sudo mkswap /swapfileOutput:
Setting up swapspace version 1, size = 2097148 KiB no label, UUID=h32b3e10-0779-4865-9ea0-6e2af8f3kea9Enable the swap file
sudo swapon /swapfileThe import process can take some time. It’s recommended to configure SSH keepalive so that you don’t lose the SSH connection. It’s very easy to do. Just open the SSH client configuration file on your local Linux machine.
sudo nano /etc/ssh/ssh_configAnd paste the following text at the end of the file.
ServerAliveInterval 60Then save the file and connect to your Ubuntu 16.04 server
Step 4: Import the Map Data to PostgreSQL
To import map data, we need to install?osm2pgsql?which converts OpenStreetMap data to postGIS-enabled PostgreSQL databases.
sudo apt install osm2pgsqlSwitch to?osm?user again.
su - osmRun the following command to load map stylesheet and map data into the?gis?Database. Replace?great-britain-latest.osm.pbf?with your own map data file.
osm2pgsql --slim -d gis -C 3600 --hstore -S openstreetmap-carto-2.41.0/openstreetmap-carto.style great-britain-latest.osm.pbf 修改成對應的osm.pbf名字 osm2pgsql --slim -d gis -C 3600 --hstore -S openstreetmap-carto-2.41.0/openstreetmap-carto.style china-latest.osm.pbf這個命令耗時特別久,不要慌,等著完成osm2gpsql?will run in slim mode which is recommended over the normal mode.?-d?stands for?--database.?-C?flag specify the cache size in MB. Bigger cache size results in faster import speed but you need to have enough RAM to use cache.?-S?flag specify the style file. And finally you need to specify the map data file.
Once the import is complete, exit from the?osm?user.
exitStep 5: Install mod_tile
mod_tile is an Apache module that is required to serve tiles. Currently no binary package is available for Ubuntu. We can compile it from Github repository.
First install build dependency.
sudo apt install git autoconf libtool libmapnik-dev apache2-dev 修改為:sudo apt install gitsudo apt install autoconfsudo apt install libtoolsudo apt install libmapnik-devsudo apt install apache2-devThen clone the repository from Github.
git clone https://github.com/openstreetmap/mod_tile.gitcd mod_tile/Compile and install
./autogen.sh ./configure make sudo make install sudo?make install-mod_tileStep 6: Generate Mapnik Stylesheet
Install required packages.
sudo apt install curl unzip gdal-bin mapnik-utils node-carto 修改為: sudo apt install curlsudo apt install unzipsudo apt install gdal-binsudo apt install mapnik-utilssudo apt install node-cartoSwitch to osm user.
su - osmCd into the carto style directory.
cd openstreetmap-carto-2.41.0/Get shapefiles.
./get-shapefiles.sh(最終效果) ./get-shapefiles.sh命令的內容如下: #!/bin/sh set?-e?-u
UNZIP_OPTS=-qqun
#?create?and?populate?data?dir mkdir?-p?data/ mkdir?-p?data/world_boundaries mkdir?-p?data/simplified-land-polygons-complete-3857 mkdir?-p?data/ne_110m_admin_0_boundary_lines_land mkdir?-p?data/land-polygons-split-3857
#?world_boundaries echo?"downloading?world_boundaries..." curl?-z?"data/world_boundaries-spherical.tgz"?-L?-o?"data/world_boundaries-spherical.tgz"?"http://planet.openstreetmap.org/historical-shapefiles/world_boundaries-spherical.tgz" echo?"expanding?world_boundaries..." tar?-xzf?data/world_boundaries-spherical.tgz?-C?data/
#?simplified-land-polygons-complete-3857 echo?"downloading?simplified-land-polygons-complete-3857..." curl?-z?"data/simplified-land-polygons-complete-3857.zip"?-L?-o?"data/simplified-land-polygons-complete-3857.zip"?"http://data.openstreetmapdata.com/simplified-land-polygons-complete-3857.zip?
" echo?"simplified-land-polygons-complete-3857..." unzip?$UNZIP_OPTS?data/simplified-land-polygons-complete-3857.zip?\ ??simplified-land-polygons-complete-3857/simplified_land_polygons.shp?\ ??simplified-land-polygons-complete-3857/simplified_land_polygons.shx?\ ??simplified-land-polygons-complete-3857/simplified_land_polygons.prj?\ ??simplified-land-polygons-complete-3857/simplified_land_polygons.dbf?\ ??simplified-land-polygons-complete-3857/simplified_land_polygons.cpg?\ ??-d?data/
#?ne_110m_admin_0_boundary_lines_land echo?"downloading?ne_110m_admin_0_boundary_lines_land..." curl?-z?data/ne_110m_admin_0_boundary_lines_land.zip?-L?-o?data/ne_110m_admin_0_boundary_lines_land.zip?http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_boundary_lines_land.zip?
echo?"expanding?ne_110m_admin_0_boundary_lines_land..." unzip?$UNZIP_OPTS?data/ne_110m_admin_0_boundary_lines_land.zip?\ ??ne_110m_admin_0_boundary_lines_land.shp?\ ??ne_110m_admin_0_boundary_lines_land.shx?\ ??ne_110m_admin_0_boundary_lines_land.prj?\ ??ne_110m_admin_0_boundary_lines_land.dbf?\ ??-d?data/ne_110m_admin_0_boundary_lines_land/
#?land-polygons-split-3857 echo?"downloading?land-polygons-split-3857..." curl?-z?"data/land-polygons-split-3857.zip"?-L?-o?"data/land-polygons-split-3857.zip"?"http://data.openstreetmapdata.com/land-polygons-split-3857.zip?
" echo?"expanding?land-polygons-split-3857..." unzip?$UNZIP_OPTS?data/land-polygons-split-3857.zip?\ ??land-polygons-split-3857/land_polygons.shp?\ ??land-polygons-split-3857/land_polygons.shx?\ ??land-polygons-split-3857/land_polygons.prj?\ ??land-polygons-split-3857/land_polygons.dbf?\ ??land-polygons-split-3857/land_polygons.cpg?\ ??-d?data/
#?antarctica-icesheet-polygons-3857 echo?"downloading?antarctica-icesheet-polygons-3857..." curl?-z?"data/antarctica-icesheet-polygons-3857.zip"?-L?-o?"data/antarctica-icesheet-polygons-3857.zip"?"http://data.openstreetmapdata.com/antarctica-icesheet-polygons-3857.zip?
" echo?"expanding?antarctica-icesheet-polygons-3857..." unzip?$UNZIP_OPTS?data/antarctica-icesheet-polygons-3857.zip?\ ??antarctica-icesheet-polygons-3857/icesheet_polygons.shp?\ ??antarctica-icesheet-polygons-3857/icesheet_polygons.shx?\ ??antarctica-icesheet-polygons-3857/icesheet_polygons.prj?\ ??antarctica-icesheet-polygons-3857/icesheet_polygons.dbf?\ ??-d?data/
#?antarctica-icesheet-outlines-3857 echo?"downloading?antarctica-icesheet-outlines-3857..." curl?-z?"data/antarctica-icesheet-outlines-3857.zip"?-L?-o?"data/antarctica-icesheet-outlines-3857.zip"?"http://data.openstreetmapdata.com/antarctica-icesheet-outlines-3857.zip?
" echo?"expanding?antarctica-icesheet-outlines-3857..." unzip?$UNZIP_OPTS?data/antarctica-icesheet-outlines-3857.zip?\ ??antarctica-icesheet-outlines-3857/icesheet_outlines.shp?\ ??antarctica-icesheet-outlines-3857/icesheet_outlines.shx?\ ??antarctica-icesheet-outlines-3857/icesheet_outlines.prj?\ ??antarctica-icesheet-outlines-3857/icesheet_outlines.dbf?\ ??-d?data/
#index echo?"indexing?shapefiles" shapeindex?--shape_files?\ data/simplified-land-polygons-complete-3857/simplified_land_polygons.shp?\ data/land-polygons-split-3857/land_polygons.shp?\ data/antarctica-icesheet-polygons-3857/icesheet_polygons.shp?\ data/antarctica-icesheet-outlines-3857/icesheet_outlines.shp?\ data/ne_110m_admin_0_boundary_lines_land/ne_110m_admin_0_boundary_lines_land.shp
#finish echo?"...done!"
Now build the Mapnik xml stylesheet.
carto project.mml > style.xml
sudo?apt-get?-y?install?fonts-wqy-zenhei
sudo apt-get install unifont sudo apt-get install ttf-unifont 下載字體保存在/usr/share/fonts/truetype路徑下
替換會出現中文亂碼不顯示問題,需要替換生成的樣式文件
Exit from the?osm?user.
exitStep 7: Configuring renderd
Edit renderd config file.
sudo nano?/usr/local/etc/renderd.confIn the?[default]?section, change the value of XML and HOST to the following.
XML=/home/osm/openstreetmap-carto-2.41.0/style.xml HOST=localhostIn [mapnik] section, change the value of plugins_dir.
plugins_dir=/usr/lib/mapnik/3.0/input/Save the file.
Install renderd init script by copying the sample init script.
cd?~sudo cp mod_tile/debian/renderd.init /etc/init.d/renderdGrant execute permission.
sudo?chmod a+x /etc/init.d/renderdEdit the init script file
sudo nano /etc/init.d/renderdChange the following variable.
DAEMON=/usr/local/bin/$NAME DAEMON_ARGS="-c /usr/local/etc/renderd.conf" RUNASUSER=osmSave the file.
Create the following file and set osm the owner.
sudo mkdir -p /var/lib/mod_tilesudo?chown osm:osm /var/lib/mod_tileThen start renderd service
sudo systemctl daemon-reloadsudo systemctl start renderdsudo systemctl enable renderdStep 8: Configure Apache
Install apache web server
sudo apt install apache2Create a module load file.
sudo nano /etc/apache2/mods-available/mod_tile.loadPaste the following line into the file.
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.soCreate a symlink.
sudo ln -s /etc/apache2/mods-available/mod_tile.load /etc/apache2/mods-enabled/Then edit the default virtual host file.
sudo nano /etc/apache2/sites-enabled/000-default.confPast the following line in <VirtualHost *:80>
LoadTileConfigFile /usr/local/etc/renderd.conf ModTileRenderdSocketName /var/run/renderd/renderd.sock # Timeout before giving up for a tile to be rendered ModTileRequestTimeout 0 # Timeout before giving up for a tile to be rendered that is otherwise missing ModTileMissingRequestTimeout 30Save and close the file. Restart Apache.
sudo systemctl restart apache2Then in your web browser address bar, type
your-server-ip/osm_tiles/0/0/0.pngYou should see the tile of world map. Congrats! You just successfully built your own OSM tile server.
Display Your Tiled Web Map
Tiled web map is also known as?slippy map?in OpenStreetMap terminology.?There are two free and open source JavaScript map libraries you can use for your tile server:?OpenLayer?and?Leaflet. The advantage of Leaflet is that it is simple to use and your map will be mobile-friendly.
OpenLayer
在Openlayer中顯示 cd?/var/www/html wget?https://github.com/openlayers/ol3/releases/download/v3.18.2/v3.18.2-dist.zip unzip?'v3.18.2.zip' 保證ol.css、ol.js下載保存到/var/www/html目錄下 cp?/var/www/html/v3.18.2-dist/ol.css?/var/www/html/ol.css cp?/var/www/html/v3.18.2-dist/ol.js?/var/www/html/ol.jssudo?nano?/var/www/html/info.html 添加 <!DOCTYPE?html> <html> <head> <title>Accessible?Map</title> <link?rel="stylesheet"?href="ol.css"?type="text/css"> <script?src="ol.js"></script> <style> ??a.skiplink?{ ????position:?absolute; ????clip:?rect(1px,?1px,?1px,?1px); ????padding:?0; ????border:?0; ????height:?1px; ????width:?1px; ????overflow:?hidden; ??} ??a.skiplink:focus?{ ????clip:?auto; ????height:?auto; ????width:?auto; ????background-color:?#fff; ????padding:?0.3em; ??} ??#map:focus?{ ????outline:?#4A74A8?solid?0.15em; ??} </style> </head> <body> ??<a?class="skiplink"?href="#map">Go?to?map</a> ??<div?id="map"?class="map"?tabindex="0"></div> ??<button?id="zoom-out">Zoom?out</button> ??<button?id="zoom-in">Zoom?in</button> ??<script> ????var?map?=?new?ol.Map({ ??????layers:?[ ????????new?ol.layer.Tile({ ??????????source:?new?ol.source.OSM({ ?????????????url:?'http://192.168.175.100/osm_tiles/{z}/{x}/{y}.png' ??????????}) ???????}) ?????], ?????target:?'map', ?????controls:?ol.control.defaults({ ????????attributionOptions:?/**?@type?{olx.control.AttributionOptions}?*/?({ ??????????collapsible:?false ????????}) ?????}), ????view:?new?ol.View({ ???????center:?[12973503.936793,4833266.172531], ???????zoom:5 ????}) ?});
??document.getElementById('zoom-out').onclick?=?function()?{ ????var?view?=?map.getView(); ????var?zoom?=?view.getZoom(); ????view.setZoom(zoom?-?1); ??};
??document.getElementById('zoom-in').onclick?=?function()?{ ?????var?view?=?map.getView(); ?????var?zoom?=?view.getZoom(); ?????view.setZoom(zoom?+?1); ??}; </script> </body> </html>
To display your slippy map with OpenLayer, first create a web folder.
sudo mkdir /var/www/osmThen download JavaScript and CSS from?openlayer.org?and extract it to the web root folder.
Next, create the?index.html?file.
sudo nano /var/www/osm/index.htmlPaste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
<!DOCTYPE html> <html> <head> <title>Accessible Map</title> <link rel="stylesheet" href="http://your-ip/ol.css" type="text/css"> <script src="http://your-ip/ol.js"></script> <style>a.skiplink {position: absolute;clip: rect(1px, 1px, 1px, 1px);padding: 0;border: 0;height: 1px;width: 1px;overflow: hidden;}a.skiplink:focus {clip: auto;height: auto;width: auto;background-color: #fff;padding: 0.3em;}#map:focus {outline: #4A74A8 solid 0.15em;} </style> </head> <body><a class="skiplink" href="#map">Go to map</a><div id="map" class="map" tabindex="0"></div><button id="zoom-out">Zoom out</button><button id="zoom-in">Zoom in</button><script>var map = new ol.Map({layers: [new ol.layer.Tile({source: new ol.source.OSM({url: 'http://your-ip/osm_tiles/{z}/{x}/{y}.png'})})],target: 'map',controls: ol.control.defaults({attributionOptions: /** @type {olx.control.AttributionOptions} */ ({collapsible: false})}),view: new ol.View({ center: [244780.24508882355, 7386452.183179816], zoom:5})});document.getElementById('zoom-out').onclick = function() {var view = map.getView();var zoom = view.getZoom();view.setZoom(zoom - 1);};document.getElementById('zoom-in').onclick = function() {var view = map.getView();var zoom = view.getZoom();view.setZoom(zoom + 1);}; </script> </body> </html>Save and close the file. Now you can view your slippy map by typing your server IP address in browser.
your-ip/index.html ? ? ? ? ? or ? ? ? ? ?your-ipLeaflet
To display your slippy map with Leftlet, first create a web folder.
sudo mkdir /var/www/osmThen download JavaScript and CSS from?leftletjs.com?and extract it to the web root folder.
cd?/var/www/html wget?http://cdn.leafletjs.com/leaflet/v1.0.0/leaflet.zip unzip?'leaflet.zip' sudo?nano?/var/www/html/show.html 添加 <html> <head> <title>My?first?osm</title> <link?rel="stylesheet"?type="text/css"?href="leaflet.css"/> <script?type="text/javascript"?src="leaflet.js"></script> <style> ???#map{width:100%;height:100%} </style> </head><body> ??<div?id="map"></div> ??<script> ????var?map?=?L.map('map').setView([12973503.936793,4833266.172531],5); ????L.tileLayer('http://192.168.175.100/osm_tiles/{z}/{x}/{y}.png',{maxZoom:18}).addTo(map); </script> </body> </html>
Next, create the?index.html?file.
sudo nano /var/www/osm/index.htmlPaste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
<html> <head> <title>My first osm</title> <link rel="stylesheet" type="text/css" href="leaflet.css"/> <script type="text/javascript" src="leaflet.js"></script> <style>#map{width:100%;height:100%} </style> </head><body><div id="map"></div><script>var map = L.map('map').setView([53.555,9.899],5);L.tileLayer('http://your-ip/osm_tiles/{z}/{x}/{y}.png',{maxZoom:18}).addTo(map); </script> </body> </html>Save and close the file. Now you can view your slippy map by typing your server IP address in browser.
your-ip/index.html ? ? ? ? ? or ? ? ? ? ?your-ipTo pre-render tiles instead of rendering on the fly, use?render_list?command. Pre-rendered tiles will be cached in/var/lib/mod_tile?directory.?-z?and?-Z?flag specify the zoom level.
render_list -m default -a -z 0 -Z 10This tutorial is made available with the help from?Miles B. Dyson.
問題一:若是顯示China地圖,地標在瀏覽器會出現亂碼問題 1.查看錯誤 su?osm?-c?"renderd?-f?-c?/usr/local/etc/renderd.conf" 里面會提示有哪些字體找不到
2.下載以下字體: sudo?apt-get?-y?install?fonts-wqy-zenhei
sudo apt-get install unifont sudo apt-get install ttf-unifont 下載字體保存在/usr/share/fonts/truetype路徑下
3.修改/home/osm/openstreetmap-carto-2.41.0/style.xml樣式表,將錯誤中提示找不到的字體樣式全部改成可以找到的字體樣式 4.刪除緩存 sudo?rm?-rf?/var/lib/mod_tile/default/*
5.打開瀏覽器重新訪問地圖
緩存tiles到/var/lib/mod_tile目錄下,方便以后加載 render_list?-m?default?-a?-z?0?-Z?18
總結
以上是生活随笔為你收集整理的openstreetmap-tile-server-ubuntu-16-04的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)Native Extensions
- 下一篇: 基于QT实现的图元拖曳、定点滚轮旋转和缩