Now with downloads from the RapiD servers!

Signed-off-by: Taylor Smock <smocktaylor@gmail.com>
pull/1/head
Taylor Smock 2019-09-14 13:28:32 -06:00
rodzic 3d5a752de8
commit d5817973fe
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 9FDE4FFEF1C4CCB7
8 zmienionych plików z 176 dodań i 12 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
# JOSM RapiD Plugin
[![pipeline status](https://gitlab.com/smocktaylor/rapid/badges/master/pipeline.svg)](https://gitlab.com/smocktaylor/rapid/commits/master)
[![code coverage](https://gitlab.com/smocktaylor/rapid/badges/master/coverage.svg)](https://codecov.io/github/smocktaylor/rapid?branch=master)
[![license](https://img.shields.io/badge/license-GPLv2-blue.svg?style=flat-square)](./LICENSE)
This plugin brings RapiD information into JOSM.

Wyświetl plik

@ -15,6 +15,7 @@ plugins {
id "com.github.spotbugs" version "2.0.0"
id "org.openstreetmap.josm" version "0.6.1"
id "net.ltgt.errorprone" version "0.7.1"
id 'de.aaschmid.cpd' version '2.0'
}
// Set up Errorprone
@ -57,6 +58,7 @@ dependencies {
testImplementation("org.openstreetmap.josm:josm-unittest:"){changing=true}
testImplementation("com.github.tomakehurst:wiremock:2.21.0")
testImplementation("org.awaitility:awaitility:3.1.6")
compile 'org.apache.httpcomponents:httpclient:4.5.10'
}
// Add dependencies from ivy.xml

Wyświetl plik

@ -9,4 +9,4 @@ plugin.author = Taylor Smock <incoming+smocktaylor/rapid@incoming.gitlab.com>
plugin.class = org.openstreetmap.josm.plugins.rapid.RapiDPlugin
#plugin.icon = images/dialogs/rapid.svg
plugin.link = https://gitlab.com/smocktaylor/rapid
plugin.description = Allows the use of RapiD data in JOSM
plugin.description = Allows the use of RapiD data in JOSM (currently VERY rudimentary)

Wyświetl plik

@ -0,0 +1,20 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.rapid;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.plugins.Plugin;
import org.openstreetmap.josm.plugins.PluginInformation;
import org.openstreetmap.josm.plugins.rapid.backend.RapiDAction;
public final class RapiDPlugin extends Plugin {
/** The name of the plugin */
public static final String NAME = "RapiD";
public RapiDPlugin(PluginInformation info) {
super(info);
RapiDAction action = new RapiDAction();
MainApplication.getMenu().dataMenu.add(action);
MainApplication.getMenu().fileMenu.add(action);
}
}

Wyświetl plik

@ -1,11 +0,0 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.rapid;
import org.openstreetmap.josm.plugins.Plugin;
import org.openstreetmap.josm.plugins.PluginInformation;
public final class RapidPlugin extends Plugin {
public RapidPlugin(PluginInformation info) {
super(info);
}
}

Wyświetl plik

@ -0,0 +1,67 @@
package org.openstreetmap.josm.plugins.rapid.backend;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import org.openstreetmap.josm.data.Bounds;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.plugins.rapid.RapiDPlugin;
public class RapiDAction extends AbstractAction {
/** UID */
private static final long serialVersionUID = 8886705479253246588L;
public RapiDAction() {
super(RapiDPlugin.NAME);
}
@Override
public void actionPerformed(ActionEvent event) {
final RapiDLayer layer = getLayer();
getRapiDData(layer);
}
public RapiDLayer getLayer() {
final List<RapiDLayer> rapidLayers = MainApplication.getLayerManager().getLayersOfType(RapiDLayer.class);
RapiDLayer layer;
synchronized (this) {
if (rapidLayers.isEmpty()) {
layer = new RapiDLayer(new DataSet(), RapiDPlugin.NAME, null);
MainApplication.getLayerManager().addLayer(layer);
} else {
layer = rapidLayers.get(0);
}
}
return layer;
}
public void getRapiDData(RapiDLayer layer) {
final List<OsmDataLayer> osmLayers = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class);
for (final OsmDataLayer osmLayer : osmLayers) {
if (!osmLayer.isLocked()) {
getRapiDData(layer, osmLayer);
}
}
}
public void getRapiDData(RapiDLayer layer, OsmDataLayer osmLayer) {
final DataSet editSet = osmLayer.getDataSet();
final List<Bounds> editSetBounds = editSet.getDataSourceBounds();
final DataSet rapidSet = layer.getDataSet();
final List<Bounds> rapidBounds = rapidSet.getDataSourceBounds();
for (final Bounds bound : editSetBounds) {
if (!rapidBounds.contains(bound)) {
final DataSet newData = RapiDDataUtils.getData(bound.toBBox());
synchronized (layer) {
layer.unlock();
layer.mergeFrom(newData);
layer.lock();
}
}
}
}
}

Wyświetl plik

@ -0,0 +1,59 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.rapid.backend;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openstreetmap.josm.data.osm.BBox;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.io.IllegalDataException;
import org.openstreetmap.josm.io.OsmReader;
import org.openstreetmap.josm.plugins.rapid.RapiDPlugin;
import org.openstreetmap.josm.tools.Logging;
/**
* @author Taylor Smock
*
*/
public final class RapiDDataUtils {
private static final String RAPID_API_TOKEN = "ASZUVdYpCkd3M6ZrzjXdQzHulqRMnxdlkeBJWEKOeTUoY_Gwm9fuEd2YObLrClgDB_xfavizBsh0oDfTWTF7Zb4C";
private RapiDDataUtils() {
// Hide the constructor
}
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public static DataSet getData(BBox bbox) {
Logging.setLogLevel(Logging.LEVEL_DEBUG);
CloseableHttpResponse response = null;
DataSet dataSet = new DataSet();
try {
final URI uri = new URIBuilder().setScheme("https").setHost("www.facebook.com").setPath("/maps/ml_roads")
.setParameter("conflate_with_osm", "true").setParameter("theme", "ml_road_vector")
.setParameter("collaborator", "fbid").setParameter("token", RAPID_API_TOKEN)
.setParameter("hash", "ASYM8LPNy8k1XoJiI7A").setParameter("bbox", bbox.toStringCSV(",")).build();
final HttpGet httpget = new HttpGet(uri);
final CloseableHttpClient httpclient = HttpClients.createDefault();
Logging.info("{0}: Getting {1}", RapiDPlugin.NAME, uri.toASCIIString());
response = httpclient.execute(httpget);
dataSet = OsmReader.parseDataSet(response.getEntity().getContent(), null);
} catch (URISyntaxException | UnsupportedOperationException | IllegalDataException | IOException e) {
Logging.debug(e);
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
Logging.debug(e);
}
}
}
return dataSet;
}
}

Wyświetl plik

@ -0,0 +1,26 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.rapid.backend;
import java.io.File;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
/**
* @author Taylor Smock
*
*/
public class RapiDLayer extends OsmDataLayer {
/**
* Create a new RapiD layer
* @param data OSM data from rapid
* @param name Layer name
* @param associatedFile an associated file (can be null)
*/
public RapiDLayer(DataSet data, String name, File associatedFile) {
super(data, name, associatedFile);
this.lock();
}
}