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