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