当前位置:   article > 正文

java发布矢量瓦片(基于springboot)_java 瓦片服务器搭建

java 瓦片服务器搭建

本文采用 CC BY-SA 4.0 协议 ,转载请注明原始链接: https://blog.wowtools.org/2022/04/28/2022-04-28-mapbox-gl-tutorial-8/

引入springboot和giscat的maven依赖

  1. <dependency>
  2. <artifactId>giscat-vector-mvt</artifactId>
  3. <groupId>org.wowtools</groupId>
  4. <version>g1.6.1</version>
  5. </dependency>

编写Controller,为了节约篇幅,这里把SpringBootApplication也写在同一个类中了

  1. import org.locationtech.jts.geom.*;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.CrossOrigin;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import org.wowtools.giscat.vector.pojo.Feature;
  9. import org.wowtools.giscat.vector.pojo.FeatureCollection;
  10. import org.wowtools.giscat.vector.pojo.converter.GeoJsonFeatureConverter;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  13. import java.io.OutputStream;
  14. import java.util.ArrayList;
  15. import java.util.Map;
  16. /**
  17. * 用springboot起一个web服务演示矢量瓦片的使用
  18. *
  19. * @author liuyu
  20. * @date 2022/4/26
  21. */
  22. @SpringBootApplication
  23. @RestController()
  24. @RequestMapping("/tile")
  25. @CrossOrigin
  26. public class WebDemo {
  27. public static void main(String[] args) {
  28. SpringApplication.run(WebDemo.class, args);
  29. }
  30. /**
  31. * 测试数据,中国省份
  32. * 数据来源
  33. * https://datav.aliyun.com/portal/school/atlas/area_selector
  34. */
  35. private static final FeatureCollection areaFeatureCollection;//面数据
  36. private static final FeatureCollection lineFeatureCollection;//线数据
  37. private static final FeatureCollection pointFeatureCollection;//点数据
  38. private static final GeometryFactory geometryFactory = new GeometryFactory();
  39. private static final String vtContentType = "application/octet-stream";
  40. @RequestMapping("/{z}/{x}/{y}")
  41. public void getTile(@PathVariable int z, @PathVariable int x, @PathVariable int y, HttpServletResponse response) {
  42. //构造一个MvtBuilder对象
  43. MvtBuilder mvtBuilder = new MvtBuilder(z, x, y, geometryFactory);
  44. //向mvt中添加layer
  45. MvtLayer layer = mvtBuilder.getOrCreateLayer("省区域");
  46. //向layer中添加feature
  47. for (Feature feature : areaFeatureCollection.getFeatures()) {
  48. //这里简单地从内存中取数据并判断其是否与瓦片有交集,实际运用中可从数据库查询,例如postgis的ST_intersects函数
  49. if (mvtBuilder.getBbox().envIntersects(feature.getGeometry())) {
  50. layer.addFeature(feature);
  51. }
  52. }
  53. //如法炮制添加layer
  54. layer = mvtBuilder.getOrCreateLayer("省边界");
  55. for (Feature feature : lineFeatureCollection.getFeatures()) {
  56. if (mvtBuilder.getBbox().envIntersects(feature.getGeometry())) {
  57. layer.addFeature(feature);
  58. }
  59. }
  60. //如法炮制添加layer
  61. layer = mvtBuilder.getOrCreateLayer("省会位置");
  62. for (Feature feature : pointFeatureCollection.getFeatures()) {
  63. if (mvtBuilder.getBbox().envIntersects(feature.getGeometry())) {
  64. layer.addFeature(feature);
  65. }
  66. }
  67. //数据添加完毕,转为
  68. byte[] bytes = mvtBuilder.toBytes();
  69. exportByte(bytes, vtContentType, response);
  70. }
  71. //将bytes写进HttpServletResponse
  72. private void exportByte(byte[] bytes, String contentType, HttpServletResponse response) {
  73. response.setContentType(contentType);
  74. try (OutputStream os = response.getOutputStream()) {
  75. os.write(bytes);
  76. os.flush();
  77. } catch (org.apache.catalina.connector.ClientAbortException e) {
  78. //地图移动时客户端主动取消, 产生异常"你的主机中的软件中止了一个已建立的连接",无需处理
  79. } catch (IOException e) {
  80. throw new RuntimeException(e);
  81. }
  82. }
  83. }

完整的服务端代码在这里
启动后,我们便成功发布了一个矢量瓦片服务http://localhost:8080/tile/{z}/{x}/{y}

新建一个空白地图,然后添加刚才发布的服务为数据源,再添加图层即可:

  1. // 新建一个空白地图
  2. const map = new mapboxgl.Map({
  3. container: 'map',
  4. style: {
  5. "version": 8,
  6. "sources": {},
  7. "layers": []
  8. },
  9. center: [102.712251, 25.040609],
  10. zoom: 4
  11. });
  12. map.on('load', function () {
  13. //添加刚才发布的mvt数据源
  14. map.addSource('tile', {
  15. "type": "vector",
  16. "tiles": [
  17. 'http://localhost:8080/tile/{z}/{x}/{y}'
  18. ],
  19. "minZoom": 1,
  20. "maxZoom": 22
  21. })
  22. //添加各图层
  23. map.addLayer({
  24. "id": "area-layer",
  25. "type": "fill",
  26. "source": "tile",// 上一步添加的数据源id
  27. "source-layer": "省区域",// source-layer和mvt服务中的图层名对应
  28. "layout": {"visibility": "visible"},
  29. "paint": {"fill-color": '#51bbd6', "fill-opacity": 0.3, "fill-outline-color": '#0000ff'}
  30. })
  31. map.addLayer({
  32. "id": "point-layer",
  33. "source": 'tile',
  34. "source-layer": "省会位置",
  35. "type": "circle",
  36. "paint": {
  37. "circle-color": "#FF00FF",
  38. "circle-radius": 5
  39. }
  40. });
  41. map.addLayer({
  42. "id": "route",
  43. "type": "line",
  44. "source": 'tile',
  45. "source-layer": "省边界",
  46. "layout": {
  47. "line-join": "round",
  48. "line-cap": "round"
  49. },
  50. "paint": {
  51. "line-color": "#FF0000",
  52. "line-width": 3
  53. }
  54. });
  55. //矢量瓦片图层可以被点击
  56. map.on('click', 'area-layer', (e) => {
  57. console.log(e.features[0])
  58. })
  59. })

完整的前端代码在这里

展现效果如下:

 本文采用 CC BY-SA 4.0 协议 ,转载请注明原始链接: https://blog.wowtools.org/2022/04/28/2022-04-28-mapbox-gl-tutorial-8/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/708919
推荐阅读
相关标签
  

闽ICP备14008679号