/* Copyright (c) 2024, MapTiler.com & OpenMapTiles contributors. 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 */ package org.openmaptiles.layers; import static org.openmaptiles.layers.Transportation.highwayClass; import static org.openmaptiles.layers.Transportation.highwaySubclass; import static org.openmaptiles.layers.Transportation.isFootwayOrSteps; import static org.openmaptiles.util.Utils.*; import com.carrotsearch.hppc.LongArrayList; import com.carrotsearch.hppc.LongByteMap; import com.carrotsearch.hppc.LongHashSet; import com.carrotsearch.hppc.LongSet; import com.onthegomap.planetiler.FeatureCollector; import com.onthegomap.planetiler.FeatureMerge; import com.onthegomap.planetiler.ForwardingProfile; import com.onthegomap.planetiler.VectorTile; import com.onthegomap.planetiler.collection.Hppc; import com.onthegomap.planetiler.config.PlanetilerConfig; import com.onthegomap.planetiler.reader.osm.OsmElement; import com.onthegomap.planetiler.stats.Stats; import com.onthegomap.planetiler.util.Parse; import com.onthegomap.planetiler.util.Translations; import com.onthegomap.planetiler.util.ZoomFunction; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import org.openmaptiles.OpenMapTilesProfile; import org.openmaptiles.generated.OpenMapTilesSchema; import org.openmaptiles.generated.Tables; import org.openmaptiles.util.OmtLanguageUtils; /** * Defines the logic for generating map elements for road, shipway, rail, and path names in the {@code * transportation_name} layer from source features. *
* This class is ported to Java from
* OpenMapTiles
* transportation_name sql files.
*/
public class TransportationName implements
OpenMapTilesSchema.TransportationName,
Tables.OsmHighwayPoint.Handler,
Tables.OsmHighwayLinestring.Handler,
Tables.OsmAerialwayLinestring.Handler,
Tables.OsmShipwayLinestring.Handler,
ForwardingProfile.LayerPostProcessor,
OpenMapTilesProfile.IgnoreWikidata,
ForwardingProfile.OsmNodePreprocessor,
ForwardingProfile.OsmWayPreprocessor {
/*
* Generate road names from OSM data. Route networkType and ref are copied
* from relations that roads are a part of - except in Great Britain which
* uses a naming convention instead of relations.
*
* The goal is to make name linestrings as long as possible to give clients
* the best chance of showing road names at different zoom levels, so do not
* limit linestrings by length at process time and merge them at tile
* render-time.
*
* Any 3-way nodes and intersections break line merging so set the
* transportation_name_limit_merge argument to true to add temporary
* "is link" and "relation" keys to prevent opposite directions of a
* divided highway or on/off ramps from getting merged for main highways.
*/
// extra temp key used to group on/off-ramps separately from main highways
private static final String LINK_TEMP_KEY = "__islink";
private static final String RELATION_ID_TEMP_KEY = "__relid";
private static final ZoomFunction.MeterToPixelThresholds MIN_LENGTH = ZoomFunction.meterThresholds()
.put(6, 20_000)
.put(7, 20_000)
.put(8, 14_000)
.put(9, 8_000)
.put(10, 4_000)
.put(11, 2_000);
private final boolean brunnel;
private final boolean sizeForShield;
private final boolean limitMerge;
private final PlanetilerConfig config;
private final boolean minorRefs;
private Transportation transportation;
private final LongByteMap motorwayJunctionHighwayClasses = Hppc.newLongByteHashMap();
private final LongSet motorwayJunctionNodes = new LongHashSet();
private final Translations translations;
public TransportationName(Translations translations, PlanetilerConfig config, Stats stats) {
this.config = config;
this.translations = translations;
this.brunnel = config.arguments().getBoolean(
"transportation_name_brunnel",
"transportation_name layer: set to false to omit brunnel and help merge long highways",
false
);
this.sizeForShield = config.arguments().getBoolean(
"transportation_name_size_for_shield",
"transportation_name layer: allow road names on shorter segments (ie. they will have a shield)",
false
);
this.limitMerge = config.arguments().getBoolean(
"transportation_name_limit_merge",
"transportation_name layer: limit merge so we don't combine different relations to help merge long highways",
false
);
this.minorRefs = config.arguments().getBoolean(
"transportation_name_minor_refs",
"transportation_name layer: include name and refs from minor road networks if not present on a way",
false
);
}
public void needsTransportationLayer(Transportation transportation) {
this.transportation = transportation;
}
@Override
public void preprocessOsmNode(OsmElement.Node node) {
if (node.hasTag("highway", "motorway_junction")) {
synchronized (motorwayJunctionNodes) {
motorwayJunctionNodes.add(node.id());
}
}
}
@Override
public void preprocessOsmWay(OsmElement.Way way) {
String highway = way.getString("highway");
if (highway != null) {
HighwayClass cls = HighwayClass.from(highway);
if (cls != HighwayClass.UNKNOWN) {
LongArrayList nodes = way.nodes();
for (int i = 0; i < nodes.size(); i++) {
long node = nodes.get(i);
if (motorwayJunctionNodes.contains(node)) {
synchronized (motorwayJunctionHighwayClasses) {
byte oldValue = motorwayJunctionHighwayClasses.getOrDefault(node, HighwayClass.UNKNOWN.value);
byte newValue = cls.value;
if (newValue > oldValue) {
motorwayJunctionHighwayClasses.put(node, newValue);
}
}
}
}
}
}
}
@Override
public void process(Tables.OsmHighwayPoint element, FeatureCollector features) {
long id = element.source().id();
byte value = motorwayJunctionHighwayClasses.getOrDefault(id, (byte) -1);
if (value > 0) {
HighwayClass cls = HighwayClass.from(value);
if (cls != HighwayClass.UNKNOWN) {
String subclass = FieldValues.SUBCLASS_JUNCTION;
String ref = element.ref();
features.point(LAYER_NAME)
.setBufferPixels(BUFFER_SIZE)
.putAttrs(OmtLanguageUtils.getNames(element.source().tags(), translations))
.setAttr(Fields.REF, ref)
.setAttr(Fields.REF_LENGTH, ref != null ? ref.length() : null)
.setAttr(Fields.CLASS, highwayClass(cls.highwayValue, null, null, null))
.setAttr(Fields.SUBCLASS, subclass)
.setAttr(Fields.LAYER, nullIfLong(element.layer(), 0))
.setSortKeyDescending(element.zOrder())
.setMinZoom(10);
}
}
}
@Override
public void process(Tables.OsmHighwayLinestring element, FeatureCollector features) {
String ref = element.ref();
List