postgis mvt矢量切片 django drf mapboxgl

postgis mvt矢量切片 django drf mapboxgl

目录

0.前提

1.sql代码

[2.django drf后端服务代码](#2.django drf后端服务代码)

3.具体的应用(整体代码)

4.参考


0.前提

1\] 静态的矢量切片可以采用 tippecanoe 生成,nginx代理,这种数据是不更新的; \[2\] 动态的矢量切片,一般采用postgis生成。基本上矢量切片80%的厂商都采用postgis,确实好用!不谈商业的。 \[3\] postgis矢量切片使用到的函数:**[ST_AsMVT](https://postgis.net/docs/manual-3.0/ST_AsMVT.html "ST_AsMVT")、[ST_AsMVTGeom](https://postgis.net/docs/manual-3.0/ST_AsMVTGeom.html "ST_AsMVTGeom")、[ST_TileEnvelope](https://postgis.net/docs/manual-3.0/ST_TileEnvelope.html "ST_TileEnvelope")、[ST_Transform](https://postgis.net/docs/manual-3.0/ST_Transform.html "ST_Transform")、[ST_Intersects](https://postgis.net/docs/manual-3.0/ST_Intersects.html "ST_Intersects")、[ST_SRID](https://postgis.net/docs/manual-3.0/ST_SRID.html "ST_SRID")。(点击查看每个函数的介绍,都是官网文档,很详细)** \[4\] postgis api参考文档官网:[Official Manual \| PostGIS](https://postgis.net/documentation/manual/ "Official Manual | PostGIS"),有postgis3.0版本以上的,点击html,点击[8. PostGIS Reference](https://postgis.net/docs/manual-3.0/reference.html "8. PostGIS Reference"),即可查看矢量处理的函数。 ![](https://file.jishuzhan.net/article/1687077854279372802/b6d328c3aa6747149768fcaad229e7f1.png) \[5\] 矢量切片mvt,需要坐标为EPSG:3857,如果想用mvt这种开源的标准,就使用ST_Transform统一转换成这个坐标。然后入库的数据都设置成EPSG:4326。 ## 1.sql代码 \[1\] 获取表的字段名称:(zzz替换成传入的表名)【PS:不建议动态查询将1-2结合,不好】 select column_name from information_schema.columns where table_name='zzz'; \[2\] 动态获取矢量切片: (1.0.0)替换成传入的z,x,y参数,zzz替换成传入的表名,geom替换成geom几何对应的字段名称。 ```sql with mvtgeom as ( select ST_AsMVTGeom(ST_Transform(geom, 3857), ST_TileEnvelope(1,0,0)) as geom, gid from zzz, (select ST_SRID(geom) as srid from zzz where geom is not null limit 1) a where ST_Intersects(geom, ST_Transform(ST_TileEnvelope(1,0,0), srid)) ) select ST_AsMVT(mvtgeom.*, 'zzz', 4096, 'geom') as mvt from mvtgeom; ``` ## 2.django drf后端服务代码 基于APIView重写get函数,再注册到urls.py中 前端访问 ip/\`table_name\`/\`z\`/\`x\`/\`y\`,eg:http://127.0.0.1:8080/getmap/zzz/2/1/1 这样就可以接收矢量切片mvt了。 ```python from django.db import connection from rest_framework.views import APIView from rest_framework.response import Response class MapView(APIView): def get(self, request, table, z, x, y): print(table, z, x, y,) table_name = table #'zzz' # 将数据库的数据导出为geojson(建议不要太大,太大用 wms服务吧、或者mvt矢量切片) # sql = f"""select json_build_object('type', 'FeatureCollection', 'name', '{table_name}', 'features', json_agg(ST_ASGeoJSON(t.*)::json)) from {table_name} AS t """ # 获取表的字段名称 列表 # sql = f"""select column_name from information_schema.columns where table_name='{table_name}'""" # where后面是string 不应被转成对象变量 # 获取表的字段名称 字符串 # sql = f"""select array_to_string(array(select column_name from information_schema.columns where table_name='{table_name}' and column_name != 'geom'), ',');""" # 动态矢量切片mvt gid字段也可替换成前端传入的字段 geom_name = 'geom' sql = f"""with mvtgeom as ( select ST_AsMVTGeom(ST_Transform({geom_name}, 3857), ST_TileEnvelope({z},{x},{y})) as geom, gid from {table_name}, (select ST_SRID({geom_name}) as srid from {table_name} where {geom_name} is not null limit 1) a where ST_Intersects({geom_name}, ST_Transform(ST_TileEnvelope({z},{x},{y}), srid)) ) select ST_AsMVT(mvtgeom.*, '{table_name}', 4096, 'geom') as mvt from mvtgeom;""" print(sql) with connection.cursor() as cursor: cursor.execute(sql) results = cursor.fetchall() return Response(results) ``` ## 3.具体的应用(整体代码) TODO \[1\] shp、geojson前端上传,后端对数据进行校验(校验坐标系-强制4326、等),然后再采用后端代码将shp、geojson导入postgres数据库中。 \[2\] jwt、permission等 \[3\] 前端如何加jwt等信息 请求数据 ## 4.参考 \[1\] 参考了这个项目的sql代码(比国内很多博客写的强太多,如果不考虑权限等,就是部署个后端、数据库,手动导入shp数据,那么直接用这个开源项目即可,前端采用mapboxgl类似的开源库加载,api访问部署后的**localhost:3000即可查看** ):[https://github.com/tobinbradley/dirt-simple-postgis-http-api/blob/master/routes/mvt.js](https://github.com/tobinbradley/dirt-simple-postgis-http-api/blob/master/routes/mvt.js "https://github.com/tobinbradley/dirt-simple-postgis-http-api/blob/master/routes/mvt.js")

相关推荐
l1t1 小时前
DeepSeek总结的 pg_regresql插件:真正可移植的 PostgreSQL 统计信息
数据库·postgresql
加农炮手Jinx5 小时前
Flutter for OpenHarmony:postgrest 直接访问 PostgreSQL 数据库的 RESTful 客户端(Supabase 核心驱动) 深度解析与鸿蒙适配指南
数据库·flutter·华为·postgresql·restful·harmonyos·鸿蒙
IvorySQL6 小时前
PostgreSQL 技术日报 (3月24日)|当 MVCC 成本被重新审视
数据库·postgresql·开源
2401_895521347 小时前
PostgreSQL_安装部署
数据库·postgresql
IvorySQL7 小时前
明晚开播|PostgreSQL 18.3 x IvorySQL 5.3:开启 AI 数据库新纪元
数据库·postgresql·开源
bearpping11 小时前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis
mpHH13 小时前
postgresql acl权限检查
数据库·postgresql·oracle
小瓦码J码15 小时前
PostgreSQL pg_stat_statements 性能分析利器(二)全表扫描导致物理IO爆炸
数据库·postgresql
AC赳赳老秦15 小时前
OpenClaw办公文档处理技能:批量转换PDF/Excel,提取数据高效办公
大数据·人工智能·python·django·去中心化·deepseek·openclaw
小陈工16 小时前
ModelEngine智能体开发实战:知识库自动生成与多Agent协作
大数据·网络·数据库·人工智能·python·django·异步