使用java產生mapbox-gl可讀的vector tile

來源:互聯網
上載者:User

標籤:link   pen   pid   inter   --   free   list   sharp   port   

概述

mapbox-gl主要資料來源來自mapbox vector tile,本文就是要闡述怎樣把postgresql中的地理空間資料轉換成vector tile,流程圖如下:

 

配置

該工程採用spring boot+maven,所以第一步少不了pom.xml配置:

<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><!-- 資料庫 --><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></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

添加墨卡托投影轉換工具類:

 

  

核心內容

解析地理資料,轉換成vector tile:

@Overridepublic byte[] getContents(String type, int x, int y, int z) {String sql = null;if (type.equals("link")){sql = "select 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_geomfromtext(?,4326)) "+ "";}try {String tile = TileUtils.parseXyz2Bound(x, y, z);List<Map<String, Object>> results = jdbc.queryForList(sql, tile);VectorTileEncoder vte = new VectorTileEncoder(4096, 16, 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關聯程式

程式已上傳github,地址為:https://github.com/lileialg/mvt

mapbox-gl解析後效果

 

使用java產生mapbox-gl可讀的vector tile

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.