| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.Graphics2D;
|
|---|
| 4 | import java.awt.image.BufferedImage;
|
|---|
| 5 | import java.io.BufferedReader;
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.io.OutputStream;
|
|---|
| 8 | import java.io.StringWriter;
|
|---|
| 9 | import java.io.UncheckedIOException;
|
|---|
| 10 | import java.io.Writer;
|
|---|
| 11 | import java.nio.file.Files;
|
|---|
| 12 | import java.nio.file.Path;
|
|---|
| 13 | import java.nio.file.Paths;
|
|---|
| 14 | import java.time.Instant;
|
|---|
| 15 | import java.time.ZoneId;
|
|---|
| 16 | import java.time.format.DateTimeFormatter;
|
|---|
| 17 | import java.util.ArrayList;
|
|---|
| 18 | import java.util.Arrays;
|
|---|
| 19 | import java.util.Collection;
|
|---|
| 20 | import java.util.Collections;
|
|---|
| 21 | import java.util.EnumSet;
|
|---|
| 22 | import java.util.LinkedHashMap;
|
|---|
| 23 | import java.util.List;
|
|---|
| 24 | import java.util.Locale;
|
|---|
| 25 | import java.util.Map;
|
|---|
| 26 | import java.util.Optional;
|
|---|
| 27 | import java.util.Set;
|
|---|
| 28 | import java.util.regex.Matcher;
|
|---|
| 29 | import java.util.regex.Pattern;
|
|---|
| 30 | import java.util.stream.Collectors;
|
|---|
| 31 | import java.util.stream.Stream;
|
|---|
| 32 |
|
|---|
| 33 | import javax.imageio.ImageIO;
|
|---|
| 34 | import javax.json.Json;
|
|---|
| 35 | import javax.json.JsonArrayBuilder;
|
|---|
| 36 | import javax.json.JsonObjectBuilder;
|
|---|
| 37 | import javax.json.JsonWriter;
|
|---|
| 38 | import javax.json.stream.JsonGenerator;
|
|---|
| 39 |
|
|---|
| 40 | import org.openstreetmap.josm.actions.DeleteAction;
|
|---|
| 41 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 42 | import org.openstreetmap.josm.data.Preferences;
|
|---|
| 43 | import org.openstreetmap.josm.data.Version;
|
|---|
| 44 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 45 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 46 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 47 | import org.openstreetmap.josm.data.osm.Tag;
|
|---|
| 48 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 49 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
|---|
| 50 | import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
|
|---|
| 51 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
|
|---|
| 52 | import org.openstreetmap.josm.data.preferences.JosmUrls;
|
|---|
| 53 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
|---|
| 54 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
|---|
| 55 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
|---|
| 56 | import org.openstreetmap.josm.data.projection.Projections;
|
|---|
| 57 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
|---|
| 58 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
|---|
| 59 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
|---|
| 60 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
|---|
| 61 | import org.openstreetmap.josm.gui.mappaint.MultiCascade;
|
|---|
| 62 | import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory;
|
|---|
| 63 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
|
|---|
| 64 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
|---|
| 65 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
|
|---|
| 66 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
|---|
| 67 | import org.openstreetmap.josm.gui.mappaint.styleelement.AreaElement;
|
|---|
| 68 | import org.openstreetmap.josm.gui.mappaint.styleelement.LineElement;
|
|---|
| 69 | import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
|
|---|
| 70 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
|---|
| 71 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
|---|
| 72 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
|---|
| 73 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
|---|
| 74 | import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
|
|---|
| 75 | import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
|
|---|
| 76 | import org.openstreetmap.josm.io.CachedFile;
|
|---|
| 77 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 78 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 79 | import org.openstreetmap.josm.tools.Http1Client;
|
|---|
| 80 | import org.openstreetmap.josm.tools.HttpClient;
|
|---|
| 81 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 82 | import org.openstreetmap.josm.tools.OptionParser;
|
|---|
| 83 | import org.openstreetmap.josm.tools.Territories;
|
|---|
| 84 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 85 | import org.xml.sax.SAXException;
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Extracts tag information for the taginfo project.
|
|---|
| 89 | * <p>
|
|---|
| 90 | * Run from the base directory of a JOSM checkout:
|
|---|
| 91 | * <p>
|
|---|
| 92 | * java -cp dist/josm-custom.jar TagInfoExtract --type mappaint
|
|---|
| 93 | * java -cp dist/josm-custom.jar TagInfoExtract --type presets
|
|---|
| 94 | * java -cp dist/josm-custom.jar TagInfoExtract --type external_presets
|
|---|
| 95 | */
|
|---|
| 96 | public class TagInfoExtract {
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Main method.
|
|---|
| 100 | * @param args Main program arguments
|
|---|
| 101 | * @throws Exception if any error occurs
|
|---|
| 102 | */
|
|---|
| 103 | public static void main(String[] args) throws Exception {
|
|---|
| 104 | HttpClient.setFactory(Http1Client::new);
|
|---|
| 105 | TagInfoExtract script = new TagInfoExtract();
|
|---|
| 106 | script.parseCommandLineArguments(args);
|
|---|
| 107 | script.init();
|
|---|
| 108 | switch (script.options.mode) {
|
|---|
| 109 | case MAPPAINT:
|
|---|
| 110 | script.new StyleSheet().run();
|
|---|
| 111 | break;
|
|---|
| 112 | case PRESETS:
|
|---|
| 113 | script.new Presets().run();
|
|---|
| 114 | break;
|
|---|
| 115 | case EXTERNAL_PRESETS:
|
|---|
| 116 | script.new ExternalPresets().run();
|
|---|
| 117 | break;
|
|---|
| 118 | default:
|
|---|
| 119 | throw new IllegalStateException("Invalid type " + script.options.mode);
|
|---|
| 120 | }
|
|---|
| 121 | if (!script.options.noexit) {
|
|---|
| 122 | System.exit(0);
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | enum Mode {
|
|---|
| 127 | MAPPAINT, PRESETS, EXTERNAL_PRESETS
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | private final Options options = new Options();
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * Parse command line arguments.
|
|---|
| 134 | * @param args command line arguments
|
|---|
| 135 | */
|
|---|
| 136 | private void parseCommandLineArguments(String[] args) {
|
|---|
| 137 | if (args.length == 1 && "--help".equals(args[0])) {
|
|---|
| 138 | this.usage();
|
|---|
| 139 | }
|
|---|
| 140 | final OptionParser parser = new OptionParser(getClass().getName());
|
|---|
| 141 | parser.addArgumentParameter("type", OptionParser.OptionCount.REQUIRED, options::setMode);
|
|---|
| 142 | parser.addArgumentParameter("input", OptionParser.OptionCount.OPTIONAL, options::setInputFile);
|
|---|
| 143 | parser.addArgumentParameter("output", OptionParser.OptionCount.OPTIONAL, options::setOutputFile);
|
|---|
| 144 | parser.addArgumentParameter("imgdir", OptionParser.OptionCount.OPTIONAL, options::setImageDir);
|
|---|
| 145 | parser.addArgumentParameter("imgurlprefix", OptionParser.OptionCount.OPTIONAL, options::setImageUrlPrefix);
|
|---|
| 146 | parser.addFlagParameter("noexit", options::setNoExit);
|
|---|
| 147 | parser.addFlagParameter("help", this::usage);
|
|---|
| 148 | parser.parseOptionsOrExit(Arrays.asList(args));
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | private void usage() {
|
|---|
| 152 | System.out.println("java " + getClass().getName());
|
|---|
| 153 | System.out.println(" --type TYPE\tthe project type to be generated: " + Arrays.toString(Mode.values()));
|
|---|
| 154 | System.out.println(" --input FILE\tthe input file to use (overrides defaults for types mappaint, presets)");
|
|---|
| 155 | System.out.println(" --output FILE\tthe output file to use (defaults to STDOUT)");
|
|---|
| 156 | System.out.println(" --imgdir DIRECTORY\tthe directory to put the generated images in (default: " + options.imageDir + ")");
|
|---|
| 157 | System.out.println(" --imgurlprefix STRING\timage URLs prefix for generated image files (public path on webserver)");
|
|---|
| 158 | System.out.println(" --noexit\tdo not call System.exit(), for use from Ant script");
|
|---|
| 159 | System.out.println(" --help\tshow this help");
|
|---|
| 160 | System.exit(0);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | private static class Options {
|
|---|
| 164 | Mode mode;
|
|---|
| 165 | int josmSvnRevision = Version.getInstance().getVersion();
|
|---|
| 166 | Path baseDir = Paths.get("");
|
|---|
| 167 | Path imageDir = Paths.get("taginfo-img");
|
|---|
| 168 | String imageUrlPrefix;
|
|---|
| 169 | CachedFile inputFile;
|
|---|
| 170 | Path outputFile;
|
|---|
| 171 | boolean noexit;
|
|---|
| 172 |
|
|---|
| 173 | void setMode(String value) {
|
|---|
| 174 | mode = Mode.valueOf(value.toUpperCase(Locale.ENGLISH));
|
|---|
| 175 | switch (mode) {
|
|---|
| 176 | case MAPPAINT:
|
|---|
| 177 | inputFile = new CachedFile("resource://styles/standard/elemstyles.mapcss");
|
|---|
| 178 | break;
|
|---|
| 179 | case PRESETS:
|
|---|
| 180 | inputFile = new CachedFile("resource://data/defaultpresets.xml");
|
|---|
| 181 | break;
|
|---|
| 182 | default:
|
|---|
| 183 | inputFile = null;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void setInputFile(String value) {
|
|---|
| 188 | inputFile = new CachedFile(value);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void setOutputFile(String value) {
|
|---|
| 192 | outputFile = Paths.get(value);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | void setImageDir(String value) {
|
|---|
| 196 | imageDir = Paths.get(value);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | void setImageUrlPrefix(String value) {
|
|---|
| 200 | imageUrlPrefix = value;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | void setNoExit() {
|
|---|
| 204 | noexit = true;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * Determine full image url (can refer to JOSM or OSM repository).
|
|---|
| 209 | * @param path the image path
|
|---|
| 210 | * @return full image url
|
|---|
| 211 | */
|
|---|
| 212 | private String findImageUrl(String path) {
|
|---|
| 213 | final Path f = baseDir.resolve("resources").resolve("images").resolve(path);
|
|---|
| 214 | if (Files.exists(f)) {
|
|---|
| 215 | return "https://josm.openstreetmap.de/export/" + josmSvnRevision + "/josm/trunk/resources/images/" + path;
|
|---|
| 216 | }
|
|---|
| 217 | throw new IllegalStateException("Cannot find image url for " + path);
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | private abstract class Extractor {
|
|---|
| 222 | abstract void run() throws Exception;
|
|---|
| 223 |
|
|---|
| 224 | void writeJson(String name, String description, Iterable<TagInfoTag> tags) throws IOException {
|
|---|
| 225 | try (Writer writer = options.outputFile != null ? Files.newBufferedWriter(options.outputFile) : new StringWriter();
|
|---|
| 226 | JsonWriter json = Json
|
|---|
| 227 | .createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true))
|
|---|
| 228 | .createWriter(writer)) {
|
|---|
| 229 | JsonObjectBuilder project = Json.createObjectBuilder()
|
|---|
| 230 | .add("name", name)
|
|---|
| 231 | .add("description", description)
|
|---|
| 232 | .add("project_url", JosmUrls.getInstance().getJOSMWebsite())
|
|---|
| 233 | .add("icon_url", options.findImageUrl("logo_16x16x8.png"))
|
|---|
| 234 | .add("contact_name", "JOSM developer team")
|
|---|
| 235 | .add("contact_email", "josm-dev@openstreetmap.org");
|
|---|
| 236 | final JsonArrayBuilder jsonTags = Json.createArrayBuilder();
|
|---|
| 237 | for (TagInfoTag t : tags) {
|
|---|
| 238 | jsonTags.add(t.toJson());
|
|---|
| 239 | }
|
|---|
| 240 | json.writeObject(Json.createObjectBuilder()
|
|---|
| 241 | .add("data_format", 1)
|
|---|
| 242 | .add("data_updated", DateTimeFormatter.ofPattern("yyyyMMdd'T'hhmmss'Z'").withZone(ZoneId.of("Z")).format(Instant.now()))
|
|---|
| 243 | .add("project", project)
|
|---|
| 244 | .add("tags", jsonTags)
|
|---|
| 245 | .build());
|
|---|
| 246 | if (options.outputFile == null) {
|
|---|
| 247 | System.out.println(writer.toString());
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | private class Presets extends Extractor {
|
|---|
| 255 |
|
|---|
| 256 | @Override
|
|---|
| 257 | void run() throws IOException, OsmTransferException, SAXException {
|
|---|
| 258 | try (BufferedReader reader = options.inputFile.getContentReader()) {
|
|---|
| 259 | Collection<TaggingPreset> presets = TaggingPresetReader.readAll(reader, true);
|
|---|
| 260 | List<TagInfoTag> tags = convertPresets(presets, "", true);
|
|---|
| 261 | Logging.info("Converting {0} internal presets", tags.size());
|
|---|
| 262 | writeJson("JOSM main presets", "Tags supported by the default presets in the OSM editor JOSM", tags);
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | List<TagInfoTag> convertPresets(Iterable<TaggingPreset> presets, String descriptionPrefix, boolean addImages) {
|
|---|
| 267 | final List<TagInfoTag> tags = new ArrayList<>();
|
|---|
| 268 | final Map<Tag, TagInfoTag> optionalTags = new LinkedHashMap<>();
|
|---|
| 269 | for (TaggingPreset preset : presets) {
|
|---|
| 270 | preset.data.stream()
|
|---|
| 271 | .flatMap(item -> item instanceof KeyedItem
|
|---|
| 272 | ? Stream.of(((KeyedItem) item))
|
|---|
| 273 | : item instanceof CheckGroup
|
|---|
| 274 | ? ((CheckGroup) item).checks.stream()
|
|---|
| 275 | : Stream.empty())
|
|---|
| 276 | .forEach(item -> {
|
|---|
| 277 | for (String value : values(item)) {
|
|---|
| 278 | Set<TagInfoTag.Type> types = TagInfoTag.Type.forPresetTypes(preset.types);
|
|---|
| 279 | if (item.isKeyRequired()) {
|
|---|
| 280 | tags.add(new TagInfoTag(descriptionPrefix + preset.getName(), item.key, value, types,
|
|---|
| 281 | addImages && preset.iconName != null ? options.findImageUrl(preset.iconName) : null));
|
|---|
| 282 | } else if (Presets.class.equals(getClass())) { // not for ExternalPresets
|
|---|
| 283 | optionalTags.compute(new Tag(item.key, value), (osmTag, tagInfoTag) -> {
|
|---|
| 284 | if (tagInfoTag == null) {
|
|---|
| 285 | String description = descriptionPrefix + TagInfoTag.OPTIONAL_FOR_COUNT + ": " + preset.getName();
|
|---|
| 286 | return new TagInfoTag(description, item.key, value, types, null);
|
|---|
| 287 | } else {
|
|---|
| 288 | tagInfoTag.descriptions.add(preset.getName());
|
|---|
| 289 | tagInfoTag.objectTypes.addAll(types);
|
|---|
| 290 | return tagInfoTag;
|
|---|
| 291 | }
|
|---|
| 292 | });
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 | });
|
|---|
| 296 | }
|
|---|
| 297 | tags.addAll(optionalTags.values());
|
|---|
| 298 | return tags;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | private Collection<String> values(KeyedItem item) {
|
|---|
| 302 | final Collection<String> values = item.getValues();
|
|---|
| 303 | return values.isEmpty() || values.size() > 50 ? Collections.singleton(null) : values;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | private class ExternalPresets extends Presets {
|
|---|
| 309 |
|
|---|
| 310 | @Override
|
|---|
| 311 | void run() throws IOException, OsmTransferException, SAXException {
|
|---|
| 312 | TaggingPresetReader.setLoadIcons(false);
|
|---|
| 313 | final Collection<ExtendedSourceEntry> sources = new TaggingPresetPreference.TaggingPresetSourceEditor().loadAndGetAvailableSources();
|
|---|
| 314 | final List<TagInfoTag> tags = new ArrayList<>();
|
|---|
| 315 | for (SourceEntry source : sources) {
|
|---|
| 316 | if (source.url.startsWith("resource")) {
|
|---|
| 317 | // default presets
|
|---|
| 318 | continue;
|
|---|
| 319 | }
|
|---|
| 320 | try {
|
|---|
| 321 | Logging.info("Loading {0}", source.url);
|
|---|
| 322 | Collection<TaggingPreset> presets = TaggingPresetReader.readAll(source.url, false);
|
|---|
| 323 | final List<TagInfoTag> t = convertPresets(presets, source.title + " ", false);
|
|---|
| 324 | Logging.info("Converting {0} presets of {1}", t.size(), source.title);
|
|---|
| 325 | tags.addAll(t);
|
|---|
| 326 | } catch (Exception ex) {
|
|---|
| 327 | Logging.warn("Skipping {0} due to error", source.url);
|
|---|
| 328 | Logging.warn(ex);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | }
|
|---|
| 332 | writeJson("JOSM user presets", "Tags supported by the user contributed presets in the OSM editor JOSM", tags);
|
|---|
| 333 | }
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | private class StyleSheet extends Extractor {
|
|---|
| 337 | private MapCSSStyleSource styleSource;
|
|---|
| 338 |
|
|---|
| 339 | @Override
|
|---|
| 340 | void run() throws IOException, ParseException {
|
|---|
| 341 | init();
|
|---|
| 342 | parseStyleSheet();
|
|---|
| 343 | final List<TagInfoTag> tags = convertStyleSheet();
|
|---|
| 344 | writeJson("JOSM main mappaint style", "Tags supported by the main mappaint style in the OSM editor JOSM", tags);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /**
|
|---|
| 348 | * Read the style sheet file and parse the MapCSS code.
|
|---|
| 349 | * @throws IOException if any I/O error occurs
|
|---|
| 350 | * @throws ParseException in case of parsing error
|
|---|
| 351 | */
|
|---|
| 352 | private void parseStyleSheet() throws IOException, ParseException {
|
|---|
| 353 | try (BufferedReader reader = options.inputFile.getContentReader()) {
|
|---|
| 354 | MapCSSParser parser = new MapCSSParser(reader, MapCSSParser.LexicalState.DEFAULT);
|
|---|
| 355 | styleSource = new MapCSSStyleSource("");
|
|---|
| 356 | styleSource.url = "";
|
|---|
| 357 | parser.sheet(styleSource);
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /**
|
|---|
| 362 | * Collect all the tag from the style sheet.
|
|---|
| 363 | * @return list of taginfo tags
|
|---|
| 364 | */
|
|---|
| 365 | private List<TagInfoTag> convertStyleSheet() {
|
|---|
| 366 | return styleSource.rules.stream()
|
|---|
| 367 | .flatMap(rule -> rule.selectors.stream())
|
|---|
| 368 | .flatMap(selector -> selector.getConditions().stream())
|
|---|
| 369 | .filter(ConditionFactory.SimpleKeyValueCondition.class::isInstance)
|
|---|
| 370 | .map(ConditionFactory.SimpleKeyValueCondition.class::cast)
|
|---|
| 371 | .map(condition -> condition.asTag(null))
|
|---|
| 372 | .distinct()
|
|---|
| 373 | .map(tag -> {
|
|---|
| 374 | String iconUrl = null;
|
|---|
| 375 | final EnumSet<TagInfoTag.Type> types = EnumSet.noneOf(TagInfoTag.Type.class);
|
|---|
| 376 | Optional<String> nodeUrl = new NodeChecker(tag).findUrl(true);
|
|---|
| 377 | if (nodeUrl.isPresent()) {
|
|---|
| 378 | iconUrl = nodeUrl.get();
|
|---|
| 379 | types.add(TagInfoTag.Type.NODE);
|
|---|
| 380 | }
|
|---|
| 381 | Optional<String> wayUrl = new WayChecker(tag).findUrl(iconUrl == null);
|
|---|
| 382 | if (wayUrl.isPresent()) {
|
|---|
| 383 | if (iconUrl == null) {
|
|---|
| 384 | iconUrl = wayUrl.get();
|
|---|
| 385 | }
|
|---|
| 386 | types.add(TagInfoTag.Type.WAY);
|
|---|
| 387 | }
|
|---|
| 388 | Optional<String> areaUrl = new AreaChecker(tag).findUrl(iconUrl == null);
|
|---|
| 389 | if (areaUrl.isPresent()) {
|
|---|
| 390 | if (iconUrl == null) {
|
|---|
| 391 | iconUrl = areaUrl.get();
|
|---|
| 392 | }
|
|---|
| 393 | types.add(TagInfoTag.Type.AREA);
|
|---|
| 394 | }
|
|---|
| 395 | return new TagInfoTag(null, tag.getKey(), tag.getValue(), types, iconUrl);
|
|---|
| 396 | })
|
|---|
| 397 | .collect(Collectors.toList());
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /**
|
|---|
| 401 | * Check if a certain tag is supported by the style as node / way / area.
|
|---|
| 402 | */
|
|---|
| 403 | private abstract class Checker {
|
|---|
| 404 | private final Pattern reservedChars = Pattern.compile("[<>:\"|\\?\\*]");
|
|---|
| 405 |
|
|---|
| 406 | Checker(Tag tag) {
|
|---|
| 407 | this.tag = tag;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | Environment applyStylesheet(OsmPrimitive osm) {
|
|---|
| 411 | osm.put(tag);
|
|---|
| 412 | MultiCascade mc = new MultiCascade();
|
|---|
| 413 |
|
|---|
| 414 | Environment env = new Environment(osm, mc, null, styleSource);
|
|---|
| 415 | for (MapCSSRule r : styleSource.rules) {
|
|---|
| 416 | env.clearSelectorMatchingInformation();
|
|---|
| 417 | if (r.matches(env)) {
|
|---|
| 418 | // ignore selector range
|
|---|
| 419 | if (env.layer == null) {
|
|---|
| 420 | env.layer = "default";
|
|---|
| 421 | }
|
|---|
| 422 | r.execute(env);
|
|---|
| 423 | }
|
|---|
| 424 | }
|
|---|
| 425 | env.layer = "default";
|
|---|
| 426 | return env;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | /**
|
|---|
| 430 | * Create image file from StyleElement.
|
|---|
| 431 | * @param element style element
|
|---|
| 432 | * @param type object type
|
|---|
| 433 | * @param nc navigatable component
|
|---|
| 434 | *
|
|---|
| 435 | * @return the URL
|
|---|
| 436 | */
|
|---|
| 437 | String createImage(StyleElement element, final String type, NavigatableComponent nc) {
|
|---|
| 438 | BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
|
|---|
| 439 | Graphics2D g = img.createGraphics();
|
|---|
| 440 | g.setClip(0, 0, 16, 16);
|
|---|
| 441 | StyledMapRenderer renderer = new StyledMapRenderer(g, nc, false);
|
|---|
| 442 | renderer.getSettings(false);
|
|---|
| 443 | element.paintPrimitive(osm, MapPaintSettings.INSTANCE, renderer, false, false, false);
|
|---|
| 444 | final String imageName = type + "_" + normalize(tag.toString()) + ".png";
|
|---|
| 445 | try (OutputStream out = Files.newOutputStream(options.imageDir.resolve(imageName))) {
|
|---|
| 446 | ImageIO.write(img, "png", out);
|
|---|
| 447 | } catch (IOException e) {
|
|---|
| 448 | throw new UncheckedIOException(e);
|
|---|
| 449 | }
|
|---|
| 450 | final String baseUrl = options.imageUrlPrefix != null ? options.imageUrlPrefix : options.imageDir.toString();
|
|---|
| 451 | return baseUrl + "/" + imageName;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | /**
|
|---|
| 455 | * Normalizes tag so that it can used as a filename on all platforms, including Windows.
|
|---|
| 456 | * @param tag OSM tag, that can contain illegal path characters
|
|---|
| 457 | * @return OSM tag with all illegal path characters replaced by underscore ('_')
|
|---|
| 458 | */
|
|---|
| 459 | String normalize(String tag) {
|
|---|
| 460 | Matcher m = reservedChars.matcher(tag);
|
|---|
| 461 | return m.find() ? m.replaceAll("_") : tag;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /**
|
|---|
| 465 | * Checks, if tag is supported and find URL for image icon in this case.
|
|---|
| 466 | *
|
|---|
| 467 | * @param generateImage if true, create or find a suitable image icon and return URL,
|
|---|
| 468 | * if false, just check if tag is supported and return true or false
|
|---|
| 469 | * @return URL for image icon if tag is supported
|
|---|
| 470 | */
|
|---|
| 471 | abstract Optional<String> findUrl(boolean generateImage);
|
|---|
| 472 |
|
|---|
| 473 | protected Tag tag;
|
|---|
| 474 | protected OsmPrimitive osm;
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | private class NodeChecker extends Checker {
|
|---|
| 478 | NodeChecker(Tag tag) {
|
|---|
| 479 | super(tag);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | @Override
|
|---|
| 483 | Optional<String> findUrl(boolean generateImage) {
|
|---|
| 484 | this.osm = new Node(LatLon.ZERO);
|
|---|
| 485 | Environment env = applyStylesheet(osm);
|
|---|
| 486 | Cascade c = env.mc.getCascade("default");
|
|---|
| 487 | Object image = c.get("icon-image");
|
|---|
| 488 | if (image instanceof MapPaintStyles.IconReference && !((MapPaintStyles.IconReference) image).isDeprecatedIcon()) {
|
|---|
| 489 | return Optional.of(options.findImageUrl(((MapPaintStyles.IconReference) image).iconName));
|
|---|
| 490 | }
|
|---|
| 491 | return Optional.empty();
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | private class WayChecker extends Checker {
|
|---|
| 497 | WayChecker(Tag tag) {
|
|---|
| 498 | super(tag);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | @Override
|
|---|
| 502 | Optional<String> findUrl(boolean generateImage) {
|
|---|
| 503 | this.osm = new Way();
|
|---|
| 504 | NavigatableComponent nc = new NavigatableComponent();
|
|---|
| 505 | Node n1 = new Node(nc.getLatLon(2, 8));
|
|---|
| 506 | Node n2 = new Node(nc.getLatLon(14, 8));
|
|---|
| 507 | ((Way) osm).addNode(n1);
|
|---|
| 508 | ((Way) osm).addNode(n2);
|
|---|
| 509 | Environment env = applyStylesheet(osm);
|
|---|
| 510 | LineElement les = LineElement.createLine(env);
|
|---|
| 511 | if (les != null) {
|
|---|
| 512 | if (!generateImage) return Optional.of("");
|
|---|
| 513 | return Optional.of(createImage(les, "way", nc));
|
|---|
| 514 | }
|
|---|
| 515 | return Optional.empty();
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | private class AreaChecker extends Checker {
|
|---|
| 521 | AreaChecker(Tag tag) {
|
|---|
| 522 | super(tag);
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | @Override
|
|---|
| 526 | Optional<String> findUrl(boolean generateImage) {
|
|---|
| 527 | this.osm = new Way();
|
|---|
| 528 | NavigatableComponent nc = new NavigatableComponent();
|
|---|
| 529 | Node n1 = new Node(nc.getLatLon(2, 2));
|
|---|
| 530 | Node n2 = new Node(nc.getLatLon(14, 2));
|
|---|
| 531 | Node n3 = new Node(nc.getLatLon(14, 14));
|
|---|
| 532 | Node n4 = new Node(nc.getLatLon(2, 14));
|
|---|
| 533 | ((Way) osm).addNode(n1);
|
|---|
| 534 | ((Way) osm).addNode(n2);
|
|---|
| 535 | ((Way) osm).addNode(n3);
|
|---|
| 536 | ((Way) osm).addNode(n4);
|
|---|
| 537 | ((Way) osm).addNode(n1);
|
|---|
| 538 | Environment env = applyStylesheet(osm);
|
|---|
| 539 | AreaElement aes = AreaElement.create(env);
|
|---|
| 540 | if (aes != null) {
|
|---|
| 541 | if (!generateImage) return Optional.of("");
|
|---|
| 542 | return Optional.of(createImage(aes, "area", nc));
|
|---|
| 543 | }
|
|---|
| 544 | return Optional.empty();
|
|---|
| 545 | }
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | /**
|
|---|
| 550 | * POJO representing a <a href="https://wiki.openstreetmap.org/wiki/Taginfo/Projects">Taginfo tag</a>.
|
|---|
| 551 | */
|
|---|
| 552 | private static class TagInfoTag {
|
|---|
| 553 | static final String OPTIONAL_FOR_COUNT = "Optional for {count}";
|
|---|
| 554 | final Collection<String> descriptions = new ArrayList<>();
|
|---|
| 555 | final String key;
|
|---|
| 556 | final String value;
|
|---|
| 557 | final Set<Type> objectTypes;
|
|---|
| 558 | final String iconURL;
|
|---|
| 559 |
|
|---|
| 560 | TagInfoTag(String description, String key, String value, Set<Type> objectTypes, String iconURL) {
|
|---|
| 561 | if (description != null) {
|
|---|
| 562 | this.descriptions.add(description);
|
|---|
| 563 | }
|
|---|
| 564 | this.key = key;
|
|---|
| 565 | this.value = value;
|
|---|
| 566 | this.objectTypes = objectTypes;
|
|---|
| 567 | this.iconURL = iconURL;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | JsonObjectBuilder toJson() {
|
|---|
| 571 | final JsonObjectBuilder object = Json.createObjectBuilder();
|
|---|
| 572 | if (!descriptions.isEmpty()) {
|
|---|
| 573 | final int size = descriptions.size();
|
|---|
| 574 | object.add("description", String.join(", ", Utils.limit(descriptions, 8, "..."))
|
|---|
| 575 | .replace(OPTIONAL_FOR_COUNT, size > 3 ? "Optional for " + size : "Optional for"));
|
|---|
| 576 | }
|
|---|
| 577 | object.add("key", key);
|
|---|
| 578 | if (value != null) {
|
|---|
| 579 | object.add("value", value);
|
|---|
| 580 | }
|
|---|
| 581 | if ((!objectTypes.isEmpty())) {
|
|---|
| 582 | final JsonArrayBuilder types = Json.createArrayBuilder();
|
|---|
| 583 | objectTypes.stream().map(Enum::name).map(String::toLowerCase).forEach(types::add);
|
|---|
| 584 | object.add("object_types", types);
|
|---|
| 585 | }
|
|---|
| 586 | if (iconURL != null) {
|
|---|
| 587 | object.add("icon_url", iconURL);
|
|---|
| 588 | }
|
|---|
| 589 | return object;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | enum Type {
|
|---|
| 593 | NODE, WAY, AREA, RELATION;
|
|---|
| 594 |
|
|---|
| 595 | static TagInfoTag.Type forPresetType(TaggingPresetType type) {
|
|---|
| 596 | switch (type) {
|
|---|
| 597 | case CLOSEDWAY:
|
|---|
| 598 | return AREA;
|
|---|
| 599 | case MULTIPOLYGON:
|
|---|
| 600 | return RELATION;
|
|---|
| 601 | default:
|
|---|
| 602 | return valueOf(type.toString());
|
|---|
| 603 | }
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | static Set<TagInfoTag.Type> forPresetTypes(Set<TaggingPresetType> types) {
|
|---|
| 607 | if (types == null) {
|
|---|
| 608 | return Collections.emptySet();
|
|---|
| 609 | }
|
|---|
| 610 | return types.stream()
|
|---|
| 611 | .map(Type::forPresetType)
|
|---|
| 612 | .collect(Collectors.toCollection(() -> EnumSet.noneOf(Type.class)));
|
|---|
| 613 | }
|
|---|
| 614 | }
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | /**
|
|---|
| 618 | * Initialize the script.
|
|---|
| 619 | * @throws IOException if any I/O error occurs
|
|---|
| 620 | */
|
|---|
| 621 | private void init() throws IOException {
|
|---|
| 622 | Logging.setLogLevel(Logging.LEVEL_INFO);
|
|---|
| 623 | Preferences.main().enableSaveOnPut(false);
|
|---|
| 624 | Config.setPreferencesInstance(Preferences.main());
|
|---|
| 625 | Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
|
|---|
| 626 | Config.setUrlsProvider(JosmUrls.getInstance());
|
|---|
| 627 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857"));
|
|---|
| 628 | Path tmpdir = Files.createTempDirectory(options.baseDir, "pref");
|
|---|
| 629 | tmpdir.toFile().deleteOnExit();
|
|---|
| 630 | System.setProperty("josm.home", tmpdir.toString());
|
|---|
| 631 | DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
|
|---|
| 632 | Territories.initializeInternalData();
|
|---|
| 633 | Files.createDirectories(options.imageDir);
|
|---|
| 634 | }
|
|---|
| 635 | }
|
|---|