PostGIS Compute vector Tiles (i)-Unique value rendering

Source: Internet
Author: User
Tags float number postgis

???? No mistake, the vector slices are calculated using PostGIS. Before that, prepare a data: A GIS data table (point data of 1 million in this case, coordinates: 4326), and add X, y fields to the table to facilitate subsequent data filtering. The SQL uses the
ST_ASMVT and St_asmvtgeom.

???? Creating a vector slice in this article is simple, using one of the following SQL to run the results like. Then write a vector slice of the HTTP service (refer to Go-vtile-example, this example, the vector tile compression rate is higher), and use the Mapbox for the front-end display (Tips: SQL ' Points ' string is consistent with the Source-layer in Mapbox in the rendering). See the bottom of the code.

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 Mainimport (_ "GITHUB.COM/LIB/PQ" "Database/sql" "Time" "Log" "Math" "Errors" "FMT" "N Et/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], ten, +) if err! = Nil {return tileid{}, err} y, err: = StrConv. Parseuint (Matches[3], ten, +) if err! = Nil {return tileid{}, err} Z, err: = StrConv. Parseuint (Matches[1], ten, +) 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, +)} 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, er r: = 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+ ", 4    326), 4096, 0, true) as Geom from (select Geom from grid20180322 "+wmaxmin+") W) as tile where tile.geom are 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", "page") return} tile:=loaddata (XYZ)//all this APi to        Be requests from the 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 ("Time-consuming:", ELAPSED2)}) log. Fatal (http. Listenandserve (": 8081", MUX))}
<! DOCTYPE html>

PostGIS compute vector Tiles (one)-Unique value rendering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.