/
MvtEncoder.java
67 lines (56 loc) · 2.52 KB
/
MvtEncoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.wdtinc.mapbox_vector_tile.adapt.jts;
import org.locationtech.jts.geom.Geometry;
import com.wdtinc.mapbox_vector_tile.VectorTile;
import com.wdtinc.mapbox_vector_tile.adapt.jts.model.JtsLayer;
import com.wdtinc.mapbox_vector_tile.adapt.jts.model.JtsMvt;
import com.wdtinc.mapbox_vector_tile.build.MvtLayerBuild;
import com.wdtinc.mapbox_vector_tile.build.MvtLayerParams;
import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps;
import java.util.Collection;
import java.util.List;
/**
* Convenience class allows easy encoding of a {@link JtsMvt} to bytes.
*/
public final class MvtEncoder {
/**
* Encode a {@link JtsMvt} to byte[] ready for writing to a file.
*
* <p>Uses {@link MvtLayerParams#DEFAULT} and {@link UserDataKeyValueMapConverter} to transform the JtsMvt.</p>
*
* @param mvt input to encode to bytes
* @return bytes ready for writing to a .mvt
* @see #encode(JtsMvt, MvtLayerParams, IUserDataConverter)
*/
public static byte[] encode(JtsMvt mvt) {
return encode(mvt, MvtLayerParams.DEFAULT, new UserDataKeyValueMapConverter());
}
/**
* Encode a {@link JtsMvt} to byte[] ready for writing to a file.
*
* @param mvt input to encode to bytes
* @param mvtLayerParams tile creation parameters
* @param userDataConverter converts {@link Geometry#userData} to MVT feature tags
* @return bytes ready for writing to a .mvt
*/
public static byte[] encode(JtsMvt mvt, MvtLayerParams mvtLayerParams, IUserDataConverter userDataConverter) {
// Build MVT
final VectorTile.Tile.Builder tileBuilder = VectorTile.Tile.newBuilder();
for(JtsLayer layer : mvt.getLayers()) {
final Collection<Geometry> layerGeoms = layer.getGeometries();
// Create MVT layer
final VectorTile.Tile.Layer.Builder layerBuilder =
MvtLayerBuild.newLayerBuilder(layer.getName(), mvtLayerParams);
final MvtLayerProps layerProps = new MvtLayerProps();
// MVT tile geometry to MVT features
final List<VectorTile.Tile.Feature> features = JtsAdapter.toFeatures(
layerGeoms, layerProps, userDataConverter);
layerBuilder.addAllFeatures(features);
MvtLayerBuild.writeProps(layerBuilder, layerProps);
// Build MVT layer
final VectorTile.Tile.Layer vtl = layerBuilder.build();
tileBuilder.addLayers(vtl);
}
// Build MVT
return tileBuilder.build().toByteArray();
}
}