服务器器基于Ubuntu14.04,以下命令仅保证能运行在ubuntu系列中。
1.安装系统依赖sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install postgresql postgresql-9.3-postgis-2.1 postgresql-contrib-9.3 postgresql-server-dev-9.3 libpq-dev pgadmin3 subversion git-core tar unzip wget bzip2 build-essential autoconf libtool libxml2-dev libgeos-dev libbz2-dev libprotobuf-c0-dev protobuf-c-compiler libfreetype6-dev libpng12-dev libtiff4-dev libicu-dev libboost-all-dev libgdal-dev libcairo-dev libcairomm-1.0-dev apache2 apache2-dev libagg-dev libgeos++-dev libproj-dev lua5.2 liblua5.2-dev libltdl-dev libltdl7 libpng12-dev libtiff5 libtiff5-dev libtiffxx5 libicu-dev python-cairo-dev python-nose libfreetype6-dev libfreetype6 libcairo2-dev libcairomm-1.0-dev libgeotiff-dev libsigc++-2.0-0c2a libsigc++-2.0-dev libsigx-2.0-2 libsigx-2.0-dev libgdal1-dev python-gdal imagemagick ttf-dejavu ttf-unifont libharfbuzz-dev libharfbuzz0b apache2 libfcgi-dev apache2-threaded-dev libagg-dev apache2-utils libxml2 libxml2-dev python-lxml osm2pgsql python-mapnik2 libmapnik-dev libmapnik2-dev openssh-server vim
2.下载源码
2.1 mapnik-style
svn co http://svn.openstreetmap.org/applications/rendering/mapnik mapnik-style
2.2 mod_tile
git clone https://github.com/openstreetmap/mod_tile/
2.3OpenStreetMap.js
wget http://www.openstreetmap.org/openlayers/OpenStreetMap.js
2.4OpenLayers
wget http://github.com/openlayers/openlayers/releases/download/release-2.13.1/OpenLayers-2.13.1.tar.gz
2.5 world_boundaries
以下五个文件从mapnik-style中get-coastlines.sh文件获取下载地址,可能有变化
wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz
wget http://tile.openstreetmap.org/processed_p.tar.bz2
wget http://tile.openstreetmap.org/shoreline_300.tar.bz2
wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_populated_places.zip
wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_boundary_lines_land.zip
3.设置用户postgres与www-data 系统密码(安装postgresql自动生成用户)
设置密码
sudo passwd postgres
sudo passwd www-data
3.2提升www-data权限
sudo vim /etc/passwd
改为/bin/bash
4.数据库配置
4.1创建数据库存储目录
cd /
sudo mkdir hugecargo
cd hugecargo
sudo mkdir pgtbs_osmgis
sudo chown postgres pgtbs_osmgis
4.2配置数据库密码和指定数据库空间
sudo su postgres
psql -d postgres
ALTER USER postgres WITH PASSWORD '123456';
CREATE ROLE "www-data" LOGIN PASSWORD '123456' SUPERUSER INHERIT CREATEDB NOCREATEROLE REPLICATION;
CREATE TABLESPACE pgtbs_osmgis OWNER "www-data" LOCATION '/hugecargo/pgtbs_osmgis';
4.3创建数据库
CREATE DATABASE osmgis WITH OWNER = "www-data" ENCODING = 'UTF8' TABLESPACE = pgtbs_osmgis CONNECTION LIMIT = -1;
GRANT ALL ON DATABASE osmgis TO public;
GRANT ALL ON DATABASE osmgis TO "www-data";
\q
exit
4.4添加POSTGIS数据库表
sudo su postgres -c "psql -dosmgis </usr/share/postgresql/9.3/contrib/postgis-2.1/postgis.sql"
sudo su postgres -c "psql -dosmgis </usr/share/postgresql/9.3/contrib/postgis-2.1/spatial_ref_sys.sql"
sudo su postgres -c "psql -dosmgis </usr/share/postgresql/9.3/contrib/postgis-2.1/postgis_comments.sql"
sudo su postgres
psql -dosmgis
create extension hstore;
\q
exit
4.5导入地图数据
下载地图数据
http://download.geofabrik.de/
导入地图数据,这里使用中国地图
osm2pgsql -dosmgis -s -C384 -Uwww-data -W -Hlocalhost -v china-latest.osm.pbf
4.6 加入地图
解压world_boundaries下载步骤中的五个文件,取出所有的文件,并放到world_boundaries中
sudo mv world_boundaries /usr/local/share/
5.配置mapnik-style
5.1生成osm.xml
cd /mapnik-style
./generate_xml.py --world_boundaries=/usr/local/share/world_boundaries --host=127.0.0.1 --port=5432 --user=www-data --password=123456 --dbname=osmgis
5.2测试渲染
./generate_image.py
保存后,直接执行,如果没错,就出现了 image.png
6.安装apache2和fcgi
访问 http://127.0.0.1
7.mod_tile
cd mod_tile
./autogen.sh
./configure
make
sudo make install
sudo make install-mod_tile
sudo ldconfig
8.配置瓦片渲染后台服务renderd
8.1创建文件夹
sudo mkdir /var/run/renderd
sudo chown www-data /var/run/renderd
8.2配置renderd
sudo vim /usr/local/etc/renderd.conf
做一下配置:
[renderd]
;把socketname设置到刚才创建的文件夹里
socketname=/var/run/renderd/renderd.sock
num_threads=4
tile_dir=/var/lib/mod_tile ; DOES NOT WORK YET
stats_file=/var/run/renderd/renderd.stats
[mapnik]
plugins_dir=/usr/lib/mapnik/2.2/input
font_dir=/usr/share/fonts/truetype/unifont
font_dir_recurse=1
[default]
URI=/osm_tiles/
XML=/home/qlaiaqu/Downloads/mapnik-style/osm.xml
HOST=localhost
;HTCPHOST=proxy.openstreetmap.org
8.3配置apache2 mod_tile
sudo mkdir /var/lib/mod_tile
sudo chown www-data /var/lib/mod_tile
(1)导入mod_tile
sudo vim /etc/apache2/mods-available/mod_tile.load
里面包含一行
LoadModule tile_module /usr/lib/apache2/modules/mod_tile.so
生效
cd /etc/apache2/mods-enabled
sudo ln -s /etc/apache2/mods-available/mod_tile.load
(2)而后,设置用户访问网页的入口点
sudo vim /etc/apache2/sites-available/000-default.conf
加入
LoadTileConfigFile /usr/local/etc/renderd.conf
ModTileRenderdSocketName /var/run/renderd/renderd.sock
ModTileRequestTimeout 0
ModTileMissingRequestTimeout 30
8.4测试
cd mod_tile
sudo su www-data
renderd -f -c /usr/local/etc/renderd.conf
8.5测试
http://localhost/osm_tiles/0/0/0.png
8.6设置时间戳
sudo touch /var/lib/mod_tile/planet-import-complete
sudo chown www-data /var/lib/mod_tile/planet-import-complete
9.OpenLayer
tar xzvf OpenLayers-2.13.1.tar.gz
mv OpenLayers-2.13.1 openlayers
mv OpenStreetMap.js openlayers
sudo mv openlayes /var/www/html/openlayers
sudo chmod +r -R /var/www/html/openlayers
10.html
sudo vim /var/www/html/map.html
<html>
<head>
<title>OSM Local Tiles</title>
<link rel="stylesheet" href="/openlayers/theme/default/style.css" type="text/css" />
<!-- bring in the OpenLayers javascript library
(here we bring it from the remote site, but you could
easily serve up this javascript yourself) -->
<script src="/openlayers/OpenLayers.js"></script>
<!-- bring in the OpenStreetMap OpenLayers layers.
Using this hosted file will make sure we are kept up
to date with any necessary changes -->
<script src="/openlayers/OpenStreetMap.js"></script>
<script type="text/javascript">
// Start position for the map (hardcoded here for simplicity)
var lat=31.27386;
var lon=121.48132;
var zoom=7;
var map; //complex object of type OpenLayers.Map
//Initialise the 'map' object
function init() {
map = new OpenLayers.Map ("map", {
controls:[
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
//new OpenLayers.Control.PanZoom(),
new OpenLayers.Control.Permalink(),
new OpenLayers.Control.ScaleLine({geodesic: true}),
new OpenLayers.Control.Permalink('permalink'),
new OpenLayers.Control.KeyboardDefaults(),
new OpenLayers.Control.NavToolbar(),
new OpenLayers.Control.Attribution()],
maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
maxResolution: 156543.0339,
numZoomLevels: 19,
units: 'm',
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
} );
// This is the layer that uses the locally stored tiles
var newLayer = new OpenLayers.Layer.OSM("Local Tiles", "/osm_tiles/${z}/${x}/${y}.png", {numZoomLevels: 19, transitionEffect: "resize"});
map.addLayer(newLayer);
var switcherControl = new OpenLayers.Control.LayerSwitcher();
map.addControl(switcherControl);
var vector_layer = new OpenLayers.Layer.Vector('Editable Vectors');
map.addControl(new OpenLayers.Control.EditingToolbar(vector_layer));
map.addLayer(vector_layer);
map.addControl(new OpenLayers.Control.Graticule({visible: false}));
var mousepos = new OpenLayers.Control.MousePosition({div: document.getElementById('mousepos_div')});
map.addControl(mousepos);
//mousepos.moveTo(new OpenLayers.Pixel(64,0));
map.addControl(new OpenLayers.Control.OverviewMap());
/*
var navigationT = new OpenLayers.Control.TouchNavigation({
dragPanOptions:{
enableKinetic: true
}
});
map.addControl(navigationT);
*/
if( ! map.getCenter() ){
var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
map.setCenter (lonLat, zoom);
}
}
</script>
</head>
<!-- body.onload is called once the page is loaded (call the 'init' function) -->
<body onload="init();">
<!-- define a DIV into which the map will appear. Make it take up the whole window -->
<div style="width: 100%; height: 93%" id="map">
</div>
<P><a href='/index.html'>start screen</a></P>
<div style="width: 100%; height: 5%" id="mousepos_div">
</div>
</body>
</html>
11.访问测试
http://localhost/map.html