当前位置: 首页 > news >正文

基于Martin实现MapboxGL自定义底图

概述

本文分享基于Martin实现MapboxGL底图的自定义。

实现后效果

image.png

Martin简介

image.png

Martin 是一个瓦片服务器,它能够从 PostGIS 数据库、PMTiles(本地或远程)以及 [MBTiles] (https://github.com/mapbox/mbtiles-spec) 文件中快速生成并提供矢量瓦片 ,允许动态地将多个瓦片源合并为一个。Martin 专为速度和大量流量进行了优化,使用 Rust 语言编写。

它可以:

  • 使用POSTGIS数据库、MBTiles、PMTiles发布矢量切片;
  • svg图自动生成精灵图;
  • otf, ttf, ttc自动生成字体切片;

实现

config.yaml配置文件

推荐使用*.yaml配置文件,配置文件的内容可参考如下:

# Connection keep alive timeout [default: 75]
keep_alive: 75# The socket address to bind [default: 0.0.0.0:3000]
listen_addresses: '0.0.0.0:3000'# Set TileJSON URL path prefix. This overides the default of respecting the X-Rewrite-URL header.
# Only modifies the JSON (TileJSON) returned, martins' API-URLs remain unchanged. If you need to rewrite URLs, please use a reverse proxy.
# Must begin with a `/`.
# Examples: `/`, `/tiles`
base_path: /tiles# Number of web server workers
worker_processes: 8# Amount of memory (in MB) to use for caching tiles [default: 512, 0 to disable]
cache_size_mb: 1024# If the client accepts multiple compression formats, and the tile source is not pre-compressed, which compression should be used. `gzip` is faster, but `brotli` is smaller, and may be faster with caching.  Default could be different depending on Martin version.
preferred_encoding: gzip# Enable or disable Martin web UI. At the moment, only allows `enable-for-all` which enables the web UI for all connections. This may be undesirable in a production environment. [default: disable]
web_ui: enable# Database configuration. This can also be a list of PG configs.
postgres:# Database connection string. You can use env vars too, for example:#   $DATABASE_URL#   ${DATABASE_URL:-postgresql://postgres@localhost/db} 'postgres://<database_username>:<database_userpassword>@<hostaddress>:<port_no>/<database_name>'connection_string: 'postgresql://postgres:root@localhost:5432/lzugis'#  If a spatial table has SRID 0, then this SRID will be used as a fallbackdefault_srid: 4326# Maximum Postgres connections pool size [default: 20]pool_size: 20# Limit the number of table geo features included in a tile. Unlimited by default.max_feature_count: 1000# Control the automatic generation of bounds for spatial tables [default: quick]# 'calc' - compute table geometry bounds on startup.# 'quick' - same as 'calc', but the calculation will be aborted if it takes more than 5 seconds.# 'skip' - do not compute table geometry bounds on startup.auto_bounds: skip# Enable automatic discovery of tables and functions.# You may set this to `false` to disable.auto_publish:# Optionally limit to just these schemasfrom_schemas:- public# Here we enable both tables and functions auto discovery.# You can also enable just one of them by not mentioning the other,# or setting it to false.  Setting one to true disables the other one as well.# E.g. `tables: false` enables just the functions auto-discovery.tables:# Optionally set how source ID should be generated based on the table's name, schema, and geometry columnsource_id_format: 'base_{table}'# Add more schemas to the ones listed above# A table column to use as the feature ID# If a table has no column with this name, `id_column` will not be set for that table.# If a list of strings is given, the first found column will be treated as a feature ID.id_columns: gid# Boolean to control if geometries should be clipped or encoded as is, optional, default to trueclip_geom: true# Buffer distance in tile coordinate space to optionally clip geometries, optional, default to 64buffer: 64# Tile extent in tile coordinate space, optional, default to 4096extent: 4096# Associative arrays of table sourcestables:my_province:# ID of the MVT layer (optional, defaults to table name)layer_id: my_province# Table schema (required)schema: public# Table name (required)table: province# Geometry SRID (required)srid: 4326# Geometry column name (required)geometry_column: geom# Feature id column nameid_column: gid# An integer specifying the minimum zoom levelminzoom: 0# An integer specifying the maximum zoom level. MUST be >= minzoommaxzoom: 22# The maximum extent of available map tiles. Bounds MUST define an area# covered by all zoom levels. The bounds are represented in WGS:84# latitude and longitude values, in the order left, bottom, right, top.# Values may be integers or floating point numbers.bounds: [ -180.0, -90.0, 180.0, 90.0 ]# Tile extent in tile coordinate spaceextent: 4096# Buffer distance in tile coordinate space to optionally clip geometriesbuffer: 64# Boolean to control if geometries should be clipped or encoded as isclip_geom: true# Geometry typegeometry_type: GEOMETRY# List of columns, that should be encoded as tile properties (required)properties:gid: int4# Associative arrays of function sourcesfunctions:function_source_id:# Schema name (required)schema: public# Function name (required)function: function_zxy_query# An integer specifying the minimum zoom levelminzoom: 0# An integer specifying the maximum zoom level. MUST be >= minzoommaxzoom: 20# The maximum extent of available map tiles. Bounds MUST define an area# covered by all zoom levels. The bounds are represented in WGS:84# latitude and longitude values, in the order left, bottom, right, top.# Values may be integers or floating point numbers.bounds: [ -180.0, -90.0, 180.0, 90.0 ]# Publish MBTiles files
mbtiles:paths:# scan this whole dir, matching all *.mbtiles files# - /dir-path# specific mbtiles file will be published as mbtiles2 source- ./world_cities.mbtilessources:# named source matching source name to a single file# mb-src1: /path/to/mbtiles1.mbtiles      
sprites:paths:# all SVG files in this dir will be published as a "my_images" sprite source# - ./icons   sources:# SVG images in this directory will be published as a "my_sprites" sprite sourcebanks: ./icons
# Font configuration
fonts:# A list of *.otf, *.ttf, and *.ttc font files and dirs to search recursively.- ./font/msyh.ttf

启动

本示例是在windows环境中,在cmd命令行输入如下命令:

.\martin.exe --config ./config.yaml

启动后界面如下。
image.png

访问http://localhost:3000/catalog,返回数据如下:
image.png

前端调用

前端调用的代码如下:

<!DOCTYPE html>
<html lang="en"><head><title>mapbox</title><meta charset="UTF-8"><link rel="stylesheet" href="lib/mapbox-gl.css" type="text/css"><link rel="stylesheet" href="style/map.css" type="text/css"><script src="lib/mapbox-gl.js"></script>
</head><body><div id="map" class="map"></div><script>var style = {"version": 8,"name": "Mapbox Streets","sprite": "http://127.0.0.1:3000/sprite/banks","glyphs": "http://127.0.0.1:3000/font/Microsoft YaHei Regular/{fontstack}/{range}.pbf","sources": {"base-tile": {"type": "vector","tiles": ['http://127.0.0.1:3000/my_province,base_capital,base_city,base_railway,base_buildings/{z}/{x}/{y}'],}},"layers": [{"id": "my_province_fill","type": "fill","source": "base-tile",'source-layer': 'my_province',"paint": {"fill-color": "#f00","fill-opacity": 0.2}},{"id": "my_province_line","type": "line","source": "base-tile",'source-layer': 'my_province',"paint": {"line-color": "#f00","line-width": 1}},{"id": "base_capital","type": "symbol","source": "base-tile",'source-layer': 'base_capital','layout': {'icon-image': 'nongye','icon-size': 0.06,'icon-allow-overlap': true,'text-field': ['get', 'name'],'text-size': 12,'text-allow-overlap': true,'text-justify': 'center','text-offset': [0, 1.5],"text-font": ["Microsoft YaHei Regular"]},paint: {'text-color': 'rgb(159, 96, 55)','text-halo-color': '#fff','text-halo-width': 1.8,'icon-color': '#f00',}},]}var map = new mapboxgl.Map({container: 'map', // container IDstyle: style,center: [107.11040599933166, 34.26271532332011], // starting position [lng, lat]zoom: 3,doubleClickZoom: false,hash: false,localFontFamily: true,logoPosition: 'bottom-right'});</script>
</body></html>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【旗芯微FC7300】如何配置PWM触发AD采样
  • 【iOS】AutoreleasePool自动释放池的实现原理
  • AWS云服务器选择最佳区域
  • 使用Linux实现FTP云盘项目2
  • 全栈物联网云平台搭建:MQTT、Node.js、MongoDB、InfluxDB与React的应用示例
  • 数据分析与应用:微信-情人节红包流向探索分析
  • 仅通过 css 为代码添加行号 | code line number with CSS only
  • vxe-table树形结构使用setCheckboxRow卡顿--已解决
  • 1章1节:认识人工智能的第一节课
  • Bash考古以及 StackOverflow的2024年度技术统计报告
  • 深入浅出 Electron 的BrowserWindow
  • 线性表-单链表-C语言实现
  • 【ESP01开发实例】- ISD1820录音控制
  • JDK17下载安装卸载
  • gdb调试core dump快速入门
  • 收藏网友的 源程序下载网
  • 自己简单写的 事件订阅机制
  • 【React系列】如何构建React应用程序
  • 【翻译】babel对TC39装饰器草案的实现
  • Akka系列(七):Actor持久化之Akka persistence
  • CentOS从零开始部署Nodejs项目
  • ECMAScript6(0):ES6简明参考手册
  • Javascript设计模式学习之Observer(观察者)模式
  • laravel with 查询列表限制条数
  • MQ框架的比较
  • select2 取值 遍历 设置默认值
  • WePY 在小程序性能调优上做出的探究
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 线上 python http server profile 实践
  • 一加3T解锁OEM、刷入TWRP、第三方ROM以及ROOT
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ​卜东波研究员:高观点下的少儿计算思维
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • # 消息中间件 RocketMQ 高级功能和源码分析(七)
  • #if和#ifdef区别
  • #WEB前端(HTML属性)
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (175)FPGA门控时钟技术
  • (30)数组元素和与数字和的绝对差
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (七)Activiti-modeler中文支持
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (四)js前端开发中设计模式之工厂方法模式
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .Net Redis的秒杀Dome和异步执行
  • .Net 代码性能 - (1)
  • .NET_WebForm_layui控件使用及与webform联合使用
  • ;号自动换行
  • [000-01-030].Zookeeper学习大纲
  • [20161101]rman备份与数据文件变化7.txt
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [AIGC] 解题神器:Python中常用的高级数据结构