Using Java to generate MAPBOX-GL-readable vector tiles

Source: Internet
Author: User
Tags readable

Overview

MAPBOX-GL main data source from Mapbox vector tile, this article is to explain how to convert the geospatial data in PostgreSQL into vector tile, the flowchart is as follows:

Configuration

The project uses spring Boot+maven, so the first step is the Pom.xml configuration:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >< Modelversion>4.0.0</modelversion><groupid>com.lilei.mvt</groupid><artifactid> vector-server</artifactid><version>0.0.1-snapshot</version><packaging>jar</ Packaging><name>vector-server</name><url>http://maven.apache.org</url><parent ><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-parent</ Artifactid><version>1.4.0.release</version><relativepath/> <!--lookup parent from Repository--></parent><repositories><repository><id>ecc</id><url>https ://github.com/electronicchartcentre/ecc-mvn-repo/raw/master/releases</url></repository></ Repositories><properties><projecT.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding> Utf-8</project.reporting.outputencoding><java.version>1.8</java.version></properties> <dependencies><dependency><groupid>org.springframework.boot</groupid><artifactid >spring-boot-devtools</artifactId></dependency><dependency><groupId> Org.springframework.boot</groupid><artifactid>spring-boot-starter-freemarker</artifactid> </dependency><dependency><groupid>org.webjars</groupid><artifactid>jquery</ artifactid><version>2.1.4</version></dependency><!--Database--><dependency>< groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-data-jpa</ Artifactid></dependency><dependency><groupid>org.postgresql</groupid><artifactid >postgresql</artifactid><version>42.1.1</version></dependency><dependency><groupid>org.springframework.boot</ groupid><artifactid>spring-boot-starter-test</artifactid><scope>test</scope></ dependency><dependency><groupid>com.vividsolutions</groupid><artifactid>jts</ Artifactid><version>1.13</version></dependency><dependency><groupid> com.google.protobuf</groupid><artifactid>protobuf-java</artifactid><version>2.6.1</ version></dependency><!--https://mvnrepository.com/artifact/com.google.protobuf.nano/ Protobuf-javanano--><dependency><groupid>com.google.protobuf.nano</groupid><artifactid >protobuf-javanano</artifactId><version>3.0.0-alpha-4</version></dependency>< dependency><groupid>no.ecc.vectortile</groupid><artifactid>java-vector-tile</ artifactid><version>1.2.1</version>&Lt;/dependency></dependencies><build><plugins><plugin><groupid> org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></ Plugin></plugins></build></project>

Add the Mercator Projection transformation Tool class:

  

Core content

Parse geographic data and convert it into vector tiles:

@Overridepublic byte[] getcontents (string type, int x, int y, int z) {String sql = Null;if (type.equals ("link")) {sql = "se Lect Link_pid,name,st_astext (Geom) as Geom from Beijing_link "+" where "+" st_intersects (Geom,st_geomfromtext (?, 4326)) " + "";} else if (type.equals ("poi")) {sql = "select Pid,st_astext (Geom) as Geom from poi5" + "where" + "st_intersects (Geom,st_geom Fromtext (?, 4326)) "+" ";} try {String tile = Tileutils.parsexyz2bound (x, y, z); List<map<string, object>> results = jdbc.queryforlist (sql, tile); Vectortileencoder VTE = new Vectortileencoder (4096, +, false); for (map<string, object> m:results) {String wkt = ( String) m.get ("Geom"); Geometry geom = new Wktreader (). read (wkt); Tileutils.convert2piexl (x, Y, Z, Geom), M.remove ("Geom"); Vte.addfeature (Type, M, Geom);} return Vte.encode ();} catch (ParseException e) {e.printstacktrace ();} return null;}

  

GitHub Affiliate Program

The program has been uploaded to GitHub at address: HTTPS://GITHUB.COM/LILEIALG/MVT

MAPBOX-GL Post-Parse effect

Using Java to generate MAPBOX-GL-readable vector tiles

Related Article

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.