Add method to remove empty keys/tags

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-05-20 10:41:08 -06:00
rodzic dc01291420
commit b23eef2302
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 60 dodań i 2 usunięć

Wyświetl plik

@ -23,6 +23,7 @@ import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.osm.AbstractPrimitive;
import org.openstreetmap.josm.data.osm.BBox;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.IPrimitive;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Relation;
@ -142,6 +143,7 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
private static synchronized void realCleanup(DataSet dataSet, Bounds bounds) {
replaceTags(dataSet);
removeCommonTags(dataSet);
removeEmptyTags(dataSet, bounds);
mergeNodes(dataSet);
cleanupDataSet(dataSet);
mergeWays(dataSet);
@ -151,6 +153,38 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
.filter(way -> !way.isDeleted()).forEach(GetDataRunnable::cleanupArtifacts);
}
/**
* Remove empty tags from primitives
*
* @param dataSet The dataset to remove tags from
* @param bounds The bounds to remove the empty tags from (performance)
*/
public static void removeEmptyTags(DataSet dataSet, Bounds bounds) {
if (bounds == null && !dataSet.getDataSourceBounds().isEmpty()) {
bounds = dataSet.getDataSourceBounds().get(0);
dataSet.getDataSourceBounds().forEach(bounds::extend);
} else if (bounds == null) {
bounds = new Bounds(-90, -180, 90, 180);
}
dataSet.searchPrimitives(bounds.toBBox()).forEach(GetDataRunnable::realRemoveEmptyTags);
}
/**
* Remove empty tags from primitives. We assume that the `getKey` implementation
* returns a new map.
*
* @param prim The primitive to remove empty tags from.
*/
private static void realRemoveEmptyTags(IPrimitive prim) {
Map<String, String> keys = prim.getKeys();
for (Map.Entry<String, String> entry : keys.entrySet()) {
if (entry.getValue() == null || entry.getValue().trim().isEmpty()) {
// The OsmPrimitives all return a new map, so this is safe.
prim.remove(entry.getKey());
}
}
}
/**
* Remove ways that have already been added to an OSM layer
*
@ -227,6 +261,30 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
final Map<Tag, Tag> replaceTags = MapWithAIPreferenceHelper.getReplacementTags().entrySet().parallelStream()
.map(entry -> new Pair<>(Tag.ofString(entry.getKey()), Tag.ofString(entry.getValue())))
.collect(Collectors.toMap(pair -> pair.a, pair -> pair.b));
replaceTags(dataSet, replaceTags);
}
/**
* Replace tags in a dataset with a set of replacement tags
*
* @param dataSet The dataset with primitives to change
* @param replaceKeys The keys to replace (does not replace values)
*/
public static void replaceKeys(DataSet dataSet, Map<String, String> replaceKeys) {
replaceKeys.entrySet().stream().filter(e -> !e.getKey().equals(e.getValue())).forEach(
e -> dataSet.allNonDeletedPrimitives().stream().filter(p -> p.hasKey(e.getKey())).forEach(p -> {
p.put(e.getValue(), p.get(e.getKey()));
p.remove(e.getKey());
}));
}
/**
* Replace tags in a dataset with a set of replacement tags
*
* @param dataSet The dataset with primitives to change
* @param map The tags to replace
*/
public static void replaceTags(DataSet dataSet, Map<Tag, Tag> replaceTags) {
replaceTags.forEach((orig, replace) -> dataSet.allNonDeletedPrimitives().parallelStream()
.filter(prim -> prim.hasTag(orig.getKey(), orig.getValue())).forEach(prim -> prim.put(replace)));
}

Wyświetl plik

@ -109,7 +109,7 @@ public class MapWithAIRemoteControlTest {
final Integer maxObj = 1;
newHandler("http://127.0.0.1:8111/mapwithai?bbox=" + getTestBBox().toStringCSV(",") + "&max_obj="
+ maxObj.toString()).handle();
Awaitility.await().atMost(Durations.ONE_SECOND)
Awaitility.await().atMost(Durations.TWO_SECONDS)
.until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());
assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());
@ -128,7 +128,7 @@ public class MapWithAIRemoteControlTest {
public void testBBox() throws Exception {
BBox temp = getTestBBox();
newHandler("http://127.0.0.1:8111/mapwithai?bbox={bbox}".replace("{bbox}", temp.toStringCSV(","))).handle();
Awaitility.await().atMost(Durations.ONE_SECOND)
Awaitility.await().atMost(Durations.TWO_SECONDS)
.until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());
assertFalse(MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());