From 9f4271be242e04304eae2cc4e4b45ca88875fc3b Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Tue, 8 Mar 2022 21:08:03 -0500 Subject: [PATCH] Migrate to eclipse formatter to support multiple IDEs (#122) --- .../planetiler/basemap/BasemapProfile.java | 42 +- .../planetiler/basemap/Generate.java | 246 +++---- .../basemap/generated/OpenMapTilesSchema.java | 550 +++++++-------- .../planetiler/basemap/generated/Tables.java | 625 ++++++------------ .../basemap/layers/AerodromeLabel.java | 3 +- .../planetiler/basemap/layers/Aeroway.java | 7 +- .../planetiler/basemap/layers/Boundary.java | 35 +- .../planetiler/basemap/layers/Building.java | 11 +- .../basemap/layers/Housenumber.java | 8 +- .../planetiler/basemap/layers/Landcover.java | 5 +- .../planetiler/basemap/layers/Landuse.java | 7 +- .../basemap/layers/MountainPeak.java | 5 +- .../planetiler/basemap/layers/Park.java | 4 +- .../planetiler/basemap/layers/Place.java | 18 +- .../planetiler/basemap/layers/Poi.java | 19 +- .../basemap/layers/Transportation.java | 13 +- .../basemap/layers/TransportationName.java | 11 +- .../planetiler/basemap/layers/Water.java | 4 +- .../planetiler/basemap/layers/WaterName.java | 5 +- .../planetiler/basemap/layers/Waterway.java | 5 +- .../basemap/util/LanguageUtils.java | 19 +- .../basemap/layers/AbstractLayerTest.java | 4 +- .../basemap/layers/BoundaryTest.java | 84 +-- .../basemap/layers/LandcoverTest.java | 2 +- .../basemap/layers/LanduseTest.java | 10 +- .../planetiler/basemap/layers/ParkTest.java | 2 +- .../planetiler/basemap/layers/WaterTest.java | 4 +- 27 files changed, 743 insertions(+), 1005 deletions(-) diff --git a/src/main/java/com/onthegomap/planetiler/basemap/BasemapProfile.java b/src/main/java/com/onthegomap/planetiler/basemap/BasemapProfile.java index bcf1107..ada8a34 100644 --- a/src/main/java/com/onthegomap/planetiler/basemap/BasemapProfile.java +++ b/src/main/java/com/onthegomap/planetiler/basemap/BasemapProfile.java @@ -27,15 +27,15 @@ import java.util.List; *

* Layer implementations extend these interfaces to subscribe to elements from different sources: *

* Layers can also subscribe to notifications when we finished processing an input source by implementing * {@link FinishHandler} or post-process features in that layer before rendering the output tile by implementing @@ -118,9 +118,8 @@ public class BasemapProfile extends ForwardingProfile { return new RowDispatch(constructor.create(), handlers); }).simplify().index(); wikidataMappings = Tables.MAPPINGS - .mapResults(constructor -> - handlerMap.getOrDefault(constructor.rowClass(), List.of()).stream() - .anyMatch(handler -> !IgnoreWikidata.class.isAssignableFrom(handler.handlerClass())) + .mapResults(constructor -> handlerMap.getOrDefault(constructor.rowClass(), List.of()).stream() + .anyMatch(handler -> !IgnoreWikidata.class.isAssignableFrom(handler.handlerClass())) ).filterResults(b -> b).simplify().index(); // register a handler for all OSM elements that forwards to imposm3 "table row" handler methods @@ -149,8 +148,8 @@ public class BasemapProfile extends ForwardingProfile { if (elem instanceof OsmElement.Node) { return wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_POINT, tags), false); } else if (elem instanceof OsmElement.Way) { - return wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_POLYGON, tags), false) - || wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_LINE, tags), false); + return wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_POLYGON, tags), false) || + wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_LINE, tags), false); } else if (elem instanceof OsmElement.Relation) { return wikidataMappings.getOrElse(SimpleFeature.create(EMPTY_POLYGON, tags), false); } else { @@ -201,14 +200,13 @@ public class BasemapProfile extends ForwardingProfile { } /** - * Layers should implement this interface to subscribe to elements from natural - * earth. + * Layers should implement this interface to subscribe to elements from + * natural earth. */ public interface NaturalEarthProcessor { /** - * Process an element from {@code table} in thenatural earth - * source. + * Process an element from {@code table} in thenatural earth source. * * @see Profile#processFeature(SourceFeature, FeatureCollector) */ @@ -216,8 +214,8 @@ public class BasemapProfile extends ForwardingProfile { } /** - * Layers should implement this interface to subscribe to elements from OSM - * lake centerlines source. + * Layers should implement this interface to subscribe to elements from + * OSM lake centerlines source. */ public interface LakeCenterlineProcessor { @@ -231,8 +229,8 @@ public class BasemapProfile extends ForwardingProfile { } /** - * Layers should implement this interface to subscribe to elements from OSM - * water polygons source. + * Layers should implement this interface to subscribe to elements from + * OSM water polygons source. */ public interface OsmWaterPolygonProcessor { diff --git a/src/main/java/com/onthegomap/planetiler/basemap/Generate.java b/src/main/java/com/onthegomap/planetiler/basemap/Generate.java index c019158..1ab032a 100644 --- a/src/main/java/com/onthegomap/planetiler/basemap/Generate.java +++ b/src/main/java/com/onthegomap/planetiler/basemap/Generate.java @@ -38,14 +38,14 @@ import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; /** - * Generates code in the {@code generated} package from the OpenMapTiles schema crawled from a tag or branch in the OpenMapTiles GitHub repo. + * Generates code in the {@code generated} package from the OpenMapTiles schema crawled from a tag or branch in the + * OpenMapTiles GitHub repo. *

* {@code OpenMapTilesSchema.java} contains the output layer definitions (i.e. attributes and allowed values) so that * layer implementations in {@code layers} package can reference them instead of hard-coding. *

* {@code Tables.java} contains the imposm3 table definitions from - * mapping.yaml files in the OpenMapTiles repo. Layers in the {@code layer} package can extend the {@code Handler} + * mapping.yaml files in the OpenMapTiles repo. Layers in the {@code layer} package can extend the {@code Handler} * nested class for a table definition to "subscribe" to OSM elements that imposm3 would put in that table. *

* To run use {@code ./scripts/regenerate-openmaptiles.sh} @@ -169,8 +169,7 @@ public class Generate { emitLayerSchemaDefinitions(config.tileset, layers, packageName, output, tag); emitTableDefinitions(tables, packageName, output, tag); - LOGGER.info( - "Done generating code in 'generated' package, now run IntelliJ 'Reformat Code' operation with 'Optimize imports' and 'Cleanup code' options selected."); + LOGGER.info("Done!"); } /** Generates {@code OpenMapTilesSchema.java} */ @@ -178,56 +177,57 @@ public class Generate { Path output, String tag) throws IOException { StringBuilder schemaClass = new StringBuilder(); - schemaClass.append(""" - %s - package %s; - - import static com.onthegomap.planetiler.expression.Expression.*; - import com.onthegomap.planetiler.config.PlanetilerConfig; - import com.onthegomap.planetiler.stats.Stats; - import com.onthegomap.planetiler.expression.MultiExpression; - import com.onthegomap.planetiler.basemap.Layer; - import com.onthegomap.planetiler.util.Translations; - import java.util.List; - import java.util.Map; - import java.util.Set; - - /** - * All vector tile layer definitions, attributes, and allowed values generated from the - * OpenMapTiles vector tile schema %s. - */ - @SuppressWarnings("unused") - public class OpenMapTilesSchema { - public static final String NAME = %s; - public static final String DESCRIPTION = %s; - public static final String VERSION = %s; - public static final String ATTRIBUTION = %s; - public static final List LANGUAGES = List.of(%s); - - /** Returns a list of expected layer implementation instances from the {@code layers} package. */ - public static List createInstances(Translations translations, PlanetilerConfig config, Stats stats) { - return List.of( - %s - ); - } + schemaClass.append( """ - .formatted( - GENERATED_FILE_HEADER, - packageName, - escapeJavadoc(tag), - escapeJavadoc(tag), - Format.quote(info.name), - Format.quote(info.description), - Format.quote(info.version), - Format.quote(info.attribution), - info.languages.stream().map(Format::quote).collect(joining(", ")), - layers.stream() - .map( - l -> "new com.onthegomap.planetiler.basemap.layers.%s(translations, config, stats)" - .formatted(lowerUnderscoreToUpperCamel(l.layer.id))) - .collect(joining("," + LINE_SEPARATOR)) - .indent(6).trim() - )); + %s + package %s; + + import static com.onthegomap.planetiler.expression.Expression.*; + import com.onthegomap.planetiler.config.PlanetilerConfig; + import com.onthegomap.planetiler.stats.Stats; + import com.onthegomap.planetiler.expression.MultiExpression; + import com.onthegomap.planetiler.basemap.Layer; + import com.onthegomap.planetiler.util.Translations; + import java.util.List; + import java.util.Map; + import java.util.Set; + + /** + * All vector tile layer definitions, attributes, and allowed values generated from the + * OpenMapTiles vector tile schema %s. + */ + @SuppressWarnings("unused") + public class OpenMapTilesSchema { + public static final String NAME = %s; + public static final String DESCRIPTION = %s; + public static final String VERSION = %s; + public static final String ATTRIBUTION = %s; + public static final List LANGUAGES = List.of(%s); + + /** Returns a list of expected layer implementation instances from the {@code layers} package. */ + public static List createInstances(Translations translations, PlanetilerConfig config, Stats stats) { + return List.of( + %s + ); + } + """ + .formatted( + GENERATED_FILE_HEADER, + packageName, + escapeJavadoc(tag), + escapeJavadoc(tag), + Format.quote(info.name), + Format.quote(info.description), + Format.quote(info.version), + Format.quote(info.attribution), + info.languages.stream().map(Format::quote).collect(joining(", ")), + layers.stream() + .map( + l -> "new com.onthegomap.planetiler.basemap.layers.%s(translations, config, stats)" + .formatted(lowerUnderscoreToUpperCamel(l.layer.id))) + .collect(joining("," + LINE_SEPARATOR)) + .indent(6).trim() + )); for (var layer : layers) { String layerCode = generateCodeForLayer(tag, layer); schemaClass.append(layerCode); @@ -344,68 +344,70 @@ public class Generate { String tag) throws IOException { StringBuilder tablesClass = new StringBuilder(); - tablesClass.append(""" - %s - package %s; + tablesClass.append( + """ + %s + package %s; - import static com.onthegomap.planetiler.expression.Expression.*; + import static com.onthegomap.planetiler.expression.Expression.*; - import com.onthegomap.planetiler.expression.Expression; - import com.onthegomap.planetiler.expression.MultiExpression; - import com.onthegomap.planetiler.FeatureCollector; - import com.onthegomap.planetiler.reader.SourceFeature; - import java.util.ArrayList; - import java.util.HashMap; - import java.util.HashSet; - import java.util.List; - import java.util.Map; - import java.util.Set; + import com.onthegomap.planetiler.expression.Expression; + import com.onthegomap.planetiler.expression.MultiExpression; + import com.onthegomap.planetiler.FeatureCollector; + import com.onthegomap.planetiler.reader.SourceFeature; + import java.util.ArrayList; + import java.util.HashMap; + import java.util.HashSet; + import java.util.List; + import java.util.Map; + import java.util.Set; - /** - * OSM element parsers generated from the imposm3 table definitions - * in the OpenMapTiles vector tile schema. - * - * These filter and parse the raw OSM key/value attribute pairs on tags into records with fields that match the - * columns in the tables that imposm3 would generate. Layer implementations can "subscribe" to elements from each - * "table" but implementing the table's {@code Handler} interface and use the element's typed API to access - * attributes. - */ - @SuppressWarnings("unused") - public class Tables { - /** A parsed OSM element that would appear in a "row" of the imposm3 table. */ - public interface Row { + /** + * OSM element parsers generated from the imposm3 table definitions + * in the OpenMapTiles vector tile schema. + * + * These filter and parse the raw OSM key/value attribute pairs on tags into records with fields that match the + * columns in the tables that imposm3 would generate. Layer implementations can "subscribe" to elements from each + * "table" but implementing the table's {@code Handler} interface and use the element's typed API to access + * attributes. + */ + @SuppressWarnings("unused") + public class Tables { + /** A parsed OSM element that would appear in a "row" of the imposm3 table. */ + public interface Row { - /** Returns the original OSM element. */ - SourceFeature source(); - } + /** Returns the original OSM element. */ + SourceFeature source(); + } - /** A functional interface that the constructor of a new table row can be coerced to. */ - @FunctionalInterface - public interface Constructor { + /** A functional interface that the constructor of a new table row can be coerced to. */ + @FunctionalInterface + public interface Constructor { - Row create(SourceFeature source, String mappingKey); - } + Row create(SourceFeature source, String mappingKey); + } - /** The {@code rowClass} of an imposm3 table row and its constructor coerced to a {@link Constructor}. */ - public record RowClassAndConstructor( - Class rowClass, - Constructor create - ) {} + /** The {@code rowClass} of an imposm3 table row and its constructor coerced to a {@link Constructor}. */ + public record RowClassAndConstructor( + Class rowClass, + Constructor create + ) {} - /** A functional interface that the typed handler method that a layer implementation can be coerced to. */ - @FunctionalInterface - public interface RowHandler { + /** A functional interface that the typed handler method that a layer implementation can be coerced to. */ + @FunctionalInterface + public interface RowHandler { - /** Process a typed element according to the profile. */ - void process(T element, FeatureCollector features); - } + /** Process a typed element according to the profile. */ + void process(T element, FeatureCollector features); + } - /** The {@code handlerClass} of a layer handler and it's {@code process} method coerced to a {@link RowHandler}. */ - public record RowHandlerAndClass( - Class handlerClass, - RowHandler handler - ) {} - """.formatted(GENERATED_FILE_HEADER, packageName, escapeJavadoc(tag))); + /** The {@code handlerClass} of a layer handler and it's {@code process} method coerced to a {@link RowHandler}. */ + public record RowHandlerAndClass( + Class handlerClass, + RowHandler handler + ) {} + """ + .formatted(GENERATED_FILE_HEADER, packageName, escapeJavadoc(tag))); List classNames = new ArrayList<>(); Map fieldNameToType = new TreeMap<>(); @@ -489,17 +491,19 @@ public class Generate { )); """.formatted( classNames.stream().map( - className -> "MultiExpression.entry(new RowClassAndConstructor(%s.class, %s::new), %s.MAPPING)".formatted( - className, className, className)) + className -> "MultiExpression.entry(new RowClassAndConstructor(%s.class, %s::new), %s.MAPPING)".formatted( + className, className, className)) .collect(joining("," + LINE_SEPARATOR)).indent(2).strip() ).indent(2)); - String handlerCondition = classNames.stream().map(className -> - """ - if (handler instanceof %s.Handler typedHandler) { - result.computeIfAbsent(%s.class, cls -> new ArrayList<>()).add(new RowHandlerAndClass<>(typedHandler.getClass(), typedHandler::process)); - }""".formatted(className, className) - ).collect(joining(LINE_SEPARATOR)); + String handlerCondition = classNames.stream() + .map( + className -> """ + if (handler instanceof %s.Handler typedHandler) { + result.computeIfAbsent(%s.class, cls -> new ArrayList<>()).add(new RowHandlerAndClass<>(typedHandler.getClass(), typedHandler::process)); + }""" + .formatted(className, className) + ).collect(joining(LINE_SEPARATOR)); tablesClass.append(""" /** * Returns a map from imposm3 "table row" class to the layers that have a handler for it from a list of layer @@ -518,15 +522,15 @@ public class Generate { } /** - * Returns an {@link Expression} that implements the same logic as the Imposm3 - * Data Mapping definition for a table. + * Returns an {@link Expression} that implements the same logic as the + * Imposm3 Data Mapping definition for a table. */ static Expression parseImposm3MappingExpression(Imposm3Table table) { if (table.type_mappings != null) { return or( - table.type_mappings.entrySet().stream().map(entry -> - parseImposm3MappingExpression(entry.getKey(), entry.getValue(), table.filters) - ).toList() + table.type_mappings.entrySet().stream() + .map(entry -> parseImposm3MappingExpression(entry.getKey(), entry.getValue(), table.filters) + ).toList() ).simplify(); } else { return parseImposm3MappingExpression(table.type, table.mapping, table.filters); @@ -534,8 +538,8 @@ public class Generate { } /** - * Returns an {@link Expression} that implements the same logic as the Imposm3 - * Data Mapping filters for a table. + * Returns an {@link Expression} that implements the same logic as the + * Imposm3 Data Mapping filters for a table. */ static Expression parseImposm3MappingExpression(String type, JsonNode mapping, Imposm3Filters filters) { return and( diff --git a/src/main/java/com/onthegomap/planetiler/basemap/generated/OpenMapTilesSchema.java b/src/main/java/com/onthegomap/planetiler/basemap/generated/OpenMapTilesSchema.java index 30a69e0..f366f7e 100644 --- a/src/main/java/com/onthegomap/planetiler/basemap/generated/OpenMapTilesSchema.java +++ b/src/main/java/com/onthegomap/planetiler/basemap/generated/OpenMapTilesSchema.java @@ -37,10 +37,7 @@ See https://github.com/openmaptiles/openmaptiles/blob/master/LICENSE.md for deta package com.onthegomap.planetiler.basemap.generated; -import static com.onthegomap.planetiler.expression.Expression.FALSE; -import static com.onthegomap.planetiler.expression.Expression.and; -import static com.onthegomap.planetiler.expression.Expression.matchAny; -import static com.onthegomap.planetiler.expression.Expression.or; +import static com.onthegomap.planetiler.expression.Expression.*; import com.onthegomap.planetiler.basemap.Layer; import com.onthegomap.planetiler.config.PlanetilerConfig; @@ -57,11 +54,11 @@ import java.util.Set; */ @SuppressWarnings("unused") public class OpenMapTilesSchema { - public static final String NAME = "OpenMapTiles"; public static final String DESCRIPTION = "A tileset showcasing all layers in OpenMapTiles. https://openmaptiles.org"; public static final String VERSION = "3.13.0"; - public static final String ATTRIBUTION = "© OpenMapTiles © OpenStreetMap contributors"; + public static final String ATTRIBUTION = + "© OpenMapTiles © OpenStreetMap contributors"; public static final List LANGUAGES = List.of("am", "ar", "az", "be", "bg", "br", "bs", "ca", "co", "cs", "cy", "da", "de", "el", "en", "eo", "es", "et", "eu", "fi", "fr", "fy", "ga", "gd", "he", "hi", "hr", "hu", "hy", "id", "is", "it", "ja", "ja_kana", "ja_rm", "ja-Latn", "ja-Hira", "ka", "kk", "kn", "ko", "ko-Latn", "ku", "la", "lb", @@ -93,15 +90,15 @@ public class OpenMapTilesSchema { /** * Water polygons representing oceans and lakes. Covered watered areas are excluded (covered=yes). On low * zoom levels all water originates from Natural Earth. To get a more correct display of the south pole you should - * also style the covering ice shelves over the water. On higher zoom levels water polygons from OpenStreetMapData are used. The polygons are split into many smaller + * also style the covering ice shelves over the water. On higher zoom levels water polygons from + * OpenStreetMapData are used. The polygons are split into many smaller * polygons to improve rendering performance. This however can lead to less rendering options in clients since these * boundaries show up. So you might not be able to use border styling for ocean water features. - *

- * Generated from water.yaml + * + * Generated from + * water.yaml */ public interface Water extends Layer { - double BUFFER_SIZE = 4.0; String LAYER_NAME = "water"; @@ -112,13 +109,14 @@ public class OpenMapTilesSchema { /** Attribute names for map elements in the water layer. */ final class Fields { - /** * All water polygons from OpenStreetMapData have the class - * ocean. Water bodies with the waterway=riverbank - * or water=river tag are classified - * as river. Wet and dry docks tagged waterway=dock - * are classified as a dock. All other water bodies are classified as lake. + * ocean. Water bodies with the + * waterway=riverbank or + * water=river tag are classified as + * river. Wet and dry docks tagged + * waterway=dock are classified as + * a dock. All other water bodies are classified as lake. *

* allowed values: *