1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
8 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
9 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
10 | import org.openstreetmap.josm.data.osm.TigerUtils;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Collection of utility methods for tag conflict resolution
|
---|
14 | *
|
---|
15 | */
|
---|
16 | public final class TagConflictResolutionUtil {
|
---|
17 |
|
---|
18 | private TagConflictResolutionUtil() {
|
---|
19 | // no constructor, just static utility methods
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Normalizes the tags in the tag collection <code>tc</code> before resolving tag conflicts.
|
---|
24 | *
|
---|
25 | * Removes irrelevant tags like "created_by".
|
---|
26 | *
|
---|
27 | * For tags which are not present on at least one of the merged nodes, the empty value ""
|
---|
28 | * is added to the list of values for this tag, but only if there are at least two
|
---|
29 | * primitives with tags, and at least one tagged primitive do not have this tag.
|
---|
30 | *
|
---|
31 | * @param tc the tag collection
|
---|
32 | * @param merged the collection of merged primitives
|
---|
33 | */
|
---|
34 | public static void normalizeTagCollectionBeforeEditing(TagCollection tc, Collection<? extends OsmPrimitive> merged) {
|
---|
35 | // remove irrelevant tags
|
---|
36 | //
|
---|
37 | for(String key : OsmPrimitive.getDiscardableKeys()) {
|
---|
38 | tc.removeByKey(key);
|
---|
39 | }
|
---|
40 |
|
---|
41 | Collection<OsmPrimitive> taggedPrimitives = new ArrayList<>();
|
---|
42 | for (OsmPrimitive p: merged) {
|
---|
43 | if (p.isTagged()) {
|
---|
44 | taggedPrimitives.add(p);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | if (taggedPrimitives.size() <= 1)
|
---|
48 | return;
|
---|
49 |
|
---|
50 | for (String key: tc.getKeys()) {
|
---|
51 | // make sure the empty value is in the tag set if a tag is not present
|
---|
52 | // on all merged nodes
|
---|
53 | //
|
---|
54 | for (OsmPrimitive p: taggedPrimitives) {
|
---|
55 | if (p.get(key) == null) {
|
---|
56 | tc.add(new Tag(key, "")); // add a tag with key and empty value
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Combines tags from TIGER data
|
---|
64 | *
|
---|
65 | * @param tc the tag collection
|
---|
66 | */
|
---|
67 | public static void combineTigerTags(TagCollection tc) {
|
---|
68 | for (String key: tc.getKeys()) {
|
---|
69 | if (TigerUtils.isTigerTag(key)) {
|
---|
70 | tc.setUniqueForKey(key, TigerUtils.combineTags(key, tc.getValues(key)));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Completes tags in the tag collection <code>tc</code> with the empty value
|
---|
77 | * for each tag. If the empty value is present the tag conflict resolution dialog
|
---|
78 | * will offer an option for removing the tag and not only options for selecting
|
---|
79 | * one of the current values of the tag.
|
---|
80 | *
|
---|
81 | * @param tc the tag collection
|
---|
82 | */
|
---|
83 | public static void completeTagCollectionForEditing(TagCollection tc) {
|
---|
84 | for (String key: tc.getKeys()) {
|
---|
85 | // make sure the empty value is in the tag set such that we can delete the tag
|
---|
86 | // in the conflict dialog if necessary
|
---|
87 | //
|
---|
88 | tc.add(new Tag(key,""));
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|