-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
see #15182 - make JOSM callable as standalone validator (patch by tay…
…lor.smock) git-svn-id: https://josm.openstreetmap.de/svn/trunk@18365 0c6e7542-c601-0410-84e7-c038aed88b3b
- Loading branch information
Showing
13 changed files
with
1,101 additions
and
37 deletions.
There are no files selected for viewing
471 changes: 471 additions & 0 deletions
471
src/org/openstreetmap/josm/data/validation/ValidatorCLI.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/org/openstreetmap/josm/gui/progress/CLIProgressMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.gui.progress; | ||
|
||
import static org.openstreetmap.josm.tools.I18n.tr; | ||
|
||
import java.awt.Component; | ||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.openstreetmap.josm.tools.Logging; | ||
import org.openstreetmap.josm.tools.Stopwatch; | ||
import org.openstreetmap.josm.tools.Utils; | ||
|
||
/** | ||
* CLI implementation of a {@link ProgressMonitor} | ||
* @author Taylor Smock | ||
* @since 18365 | ||
*/ | ||
public class CLIProgressMonitor extends AbstractProgressMonitor { | ||
/** The current task id */ | ||
private ProgressTaskId taskId; | ||
/** The current task title */ | ||
private String title = ""; | ||
/** The custom text (prepended with '/') */ | ||
private String customText = ""; | ||
/** The last time we updated the progress information */ | ||
private Stopwatch lastUpdateTime; | ||
/** The start time of the monitor */ | ||
private Stopwatch startTime; | ||
|
||
/** | ||
* Create a new {@link CLIProgressMonitor} | ||
*/ | ||
public CLIProgressMonitor() { | ||
super(new CancelHandler()); | ||
} | ||
|
||
@Override | ||
protected void doBeginTask() { | ||
if (!Utils.isBlank(this.title)) { | ||
Logging.info(tr("Beginning task{2}: {0}{1}", this.title, this.customText, | ||
Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""))); | ||
} | ||
this.startTime = Stopwatch.createStarted(); | ||
this.lastUpdateTime = this.startTime; | ||
} | ||
|
||
@Override | ||
protected void doFinishTask() { | ||
Logging.info(tr("Finishing task{2}: {0}{1} ({3})", this.title, this.customText, | ||
Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), this.startTime)); | ||
this.lastUpdateTime = null; | ||
} | ||
|
||
@Override | ||
protected void doSetIntermediate(boolean value) { | ||
// Do nothing for now | ||
} | ||
|
||
@Override | ||
protected void doSetTitle(String title) { | ||
this.title = Optional.ofNullable(title).orElse(""); | ||
} | ||
|
||
@Override | ||
protected void doSetCustomText(String customText) { | ||
this.customText = Optional.ofNullable(customText).map(str -> '/' + str).orElse(""); | ||
} | ||
|
||
@Override | ||
protected void updateProgress(double value) { | ||
if (this.lastUpdateTime == null || this.lastUpdateTime.elapsed() > TimeUnit.SECONDS.toMillis(10)) { | ||
this.lastUpdateTime = Stopwatch.createStarted(); | ||
Logging.info(tr("Progress of task{2}: {0}{1} is {3}% ({4})", this.title, this.customText, | ||
Optional.ofNullable(this.taskId).map(ProgressTaskId::getId).map(id -> ' ' + id).orElse(""), value * 100, this.startTime)); | ||
} | ||
} | ||
|
||
@Override | ||
public void setProgressTaskId(ProgressTaskId taskId) { | ||
this.taskId = taskId; | ||
} | ||
|
||
@Override | ||
public ProgressTaskId getProgressTaskId() { | ||
return this.taskId; | ||
} | ||
|
||
@Override | ||
public Component getWindowParent() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/org/openstreetmap/josm/io/GeoJSONMapRouletteWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.io; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonArrayBuilder; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonObjectBuilder; | ||
import javax.json.JsonValue; | ||
|
||
import org.openstreetmap.josm.data.osm.DataSet; | ||
import org.openstreetmap.josm.data.osm.OsmPrimitive; | ||
import org.openstreetmap.josm.data.osm.WaySegment; | ||
import org.openstreetmap.josm.data.validation.TestError; | ||
import org.openstreetmap.josm.tools.Logging; | ||
|
||
/** | ||
* Convert {@link TestError} to MapRoulette Tasks | ||
* @author Taylor Smock | ||
* @since 18365 | ||
*/ | ||
public class GeoJSONMapRouletteWriter extends GeoJSONWriter { | ||
|
||
/** | ||
* Constructs a new {@code GeoJSONWriter}. | ||
* @param ds The originating OSM dataset | ||
*/ | ||
public GeoJSONMapRouletteWriter(DataSet ds) { | ||
super(ds); | ||
super.setOptions(Options.RIGHT_HAND_RULE, Options.WRITE_OSM_INFORMATION); | ||
} | ||
|
||
/** | ||
* Convert a test error to a string | ||
* @param testError The test error to convert | ||
* @return The MapRoulette challenge object | ||
*/ | ||
public Optional<JsonObject> write(final TestError testError) { | ||
final JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); | ||
final JsonArrayBuilder featuresBuilder = Json.createArrayBuilder(); | ||
final JsonObjectBuilder propertiesBuilder = Json.createObjectBuilder(); | ||
propertiesBuilder.add("message", testError.getMessage()); | ||
Optional.ofNullable(testError.getDescription()).ifPresent(description -> propertiesBuilder.add("description", description)); | ||
propertiesBuilder.add("code", testError.getCode()); | ||
propertiesBuilder.add("fixable", testError.isFixable()); | ||
propertiesBuilder.add("severity", testError.getSeverity().toString()); | ||
propertiesBuilder.add("severityInteger", testError.getSeverity().getLevel()); | ||
propertiesBuilder.add("test", testError.getTester().getName()); | ||
Stream.concat(testError.getPrimitives().stream(), testError.getHighlighted().stream()).distinct().map(p -> { | ||
if (p instanceof OsmPrimitive) { | ||
return p; | ||
} else if (p instanceof WaySegment) { | ||
return ((WaySegment) p).toWay(); | ||
} | ||
Logging.trace("Could not convert {0} to an OsmPrimitive", p); | ||
return null; | ||
}).filter(Objects::nonNull).distinct().map(OsmPrimitive.class::cast) | ||
.forEach(primitive -> super.appendPrimitive(primitive, featuresBuilder)); | ||
final JsonArray featureArray = featuresBuilder.build(); | ||
final JsonArrayBuilder featuresMessageBuilder = Json.createArrayBuilder(); | ||
if (featureArray.isEmpty()) { | ||
Logging.trace("Could not generate task for {0}", testError.getMessage()); | ||
return Optional.empty(); | ||
} | ||
JsonObject primitive = featureArray.getJsonObject(0); | ||
JsonObjectBuilder replacementPrimitive = Json.createObjectBuilder(primitive); | ||
final JsonObjectBuilder properties; | ||
if (primitive.containsKey("properties") && primitive.get("properties").getValueType() == JsonValue.ValueType.OBJECT) { | ||
properties = Json.createObjectBuilder(primitive.getJsonObject("properties")); | ||
} else { | ||
properties = Json.createObjectBuilder(); | ||
} | ||
properties.addAll(propertiesBuilder); | ||
replacementPrimitive.add("properties", properties); | ||
featuresMessageBuilder.add(replacementPrimitive); | ||
for (int i = 1; i < featureArray.size(); i++) { | ||
featuresMessageBuilder.add(featureArray.get(i)); | ||
} | ||
// For now, don't add any cooperativeWork objects, as JOSM should be able to find the fixes. | ||
// This should change if the ValidatorCLI can use plugins (especially those introducing external data, like | ||
// the ElevationProfile plugin (which provides elevation data)). | ||
jsonObjectBuilder.add("type", "FeatureCollection"); | ||
jsonObjectBuilder.add("features", featuresMessageBuilder); | ||
return Optional.of(jsonObjectBuilder.build()); | ||
} | ||
} |
Oops, something went wrong.