您的位置:首页 > 运维架构

使用Phyghtmap为OpenStreetMap添加 DEM 高程图层

2017-03-09 22:11 483 查看
玩OSM很久了,今天准备为自己的ArchOSM服务器添加NASA的SRTM图层。我们这就来弄。数字高程数据(DEM)一般来自大地遥感卫星,NASA官网有免费的开源数字高程数据可以利用。DEM是对地表栅格化后采样测量的海拔值,转换为地图,还要经过几步。

- 第一步,获取DEM数据并转化为OSM使用的xml或PBF格式文件

- 第二步,为osm2pgsql指定style与数据库,导入PBF文件

- 第三步,嵌入DEM图层到Mapnik的样式表

- 第四步, 重新渲染瓦片

经过下面四步,我们可以得到类似这样的显示效果



下面我们开始做喽!

1. 从DEM到PBF

1.1 到 NASA网站注册一个账号

NASA的数字高程网站在https://urs.earthdata.nasa.gov/home/, 要首先注册一个账号,才能下载数字高程影像。当然,需要填写用户、口令。

1.2 安装 phyghtmap

Phyghtmap 是一款傻瓜化的 DEM到PBF转换软件,傻瓜到给定经纬度范围,就能产生PBF。这里,我们使用万能的ArchLinux yaourt 进行傻瓜安装(越老越爱ArchLinux了)。

yaourt -S phyghtmap


一顿默认yes后,就搞定了。

1.3 下载并转换数据

而后,测试一下下载效果,以上海周边为例子(经度119~133,纬度30~32)。

如果是首次运行,指定1.1里申请的用户、口令(XXXXXXX)。后面就不用了,会在当前文件夹产生文件

.phyghtmaprc

里面保存了口令。

phyghtmap -a 119:30:133:32 --earthdata-user xxxxxx --earthdata-password "xxxxxx"    --source=view3 --pbf --max-nodes-per-tile 0


上面的参数:

–source=view3是说采用较新整合的全球dem数据源,否则很多非北美的数据木有哦!

–max-nodes-per-tile 0 是要求他捣鼓一个单一的PBF文件出来,而不是一堆。

具体很多参数的含义可以参考官方网站。执行命令下载、计算后,一个PBF文件就生成啦!这里是名字:

shanghai_lon119.00_133.00lat30.00_32.00_view3.osm.pbf

2.导入DEM到PostgreSQL

这里我们用生成的PBF,配合osm2pgsql,导入到新建的数据库 contours 里。osm2pgsql导入时需要 style文件,以便于知道该过滤PBF中的哪些要素。这个 style文件是这样的,可以参考网站或者直接用下面这个contour.style:

# OsmType  Tag          DataType     Flags
node,way    contour                 text    linear
way         contour_ext             text    linear
way         ele                     text    linear


而后执行osm2pgsql

osm2pgsql -c -s -S"./contour.style" -C32000 -dcontours  ./shanghai_lon119.00_133.00lat30.00_32.00_view3.osm.pbf


导入后,进入数据库 contours,可以看到已经有数据了:



3. 向MapNik里添加图层

我们的ArchLinux服务器采用openstreetmap_carto 分支 3.0.x 作为风格工程。签出这个分支后,依照这个网站的指示一步步添加图层。注意了,因为该网站似乎有些技术延迟,我会把自己使用的方法在下面重新复述一遍。

3.1 建立 contour.mss 风格文件

这个文件将作为主工程的一部分被包含,内容如下:

@contour-below: #886A08;
@contour-lowland: #0B3B0B;
@contour-highland: #3B240B;
@contour-veryhigh: #0B3B39;

#contours-minor {
line-width: 0.2;
[elevation < 0] {
line-color: @contour-below;
}
[elevation > 0][elevation < 2000] {
line-color: @contour-lowland;
}
[elevation >= 2000][elevation < 5000] {
line-color: @contour-highland;
}
[elevation >= 5000] {
line-color: @contour-veryhigh;
}
}
#contours-medium {
line-width: 0.4;
[elevation < 0] {
line-color: @contour-below;
}
[elevation > 0][elevation < 2000] {
line-color: @contour-lowland;
}
[elevation >= 2000][elevation < 5000] {
line-color: @contour-highland;
}
[elevation >= 5000] {
line-color: @contour-veryhigh;
}
}
#contours-major {
line-width: 0.6;
[elevation < 0] {
line-color: @contour-below;
}
[elevation > 0][elevation < 2000] {
line-color: @contour-lowland;
}
[elevation >= 2000][elevation < 5000] {
line-color: @contour-highland;
}
[elevation >= 5000] {
line-color: @contour-veryhigh;
}
}

#contours-medium-text {
text-name: "[ele]";
text-face-name: @book-fonts;
text-placement: line;
text-spacing: 500;
text-size: 7;
text-fill: #FF0000;
[elevation < 0] {
text-fill: @contour-below;
}
[elevation > 0][elevation < 2000] {
text-fill: @contour-lowland;
}
[elevation >= 2000][elevation < 5000] {
text-fill: @contour-highland;
}
[elevation >= 5000] {
text-fill: @contour-veryhigh;
}
}
#contours-major-text {
text-name: "[ele]";
text-face-name: @book-fonts;
text-placement: line;
text-spacing: 1000;
text-size: 8;
text-fill: #FF0000;
[elevation < 0] {
text-fill: @contour-below;
}
[elevation > 0][elevation < 2000] {
text-fill: @contour-lowland;
}
[elevation >= 2000][elevation < 5000] {
text-fill: @contour-highland;
}
[elevation >= 5000] {
text-fill: @contour-veryhigh;
}
}


把这个文件放在与project.mml相同的文件夹下。

3.2 植入style与图层

打开project.mml,备份好,而后开改!

3.2.1 加入数据库入口

首先,加入数据库入口。找到_parts块,加入前:

osm2pgsql: &osm2pgsql
type: "postgis"
dbname: "gis"
key_field: ""
geometry_field: "way"
extent: "-20037508,-20037508,20037508,20037508"


加入后:

osm2pgsql: &osm2pgsql
type: "postgis"
dbname: "gis"
key_field: ""
geometry_field: "way"
extent: "-20037508,-20037508,20037508,20037508"
contour: &contour
type: "postgis"
dbname: "contours"
key_field: ""
geometry_field: "way"
extent: "-20037508,-20037508,20037508,20037508"


3.2.2 引用样式并粘贴图层

在入口 Stylesheet里把3.1中新建的contour.mms引用进来。

Stylesheet:
...
- "contour.mss"
...


而后,跑到文件末尾,粘贴图层:

- id: "contours-minor"
name: "contours-minor"
class: ""
geometry: "linestring"
<<: *extents
Datasource:
<<: *contour
table: |-
(SELECT
way,
CAST (ele AS INTEGER) AS elevation,
ele
FROM planet_osm_line
WHERE contour_ext='elevation_minor'
) AS contours_minor
properties:
minzoom: 14
advanced: {}
- id: "contours-medium"
name: "contours-medium"
class: ""
geometry: "linestring"
<<: *extents
Datasource:
<<: *contour
table: |-
(SELECT
way,
CAST (ele AS INTEGER) AS elevation,
ele
FROM planet_osm_line
WHERE contour_ext='elevation_medium'
) AS contours_medium
properties:
minzoom: 12
advanced: {}
- id: "contours-major"
name: "contours-major"
class: ""
geometry: "linestring"
<<: *extents
Datasource:
<<: *contour
table: |-
(SELECT
way,
CAST (ele AS INTEGER) AS elevation,
ele
FROM planet_osm_line
WHERE contour_ext='elevation_major' AND ele != '0'
) AS contours_major
properties:
minzoom: 9
advanced: {}
- id: "contours-medium-text"
name: "contours-medium-text"
class: ""
geometry: "linestring"
<<: *extents
Datasource:
<<: *contour
table: |-
(SELECT
way,
CAST (ele AS INTEGER) AS elevation,
ele
FROM planet_osm_line
WHERE contour_ext='elevation_medium'
) AS contours_medium_text
properties:
minzoom: 13
advanced: {}
- id: "contours-major-text"
name: "contours-major-text"
class: ""
geometry: "linestring"
<<: *extents
Datasource:
<<: *contour
table: |-
(SELECT
way,
CAST (ele AS INTEGER) AS elevation,
ele
FROM planet_osm_line
WHERE contour_ext='elevation_major' AND ele != '0'
) AS contours_major_text
properties:
minzoom: 9
advanced: {}


要特别注意各部分的缩进与文档相同级别的缩进一致。楼主因为一开始少了一个空格(图层的id行缩进为2空格)折腾的要死。

3.3 编译样式表

我们用 node.js的工具 cartocss编译project.mml为mapnik.xml

如果没有安装,要使用 npm 傻瓜安装

npm install cartocss


当然,这玩意有依赖,根据错误提示,把依赖安装完了就ok了。而后编译:

carto project.mml > mapnik.xml


生成的 mapnik.xml 就可以直接被renderd使用了。

4 测试

在导入了高程后,刷新时戳

sudo touch  /var/lib/mod_tile/planet-import-complete


重启服务后,渲染瓦片

render_old




好啦!成功了!

后续会试试看全球的高程到底会吃掉多少磁盘空间(-:<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: