没写错,是使用postgis计算出来矢量切片。在这之前先准备一个数据:一个GIS数据表(本例中数据为一百万的点数据,坐标:4326),并在表中添加x,y字段,方便后面的数据筛选。sql中用到了

ST_AsMVTST_AsMVTGeom

    本文中创建矢量切片很简单,就是使用下方的一个sql,运行结果如下图。接着写一个矢量切片的http服务(参考go-vtile-example,这个例子中矢量切片压缩率更高),并且使用mapbox进行前端展示(小贴士:sql中‘points’的字符串与渲染中mapbox里的source-layer一致).代码见最下方

SELECT ST_AsMVT(tile,'points') tile  FROM(
SELECT ST_AsMVTGeom(geom,ST_MakeEnvelope(100,10,125,22, 4326),4096, 0, true)
AS geom FROM grid20180322 )
AS tile where tile.geom is not null





package main

import
(
_ "github.com/lib/pq"
"database/sql"
"time"
"log"
"math"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
) func tilePathToXYZ(path string) (TileID, error) {
xyzReg := regexp.MustCompile("(?P<z>[0-9]+)/(?P<x>[0-9]+)/(?P<y>[0-9]+)")
matches := xyzReg.FindStringSubmatch(path)
if len(matches) == 0 {
return TileID{}, errors.New("Unable to parse path as tile")
}
x, err := strconv.ParseUint(matches[2], 10, 32)
if err != nil {
return TileID{}, err
}
y, err := strconv.ParseUint(matches[3], 10, 32)
if err != nil {
return TileID{}, err
}
z, err := strconv.ParseUint(matches[1], 10, 32)
if err != nil {
return TileID{}, err
}
return TileID{x: uint32(x), y: uint32(y), z: uint32(z)}, nil
}
type LngLat struct {
lng float64
lat float64
}
type TileID struct {
x uint32
y uint32
z uint32
} func tile2lon( x int, z int)(a float64) {
return float64(x) /math.Pow(2, float64(z)) * 360.0 - 180;
} func tile2lat( y int, z int)(a float64) {
n := math.Pi - (2.0 * math.Pi * float64(y)) / math.Pow(2, float64(z));
return math.Atan(math.Sinh(n))*180/math.Pi;
} func FloatToString(input_num float64) string {
// to convert a float number to a string
return strconv.FormatFloat(input_num, 'f', 6, 64)
} func loadData(xyz TileID)(a []byte){
ymax :=FloatToString(tile2lat(int(xyz.y), int(xyz.z)));
ymin := FloatToString(tile2lat(int(xyz.y+1), int(xyz.z)));
xmin := FloatToString(tile2lon(int(xyz.x), int(xyz.z)));
xmax := FloatToString(tile2lon(int(xyz.x+1), int(xyz.z)));
fmt.Println("ymax: ", ymax)
fmt.Println("ymin: ",ymin)
fmt.Println("xmin : ",xmin )
fmt.Println("xmax : ",xmax )
connStr := "dbname=xx user=xx password=xx host=localhost port=5433 sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
panic(err)
}
defer db.Close() err = db.Ping()
if err != nil {
panic(err)
} fmt.Println("Successfully connected!")
var tile []byte
s := []string{xmin,ymin,xmax,ymax}
maxmin:=strings.Join(s, ",")
s2 := []string{" where (x between", xmin,"and",xmax,") and ( y between",ymin,"and",ymax,")"}
wmaxmin:=strings.Join(s2, " ")
sql:="SELECT ST_AsMVT(tile,'points') tile FROM(SELECT ST_AsMVTGeom(w.geom,ST_MakeEnvelope("+maxmin+", 4326),4096, 0, true) AS geom FROM (select geom from grid20180322"+wmaxmin+") w) AS tile where tile.geom is not null"
rows1:= db.QueryRow(sql)
err1 := rows1.Scan(&tile)
if err1 != nil {
log.Fatal(err1)
}
fmt.Println(sql)
//defer rows1.Close()
return tile
}
func main(){
//t1 := time.Now()
mux := http.NewServeMux()
tileBase := "/tiles/"
mux.HandleFunc(tileBase, func(w http.ResponseWriter, r *http.Request) {
t2 := time.Now()
log.Printf("url: %s", r.URL.Path)
tilePart := r.URL.Path[len(tileBase):]
fmt.Println("tilePart: ", tilePart)
xyz, err := tilePathToXYZ(tilePart)
fmt.Println("xyz: ", xyz) if err != nil {
http.Error(w, "Invalid tile url", 400)
return
}
tile:=loadData(xyz) // All this APi to be requests from other domains.
w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Write(tile)
elapsed2 := time.Since(t2) fmt.Println("耗时: ", elapsed2)
})
log.Fatal(http.ListenAndServe(":8081", mux))
}
<!DOCTYPE html>
<html> <head>
<meta charset='utf-8' />
<title>Add a third party vector tile source</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='mapbox-gl.js'></script>
<link href='mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
} #map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head> <body> <div id='map'></div>
<script>
mapboxgl.accessToken = undenfined;
var tileset = 'mapbox.streets';
var map = new mapboxgl.Map({
container: 'map',
zoom: 12,
center: [109.898625809072612, 19.106708155556731],
style: 'mapbox://styles/mapbox/light-v9',
hash: false
}); map.on('load', function loaded() {
map.addSource('custom-go-vector-tile-source', {
type: 'vector',
tiles: ['http://localhost:8081/tiles/{z}/{x}/{y}']
});
map.addLayer({
id: 'background',
type: 'background',
paint: {
'background-color': 'white'
}
});
map.addLayer({
"id": "custom-go-vector-tile-layer",
"type": "circle",
"source": "custom-go-vector-tile-source",
"source-layer": "points",
paint: {
'circle-radius': {
stops: [
[8, 0.1],
[11, 0.5],
[15, 3],
[20, 20]
]
},
'circle-color': '#e74c3c',
'circle-opacity': 1
}
});
});
</script> </body> </html>

PostGIS计算矢量切片(一)--渲染数据的更多相关文章

  1. PostGIS计算矢量切片(二)--按值渲染

    方案背景     今年三月份写了一篇postgis计算矢量切片,参考了网上资料给出了一份很粗糙的相关方案(文章写的更粗糙).当时的方案中只能针对gis形状进行渲染,而不能用属性渲染.针对这个情况,本文 ...

  2. 开源方案搭建可离线的精美矢量切片地图服务-2.PostGIS+GeoServer矢量切片

    项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...

  3. 开源方案搭建可离线的精美矢量切片地图服务-3.Mapbox个性化地图定制入门

    1.简介 mapbox是一家非常牛的公司,比如像特斯拉.DJI大疆创新.孤独星球.Airbnb.GitHub.Cisco.Snap.飞猪.Keep.Bosch这些在国内外各自领域中响当当的企业都是它的 ...

  4. 开源方案搭建可离线的精美矢量切片地图服务-4.Mapbox样式设计

    项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...

  5. 开源方案搭建可离线的精美矢量切片地图服务-8.mapbox 之sprite大图图标文件生成(附源码)

    项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...

  6. 开源方案搭建可离线的精美矢量切片地图服务-7.Arcgis Pro企业级应用

    1.前言 上篇讲.pbf字体库的时候说到我们使用的字体通过Arcgis Pro 生成,Arcgis Pro样式基于Mapbox做的矢量切片地图渲染.这篇主要讲一下Arcgis Pro矢量切片生成的的具 ...

  7. 开源方案搭建可离线的精美矢量切片地图服务-6.Mapbox之.pbf字体库

    项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...

  8. 开源方案搭建可离线的精美矢量切片地图服务-5.Mapbox离线项目实现

    项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...

  9. PIE SDK矢量唯一值渲染

    1. 功能简介 图层的唯一值渲染即是根据矢量图层的某一个数值字段的属性值,按照值的不同大小设置不同的显示符号.属性数值相等的所有要素归为同一种类,即同一符号. 2. 功能实现说明 2.1. 实现思路及 ...

随机推荐

  1. rz快速上传文件到ssh服务器

    [root@k8s01 ~]# rz --helprz version 0.12.20Usage: rz [options] [filename.if.xmodem]Receive files wit ...

  2. [Swift]LeetCode92. 反转链表 II | Reverse Linked List II

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  3. MySql和Oracle数据库区别

    Oracle与mysql区别: 1.Oracle有表空间,mysql没有表空间. 2.mysql的char类型取值范围0-255字节,varchar为0-65535字节 3.oracle的char类型 ...

  4. CentOS7 Linux中通过加密grub防止黑客通过单用户系统破解root密码

    如何防止别人恶意通过单用户系统破解root密码,进入系统窃取数据? 给grub加密,不让别人通过grub进入单用户. 17.3.1  基于centos6进行grub加密 [root@63 ~]# gr ...

  5. 我们身边那些优秀的.NET开发者-

    我们身边那些优秀的.NET开发者----邹琼俊 初识大佬 非常有幸通过博客园认识了邹琼俊邹老师,他也是<ASP.NET企业级开发实战>这本书的作者,这本书的销量达到了将近九千本,在这个实体 ...

  6. BUGKU-逆向(reverse)-writeup

    目录 入门逆向 Easy_vb Easy_Re 游戏过关 Timer(阿里CTF) 逆向入门 love LoopAndLoop(阿里CTF) easy-100(LCTF) SafeBox(NJCTF) ...

  7. 分布式事务之如何基于RocketMQ的事务消息特性实现分布式系统的最终一致性?

    导读 在之前的文章中我们介绍了如何基于RocketMQ搭建生产级消息集群,以及2PC.3PC和TCC等与分布式事务相关的基本概念(没有读过的读者详见

  8. 6.Git基础-远程仓库的使用

    远程仓库是指托管在因特网或其他网络中的你的项目的版本库.比如你在GitHub中托管的代码库,就是远程仓库. 1.查看远程仓库 --  git remote  git remote 查看已经配置的远程仓 ...

  9. asp.net core 系列 2 启动Startup类介绍

    一.Startup类 ASP.NET Core 应用是一个控制台应用,它在其 Program.Main 方法中创建 Web 服务器.其中Main方法是应用的托管入口点,Main 方法调用 WebHos ...

  10. TypeError: unorderable types: str() >= int()

    1.问题描述 age=input('please enter your age') if age >=18: print('your age is',age) print('adult') el ...