diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/IUserDataConverter.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/IUserDataConverter.java index 6631208baf..e3af8bcafc 100644 --- a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/IUserDataConverter.java +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/IUserDataConverter.java @@ -1,6 +1,6 @@ package com.wdtinc.mapbox_vector_tile.adapt.jts; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerProps; import net.osmand.binary.VectorTile; diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/JtsAdapter.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/JtsAdapter.java index e82213cfff..ac9b2f736d 100644 --- a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/JtsAdapter.java +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/JtsAdapter.java @@ -16,8 +16,8 @@ import com.vividsolutions.jts.geom.Polygon; import com.vividsolutions.jts.geom.TopologyException; import com.vividsolutions.jts.geom.util.AffineTransformation; import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerParams; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerParams; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerProps; import com.wdtinc.mapbox_vector_tile.encoding.GeomCmd; import com.wdtinc.mapbox_vector_tile.encoding.GeomCmdHdr; import com.wdtinc.mapbox_vector_tile.encoding.MvtUtil; diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/TileGeomResult.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/TileGeomResult.java index f74ca25392..d5eb0c3e48 100644 --- a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/TileGeomResult.java +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/TileGeomResult.java @@ -3,7 +3,7 @@ package com.wdtinc.mapbox_vector_tile.adapt.jts; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerParams; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerParams; import java.util.List; diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataIgnoreConverter.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataIgnoreConverter.java index c61f13855e..5f1dcaa85a 100644 --- a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataIgnoreConverter.java +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataIgnoreConverter.java @@ -1,6 +1,6 @@ package com.wdtinc.mapbox_vector_tile.adapt.jts; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerProps; import net.osmand.binary.VectorTile; diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataKeyValueMapConverter.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataKeyValueMapConverter.java index 2325251c2c..ff9a526645 100644 --- a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataKeyValueMapConverter.java +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/adapt/jts/UserDataKeyValueMapConverter.java @@ -1,11 +1,10 @@ package com.wdtinc.mapbox_vector_tile.adapt.jts; -import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps; +import com.wdtinc.mapbox_vector_tile.builder.MvtLayerProps; import net.osmand.binary.VectorTile; import java.util.Map; -import java.util.Objects; /** * Convert simple user data {@link Map} where the keys are {@link String} and values are {@link Object}. Supports diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerBuild.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerBuild.java new file mode 100644 index 0000000000..44dced871c --- /dev/null +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerBuild.java @@ -0,0 +1,46 @@ +package com.wdtinc.mapbox_vector_tile.builder; + +import com.wdtinc.mapbox_vector_tile.encoding.MvtValue; + +import net.osmand.binary.VectorTile; + +/** + * Utility methods for building Mapbox-Vector-Tile layers. + */ +public final class MvtLayerBuild { + + /** + * Create a new {@link com.wdtinc.mapbox_vector_tile.VectorTile.Tile.Layer.Builder} instance with + * initialized version, name, and extent metadata. + * + * @param layerName name of the layer + * @param mvtLayerParams tile creation parameters + * @return new layer builder instance with initialized metadata. + */ + public static VectorTile.Tile.Layer.Builder newLayerBuilder(String layerName, MvtLayerParams mvtLayerParams) { + final VectorTile.Tile.Layer.Builder layerBuilder = VectorTile.Tile.Layer.newBuilder(); + layerBuilder.setVersion(2); + layerBuilder.setName(layerName); + layerBuilder.setExtent(mvtLayerParams.extent); + + return layerBuilder; + } + + /** + * Modifies {@code layerBuilder} to contain properties from {@code layerProps}. + * + * @param layerBuilder layer builder to write to + * @param layerProps properties to write + */ + public static void writeProps(VectorTile.Tile.Layer.Builder layerBuilder, MvtLayerProps layerProps) { + + // Add keys + layerBuilder.addAllKeys(layerProps.getKeys()); + + // Add values + final Iterable vals = layerProps.getVals(); + for(Object o:vals){ + layerBuilder.addValues(MvtValue.toValue(o)); + } + } +} diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerParams.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerParams.java new file mode 100644 index 0000000000..b22fca2e74 --- /dev/null +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerParams.java @@ -0,0 +1,49 @@ +package com.wdtinc.mapbox_vector_tile.builder; + + +/** + * Immutable parameters collection for Mapbox-Vector-Tile creation. + */ +public final class MvtLayerParams { + + /** the resolution of the tile in 'pixel' dimensions */ + public final int tileSize; + + /** the resolution of the MVT local coordinate system */ + public final int extent; + + /** ratio of tile 'pixel' dimensions to tile extent dimensions */ + public final float ratio; + + /** + * Uses defaults: + * + * + * @see #MvtLayerParams(int, int) + */ + public MvtLayerParams() { + this(256, 4096); + } + + /** + * @param tileSize the resolution of the tile in pixel coordinates, must be > 0 + * @param extent the resolution of the MVT local coordinate system, must be > 0 + */ + public MvtLayerParams(int tileSize, int extent) { + if(tileSize <= 0) { + throw new IllegalArgumentException("tileSize must be > 0"); + } + + if(extent <= 0) { + throw new IllegalArgumentException("extent must be > 0"); + } + + + this.tileSize = tileSize; + this.extent = extent; + this.ratio = extent / tileSize; + } +} diff --git a/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerProps.java b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerProps.java new file mode 100644 index 0000000000..f22a50d34e --- /dev/null +++ b/OsmAnd-java/src/com/wdtinc/mapbox_vector_tile/builder/MvtLayerProps.java @@ -0,0 +1,68 @@ +package com.wdtinc.mapbox_vector_tile.builder; + +import com.wdtinc.mapbox_vector_tile.encoding.MvtValue; + +import java.util.*; + +/** + * Support MVT features that must reference properties by their key and value index. + */ +public final class MvtLayerProps { + private LinkedHashMap keys; + private LinkedHashMap vals; + + public MvtLayerProps() { + keys = new LinkedHashMap<>(); + vals = new LinkedHashMap<>(); + } + + public Integer keyIndex(String k) { + return keys.get(k); + } + + public Integer valueIndex(Object v) { + return vals.get(v); + } + + /** + * Add the key and return it's index code. If the key already is present, the previous + * index code is returned and no insertion is done. + * + * @param key key to add + * @return index of the key + */ + public int addKey(String key) { + Objects.requireNonNull(key); + int nextIndex = keys.size(); + final Integer mapIndex = keys.putIfAbsent(key, nextIndex); + return mapIndex == null ? nextIndex : mapIndex; + } + + /** + * Add the value and return it's index code. If the value already is present, the previous + * index code is returned and no insertion is done. If {@code value} is an unsupported type + * for encoding in a MVT, then it will not be added. + * + * @param value value to add + * @return index of the value, -1 on unsupported value types + * @see MvtValue#isValidPropValue(Object) + */ + public int addValue(Object value) { + Objects.requireNonNull(value); + if(!MvtValue.isValidPropValue(value)) { + return -1; + } + + int nextIndex = vals.size(); + final Integer mapIndex = vals.putIfAbsent(value, nextIndex); + return mapIndex == null ? nextIndex : mapIndex; + } + + public Iterable getKeys() { + return keys.keySet(); + } + + public Iterable getVals() { + return vals.keySet(); + } +}