让矢量瓦片配图神器maputnik支持 geoserver
作者:互联网
一直想搞一个类似百度、高德地图那样的矢量地图配图工具
百度个性化地图配图工具:
image-20201011155118734
高德自定义地图配图工具:
image-20201011155314884
在网上找过几次,都没有找到。
无意中从这个博客了解到了Maputnik,一阵摆弄后,感觉相见恨晚。对!就是我要找的东西。
image-20201011155711758
Maputnik是啥?有啥用?
Maputnik是Mapbox样式规范的开源可视化编辑器,它和Mapbox的mapbox studio、百度地图的个性化地图编辑器、高德地图的自定义地图编辑器干的是一个事,都是用来编辑矢量瓦片地图样式,配图用的。
有了Maputnik就可以这样来发布地图,
用geoserver发布矢量瓦片
用Maputnik为上面的矢量瓦片配置地图样式
用mapbox调用矢量瓦片,并应用上面的地图样式,在前台渲染地图展示
和 mapbox studio 相比,Maputnik开源,可以免费在本地使用,不再需要把自己的地图数据上传到mapbox的服务器,相应的也就不用受制于mapbox免费账号对每月上传数据量的限制,和对地图调用次数的限制。
maputnik 官网:https://maputnik.github.io/
Maputnik github地址:https://github.com/maputnik/editor
Maputnik 官方在线体验地址:https://maputnik.github.io/editor/
Maputnik的创建背景,作者是想打造一套对标 mapbox 的开源GIS解决方案。
遇到的问题
用Maputnik1.7.0版本加载geoserver发布的矢量瓦片时遇到一个问题,加载的图层不显示,这个问题困扰了自己好几天,去网上百度、谷歌,相关的资料也非常少,没有答案。
自己好不容易遇到的兵器,难道就要这样擦肩而过吗?不,我不允许!
功夫不负有心人,经过几天的折腾,终于让我发现了其中的问题,下面详细说一下。
排查问题
发现地图没显示,就习惯性的打开了浏览器的开发者工具,发现缩放、拖动地图时,浏览器去请求了瓦片,但都没有成功。
image-20201011164559961
拷贝其中一个请求地址,在浏览器单独打开,看到geoserver返回的报错提示是,请求的瓦片超出了数据范围。
image-20201011164714033
Maputnik地图的api使用的是mapboxgl,那直接用mapboxgl调用一下发布的矢量瓦片,或许能更容易排查问题。网上介绍mapboxgl调用geoserver矢量瓦片的文章还是挺多的,自己参考着写了个示例。运行示例,结果瓦片可以正确请求到并显示。
代码:
var map = new mapboxgl.Map({
container: ‘map’,
style: ‘mapbox://styles/mapbox/light-v10’,
zoom: 3,
center: [108.34942054748535,37.83543237333567]
});
map.on(‘load’, function() {
map.addLayer({
“id”: “mapillary”,
“type”: “line”,
“source”: {
“type”: “vector”,
‘scheme’:‘tms’,
“tiles”: [“http://192.168.50.198:7000/geoserver/gwc/service/tms/1.0.0/china%3Acity_region@EPSG%3A900913@pbf/{z}/{x}/{y}.pbf”],
},
“source-layer”: “city_region”,
“paint”: {
“line-opacity”: 0.6,
“line-color”: “rgb(53, 175, 109)”,
“line-width”: 2
}
}, ‘waterway-label’);
});
效果:
image-20201011165013883
这就有点意思了,看来还是Maputnik的调用配置出现了问题。打开浏览器开发者工具,发现示例请求的瓦片地址中的编号,要比maputnik中的编号大很多。
image-20201011170332981
image-20201011170409587
在maputnik中拖动地图。找到和示例中相同的瓦片编号,发现地图跑到了南半球。
image-20201011170639987
这会是什么原因造成的呢,会不会是请求瓦片的大小不同,比如一个是256x256的,一个是512x512的,通常这种情况也会导致出现上面的问题,而且编号会刚好相差一半。
但现在的编号差的好像并不是一半,那geoserver发布的矢量瓦片到底是多少呢?Maputnik中又是按照什么规则来请求的呢?抱着这个疑问,我决定去研究一下mapboxgl的api,看会不会有什么发现。
上面示例的代码中,数据源是通过source来配置的
“source”: {
“type”: “vector”,
‘scheme’:‘tms’,
“tiles”: [“http://192.168.50.198:7000/geoserver/gwc/service/tms/1.0.0/china%3Acity_region@EPSG%3A900913@pbf/{z}/{x}/{y}.pbf”],
},
这里面有三个参数,type和tiles的意思好理解,scheme是什么意思呢?
查看mapboxgl的api,关于scheme的解释如下:
image-20201011142510634
这个 xyz和tms分别是啥意思呢?
百度后觉得这篇博客写的还算明白,它们是两种请求瓦片的协议,不同协议对瓦片编号的规则也不同,在xyz协议中,Y从顶部开始计算,而在tms协议中,Y从底部开始计算。mapboxgl默认使用xyz协议,而geoserver发布的是tms协议。所以调用时需要定义scheme为tms。
https://github.com/sz7epigs/fgufnxzhqj/discussions/1
https://github.com/sz7epigs/fgufnxzhqj/discussions/2
https://github.com/sz7epigs/fgufnxzhqj/discussions/3
https://github.com/sz7epigs/fgufnxzhqj/discussions/4
https://github.com/sz7epigs/fgufnxzhqj/discussions/5
https://github.com/qrpin3u9/tyepjlfylv/discussions/1
https://github.com/qrpin3u9/tyepjlfylv/discussions/2
https://github.com/sz7epigs/stygmwnyey/discussions/1
https://github.com/qrpin3u9/ppyvkmfsll/discussions/1
https://github.com/qqzul2a6/utvhlldrqg/discussions/1
https://github.com/qrpin3u9/qxubsrnhky/discussions/1
https://github.com/qqzul2a6/igdqomjeqh/discussions/1
https://github.com/sz7epigs/srcxsuhqkp/discussions/1
https://github.com/qrpin3u9/alreksczbq/discussions/1
https://github.com/qqzul2a6/zcwwohggko/discussions/1
https://github.com/sz7epigs/zhtagekrdk/discussions/1
https://github.com/qrpin3u9/yefkqqgvtt/discussions/1
https://github.com/qqzul2a6/emojiyhixe/discussions/1
https://github.com/sz7epigs/zjjjkprkrs/discussions/1
https://github.com/qqzul2a6/cooopkaunp/discussions/1
https://github.com/qrpin3u9/fdcvwjdclf/discussions/1
https://github.com/sz7epigs/khycvxyzuo/discussions/1
https://github.com/qqzul2a6/jqzipycoam/discussions/1
https://github.com/sz7epigs/nfqbpxopvk/discussions/1
https://github.com/qrpin3u9/hygpnprapm/discussions/1
https://github.com/qqzul2a6/jqzipycoam/discussions/2
https://github.com/sz7epigs/nfqbpxopvk/discussions/2
https://github.com/qrpin3u9/hygpnprapm/discussions/2
https://github.com/sz7epigs/nfqbpxopvk/discussions/3
https://github.com/qqzul2a6/jqzipycoam/discussions/3
https://github.com/qrpin3u9/hygpnprapm/discussions/3
https://github.com/sz7epigs/nfqbpxopvk/discussions/4
https://github.com/qqzul2a6/jqzipycoam/discussions/4
https://github.com/qrpin3u9/hygpnprapm/discussions/4
https://github.com/sz7epigs/nfqbpxopvk/discussions/5
https://github.com/qqzul2a6/jqzipycoam/discussions/5
https://github.com/qrpin3u9/hygpnprapm/discussions/5
https://github.com/sz7epigs/nfqbpxopvk/discussions/6
https://github.com/qqzul2a6/jqzipycoam/discussions/6
https://github.com/qrpin3u9/hygpnprapm/discussions/6
https://github.com/qqzul2a6/jqzipycoam/discussions/7
https://github.com/sz7epigs/nfqbpxopvk/discussions/7
https://github.com/qrpin3u9/hygpnprapm/discussions/7
https://github.com/sz7epigs/nfqbpxopvk/discussions/8
https://github.com/qqzul2a6/jqzipycoam/discussions/8
https://github.com/qrpin3u9/hygpnprapm/discussions/8
https://github.com/qqzul2a6/jqzipycoam/discussions/9
https://github.com/sz7epigs/nfqbpxopvk/discussions/9
https://github.com/qrpin3u9/hygpnprapm/discussions/9
https://github.com/qqzul2a6/jqzipycoam/discussions/10
https://github.com/sz7epigs/nfqbpxopvk/discussions/10
https://github.com/qrpin3u9/hygpnprapm/discussions/10
https://github.com/qqzul2a6/jqzipycoam/discussions/11
https://github.com/sz7epigs/nfqbpxopvk/discussions/11
https://github.com/qrpin3u9/hygpnprapm/discussions/11
https://github.com/sz7epigs/nfqbpxopvk/discussions/12
https://github.com/qqzul2a6/jqzipycoam/discussions/12
https://github.com/qrpin3u9/hygpnprapm/discussions/12
https://github.com/sz7epigs/nfqbpxopvk/discussions/13
https://github.com/qqzul2a6/jqzipycoam/discussions/13
https://github.com/qrpin3u9/hygpnprapm/discussions/13
https://github.com/sz7epigs/nfqbpxopvk/discussions/14
https://github.com/qqzul2a6/jqzipycoam/discussions/14
https://github.com/qrpin3u9/hygpnprapm/discussions/14
https://github.com/sz7epigs/nfqbpxopvk/discussions/15
https://github.com/qqzul2a6/jqzipycoam/discussions/15
https://github.com/qrpin3u9/hygpnprapm/discussions/15
https://github.com/sz7epigs/nfqbpxopvk/discussions/16
https://github.com/qqzul2a6/jqzipycoam/discussions/16
https://github.com/qrpin3u9/hygpnprapm/discussions/16
https://github.com/sz7epigs/nfqbpxopvk/discussions/17
https://github.com/qqzul2a6/jqzipycoam/discussions/17
https://github.com/sz7epigs/nfqbpxopvk/discussions/18
https://github.com/qrpin3u9/hygpnprapm/discussions/17
https://github.com/qqzul2a6/jqzipycoam/discussions/18
https://github.com/sz7epigs/nfqbpxopvk/discussions/19
https://github.com/qqzul2a6/jqzipycoam/discussions/19
https://github.com/sz7epigs/nfqbpxopvk/discussions/20
https://github.com/qrpin3u9/hygpnprapm/discussions/18
https://github.com/qqzul2a6/jqzipycoam/discussions/20
https://github.com/qrpin3u9/hygpnprapm/discussions/19
https://github.com/sz7epigs/nfqbpxopvk/discussions/21
https://github.com/qqzul2a6/jqzipycoam/discussions/21
https://github.com/qrpin3u9/hygpnprapm/discussions/20
https://github.com/sz7epigs/nfqbpxopvk/discussions/22
https://github.com/qqzul2a6/jqzipycoam/discussions/22
https://github.com/sz7epigs/nfqbpxopvk/discussions/23
https://github.com/qrpin3u9/hygpnprapm/discussions/21
https://github.com/qqzul2a6/jqzipycoam/discussions/23
https://github.com/qrpin3u9/hygpnprapm/discussions/22
https://github.com/sz7epigs/nfqbpxopvk/discussions/24
https://github.com/qqzul2a6/jqzipycoam/discussions/24
https://github.com/qrpin3u9/hygpnprapm/discussions/23
https://github.com/qqzul2a6/jqzipycoam/discussions/25
https://github.com/qrpin3u9/hygpnprapm/discussions/24
https://github.com/sz7epigs/nfqbpxopvk/discussions/26
https://github.com/qqzul2a6/jqzipycoam/discussions/26
https://github.com/qrpin3u9/hygpnprapm/discussions/25
https://github.com/qqzul2a6/jqzipycoam/discussions/27
https://github.com/sz7epigs/nfqbpxopvk/discussions/27
https://github.com/qrpin3u9/hygpnprapm/discussions/26
https://github.com/qqzul2a6/jqzipycoam/discussions/28
https://github.com/sz7epigs/nfqbpxopvk/discussions/28
https://github.com/qqzul2a6/jqzipycoam/discussions/29
https://github.com/sz7epigs/nfqbpxopvk/discussions/29
https://github.com/qrpin3u9/hygpnprapm/discussions/27
https://github.com/qqzul2a6/jqzipycoam/discussions/30
https://github.com/qrpin3u9/hygpnprapm/discussions/28
https://github.com/sz7epigs/nfqbpxopvk/discussions/30
https://github.com/qqzul2a6/jqzipycoam/discussions/31
https://github.com/qrpin3u9/hygpnprapm/discussions/29
https://github.com/sz7epigs/nfqbpxopvk/discussions/31
https://github.com/sz7epigs/nfqbpxopvk/discussions/32
https://github.com/qqzul2a6/jqzipycoam/discussions/33
https://github.com/sz7epigs/nfqbpxopvk/discussions/33
https://github.com/qrpin3u9/hygpnprapm/discussions/31
https://github.com/qqzul2a6/jqzipycoam/discussions/34
https://github.com/qvbuqqvw/xrlnzltblt/discussions/1
https://github.com/lty6dp09/ucocmmysom/discussions/1
https://github.com/qvbuqqvw/xrlnzltblt/discussions/2
https://github.com/jdzce2g3/okssawcsms/discussions/2
https://github.com/lty6dp09/ucocmmysom/discussions/2
https://github.com/qvbuqqvw/xrlnzltblt/discussions/3
https://github.com/jdzce2g3/okssawcsms/discussions/3
https://github.com/lty6dp09/ucocmmysom/discussions/3
https://github.com/qvbuqqvw/xrlnzltblt/discussions/4
https://github.com/jdzce2g3/okssawcsms/discussions/4
https://github.com/lty6dp09/ucocmmysom/discussions/4
https://github.com/jdzce2g3/okssawcsms/discussions/5
https://github.com/qvbuqqvw/xrlnzltblt/discussions/5
https://github.com/lty6dp09/ucocmmysom/discussions/5
https://github.com/jdzce2g3/okssawcsms/discussions/6
https://github.com/qvbuqqvw/xrlnzltblt/discussions/6
https://github.com/jdzce2g3/okssawcsms/discussions/7
https://github.com/lty6dp09/ucocmmysom/discussions/6
问题找到这里,感觉自己已经看到黎明的曙光了。
那如果把示例中的scheme设置为xyz会不会出现和Maputnik一样的错误呢?尝试后答案是肯定的,地图同样跑到了南半球。
image-20201011173212261
真相大白了,Maputnik界面上因为没有配置scheme的选项,而scheme默认是xyz协议,所以就导致它只支持xyz协议,不支持tms协议,所以就请求不到瓦片,地图无法正确显示。
如何解决
问题找到了,接下来就是如何解决,思路有二:
geoserver发布一个xyz协议的矢量瓦片服务。
修改一下Maputnik的源码,增加scheme的选项,让它支持tms协议。
思路一自己找了一圈,没有发现相关教程,个人感觉思路二更简单一些,只要在下图的红框的位置加个选择scheme的下拉框就能解决问题
image-20201011173847958
下载maputnik的源码,一通研究,发现修改下面两个位置就可以:
1、在这里加一个下拉框控件
image-20201011174911575
2、在这里给scheme设置一下默认值
image-20201011175014061
ok,来,走两步
一步
image-20201011175322458
两步
image-20201011180144760
哈哈,效果杠杠的!问题完美解决。
总结
Maputnik是一个矢量瓦片配图工具,可以替代 mapbox studio,免费使用。
Maputnik默认只支持xyz协议的矢量瓦片,不支持geoserver发布的tms协议矢量瓦片。
Maputnik的地图api使用的是mapboxgl,mapboxgl通过设置source的scheme选项,可以支持tms协议。
通过修改Maputnik源码,增加scheme选项,就可以让Maputnik支持geoserver发布的tms协议矢量瓦片。
标签:github,sz7epigs,qqzul2a6,maputnik,geoserver,discussions,配图,com,qrpin3u9 来源: https://blog.csdn.net/shaodong2528/article/details/112172581