vtm-mvt module with new MVT tile decoder (#481)

This commit is contained in:
Robin
2018-01-12 10:02:04 +11:00
committed by Emux
parent 5093f292ba
commit d049d378dd
15 changed files with 484 additions and 2 deletions

17
vtm-mvt/build.gradle Normal file
View File

@@ -0,0 +1,17 @@
apply plugin: 'java-library'
apply plugin: 'maven'
dependencies {
api project(':vtm')
api 'com.wdtinc:mapbox-vector-tile:2.0.0'
}
sourceSets {
main.java.srcDirs = ['src']
}
if (project.hasProperty("SONATYPE_USERNAME")) {
afterEvaluate {
project.apply from: "${rootProject.projectDir}/deploy.gradle"
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016-2017 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.tiling.source.mvt;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
public class MapzenMvtTileSource extends UrlTileSource {
private final static String DEFAULT_URL = "https://tile.mapzen.com/mapzen/vector/v1/all";
private final static String DEFAULT_PATH = "/{Z}/{X}/{Y}.mvt";
public static class Builder<T extends Builder<T>> extends UrlTileSource.Builder<T> {
private String locale = "";
public Builder() {
super(DEFAULT_URL, DEFAULT_PATH, 1, 17);
keyName("api_key");
}
public T locale(String locale) {
this.locale = locale;
return self();
}
public MapzenMvtTileSource build() {
return new MapzenMvtTileSource(this);
}
}
@SuppressWarnings("rawtypes")
public static Builder<?> builder() {
return new Builder();
}
private final String locale;
public MapzenMvtTileSource(Builder<?> builder) {
super(builder);
this.locale = builder.locale;
}
public MapzenMvtTileSource() {
this(builder());
}
public MapzenMvtTileSource(String urlString) {
this(builder().url(urlString));
}
@Override
public ITileDataSource getDataSource() {
return new UrlTileDataSource(this, new MvtTileDecoder(locale), getHttpEngine());
}
}

View File

@@ -0,0 +1,181 @@
/*
* Copyright 2014 Hannes Janetzek
* Copyright 2017 devemux86
* Copyright 2018 boldtrn
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.tiling.source.mvt;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.wdtinc.mapbox_vector_tile.adapt.jts.MvtReader;
import com.wdtinc.mapbox_vector_tile.adapt.jts.TagKeyValueMapConverter;
import com.wdtinc.mapbox_vector_tile.adapt.jts.model.JtsLayer;
import com.wdtinc.mapbox_vector_tile.adapt.jts.model.JtsMvt;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.source.ITileDecoder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class MvtTileDecoder implements ITileDecoder {
private final String mLocale;
private final static float REF_TILE_SIZE = 4096.0f;
private float mScale;
private final GeometryFactory mGeomFactory;
private final MapElement mMapElement;
private ITileDataSink mTileDataSink;
public MvtTileDecoder() {
this("");
}
public MvtTileDecoder(String locale) {
mLocale = locale;
mGeomFactory = new GeometryFactory();
mMapElement = new MapElement();
mMapElement.layer = 5;
}
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is)
throws IOException {
mTileDataSink = sink;
mScale = REF_TILE_SIZE / Tile.SIZE;
JtsMvt jtsMvt = MvtReader.loadMvt(
is,
mGeomFactory,
new TagKeyValueMapConverter(),
MvtReader.RING_CLASSIFIER_V1);
for (JtsLayer layer : jtsMvt.getLayers()) {
for (Geometry geometry : layer.getGeometries()) {
parseGeometry(layer.getName(), geometry, (Map<String, Object>) geometry.getUserData());
}
}
return true;
}
private void parseGeometry(String layerName, Geometry geometry, Map<String, Object> tags) {
mMapElement.clear();
mMapElement.tags.clear();
parseTags(tags, layerName);
if (mMapElement.tags.size() == 0) {
return;
}
boolean err = false;
if (geometry instanceof Point) {
mMapElement.startPoints();
processCoordinateArray(geometry.getCoordinates(), false);
} else if (geometry instanceof MultiPoint) {
MultiPoint multiPoint = (MultiPoint) geometry;
for (int i = 0; i < multiPoint.getNumGeometries(); i++) {
mMapElement.startPoints();
processCoordinateArray(multiPoint.getGeometryN(i).getCoordinates(), false);
}
} else if (geometry instanceof LineString) {
processLineString((LineString) geometry);
} else if (geometry instanceof MultiLineString) {
MultiLineString multiLineString = (MultiLineString) geometry;
for (int i = 0; i < multiLineString.getNumGeometries(); i++) {
processLineString((LineString) multiLineString.getGeometryN(i));
}
} else if (geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
processPolygon(polygon);
} else if (geometry instanceof MultiPolygon) {
MultiPolygon multiPolygon = (MultiPolygon) geometry;
for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
processPolygon((Polygon) multiPolygon.getGeometryN(i));
}
} else {
err = true;
}
if (!err) {
mTileDataSink.process(mMapElement);
}
}
private void processLineString(LineString lineString) {
mMapElement.startLine();
processCoordinateArray(lineString.getCoordinates(), false);
}
private void processPolygon(Polygon polygon) {
mMapElement.startPolygon();
processCoordinateArray(polygon.getExteriorRing().getCoordinates(), true);
for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
mMapElement.startHole();
processCoordinateArray(polygon.getInteriorRingN(i).getCoordinates(), true);
}
}
private void processCoordinateArray(Coordinate[] coordinates, boolean removeLast) {
int length = removeLast ? coordinates.length - 1 : coordinates.length;
for (int i = 0; i < length; i++) {
mMapElement.addPoint((float) coordinates[i].x / mScale, (float) coordinates[i].y / mScale);
}
}
private void parseTags(Map<String, Object> map, String layerName) {
mMapElement.tags.add(new Tag("layer", layerName));
boolean hasName = false;
String fallbackName = null;
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
String val = (value instanceof String) ? (String) value : String.valueOf(value);
if (key.startsWith(Tag.KEY_NAME)) {
int len = key.length();
if (len == 4) {
fallbackName = val;
continue;
}
if (len < 7)
continue;
if (mLocale.equals(key.substring(5))) {
hasName = true;
mMapElement.tags.add(new Tag(Tag.KEY_NAME, val, false));
}
} else {
mMapElement.tags.add(new Tag(key, val));
}
}
if (!hasName && fallbackName != null)
mMapElement.tags.add(new Tag(Tag.KEY_NAME, fallbackName, false));
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016-2017 devemux86
* Copyright 2018 boldtrn
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.tiling.source.mvt;
import org.oscim.tiling.ITileDataSource;
import org.oscim.tiling.source.UrlTileDataSource;
import org.oscim.tiling.source.UrlTileSource;
public class OpenMapTilesMvtTileSource extends UrlTileSource {
private final static String DEFAULT_URL = "https://free.tilehosting.com/data/v3";
private final static String DEFAULT_PATH = "/{Z}/{X}/{Y}.pbf.pict";
public static class Builder<T extends Builder<T>> extends UrlTileSource.Builder<T> {
private String locale = "";
public Builder() {
super(DEFAULT_URL, DEFAULT_PATH, 1, 14);
}
public T locale(String locale) {
this.locale = locale;
return self();
}
public OpenMapTilesMvtTileSource build() {
return new OpenMapTilesMvtTileSource(this);
}
}
@SuppressWarnings("rawtypes")
public static Builder<?> builder() {
return new Builder();
}
private final String locale;
public OpenMapTilesMvtTileSource(Builder<?> builder) {
super(builder);
this.locale = builder.locale;
}
public OpenMapTilesMvtTileSource() {
this(builder());
}
public OpenMapTilesMvtTileSource(String urlString) {
this(builder().url(urlString));
}
@Override
public ITileDataSource getDataSource() {
return new UrlTileDataSource(this, new MvtTileDecoder(locale), getHttpEngine());
}
}