Improve matching for sources from arbitrary data locations

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head v1.3.3
Taylor Smock 2020-02-20 16:10:33 -07:00
rodzic cdf42b0a88
commit 7f8b79acb0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
2 zmienionych plików z 36 dodań i 7 usunięć

Wyświetl plik

@ -4,6 +4,9 @@ package org.openstreetmap.josm.plugins.mapwithai.backend;
import static org.openstreetmap.josm.tools.I18n.tr; import static org.openstreetmap.josm.tools.I18n.tr;
import java.io.InputStream; import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.openstreetmap.josm.data.Bounds; import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.data.osm.DataSet;
@ -37,13 +40,29 @@ public class BoundingBoxMapWithAIDownloader extends BoundingBoxDownloader {
@Override @Override
protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException { protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
DataSet ds = OsmReaderCustom.parseDataSet(source, progressMonitor, true); DataSet ds = OsmReaderCustom.parseDataSet(source, progressMonitor, true);
GetDataRunnable.addMapWithAISourceTag(ds, MapWithAIPreferenceHelper.getMapWithAIUrl().stream() if (url != null) {
.filter(map -> map.getOrDefault("url", "no-url").equals(url)) GetDataRunnable.addMapWithAISourceTag(ds, getSourceTag(url));
.map(map -> map.getOrDefault("source", MapWithAIPlugin.NAME)).findFirst().orElse(MapWithAIPlugin.NAME)); }
GetDataRunnable.cleanup(ds, downloadArea); GetDataRunnable.cleanup(ds, downloadArea);
return ds; return ds;
} }
private static String getSourceTag(String url) {
List<Map<String, String>> possible = MapWithAIPreferenceHelper.getMapWithAIUrl().stream()
.filter(map -> url.contains(map.getOrDefault("url", null))).collect(Collectors.toList());
Map<String, String> probable = null;
long max = 0;
for (Map<String, String> check : possible) {
List<String> parameters = MapWithAIDataUtils.parseParameters(check.get("parameters"));
long count = parameters.parallelStream().filter(url::contains).count();
if (count > max || probable == null) {
max = count;
probable = check;
}
}
return probable == null ? MapWithAIPlugin.NAME : probable.getOrDefault("source", MapWithAIPlugin.NAME);
}
/** /**
* Returns the name of the download task to be displayed in the * Returns the name of the download task to be displayed in the
* {@link ProgressMonitor}. * {@link ProgressMonitor}.

Wyświetl plik

@ -245,10 +245,7 @@ public final class MapWithAIDataUtils {
if (urlInformation.containsKey("url")) { if (urlInformation.containsKey("url")) {
sb.append(urlInformation.get("url")); sb.append(urlInformation.get("url"));
if (urlInformation.containsKey("parameters")) { if (urlInformation.containsKey("parameters")) {
List<String> parameters = DataUrl.readJsonStringArraySimple(urlInformation.get("parameters")) List<String> parameters = parseParameters(urlInformation.get("parameters"));
.parallelStream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)
.filter(map -> map.getBoolean("enabled", false)).filter(map -> map.containsKey("parameter"))
.map(map -> map.getString("parameter")).collect(Collectors.toList());
if (!parameters.isEmpty()) { if (!parameters.isEmpty()) {
sb.append('&').append(String.join("&", parameters)); sb.append('&').append(String.join("&", parameters));
} }
@ -257,6 +254,19 @@ public final class MapWithAIDataUtils {
return sb.toString(); return sb.toString();
} }
/**
* Parse a parameters string and return relevant parameters
*
* @param parameters A JSON string of parameters
* @return Enabled parameters (list thereof)
*/
public static List<String> parseParameters(String parameters) {
return DataUrl.readJsonStringArraySimple(parameters).parallelStream().filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast).filter(map -> map.getBoolean("enabled", false))
.filter(map -> map.containsKey("parameter")).map(map -> map.getString("parameter"))
.collect(Collectors.toList());
}
private static Bounds bboxToBounds(BBox bbox) { private static Bounds bboxToBounds(BBox bbox) {
Bounds bound = new Bounds(bbox.getBottomRight()); Bounds bound = new Bounds(bbox.getBottomRight());
bound.extend(bbox.getTopLeft()); bound.extend(bbox.getTopLeft());