Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java Mapbox Vector Tile Library for Encoding/Decoding

License

NotificationsYou must be signed in to change notification settings

sebasbaumh/mapbox-vector-tile-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeQLMaven CentraljavadocLicense

Lines of CodeSecurity RatingReliability RatingMaintainability Rating

This project allows encoding and decoding of MapBox Vector Tiles (MVT).
It is originally based onmapbox-vector-tile-java, which is unfortunatelydiscontinued and I want to thank its authors for their work.

Project goals and improvements:

  • protoc generated model for Mapbox Vector Tiles v2.1.
  • Provides MVT encoding through use of the Java Topology Suite (JTS).
  • All dependencies were upgraded to their latest version (including JTS)
  • Pull requests from the original source were integrated (52 and53)
  • Clean up of the code and optimizations (use null annotations and streamline flow)
  • Support for current JDKs
  • The license is stillApache-2.0

See also:

How to use it

Maven

There is a Maven artifact in the official Maven repository, so just add this to your Maven POM:

<dependency><groupId>io.github.sebasbaumh</groupId><artifactId>mapbox-vector-tile-java</artifactId><version>25.1.0</version></dependency>

Gradle

compile 'io.github.sebasbaumh:mapbox-vector-tile-java:25.1.0'

Overview

The version reflects the year of the release, e.g.25.1.0 is a version released in 2025.

Changes from to the originalmapbox-vector-tile-java

The API differs a bit frommapbox-vector-tile-java with the main point being a different namespace (io.github.sebasbaumh.mapbox.vectortile) as publishing a project to Maven Central requires to own that namespace.

EspeciallyJtsAdapter has been reworked and optimized. Usually you will have to move fromaddAllFeatures/toFeatures toaddFeatures instead.

There are also some changes in the class structure, so make sure check your existing code for errors or deprecation warnings. For converters and filters it is now possible to usenull values to use none/ignore them.

Usage

Encoding vector tiles

Example for encoding a JTS geometry to a vector tile asbyte[]:

// prepare helper classesGeometryFactorygeomFactory =newGeometryFactory();MvtLayerPropsmvtLayerProps =newMvtLayerProps();MvtLayerParamsmvtLayerParams =MvtLayerParams.DEFAULT;VectorTile.Tile.Layer.BuildermvtLayerBuilder =MvtUtil.newLayerBuilder("test",mvtLayerParams);// build tile envelope - 1 quadrant of the worldEnvelopetileEnvelope =newEnvelope(0d,WORLD_SIZE *.5d,0d,WORLD_SIZE *.5d);// this is the geometryorg.locationtech.jts.geom.Pointpoint =geomFactory.createPoint(newCoordinate(12,45));// encode the geometryorg.locationtech.jts.geom.GeometrytileGeom =JtsAdapter.createTileGeom(point,tileEnvelope,geomFactory,mvtLayerParams,null);// add it to the layer buildermvtLayerBuilder.addAllFeatures(JtsAdapter.addFeatures(tileGeom,mvtLayerProps,null));// finish writing of featuresMvtUtil.writeProps(mvtLayerBuilder,mvtLayerProps);VectorTile.Tile.BuildermvtBuilder =VectorTile.Tile.newBuilder();mvtBuilder.addLayers(mvtLayerBuilder.build());// build the vector tile (here as byte array)byte[]mvtData =mvtBuilder.build().toByteArray();

Overview

Reading MVTs

Per-tile geometry conversion overview:

Image of Geometry Conversion Overview

Use MvtReader.loadMvt() to load MVT data from a path or input streaminto JTS geometry. The TagKeyValueMapConverter instance will convertMVT feature tags to a Map with primitive values. The map will bestored as a JTS geometry user data object within the Geometry.

The JtsMvt object wraps the JTS Geometry with MVT layer informationand structure.

GeometryFactorygeomFactory =newGeometryFactory();JtsMvtjtsMvt =MvtReader.loadMvt(Paths.get("path/to/your.mvt"),geomFactory,newTagKeyValueMapConverter());// Allow negative-area exterior rings with classifier// (recommended for Mapbox compatibility)JtsMvtjtsMvt =MvtReader.loadMvt(Paths.get("path/to/your.mvt"),geomFactory,newTagKeyValueMapConverter(),MvtReader.RING_CLASSIFIER_V1);

Building and Writing MVTs

Per-layer geometry conversion overview:

Image of Geometry Conversion Overview

1) Create or Load JTS Geometry

Create or load any JTS Geometry that will be included in the MVT. The Geometries are assumedto be in the global/world units for your target projection. Example: meters for EPSG:3857.

2) Create Tiled JTS Geometry in MVT Coordinates

Create tiled JTS geometry with JtsAdapter#createTileGeom(). MVTs currentlydo not support feature collections so any JTS geometry collections will be flattenedto a single level.Keep in mind that MVTs use local 'screen coordinates' with inverted y-axis compared with cartesian.

IGeometryFilteracceptAllGeomFilter =geometry ->true;EnvelopetileEnvelope =newEnvelope(0d,100d,0d,100d);// TODO: Your tile extent hereMvtLayerParamslayerParams =newMvtLayerParams();// Default extentGeometrytileGeom =JtsAdapter.createTileGeom(jtsGeom,// Your geometrytileEnvelope,geomFactory,layerParams,acceptAllGeomFilter);

3) Create MVT Builder, Layers, and Features

After creating a tile's geometry in step 2, it is ready to be encoded in a MVT protobuf.

Note: Applications can perform step 2 multiple times to place geometry in separate MVT layers.

Create the VectorTile.Tile.Builder responsible for the MVT protobufbyte array. This is the top-level object for writing the MVT:

VectorTile.Tile.BuildertileBuilder =VectorTile.Tile.newBuilder();

Create an empty layer for the MVT using the MvtUtil#newLayerBuilder() utility function:

VectorTile.Tile.Layer.BuilderlayerBuilder =MvtUtil.newLayerBuilder("myLayerName",layerParams);

MVT JTS Geometry from step 2 need to be converted to MVT features.

MvtLayerProps is a supporting class for building MVT layerkey/value dictionaries. A user data converter will take JTS Geometryuser data (preserved during MVT tile geometry conversion) and convert it to MVT tags:

MvtLayerPropslayerProps =newMvtLayerProps();IUserDataConverteruserDataConverter =newUserDataKeyValueMapConverter();List<VectorTile.Tile.Feature>features =JtsAdapter.addFeatures(layerBuilder,tileGeom,layerProps,userDataConverter);

Use MvtUtil#writeProps() utility function after JtsAdapter#addFeatures() to add the key/value dictionary to theMVT layer:

MvtUtil.writeProps(layerBuilder,layerProps);

4) Write MVT

This example writes the MVT protobuf byte array to an output file.

VectorTile.Tilemvt =tileBuilder.build();try {Files.write(path,mvt.toByteArray());}catch (IOExceptione) {logger.error(e.getMessage(),e);}

Buffering Polygons Beyond MVT Extent

For polygon geometry that will be styled with outlines, it is recommended thatthe clipping area be larger than the tile extent area. This can be handled likethe example in MvtBuildTest#testBufferedPolygon(). Code example:

// Create input geometryfinalGeometryFactorygeomFactory =newGeometryFactory();finalGeometryinputGeom =buildPolygon(RANDOM,200,geomFactory);// Build tile envelope - 1 quadrant of the worldfinaldoubletileWidth =WORLD_SIZE *.5d;finaldoubletileHeight =WORLD_SIZE *.5d;finalEnvelopetileEnvelope =newEnvelope(0d,tileWidth,0d,tileHeight);// Build clip envelope - (10 * 2)% buffered area of the tile envelopefinalEnvelopeclipEnvelope =newEnvelope(tileEnvelope);finaldoublebufferWidth =tileWidth *.1f;finaldoublebufferHeight =tileHeight *.1f;clipEnvelope.expandBy(bufferWidth,bufferHeight);// Build buffered MVT tile geometryfinalTileGeomResultbufferedTileGeom =JtsAdapter.createTileGeom(inputGeom,tileEnvelope,clipEnvelope,geomFactory,DEFAULT_MVT_PARAMS,null);// Create MVT layerfinalVectorTile.Tilemvt =encodeMvt(DEFAULT_MVT_PARAMS,bufferedTileGeom);

Examples

Seetests.

How to generate VectorTile class using vector_tile.proto

If vector_tile.proto is changed in the specification, VectorTile may need to be regenerated.

Commandprotoc version should be the same version as the POM.xml dependency.

protoc --java_out=src/main/java src/main/resources/vector_tile.proto

Extra .proto config

These options were added to the .proto file:

  • syntax = "proto2";
  • option java_package = "io.github.sebasbaumh.mapbox.vectortile";
  • option java_outer_classname = "VectorTile";

Known Issues

  • Creating tile geometry with non-simple line strings that self-cross in many places will be 'noded' by JTS during an intersection operation. This results in ugly output.
  • Invalid or non-simple geometry may not work correctly with JTS operations when creating tile geometry.

About

Java Mapbox Vector Tile Library for Encoding/Decoding

Topics

Resources

License

Stars

Watchers

Forks

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp