From d3aa2af73b2916a86b818f52be27719723cd9af7 Mon Sep 17 00:00:00 2001 From: Emux Date: Fri, 11 Aug 2017 21:33:28 +0300 Subject: [PATCH] OpenMapTiles GeoJSON vector tiles #385 --- vtm-android-example/AndroidManifest.xml | 3 + .../test/OpenMapTilesGeojsonMapActivity.java | 68 + .../src/org/oscim/android/test/Samples.java | 1 + .../OpenMapTilesGeojsonTileSource.java | 108 ++ .../oscim/test/OpenMapTilesGeojsonTest.java | 65 + .../resources/assets/vtm/openmaptiles.xml | 1300 +++++++++++++++++ vtm-themes/src/org/oscim/theme/VtmThemes.java | 1 + 7 files changed, 1546 insertions(+) create mode 100644 vtm-android-example/src/org/oscim/android/test/OpenMapTilesGeojsonMapActivity.java create mode 100644 vtm-json/src/org/oscim/tiling/source/geojson/OpenMapTilesGeojsonTileSource.java create mode 100644 vtm-playground/src/org/oscim/test/OpenMapTilesGeojsonTest.java create mode 100644 vtm-themes/resources/assets/vtm/openmaptiles.xml diff --git a/vtm-android-example/AndroidManifest.xml b/vtm-android-example/AndroidManifest.xml index dd3898de..e5404b01 100644 --- a/vtm-android-example/AndroidManifest.xml +++ b/vtm-android-example/AndroidManifest.xml @@ -79,6 +79,9 @@ + diff --git a/vtm-android-example/src/org/oscim/android/test/OpenMapTilesGeojsonMapActivity.java b/vtm-android-example/src/org/oscim/android/test/OpenMapTilesGeojsonMapActivity.java new file mode 100644 index 00000000..90f1c050 --- /dev/null +++ b/vtm-android-example/src/org/oscim/android/test/OpenMapTilesGeojsonMapActivity.java @@ -0,0 +1,68 @@ +/* + * Copyright 2017 devemux86 + * + * 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 . + */ +package org.oscim.android.test; + +import android.os.Bundle; + +import org.oscim.android.cache.TileCache; +import org.oscim.layers.TileGridLayer; +import org.oscim.layers.tile.buildings.BuildingLayer; +import org.oscim.layers.tile.vector.VectorTileLayer; +import org.oscim.layers.tile.vector.labeling.LabelLayer; +import org.oscim.theme.VtmThemes; +import org.oscim.tiling.source.OkHttpEngine; +import org.oscim.tiling.source.UrlTileSource; +import org.oscim.tiling.source.geojson.OpenMapTilesGeojsonTileSource; + +public class OpenMapTilesGeojsonMapActivity extends MapActivity { + + private static final boolean USE_CACHE = false; + + private TileCache mCache; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + UrlTileSource tileSource = OpenMapTilesGeojsonTileSource.builder() + .apiKey("xxxxxxx") // Put a proper API key + .httpFactory(new OkHttpEngine.OkHttpFactory()) + //.locale("en") + .build(); + + if (USE_CACHE) { + // Cache the tiles into a local SQLite database + mCache = new TileCache(this, null, "tile.db"); + mCache.setCacheSize(512 * (1 << 10)); + tileSource.setCache(mCache); + } + + VectorTileLayer l = mMap.setBaseMap(tileSource); + mMap.setTheme(VtmThemes.OPENMAPTILES); + + mMap.layers().add(new BuildingLayer(mMap, l)); + mMap.layers().add(new LabelLayer(mMap, l)); + + mMap.layers().add(new TileGridLayer(mMap, getResources().getDisplayMetrics().density)); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (mCache != null) + mCache.dispose(); + } +} diff --git a/vtm-android-example/src/org/oscim/android/test/Samples.java b/vtm-android-example/src/org/oscim/android/test/Samples.java index c404956a..d3d4ca3f 100644 --- a/vtm-android-example/src/org/oscim/android/test/Samples.java +++ b/vtm-android-example/src/org/oscim/android/test/Samples.java @@ -45,6 +45,7 @@ public class Samples extends Activity { linearLayout.addView(createButton(MapsforgeMapActivity.class)); linearLayout.addView(createButton(MapzenMvtMapActivity.class)); linearLayout.addView(createButton(MapzenGeojsonMapActivity.class)); + linearLayout.addView(createButton(OpenMapTilesGeojsonMapActivity.class)); linearLayout.addView(createLabel("Vector Features")); linearLayout.addView(createButton(MapsforgeStyleActivity.class)); diff --git a/vtm-json/src/org/oscim/tiling/source/geojson/OpenMapTilesGeojsonTileSource.java b/vtm-json/src/org/oscim/tiling/source/geojson/OpenMapTilesGeojsonTileSource.java new file mode 100644 index 00000000..f4d9fe1f --- /dev/null +++ b/vtm-json/src/org/oscim/tiling/source/geojson/OpenMapTilesGeojsonTileSource.java @@ -0,0 +1,108 @@ +/* + * Copyright 2017 devemux86 + * + * 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 . + */ +package org.oscim.tiling.source.geojson; + +import org.oscim.core.MapElement; +import org.oscim.core.Tag; +import org.oscim.tiling.source.UrlTileSource; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class OpenMapTilesGeojsonTileSource extends GeojsonTileSource { + + private final static String DEFAULT_URL = "https://free-0.tilehosting.com/data/v3"; + private final static String DEFAULT_PATH = "/{Z}/{X}/{Y}.geojson"; + + public static class Builder> extends UrlTileSource.Builder { + private String locale = ""; + + public Builder() { + super(DEFAULT_URL, DEFAULT_PATH, 1, 17); + } + + public T locale(String locale) { + this.locale = locale; + return self(); + } + + public OpenMapTilesGeojsonTileSource build() { + return new OpenMapTilesGeojsonTileSource(this); + } + } + + @SuppressWarnings("rawtypes") + public static Builder builder() { + return new Builder(); + } + + private static Map mappings = new LinkedHashMap<>(); + + private static Tag addMapping(String key, String val) { + Tag tag = new Tag(key, val); + mappings.put(key + "=" + val, tag); + return tag; + } + + private final String locale; + + public OpenMapTilesGeojsonTileSource(Builder builder) { + super(builder); + this.locale = builder.locale; + } + + public OpenMapTilesGeojsonTileSource() { + this(builder()); + } + + public OpenMapTilesGeojsonTileSource(String urlString) { + this(builder().url(urlString)); + } + + @Override + public void decodeTags(MapElement mapElement, Map properties) { + boolean hasName = false; + String fallbackName = null; + + for (Map.Entry entry : properties.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 (locale.equals(key.substring(5))) { + hasName = true; + mapElement.tags.add(new Tag(Tag.KEY_NAME, val, false)); + } + continue; + } + + Tag tag = mappings.get(key + "=" + val); + if (tag == null) + tag = addMapping(key, val); + mapElement.tags.add(tag); + } + + if (!hasName && fallbackName != null) + mapElement.tags.add(new Tag(Tag.KEY_NAME, fallbackName, false)); + } +} diff --git a/vtm-playground/src/org/oscim/test/OpenMapTilesGeojsonTest.java b/vtm-playground/src/org/oscim/test/OpenMapTilesGeojsonTest.java new file mode 100644 index 00000000..3a09b51c --- /dev/null +++ b/vtm-playground/src/org/oscim/test/OpenMapTilesGeojsonTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2017 devemux86 + * + * 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 . + */ +package org.oscim.test; + +import org.oscim.gdx.GdxMapApp; +import org.oscim.layers.tile.buildings.BuildingLayer; +import org.oscim.layers.tile.vector.VectorTileLayer; +import org.oscim.layers.tile.vector.labeling.LabelLayer; +import org.oscim.theme.VtmThemes; +import org.oscim.tiling.source.OkHttpEngine; +import org.oscim.tiling.source.UrlTileSource; +import org.oscim.tiling.source.geojson.OpenMapTilesGeojsonTileSource; + +import java.io.File; +import java.util.UUID; + +import okhttp3.Cache; +import okhttp3.OkHttpClient; + +public class OpenMapTilesGeojsonTest extends GdxMapApp { + + private static final boolean USE_CACHE = false; + + @Override + public void createLayers() { + OkHttpClient.Builder builder = new OkHttpClient.Builder(); + if (USE_CACHE) { + // Cache the tiles into file system + File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); + int cacheSize = 10 * 1024 * 1024; // 10 MB + Cache cache = new Cache(cacheDirectory, cacheSize); + builder.cache(cache); + } + OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory(builder); + + UrlTileSource tileSource = OpenMapTilesGeojsonTileSource.builder() + .apiKey("xxxxxxx") // Put a proper API key + .httpFactory(factory) + //.locale("en") + .build(); + + VectorTileLayer l = mMap.setBaseMap(tileSource); + mMap.setTheme(VtmThemes.OPENMAPTILES); + + mMap.layers().add(new BuildingLayer(mMap, l)); + mMap.layers().add(new LabelLayer(mMap, l)); + } + + public static void main(String[] args) { + GdxMapApp.init(); + GdxMapApp.run(new OpenMapTilesGeojsonTest()); + } +} diff --git a/vtm-themes/resources/assets/vtm/openmaptiles.xml b/vtm-themes/resources/assets/vtm/openmaptiles.xml new file mode 100644 index 00000000..9b1ffb0c --- /dev/null +++ b/vtm-themes/resources/assets/vtm/openmaptiles.xml @@ -0,0 +1,1300 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/vtm-themes/src/org/oscim/theme/VtmThemes.java b/vtm-themes/src/org/oscim/theme/VtmThemes.java index bb0146d2..41dc6403 100644 --- a/vtm-themes/src/org/oscim/theme/VtmThemes.java +++ b/vtm-themes/src/org/oscim/theme/VtmThemes.java @@ -32,6 +32,7 @@ public enum VtmThemes implements ThemeFile { DEFAULT("vtm/default.xml"), MAPZEN("vtm/mapzen.xml"), NEWTRON("vtm/newtron.xml"), + OPENMAPTILES("vtm/openmaptiles.xml"), OSMAGRAY("vtm/osmagray.xml"), OSMARENDER("vtm/osmarender.xml"), TRONRENDER("vtm/tronrender.xml");