在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
sudo apt update sudo apt upgrade
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 postgresql
sudo apt install postgresql-contrib
sudo apt install postgis
sudo apt install postgresql-9.5-postgis-2.2
A 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 -i
Create a PostgreSQL database user osm
.
createuser osm
Create 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 gis
Create hstore and postgis extension.
psql -c "CREATE EXTENSION hstore;" -d gis psql -c "CREATE EXTENSION postgis;" -d gis
Exit from the postgres
user.
exit
Create osm
user on your operating system so the tile server can run as osm
user.
sudo adduser osm
First switch to osm
user
su - osm
Download the latest CartoCSS map stylesheets to the osm
user’s home directory.
wget https://github.com/gravitystorm/openstreetmap-carto/archive/v2.41.0.tar.gz
Extract it.
tar xvf v2.41.0.tar.gz
Next, 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.pbf
If 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
可以下载中国地图或者是北京地图
Now exit from the osm
user.
exit
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 /swapfile
Then make sure only root can read and write to it.
sudo chmod 600 /swapfile
Format it to swap:
sudo mkswap /swapfile
Output:
Setting up swapspace version 1, size = 2097148 KiB no label, UUID=h32b3e10-0779-4865-9ea0-6e2af8f3kea9
Enable the swap file
sudo swapon /swapfile
The 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_config
And paste the following text at the end of the file.
ServerAliveInterval 60
Then save the file and connect to your Ubuntu 16.04 server
To import map data, we need to install osm2pgsql
which converts OpenStreetMap data to postGIS-enabled PostgreSQL databases.
sudo apt install osm2pgsql
Switch to osm
user again.
su - osm
Run 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.
exit
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 git
sudo apt install autoconf
sudo apt install libtool
sudo apt install libmapnik-dev
sudo apt install apache2-dev
Then clone the repository from Github.
git clone https://github.com/openstreetmap/mod_tile.git cd mod_tile/
Compile and install
./autogen.sh ./configure make sudo make install sudo make install-mod_tile
Install required packages.
sudo apt install curl unzip gdal-bin mapnik-utils node-carto
修改为:
sudo apt install curl
sudo apt install unzip
sudo apt install gdal-bin
sudo apt install mapnik-utils
sudo apt install node-carto
Switch to osm user.
su - osm
Cd into the carto style directory.
cd openstreetmap-carto-2.41.0/
Get shapefiles.
./get-shapefiles.sh
(最终效果)
./get-shapefiles.sh命令的内容如下:
Now build the Mapnik xml stylesheet.
carto project.mml > style.xml
替换会出现中文乱码不显示问题,需要替换生成的样式文件
Exit from the osm
user.
exit
Edit renderd config file.
sudo nano /usr/local/etc/renderd.conf
In the [default]
section, change the value of XML and HOST to the following.
XML=/home/osm/openstreetmap-carto-2.41.0/style.xml HOST=localhost
In [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.
Grant execute permission.
sudo chmod a+x /etc/init.d/renderd
Edit the init script file
sudo nano /etc/init.d/renderd
Change the following variable.
DAEMON=/usr/local/bin/$NAME DAEMON_ARGS="-c /usr/local/etc/renderd.conf" RUNASUSER=osm
Save the file.
Create the following file and set osm the owner.
sudo mkdir -p /var/lib/mod_tile sudo chown osm:osm /var/lib/mod_tile
Then start renderd service
sudo systemctl daemon-reload sudo systemctl start renderd sudo systemctl enable renderd
Install apache web server
sudo apt install apache2
Create a module load file.
sudo nano /etc/apache2/mods-available/mod_tile.load
Paste the following line into the file.
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so
Create 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.conf
Past the following line in
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 30
Save and close the file. Restart Apache.
sudo systemctl restart apache2
Then in your web browser address bar, type
your-server-ip/osm_tiles/0/0/0.png
You should see the tile of world map. Congrats! You just successfully built your own OSM tile server.
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.
To display your slippy map with OpenLayer, first create a web folder.
sudo mkdir /var/www/osm
Then 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.html
Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
Accessible Map http://your-ip/ol.css" type="text/css"> Go to map
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-ip
To display your slippy map with Leftlet, first create a web folder.
sudo mkdir /var/www/osm
Then download JavaScript and CSS from leftletjs.com and extract it to the web root folder.
Next, create the index.html
file.
sudo nano /var/www/osm/index.html
Paste the following HTML code in the file. Replace red-colored text and adjust the longitude, latitude and zoom level according to your needs.
My first osm
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-ip
To 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 10
This tutorial is made available with the help from Miles B. Dyson.