2021-08-02 00:26:22 +00:00
|
|
|
/*
|
2022-01-19 10:36:44 +00:00
|
|
|
Copyright (c) 2021, MapTiler.com & OpenMapTiles contributors.
|
2021-08-02 00:26:22 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Code license: BSD 3-Clause License
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the copyright holder nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
Design license: CC-BY 4.0
|
|
|
|
|
|
|
|
See https://github.com/openmaptiles/openmaptiles/blob/master/LICENSE.md for details on usage
|
|
|
|
*/
|
2021-12-23 10:42:24 +00:00
|
|
|
package com.onthegomap.planetiler.basemap.layers;
|
2021-06-14 11:04:03 +00:00
|
|
|
|
2021-12-23 10:42:24 +00:00
|
|
|
import com.onthegomap.planetiler.FeatureCollector;
|
|
|
|
import com.onthegomap.planetiler.FeatureMerge;
|
|
|
|
import com.onthegomap.planetiler.VectorTile;
|
|
|
|
import com.onthegomap.planetiler.basemap.BasemapProfile;
|
|
|
|
import com.onthegomap.planetiler.basemap.generated.OpenMapTilesSchema;
|
|
|
|
import com.onthegomap.planetiler.basemap.generated.Tables;
|
|
|
|
import com.onthegomap.planetiler.config.PlanetilerConfig;
|
|
|
|
import com.onthegomap.planetiler.expression.MultiExpression;
|
|
|
|
import com.onthegomap.planetiler.geo.GeometryException;
|
|
|
|
import com.onthegomap.planetiler.reader.SourceFeature;
|
|
|
|
import com.onthegomap.planetiler.stats.Stats;
|
|
|
|
import com.onthegomap.planetiler.util.Translations;
|
|
|
|
import com.onthegomap.planetiler.util.ZoomFunction;
|
2021-06-25 11:06:55 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2021-06-14 11:04:03 +00:00
|
|
|
|
2021-08-02 00:26:22 +00:00
|
|
|
/**
|
2021-09-16 09:15:43 +00:00
|
|
|
* Defines the logic for generating map elements for natural land cover polygons like ice, sand, and forest in the
|
|
|
|
* {@code landcover} layer from source features.
|
|
|
|
* <p>
|
2022-03-09 02:08:03 +00:00
|
|
|
* This class is ported to Java from
|
|
|
|
* <a href="https://github.com/openmaptiles/openmaptiles/tree/master/layers/landcover">OpenMapTiles landcover sql
|
|
|
|
* files</a>.
|
2021-08-02 00:26:22 +00:00
|
|
|
*/
|
2021-06-25 11:06:55 +00:00
|
|
|
public class Landcover implements
|
|
|
|
OpenMapTilesSchema.Landcover,
|
2021-10-20 01:57:47 +00:00
|
|
|
BasemapProfile.NaturalEarthProcessor,
|
2021-06-25 11:06:55 +00:00
|
|
|
Tables.OsmLandcoverPolygon.Handler,
|
2021-10-20 01:57:47 +00:00
|
|
|
BasemapProfile.FeaturePostProcessor {
|
2021-06-25 11:06:55 +00:00
|
|
|
|
2021-09-16 09:15:43 +00:00
|
|
|
/*
|
|
|
|
* Large ice areas come from natural earth and the rest come from OpenStreetMap at higher zoom
|
|
|
|
* levels. At render-time, postProcess() merges polygons into larger connected area based
|
|
|
|
* on the number of points in the original area. Since postProcess() only has visibility into
|
|
|
|
* features on a single tile, process() needs to pass the number of points the original feature
|
|
|
|
* had through using a temporary "_numpoints" attribute.
|
|
|
|
*/
|
|
|
|
|
2021-06-25 11:06:55 +00:00
|
|
|
public static final ZoomFunction<Number> MIN_PIXEL_SIZE_THRESHOLDS = ZoomFunction.fromMaxZoomThresholds(Map.of(
|
|
|
|
13, 8,
|
|
|
|
10, 4,
|
|
|
|
9, 2
|
|
|
|
));
|
2021-09-16 09:15:43 +00:00
|
|
|
private static final String TEMP_NUM_POINTS_ATTR = "_numpoints";
|
2021-06-25 11:06:55 +00:00
|
|
|
private static final Set<String> WOOD_OR_FOREST = Set.of(
|
|
|
|
FieldValues.SUBCLASS_WOOD,
|
|
|
|
FieldValues.SUBCLASS_FOREST
|
|
|
|
);
|
2021-09-16 09:15:43 +00:00
|
|
|
private final MultiExpression.Index<String> classMapping;
|
2021-06-16 10:01:39 +00:00
|
|
|
|
2021-12-23 10:42:24 +00:00
|
|
|
public Landcover(Translations translations, PlanetilerConfig config, Stats stats) {
|
2021-06-25 11:06:55 +00:00
|
|
|
this.classMapping = FieldMappings.Class.index();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getClassFromSubclass(String subclass) {
|
|
|
|
return subclass == null ? null : classMapping.getOrElse(Map.of(Fields.SUBCLASS, subclass), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void processNaturalEarth(String table, SourceFeature feature,
|
|
|
|
FeatureCollector features) {
|
|
|
|
record LandcoverInfo(String subclass, int minzoom, int maxzoom) {}
|
|
|
|
LandcoverInfo info = switch (table) {
|
|
|
|
case "ne_110m_glaciated_areas" -> new LandcoverInfo(FieldValues.SUBCLASS_GLACIER, 0, 1);
|
|
|
|
case "ne_50m_glaciated_areas" -> new LandcoverInfo(FieldValues.SUBCLASS_GLACIER, 2, 4);
|
|
|
|
case "ne_10m_glaciated_areas" -> new LandcoverInfo(FieldValues.SUBCLASS_GLACIER, 5, 6);
|
|
|
|
case "ne_50m_antarctic_ice_shelves_polys" -> new LandcoverInfo("ice_shelf", 2, 4);
|
|
|
|
case "ne_10m_antarctic_ice_shelves_polys" -> new LandcoverInfo("ice_shelf", 5, 6);
|
|
|
|
default -> null;
|
|
|
|
};
|
|
|
|
if (info != null) {
|
|
|
|
String clazz = getClassFromSubclass(info.subclass);
|
|
|
|
if (clazz != null) {
|
|
|
|
features.polygon(LAYER_NAME).setBufferPixels(BUFFER_SIZE)
|
|
|
|
.setAttr(Fields.CLASS, clazz)
|
|
|
|
.setAttr(Fields.SUBCLASS, info.subclass)
|
|
|
|
.setZoomRange(info.minzoom, info.maxzoom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void process(Tables.OsmLandcoverPolygon element, FeatureCollector features) {
|
|
|
|
String subclass = element.subclass();
|
|
|
|
String clazz = getClassFromSubclass(subclass);
|
|
|
|
if (clazz != null) {
|
|
|
|
features.polygon(LAYER_NAME).setBufferPixels(BUFFER_SIZE)
|
2021-09-10 00:46:20 +00:00
|
|
|
.setMinPixelSizeOverrides(MIN_PIXEL_SIZE_THRESHOLDS)
|
2021-06-25 11:06:55 +00:00
|
|
|
.setAttr(Fields.CLASS, clazz)
|
|
|
|
.setAttr(Fields.SUBCLASS, subclass)
|
2021-09-16 09:15:43 +00:00
|
|
|
.setNumPointsAttr(TEMP_NUM_POINTS_ATTR)
|
|
|
|
.setMinZoom(WOOD_OR_FOREST.contains(subclass) ? 9 : 7);
|
2021-06-25 11:06:55 +00:00
|
|
|
}
|
2021-06-16 10:01:39 +00:00
|
|
|
}
|
2021-06-19 20:09:32 +00:00
|
|
|
|
2021-06-25 11:06:55 +00:00
|
|
|
@Override
|
2021-09-16 09:15:43 +00:00
|
|
|
public List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) throws GeometryException {
|
2021-06-25 11:06:55 +00:00
|
|
|
if (zoom < 7 || zoom > 13) {
|
|
|
|
for (var item : items) {
|
2021-09-16 09:15:43 +00:00
|
|
|
item.attrs().remove(TEMP_NUM_POINTS_ATTR);
|
2021-06-25 11:06:55 +00:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
} else { // z7-13
|
2021-09-16 09:15:43 +00:00
|
|
|
// merging only merges polygons with the same attributes, so use this temporary key
|
|
|
|
// to separate features into layers that will be merged separately
|
|
|
|
String tempGroupKey = "_group";
|
2021-09-10 00:46:20 +00:00
|
|
|
List<VectorTile.Feature> result = new ArrayList<>();
|
|
|
|
List<VectorTile.Feature> toMerge = new ArrayList<>();
|
2021-06-25 11:06:55 +00:00
|
|
|
for (var item : items) {
|
|
|
|
Map<String, Object> attrs = item.attrs();
|
2021-09-16 09:15:43 +00:00
|
|
|
Object numPointsObj = attrs.remove(TEMP_NUM_POINTS_ATTR);
|
2021-06-25 11:06:55 +00:00
|
|
|
Object subclassObj = attrs.get(Fields.SUBCLASS);
|
|
|
|
if (numPointsObj instanceof Number num && subclassObj instanceof String subclass) {
|
|
|
|
long numPoints = num.longValue();
|
|
|
|
if (zoom >= 10) {
|
|
|
|
if (WOOD_OR_FOREST.contains(subclass) && numPoints < 300) {
|
2022-01-19 10:36:44 +00:00
|
|
|
attrs.put(tempGroupKey, "<300");
|
2021-06-25 11:06:55 +00:00
|
|
|
toMerge.add(item);
|
|
|
|
} else { // don't merge
|
|
|
|
result.add(item);
|
|
|
|
}
|
|
|
|
} else if (zoom == 9) {
|
|
|
|
if (WOOD_OR_FOREST.contains(subclass)) {
|
2022-01-19 10:36:44 +00:00
|
|
|
attrs.put(tempGroupKey, numPoints < 300 ? "<300" : ">300");
|
2021-06-25 11:06:55 +00:00
|
|
|
toMerge.add(item);
|
|
|
|
} else { // don't merge
|
|
|
|
result.add(item);
|
|
|
|
}
|
|
|
|
} else { // zoom between 7 and 8
|
|
|
|
toMerge.add(item);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.add(item);
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 00:46:20 +00:00
|
|
|
var merged = FeatureMerge.mergeOverlappingPolygons(toMerge, 4);
|
2021-06-25 11:06:55 +00:00
|
|
|
for (var item : merged) {
|
2021-09-16 09:15:43 +00:00
|
|
|
item.attrs().remove(tempGroupKey);
|
2021-06-25 11:06:55 +00:00
|
|
|
}
|
|
|
|
result.addAll(merged);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 11:04:03 +00:00
|
|
|
}
|