Mass edit for formatting

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-09-26 13:28:20 -06:00
rodzic 63bc9e1d06
commit 42109b4533
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
8 zmienionych plików z 634 dodań i 635 usunięć

Wyświetl plik

@ -13,30 +13,30 @@ import org.openstreetmap.josm.plugins.rapid.backend.RapiDDataUtils;
import org.openstreetmap.josm.plugins.rapid.backend.RapiDMoveAction;
public final class RapiDPlugin extends Plugin {
/** The name of the plugin */
public static final String NAME = "RapiD";
private static String versionInfo;
/** The name of the plugin */
public static final String NAME = "RapiD";
private static String versionInfo;
private final PreferenceSetting preferences = new RapiDPreferences();
private final PreferenceSetting preferences = new RapiDPreferences();
public RapiDPlugin(PluginInformation info) {
super(info);
public RapiDPlugin(PluginInformation info) {
super(info);
JMenu dataMenu = MainApplication.getMenu().dataMenu;
MainMenu.add(dataMenu, new RapiDAction(), false);
MainMenu.add(dataMenu, new RapiDMoveAction(), false);
JMenu dataMenu = MainApplication.getMenu().dataMenu;
MainMenu.add(dataMenu, new RapiDAction(), false);
MainMenu.add(dataMenu, new RapiDMoveAction(), false);
RapiDDataUtils.addRapiDPaintStyles();
RapiDDataUtils.addRapiDPaintStyles();
versionInfo = info.localversion;
}
versionInfo = info.localversion;
}
@Override
public PreferenceSetting getPreferenceSetting() {
return preferences;
}
@Override
public PreferenceSetting getPreferenceSetting() {
return preferences;
}
public static String getVersionInfo() {
return versionInfo;
}
public static String getVersionInfo() {
return versionInfo;
}
}

Wyświetl plik

@ -17,67 +17,65 @@ import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
import org.openstreetmap.josm.plugins.rapid.backend.RapiDDataUtils;
public class RapiDPreferences implements SubPreferenceSetting {
private final JLabel rapidApiUrl = new JLabel(tr("RapiD API URL"));
private final JComboBox<String> possibleRapidApiUrl = new JComboBox<>();
private final JLabel rapidApiUrl = new JLabel(tr("RapiD API URL"));
private final JComboBox<String> possibleRapidApiUrl = new JComboBox<>();
private final JLabel switchLayer = new JLabel(tr("Automatically switch layers"));
private final JCheckBox switchLayerCheckBox = new JCheckBox();
private final JLabel switchLayer = new JLabel(tr("Automatically switch layers"));
private final JCheckBox switchLayerCheckBox = new JCheckBox();
@Override
public void addGui(PreferenceTabbedPane gui) {
final JPanel container = new JPanel(new GridBagLayout());
container.setAlignmentY(JPanel.TOP_ALIGNMENT);
final GridBagConstraints constraints = new GridBagConstraints();
@Override
public void addGui(PreferenceTabbedPane gui) {
final JPanel container = new JPanel(new GridBagLayout());
container.setAlignmentY(JPanel.TOP_ALIGNMENT);
final GridBagConstraints constraints = new GridBagConstraints();
for (String url : RapiDDataUtils.getRapiDURLs()) {
possibleRapidApiUrl.addItem(url);
}
for (String url : RapiDDataUtils.getRapiDURLs()) {
possibleRapidApiUrl.addItem(url);
}
possibleRapidApiUrl.setEditable(true);
possibleRapidApiUrl.setPrototypeDisplayValue("https://example.url/some/end/point");
possibleRapidApiUrl.setSelectedItem(RapiDDataUtils.getRapiDURL());
possibleRapidApiUrl.setEditable(true);
possibleRapidApiUrl.setPrototypeDisplayValue("https://example.url/some/end/point");
possibleRapidApiUrl.setSelectedItem(RapiDDataUtils.getRapiDURL());
switchLayerCheckBox.setSelected(RapiDDataUtils.getSwitchLayers());
switchLayerCheckBox.setSelected(RapiDDataUtils.getSwitchLayers());
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = .1;
constraints.weighty = 0;
constraints.insets = new Insets(5, 10, 5, 10);
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.HORIZONTAL;
container.add(rapidApiUrl, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = .1;
constraints.weighty = 0;
constraints.insets = new Insets(5, 10, 5, 10);
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.HORIZONTAL;
container.add(rapidApiUrl, constraints);
constraints.gridx++;
constraints.weightx = 1;
container.add(possibleRapidApiUrl, constraints);
constraints.gridx++;
constraints.weightx = 1;
container.add(possibleRapidApiUrl, constraints);
constraints.gridx--;
constraints.gridy++;
container.add(switchLayer, constraints);
constraints.gridx--;
constraints.gridy++;
container.add(switchLayer, constraints);
constraints.gridx++;
container.add(switchLayerCheckBox, constraints);
constraints.gridx++;
container.add(switchLayerCheckBox, constraints);
getTabPreferenceSetting(gui).addSubTab(this, "RapiD", container);
}
getTabPreferenceSetting(gui).addSubTab(this, "RapiD", container);
}
@Override
public boolean ok() {
RapiDDataUtils.setRapiDUrl((String) possibleRapidApiUrl.getSelectedItem());
RapiDDataUtils.setSwitchLayers(switchLayerCheckBox.isSelected());
return false;
}
@Override
public boolean ok() {
RapiDDataUtils.setRapiDUrl((String) possibleRapidApiUrl.getSelectedItem());
RapiDDataUtils.setSwitchLayers(switchLayerCheckBox.isSelected());
return false;
}
@Override
public boolean isExpert() {
return false;
}
@Override
public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
return gui.getPluginPreference();
}
@Override
public boolean isExpert() {
return false;
}
@Override
public TabPreferenceSetting getTabPreferenceSetting(PreferenceTabbedPane gui) {
return gui.getPluginPreference();
}
}

Wyświetl plik

@ -15,89 +15,91 @@ import org.openstreetmap.josm.plugins.rapid.RapiDPlugin;
import org.openstreetmap.josm.tools.Shortcut;
public class RapiDAction extends JosmAction {
/** UID */
private static final long serialVersionUID = 8886705479253246588L;
/** UID */
private static final long serialVersionUID = 8886705479253246588L;
public RapiDAction() {
super(RapiDPlugin.NAME, null, tr("Get data from RapiD"),
Shortcut.registerShortcut("data:rapid", tr("Data: {0}", tr("RapiD")), KeyEvent.VK_R, Shortcut.SHIFT),
true);
}
private static final Object layerLock = new Object();
@Override
public void actionPerformed(ActionEvent event) {
final RapiDLayer layer = getLayer(true);
getRapiDData(layer);
}
public RapiDAction() {
super(RapiDPlugin.NAME, null, tr("Get data from RapiD"),
Shortcut.registerShortcut("data:rapid", tr("Data: {0}", tr("RapiD")), KeyEvent.VK_R, Shortcut.SHIFT),
true);
}
/**
* Get the first {@link RapiDLayer} that we can find.
*
* @param create true if we want to create a new layer
* @return A RapiDLayer, or a new RapiDLayer if none exist. May return
* {@code null} if {@code create} is {@code false}.
*/
public RapiDLayer getLayer(boolean create) {
final List<RapiDLayer> rapidLayers = MainApplication.getLayerManager().getLayersOfType(RapiDLayer.class);
RapiDLayer layer;
synchronized (this) {
if (rapidLayers.isEmpty() && create) {
layer = new RapiDLayer(new DataSet(), RapiDPlugin.NAME, null);
MainApplication.getLayerManager().addLayer(layer);
} else if (!rapidLayers.isEmpty()) {
layer = rapidLayers.get(0);
} else {
layer = null;
}
}
return layer;
}
@Override
public void actionPerformed(ActionEvent event) {
final RapiDLayer layer = getLayer(true);
getRapiDData(layer);
}
/**
* Get data for a {@link RapiDLayer}
*
* @param layer The {@link RapiDLayer} to add data to
*/
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);
}
}
}
/**
* Get the first {@link RapiDLayer} that we can find.
*
* @param create true if we want to create a new layer
* @return A RapiDLayer, or a new RapiDLayer if none exist. May return
* {@code null} if {@code create} is {@code false}.
*/
public RapiDLayer getLayer(boolean create) {
final List<RapiDLayer> rapidLayers = MainApplication.getLayerManager().getLayersOfType(RapiDLayer.class);
RapiDLayer layer;
synchronized (this) {
if (rapidLayers.isEmpty() && create) {
layer = new RapiDLayer(new DataSet(), RapiDPlugin.NAME, null);
MainApplication.getLayerManager().addLayer(layer);
} else if (!rapidLayers.isEmpty()) {
layer = rapidLayers.get(0);
} else {
layer = null;
}
}
return layer;
}
/**
* Get the data for RapiD
*
* @param layer A pre-existing {@link RapiDLayer}
* @param osmLayer The osm datalayer with a set of bounds
*/
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());
/* Microsoft buildings don't have a source, so we add one */
RapiDDataUtils.addSourceTags(newData, "building", "Microsoft");
synchronized (layer) {
layer.unlock();
layer.mergeFrom(newData);
layer.lock();
}
}
}
}
/**
* Get data for a {@link RapiDLayer}
*
* @param layer The {@link RapiDLayer} to add data to
*/
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);
}
}
}
@Override
protected void updateEnabledState() {
if (getLayerManager().getEditDataSet() == null) {
setEnabled(false);
} else {
setEnabled(true);
}
}
/**
* Get the data for RapiD
*
* @param layer A pre-existing {@link RapiDLayer}
* @param osmLayer The osm datalayer with a set of bounds
*/
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());
/* Microsoft buildings don't have a source, so we add one */
RapiDDataUtils.addSourceTags(newData, "building", "Microsoft");
synchronized (layerLock) {
layer.unlock();
layer.mergeFrom(newData);
layer.lock();
}
}
}
}
@Override
protected void updateEnabledState() {
if (getLayerManager().getEditDataSet() == null) {
setEnabled(false);
} else {
setEnabled(true);
}
}
}

Wyświetl plik

@ -30,210 +30,209 @@ import org.openstreetmap.josm.tools.Pair;
import org.openstreetmap.josm.tools.Utils;
public class RapiDAddCommand extends Command {
DataSet editable;
DataSet rapid;
Collection<OsmPrimitive> primitives;
AddPrimitivesCommand addPrimitivesCommand;
Collection<OsmPrimitive> modifiedPrimitives;
DataSet editable;
DataSet rapid;
Collection<OsmPrimitive> primitives;
AddPrimitivesCommand addPrimitivesCommand;
Collection<OsmPrimitive> modifiedPrimitives;
OsmDataLayer editLayer = null;
OsmDataLayer editLayer = null;
public RapiDAddCommand(RapiDLayer rapidLayer, OsmDataLayer editLayer, Collection<OsmPrimitive> selection) {
super(rapidLayer.getDataSet());
this.rapid = rapidLayer.getDataSet();
this.editable = editLayer.getDataSet();
this.primitives = selection;
this.editLayer = editLayer;
modifiedPrimitives = null;
public RapiDAddCommand(RapiDLayer rapidLayer, OsmDataLayer editLayer, Collection<OsmPrimitive> selection) {
super(rapidLayer.getDataSet());
this.rapid = rapidLayer.getDataSet();
this.editable = editLayer.getDataSet();
this.primitives = selection;
this.editLayer = editLayer;
modifiedPrimitives = null;
}
/**
* Add primitives from RapiD to the OSM data layer
*
* @param rapid The rapid dataset
* @param editable The OSM dataset
* @param selection The primitives to add from RapiD
*/
public RapiDAddCommand(DataSet rapid, DataSet editable, Collection<OsmPrimitive> selection) {
super(rapid);
this.rapid = rapid;
this.editable = editable;
this.primitives = selection;
modifiedPrimitives = null;
}
}
/**
* Add primitives from RapiD to the OSM data layer
*
* @param rapid The rapid dataset
* @param editable The OSM dataset
* @param selection The primitives to add from RapiD
*/
public RapiDAddCommand(DataSet rapid, DataSet editable, Collection<OsmPrimitive> selection) {
super(rapid);
this.rapid = rapid;
this.editable = editable;
this.primitives = selection;
modifiedPrimitives = null;
}
@Override
public boolean executeCommand() {
if (rapid.equals(editable)) {
Logging.error("{0}: DataSet rapid ({1}) should not be the same as DataSet editable ({2})", RapiDPlugin.NAME,
rapid, editable);
throw new IllegalArgumentException();
}
primitives = new HashSet<>(primitives);
RapiDDataUtils.addPrimitivesToCollection(/* collection= */ primitives, /* primitives= */ primitives);
synchronized (this) {
rapid.unlock();
Collection<OsmPrimitive> newPrimitives = new TreeSet<>(moveCollection(rapid, editable, primitives));
createConnections(editable, newPrimitives);
RapiDDataUtils.removePrimitivesFromDataSet(primitives);
rapid.lock();
}
if (editLayer != null && RapiDDataUtils.getSwitchLayers()) {
MainApplication.getLayerManager().setActiveLayer(editLayer);
editable.setSelected(
editable.getSelected().stream().filter(OsmPrimitive::isTagged).collect(Collectors.toSet()));
}
return true;
}
@Override
public boolean executeCommand() {
if (rapid.equals(editable)) {
Logging.error("{0}: DataSet rapid ({1}) should not be the same as DataSet editable ({2})", RapiDPlugin.NAME,
rapid, editable);
throw new IllegalArgumentException();
}
primitives = new HashSet<>(primitives);
RapiDDataUtils.addPrimitivesToCollection(/* collection= */ primitives, /* primitives= */ primitives);
synchronized (this) {
rapid.unlock();
Collection<OsmPrimitive> newPrimitives = new TreeSet<>(moveCollection(rapid, editable, primitives));
createConnections(editable, newPrimitives);
RapiDDataUtils.removePrimitivesFromDataSet(primitives);
rapid.lock();
}
if (editLayer != null && RapiDDataUtils.getSwitchLayers()) {
MainApplication.getLayerManager().setActiveLayer(editLayer);
editable.setSelected(
editable.getSelected().stream().filter(OsmPrimitive::isTagged).collect(Collectors.toSet()));
}
return true;
}
/**
* Create connections based off of current RapiD syntax
*
* @param dataSet The {@link DataSet} that should have the primitives we are
* connecting to
* @param collection The primitives with connection information (currently only
* checks Nodes)
*/
public static void createConnections(DataSet dataSet, Collection<OsmPrimitive> collection) {
Collection<Node> nodes = Utils.filteredCollection(collection, Node.class);
for (Node node : nodes) {
if (node.hasKey("conn")) {
// Currently w<way id>,n<node1>,n<node2>
OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get("conn"));
for (int i = 0; i < primitiveConnections.length / 3; i++) {
if (primitiveConnections[i] instanceof Way && primitiveConnections[i + 1] instanceof Node
&& primitiveConnections[i + 2] instanceof Node) {
addNodesToWay(node, (Way) primitiveConnections[i], (Node) primitiveConnections[i + 1],
(Node) primitiveConnections[i + 2]);
} else {
Logging.error("{0}: {1}, {2}: {3}, {4}: {5}", i, primitiveConnections[i].getClass(), i + 1,
primitiveConnections[i + 1].getClass(), i + 2, primitiveConnections[i + 2].getClass());
}
}
Logging.debug("RapiD: Removing conn from {0} in {1}", node, dataSet.getName());
node.remove("conn");
}
if (node.hasKey("dupe")) {
OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get("dupe"));
if (primitiveConnections.length != 1) {
Logging.error("RapiD: dupe connection connected to more than one node? (dupe={0})",
node.get("dupe"));
}
replaceNode(node, (Node) primitiveConnections[0]);
}
}
}
/**
* Create connections based off of current RapiD syntax
*
* @param dataSet The {@link DataSet} that should have the primitives we are
* connecting to
* @param collection The primitives with connection information (currently only
* checks Nodes)
*/
public static void createConnections(DataSet dataSet, Collection<OsmPrimitive> collection) {
Collection<Node> nodes = Utils.filteredCollection(collection, Node.class);
for (Node node : nodes) {
if (node.hasKey("conn")) {
// Currently w<way id>,n<node1>,n<node2>
OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get("conn"));
for (int i = 0; i < primitiveConnections.length / 3; i++) {
if (primitiveConnections[i] instanceof Way && primitiveConnections[i + 1] instanceof Node
&& primitiveConnections[i + 2] instanceof Node) {
addNodesToWay(node, (Way) primitiveConnections[i], (Node) primitiveConnections[i + 1],
(Node) primitiveConnections[i + 2]);
} else {
Logging.error("{0}: {1}, {2}: {3}, {4}: {5}", i, primitiveConnections[i].getClass(), i + 1,
primitiveConnections[i + 1].getClass(), i + 2, primitiveConnections[i + 2].getClass());
}
}
Logging.debug("RapiD: Removing conn from {0} in {1}", node, dataSet.getName());
node.remove("conn");
}
if (node.hasKey("dupe")) {
OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get("dupe"));
if (primitiveConnections.length != 1) {
Logging.error("RapiD: dupe connection connected to more than one node? (dupe={0})",
node.get("dupe"));
}
replaceNode(node, (Node) primitiveConnections[0]);
}
}
}
/**
* Get the primitives from a dataset with specified ids
*
* @param dataSet The dataset holding the primitives (hopefully)
* @param ids The ids formated like n<NUMBER>,r<NUMBER>,w<NUMBER>
* @return The primitives that the ids point to, if in the dataset.
*/
private static OsmPrimitive[] getPrimitives(DataSet dataSet, String ids) {
String[] connections = ids.split(",", -1);
OsmPrimitive[] primitiveConnections = new OsmPrimitive[connections.length];
for (int i = 0; i < connections.length; i++) {
String member = connections[i];
long id = Long.parseLong(member.substring(1));
char firstChar = member.charAt(0);
if (firstChar == 'w') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.WAY);
} else if (firstChar == 'n') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.NODE);
} else if (firstChar == 'r') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.RELATION);
}
}
return primitiveConnections;
}
/**
* Get the primitives from a dataset with specified ids
*
* @param dataSet The dataset holding the primitives (hopefully)
* @param ids The ids formated like n<NUMBER>,r<NUMBER>,w<NUMBER>
* @return The primitives that the ids point to, if in the dataset.
*/
private static OsmPrimitive[] getPrimitives(DataSet dataSet, String ids) {
String[] connections = ids.split(",", -1);
OsmPrimitive[] primitiveConnections = new OsmPrimitive[connections.length];
for (int i = 0; i < connections.length; i++) {
String member = connections[i];
long id = Long.parseLong(member.substring(1));
char firstChar = member.charAt(0);
if (firstChar == 'w') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.WAY);
} else if (firstChar == 'n') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.NODE);
} else if (firstChar == 'r') {
primitiveConnections[i] = dataSet.getPrimitiveById(id, OsmPrimitiveType.RELATION);
}
}
return primitiveConnections;
}
/**
* Add a node to a way
*
* @param toAddNode The node to add
* @param way The way to add the node to
* @param first The first node in a waysegment (the node is between this and
* the second node)
* @param second The second node in a waysegemnt
*/
public static void addNodesToWay(Node toAddNode, Way way, Node first, Node second) {
int index = Math.max(way.getNodes().indexOf(first), way.getNodes().indexOf(second));
way.addNode(index, toAddNode);
}
/**
* Add a node to a way
*
* @param toAddNode The node to add
* @param way The way to add the node to
* @param first The first node in a waysegment (the node is between this and
* the second node)
* @param second The second node in a waysegemnt
*/
public static void addNodesToWay(Node toAddNode, Way way, Node first, Node second) {
int index = Math.max(way.getNodes().indexOf(first), way.getNodes().indexOf(second));
way.addNode(index, toAddNode);
}
public static void replaceNode(Node original, Node newNode) {
for (OsmPrimitive primitive : original.getReferrers()) {
if (primitive instanceof Way) {
Way way = (Way) primitive;
List<Integer> indexes = new ArrayList<>();
List<Node> nodes = way.getNodes();
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(original)) {
indexes.add(i);
}
}
while (way.getNodes().contains(original)) {
way.removeNode(original);
}
for (int index : indexes) {
way.addNode(index, newNode);
}
} else if (primitive instanceof Relation) {
List<Pair<Integer, RelationMember>> replaceMembers = new ArrayList<>();
Relation relation = (Relation) primitive;
List<RelationMember> relationMembers = relation.getMembers();
for (int i = 0; i < relationMembers.size(); i++) {
RelationMember member = relationMembers.get(i);
if (member.getMember().equals(original)) {
replaceMembers.add(new Pair<>(i, new RelationMember(member.getRole(), newNode)));
}
}
relation.removeMembersFor(original);
for (Pair<Integer, RelationMember> pair : replaceMembers) {
relation.addMember(pair.a, pair.b);
}
}
}
original.getDataSet().removePrimitive(original);
}
public static void replaceNode(Node original, Node newNode) {
for (OsmPrimitive primitive : original.getReferrers()) {
if (primitive instanceof Way) {
Way way = (Way) primitive;
List<Integer> indexes = new ArrayList<>();
List<Node> nodes = way.getNodes();
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(original)) {
indexes.add(i);
}
}
while (way.getNodes().contains(original)) {
way.removeNode(original);
}
for (int index : indexes) {
way.addNode(index, newNode);
}
} else if (primitive instanceof Relation) {
List<Pair<Integer, RelationMember>> replaceMembers = new ArrayList<>();
Relation relation = (Relation) primitive;
List<RelationMember> relationMembers = relation.getMembers();
for (int i = 0; i < relationMembers.size(); i++) {
RelationMember member = relationMembers.get(i);
if (member.getMember().equals(original)) {
replaceMembers.add(new Pair<>(i, new RelationMember(member.getRole(), newNode)));
}
}
relation.removeMembersFor(original);
for (Pair<Integer, RelationMember> pair : replaceMembers) {
relation.addMember(pair.a, pair.b);
}
}
}
original.getDataSet().removePrimitive(original);
}
/**
* Move primitives from one dataset to another
*
* @param to The receiving dataset
* @param from The sending dataset
* @param selection The primitives to move
* @return true if the primitives have moved datasets
*/
public Collection<? extends OsmPrimitive> moveCollection(DataSet from, DataSet to,
Collection<OsmPrimitive> selection) {
if (from == null || to.isLocked() || from.isLocked()) {
Logging.error("RapiD: Cannot move primitives from {0} to {1}", from, to);
return Collections.emptySet();
}
Collection<OsmPrimitive> originalSelection = from.getSelected();
from.setSelected(selection);
MergeSourceBuildingVisitor mergeBuilder = new MergeSourceBuildingVisitor(from);
List<PrimitiveData> primitiveDataList = mergeBuilder.build().allPrimitives().stream().map(OsmPrimitive::save)
.collect(Collectors.toList());
from.setSelected(originalSelection);
addPrimitivesCommand = new AddPrimitivesCommand(primitiveDataList, primitiveDataList, to);
addPrimitivesCommand.executeCommand();
return addPrimitivesCommand.getParticipatingPrimitives();
}
/**
* Move primitives from one dataset to another
*
* @param to The receiving dataset
* @param from The sending dataset
* @param selection The primitives to move
* @return true if the primitives have moved datasets
*/
public Collection<? extends OsmPrimitive> moveCollection(DataSet from, DataSet to,
Collection<OsmPrimitive> selection) {
if (from == null || to.isLocked() || from.isLocked()) {
Logging.error("RapiD: Cannot move primitives from {0} to {1}", from, to);
return Collections.emptySet();
}
Collection<OsmPrimitive> originalSelection = from.getSelected();
from.setSelected(selection);
MergeSourceBuildingVisitor mergeBuilder = new MergeSourceBuildingVisitor(from);
List<PrimitiveData> primitiveDataList = mergeBuilder.build().allPrimitives().stream().map(OsmPrimitive::save)
.collect(Collectors.toList());
from.setSelected(originalSelection);
addPrimitivesCommand = new AddPrimitivesCommand(primitiveDataList, primitiveDataList, to);
addPrimitivesCommand.executeCommand();
return addPrimitivesCommand.getParticipatingPrimitives();
}
@Override
public String getDescriptionText() {
return tr("Add object from RapiD");
}
@Override
public String getDescriptionText() {
return tr("Add object from RapiD");
}
@Override
public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
Collection<OsmPrimitive> added) {
// TODO Auto-generated method stub
}
@Override
public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
Collection<OsmPrimitive> added) {
// TODO Auto-generated method stub
}
}

Wyświetl plik

@ -35,187 +35,187 @@ import org.openstreetmap.josm.tools.Logging;
*
*/
public final class RapiDDataUtils {
public static final String DEFAULT_RAPID_API = "https://www.facebook.com/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=fbid&token=ASZUVdYpCkd3M6ZrzjXdQzHulqRMnxdlkeBJWEKOeTUoY_Gwm9fuEd2YObLrClgDB_xfavizBsh0oDfTWTF7Zb4C&hash=ASYM8LPNy8k1XoJiI7A&result_type=road_building_vector_xml&bbox={bbox}";
public static final String DEFAULT_RAPID_API = "https://www.facebook.com/maps/ml_roads?conflate_with_osm=true&theme=ml_road_vector&collaborator=fbid&token=ASZUVdYpCkd3M6ZrzjXdQzHulqRMnxdlkeBJWEKOeTUoY_Gwm9fuEd2YObLrClgDB_xfavizBsh0oDfTWTF7Zb4C&hash=ASYM8LPNy8k1XoJiI7A&result_type=road_building_vector_xml&bbox={bbox}";
private RapiDDataUtils() {
// Hide the constructor
}
private RapiDDataUtils() {
// Hide the constructor
}
/**
* Get a dataset from the API servers using a bbox
*
* @param bbox The bbox from which to get data
* @return A DataSet with data inside the bbox
*/
public static DataSet getData(BBox bbox) {
InputStream inputStream = null;
DataSet dataSet = new DataSet();
String urlString = getRapiDURL();
try {
final URL url = new URL(urlString.replace("{bbox}", bbox.toStringCSV(",")));
HttpClient client = HttpClient.create(url);
StringBuilder defaultUserAgent = new StringBuilder();
defaultUserAgent.append(client.getHeaders().get("User-Agent"));
if (defaultUserAgent.length() == 0) {
defaultUserAgent.append("JOSM");
}
defaultUserAgent.append(tr("/ {0} {1}", RapiDPlugin.NAME, RapiDPlugin.getVersionInfo()));
client.setHeader("User-Agent", defaultUserAgent.toString());
Logging.debug("{0}: Getting {1}", RapiDPlugin.NAME, client.getURL().toString());
Response response = client.connect();
inputStream = response.getContent();
dataSet.mergeFrom(OsmReader.parseDataSet(inputStream, null));
response.disconnect();
} catch (UnsupportedOperationException | IllegalDataException | IOException e) {
Logging.debug(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Logging.debug(e);
}
}
}
return dataSet;
}
/**
* Get a dataset from the API servers using a bbox
*
* @param bbox The bbox from which to get data
* @return A DataSet with data inside the bbox
*/
public static DataSet getData(BBox bbox) {
InputStream inputStream = null;
DataSet dataSet = new DataSet();
String urlString = getRapiDURL();
try {
final URL url = new URL(urlString.replace("{bbox}", bbox.toStringCSV(",")));
HttpClient client = HttpClient.create(url);
StringBuilder defaultUserAgent = new StringBuilder();
defaultUserAgent.append(client.getHeaders().get("User-Agent"));
if (defaultUserAgent.length() == 0) {
defaultUserAgent.append("JOSM");
}
defaultUserAgent.append(tr("/ {0} {1}", RapiDPlugin.NAME, RapiDPlugin.getVersionInfo()));
client.setHeader("User-Agent", defaultUserAgent.toString());
Logging.debug("{0}: Getting {1}", RapiDPlugin.NAME, client.getURL().toString());
Response response = client.connect();
inputStream = response.getContent();
dataSet.mergeFrom(OsmReader.parseDataSet(inputStream, null));
response.disconnect();
} catch (UnsupportedOperationException | IllegalDataException | IOException e) {
Logging.debug(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Logging.debug(e);
}
}
}
return dataSet;
}
/**
* Add specified source tags to objects without a source tag that also have a
* specific key
*
* @param dataSet The {#link DataSet} to look through
* @param primaryKey The primary key that must be in the {@link OsmPrimitive}
* @param source The specified source value (not tag)
*/
public static void addSourceTags(DataSet dataSet, String primaryKey, String source) {
dataSet.allPrimitives().stream().filter(p -> p.hasKey(primaryKey) && !p.hasKey("source")).forEach(p -> {
p.put("source", source);
p.save();
});
}
/**
* Add specified source tags to objects without a source tag that also have a
* specific key
*
* @param dataSet The {#link DataSet} to look through
* @param primaryKey The primary key that must be in the {@link OsmPrimitive}
* @param source The specified source value (not tag)
*/
public static void addSourceTags(DataSet dataSet, String primaryKey, String source) {
dataSet.allPrimitives().stream().filter(p -> p.hasKey(primaryKey) && !p.hasKey("source")).forEach(p -> {
p.put("source", source);
p.save();
});
}
/**
* Remove primitives and their children from a dataset.
*
* @param primitives The primitives to remove
*/
public static void removePrimitivesFromDataSet(Collection<OsmPrimitive> primitives) {
for (OsmPrimitive primitive : primitives) {
if (primitive instanceof Relation) {
removePrimitivesFromDataSet(((Relation) primitive).getMemberPrimitives());
} else if (primitive instanceof Way) {
for (Node node : ((Way) primitive).getNodes()) {
DataSet ds = node.getDataSet();
if (ds != null) {
ds.removePrimitive(node);
}
}
}
DataSet ds = primitive.getDataSet();
if (ds != null) {
ds.removePrimitive(primitive);
}
}
}
/**
* Remove primitives and their children from a dataset.
*
* @param primitives The primitives to remove
*/
public static void removePrimitivesFromDataSet(Collection<OsmPrimitive> primitives) {
for (OsmPrimitive primitive : primitives) {
if (primitive instanceof Relation) {
removePrimitivesFromDataSet(((Relation) primitive).getMemberPrimitives());
} else if (primitive instanceof Way) {
for (Node node : ((Way) primitive).getNodes()) {
DataSet ds = node.getDataSet();
if (ds != null) {
ds.removePrimitive(node);
}
}
}
DataSet ds = primitive.getDataSet();
if (ds != null) {
ds.removePrimitive(primitive);
}
}
}
/**
* Add primitives and their children to a collection
*
* @param collection A collection to add the primitives to
* @param primitives The primitives to add to the collection
*/
public static void addPrimitivesToCollection(Collection<OsmPrimitive> collection,
Collection<OsmPrimitive> primitives) {
Collection<OsmPrimitive> temporaryCollection = new TreeSet<>();
for (OsmPrimitive primitive : primitives) {
if (primitive instanceof Way) {
temporaryCollection.addAll(((Way) primitive).getNodes());
} else if (primitive instanceof Relation) {
addPrimitivesToCollection(temporaryCollection, ((Relation) primitive).getMemberPrimitives());
}
temporaryCollection.add(primitive);
}
collection.addAll(temporaryCollection);
}
/**
* Add primitives and their children to a collection
*
* @param collection A collection to add the primitives to
* @param primitives The primitives to add to the collection
*/
public static void addPrimitivesToCollection(Collection<OsmPrimitive> collection,
Collection<OsmPrimitive> primitives) {
Collection<OsmPrimitive> temporaryCollection = new TreeSet<>();
for (OsmPrimitive primitive : primitives) {
if (primitive instanceof Way) {
temporaryCollection.addAll(((Way) primitive).getNodes());
} else if (primitive instanceof Relation) {
addPrimitivesToCollection(temporaryCollection, ((Relation) primitive).getMemberPrimitives());
}
temporaryCollection.add(primitive);
}
collection.addAll(temporaryCollection);
}
/**
* Get the current RapiD url
*
* @return A RapiD url
*/
public static String getRapiDURL() {
List<String> urls = getRapiDURLs();
String url = Config.getPref().get(RapiDPlugin.NAME.concat(".current_api"), DEFAULT_RAPID_API);
if (!urls.contains(url)) {
url = DEFAULT_RAPID_API;
setRapiDUrl(DEFAULT_RAPID_API);
}
return url;
}
/**
* Get the current RapiD url
*
* @return A RapiD url
*/
public static String getRapiDURL() {
List<String> urls = getRapiDURLs();
String url = Config.getPref().get(RapiDPlugin.NAME.concat(".current_api"), DEFAULT_RAPID_API);
if (!urls.contains(url)) {
url = DEFAULT_RAPID_API;
setRapiDUrl(DEFAULT_RAPID_API);
}
return url;
}
/**
* Set the RapiD url
*
* @param url The url to set as the default
*/
public static void setRapiDUrl(String url) {
List<String> urls = getRapiDURLs();
if (!urls.contains(url)) {
urls.add(url);
setRapiDURLs(urls);
}
Config.getPref().put(RapiDPlugin.NAME.concat(".current_api"), url);
}
/**
* Set the RapiD url
*
* @param url The url to set as the default
*/
public static void setRapiDUrl(String url) {
List<String> urls = getRapiDURLs();
if (!urls.contains(url)) {
urls.add(url);
setRapiDURLs(urls);
}
Config.getPref().put(RapiDPlugin.NAME.concat(".current_api"), url);
}
/**
* Set the RapiD urls
*
* @param urls A list of URLs
*/
public static void setRapiDURLs(List<String> urls) {
Config.getPref().putList(RapiDPlugin.NAME.concat(".apis"), urls);
}
/**
* Set the RapiD urls
*
* @param urls A list of URLs
*/
public static void setRapiDURLs(List<String> urls) {
Config.getPref().putList(RapiDPlugin.NAME.concat(".apis"), urls);
}
/**
* Get the RapiD urls (or the default)
*
* @return The urls for RapiD endpoints
*/
public static List<String> getRapiDURLs() {
return Config.getPref().getList(RapiDPlugin.NAME.concat(".apis"),
new ArrayList<>(Arrays.asList(DEFAULT_RAPID_API)));
}
/**
* Get the RapiD urls (or the default)
*
* @return The urls for RapiD endpoints
*/
public static List<String> getRapiDURLs() {
return Config.getPref().getList(RapiDPlugin.NAME.concat(".apis"),
new ArrayList<>(Arrays.asList(DEFAULT_RAPID_API)));
}
/**
* Add a paintstyle from the jar (TODO)
*/
public static void addRapiDPaintStyles() {
// TODO figure out how to use the one in the jar file
ExtendedSourceEntry rapid = new ExtendedSourceEntry(SourceType.MAP_PAINT_STYLE, "rapid.mapcss",
"https://gitlab.com/smocktaylor/rapid/raw/master/src/resources/styles/standard/rapid.mapcss");
List<SourceEntry> paintStyles = MapPaintPrefHelper.INSTANCE.get();
for (SourceEntry paintStyle : paintStyles) {
if (rapid.url.equals(paintStyle.url))
return;
}
paintStyles.add(rapid);
MapPaintPrefHelper.INSTANCE.put(paintStyles);
}
/**
* Add a paintstyle from the jar (TODO)
*/
public static void addRapiDPaintStyles() {
// TODO figure out how to use the one in the jar file
ExtendedSourceEntry rapid = new ExtendedSourceEntry(SourceType.MAP_PAINT_STYLE, "rapid.mapcss",
"https://gitlab.com/smocktaylor/rapid/raw/master/src/resources/styles/standard/rapid.mapcss");
List<SourceEntry> paintStyles = MapPaintPrefHelper.INSTANCE.get();
for (SourceEntry paintStyle : paintStyles) {
if (rapid.url.equals(paintStyle.url))
return;
}
paintStyles.add(rapid);
MapPaintPrefHelper.INSTANCE.put(paintStyles);
}
/**
* Set whether or not a we switch from the RapiD layer to an OSM data layer
*
* @param selected true if we are going to switch layers
*/
public static void setSwitchLayers(boolean selected) {
Config.getPref().putBoolean(RapiDPlugin.NAME.concat(".autoswitchlayers"), selected);
}
/**
* Set whether or not a we switch from the RapiD layer to an OSM data layer
*
* @param selected true if we are going to switch layers
*/
public static void setSwitchLayers(boolean selected) {
Config.getPref().putBoolean(RapiDPlugin.NAME.concat(".autoswitchlayers"), selected);
}
/**
* @return {@code true} if we want to automatically switch layers
*/
public static boolean getSwitchLayers() {
return Config.getPref().getBoolean(RapiDPlugin.NAME.concat(".autoswitchlayers"), true);
}
/**
* @return {@code true} if we want to automatically switch layers
*/
public static boolean getSwitchLayers() {
return Config.getPref().getBoolean(RapiDPlugin.NAME.concat(".autoswitchlayers"), true);
}
}

Wyświetl plik

@ -12,16 +12,16 @@ import org.openstreetmap.josm.gui.layer.OsmDataLayer;
*/
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();
}
/**
* 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();
}
// @Override only JOSM > 15323
public String getChangesetSourceTag() {

Wyświetl plik

@ -17,55 +17,55 @@ import org.openstreetmap.josm.plugins.rapid.RapiDPlugin;
import org.openstreetmap.josm.tools.Shortcut;
public class RapiDMoveAction extends JosmAction {
/** UID for abstract action */
private static final long serialVersionUID = 319374598;
/** UID for abstract action */
private static final long serialVersionUID = 319374598;
public RapiDMoveAction() {
super(tr("Add from ".concat(RapiDPlugin.NAME)), null, tr("Add data from RapiD"), Shortcut.registerShortcut(
"data:rapidadd", tr("Rapid: {0}", tr("Add selected data")), KeyEvent.VK_A, Shortcut.SHIFT), true);
}
public RapiDMoveAction() {
super(tr("Add from ".concat(RapiDPlugin.NAME)), null, tr("Add data from RapiD"), Shortcut.registerShortcut(
"data:rapidadd", tr("Rapid: {0}", tr("Add selected data")), KeyEvent.VK_A, Shortcut.SHIFT), true);
}
@Override
public void actionPerformed(ActionEvent event) {
for (RapiDLayer rapid : MainApplication.getLayerManager().getLayersOfType(RapiDLayer.class)) {
List<OsmDataLayer> osmLayers = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class);
OsmDataLayer editLayer = null;
Collection<OsmPrimitive> selected = rapid.getDataSet().getSelected();
for (OsmDataLayer osmLayer : osmLayers) {
if (!osmLayer.isLocked() && osmLayer.isVisible() && osmLayer.isUploadable()
&& osmLayer.getClass().equals(OsmDataLayer.class)) {
editLayer = osmLayer;
break;
}
}
if (editLayer != null) {
RapiDAddCommand command = new RapiDAddCommand(rapid, editLayer, selected);
UndoRedoHandler.getInstance().add(command);
}
}
}
@Override
public void actionPerformed(ActionEvent event) {
for (RapiDLayer rapid : MainApplication.getLayerManager().getLayersOfType(RapiDLayer.class)) {
List<OsmDataLayer> osmLayers = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class);
OsmDataLayer editLayer = null;
Collection<OsmPrimitive> selected = rapid.getDataSet().getSelected();
for (OsmDataLayer osmLayer : osmLayers) {
if (!osmLayer.isLocked() && osmLayer.isVisible() && osmLayer.isUploadable()
&& osmLayer.getClass().equals(OsmDataLayer.class)) {
editLayer = osmLayer;
break;
}
}
if (editLayer != null) {
RapiDAddCommand command = new RapiDAddCommand(rapid, editLayer, selected);
UndoRedoHandler.getInstance().add(command);
}
}
}
@Override
protected void updateEnabledState() {
setEnabled(checkIfActionEnabled());
}
@Override
protected void updateEnabledState() {
setEnabled(checkIfActionEnabled());
}
@Override
protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
if (selection == null || selection.isEmpty()) {
setEnabled(false);
} else {
setEnabled(checkIfActionEnabled());
}
}
@Override
protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
if (selection == null || selection.isEmpty()) {
setEnabled(false);
} else {
setEnabled(checkIfActionEnabled());
}
}
private boolean checkIfActionEnabled() {
Layer active = getLayerManager().getActiveLayer();
if (active instanceof RapiDLayer) {
RapiDLayer rapid = (RapiDLayer) active;
Collection<OsmPrimitive> selection = rapid.getDataSet().getAllSelected();
return (selection != null && !selection.isEmpty());
} else
return false;
}
private boolean checkIfActionEnabled() {
Layer active = getLayerManager().getActiveLayer();
if (active instanceof RapiDLayer) {
RapiDLayer rapid = (RapiDLayer) active;
Collection<OsmPrimitive> selection = rapid.getDataSet().getAllSelected();
return (selection != null && !selection.isEmpty());
} else
return false;
}
}

Wyświetl plik

@ -16,74 +16,74 @@ import org.openstreetmap.josm.plugins.rapid.backend.RapiDAddCommand;
import org.openstreetmap.josm.testutils.JOSMTestRules;
public class RapiDAddComandTest {
@Rule
public JOSMTestRules test = new JOSMTestRules();
@Rule
public JOSMTestRules test = new JOSMTestRules();
@Test
public void testMoveCollection() {
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(0, 0.1)));
Way way2 = TestUtils.newWay("highway=residential", new Node(new LatLon(-0.1, -0.2)), way1.firstNode());
Way way3 = TestUtils.newWay("highway=residential", new Node(new LatLon(65, 65)), new Node(new LatLon(66, 66)));
for (Way way : Arrays.asList(way1, way2, way3)) {
for (Node node : way.getNodes()) {
if (!ds1.containsNode(node)) {
ds1.addPrimitive(node);
}
}
if (!ds1.containsWay(way)) {
ds1.addPrimitive(way);
}
}
ds1.lock();
RapiDAddCommand command = new RapiDAddCommand(ds1, ds2, Arrays.asList(way1, way2));
command.executeCommand();
Assert.assertTrue(ds2.containsWay(way1));
Assert.assertTrue(ds2.containsNode(way1.firstNode()));
Assert.assertTrue(ds2.containsNode(way1.lastNode()));
Assert.assertFalse(ds1.containsWay(way1));
Assert.assertTrue(ds2.containsWay(way2));
Assert.assertTrue(ds2.containsNode(way2.firstNode()));
Assert.assertTrue(ds2.containsNode(way2.lastNode()));
Assert.assertFalse(ds1.containsWay(way2));
@Test
public void testMoveCollection() {
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(0, 0.1)));
Way way2 = TestUtils.newWay("highway=residential", new Node(new LatLon(-0.1, -0.2)), way1.firstNode());
Way way3 = TestUtils.newWay("highway=residential", new Node(new LatLon(65, 65)), new Node(new LatLon(66, 66)));
for (Way way : Arrays.asList(way1, way2, way3)) {
for (Node node : way.getNodes()) {
if (!ds1.containsNode(node)) {
ds1.addPrimitive(node);
}
}
if (!ds1.containsWay(way)) {
ds1.addPrimitive(way);
}
}
ds1.lock();
RapiDAddCommand command = new RapiDAddCommand(ds1, ds2, Arrays.asList(way1, way2));
command.executeCommand();
Assert.assertTrue(ds2.containsWay(way1));
Assert.assertTrue(ds2.containsNode(way1.firstNode()));
Assert.assertTrue(ds2.containsNode(way1.lastNode()));
Assert.assertFalse(ds1.containsWay(way1));
Assert.assertTrue(ds2.containsWay(way2));
Assert.assertTrue(ds2.containsNode(way2.firstNode()));
Assert.assertTrue(ds2.containsNode(way2.lastNode()));
Assert.assertFalse(ds1.containsWay(way2));
Assert.assertFalse(ds2.containsWay(way3));
command = new RapiDAddCommand(ds1, ds2, Arrays.asList(way3));
command.executeCommand();
Assert.assertTrue(ds2.containsWay(way3));
Assert.assertTrue(ds2.containsNode(way3.firstNode()));
Assert.assertTrue(ds2.containsNode(way3.lastNode()));
Assert.assertFalse(ds1.containsWay(way3));
}
Assert.assertFalse(ds2.containsWay(way3));
command = new RapiDAddCommand(ds1, ds2, Arrays.asList(way3));
command.executeCommand();
Assert.assertTrue(ds2.containsWay(way3));
Assert.assertTrue(ds2.containsNode(way3.firstNode()));
Assert.assertTrue(ds2.containsNode(way3.lastNode()));
Assert.assertFalse(ds1.containsWay(way3));
}
@Test
public void testCreateConnections() {
DataSet ds1 = new DataSet();
Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(0, 0.15)));
Way way2 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0.05)),
new Node(new LatLon(0.05, 0.2)));
way2.firstNode().put("conn",
"w".concat(Long.toString(way1.getUniqueId())).concat(",n")
.concat(Long.toString(way1.firstNode().getUniqueId())).concat(",n")
.concat(Long.toString(way1.lastNode().getUniqueId())));
way1.getNodes().forEach(node -> ds1.addPrimitive(node));
way2.getNodes().forEach(node -> ds1.addPrimitive(node));
ds1.addPrimitive(way2);
ds1.addPrimitive(way1);
RapiDAddCommand.createConnections(ds1, Collections.singletonList(way2.firstNode()));
Assert.assertEquals(3, way1.getNodesCount());
Assert.assertFalse(way1.isFirstLastNode(way2.firstNode()));
@Test
public void testCreateConnections() {
DataSet ds1 = new DataSet();
Way way1 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)), new Node(new LatLon(0, 0.15)));
Way way2 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0.05)),
new Node(new LatLon(0.05, 0.2)));
way2.firstNode().put("conn",
"w".concat(Long.toString(way1.getUniqueId())).concat(",n")
.concat(Long.toString(way1.firstNode().getUniqueId())).concat(",n")
.concat(Long.toString(way1.lastNode().getUniqueId())));
way1.getNodes().forEach(node -> ds1.addPrimitive(node));
way2.getNodes().forEach(node -> ds1.addPrimitive(node));
ds1.addPrimitive(way2);
ds1.addPrimitive(way1);
RapiDAddCommand.createConnections(ds1, Collections.singletonList(way2.firstNode()));
Assert.assertEquals(3, way1.getNodesCount());
Assert.assertFalse(way1.isFirstLastNode(way2.firstNode()));
Way way3 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)),
new Node(new LatLon(-0.1, -0.1)));
way3.firstNode().put("dupe", "n".concat(Long.toString(way1.firstNode().getUniqueId())));
way3.getNodes().forEach(node -> ds1.addPrimitive(node));
ds1.addPrimitive(way3);
Node way3Node1 = way3.firstNode();
RapiDAddCommand.createConnections(ds1, Collections.singletonList(way3.firstNode()));
Assert.assertNotEquals(way3Node1, way3.firstNode());
Assert.assertEquals(way1.firstNode(), way3.firstNode());
Assert.assertFalse(ds1.containsNode(way3Node1));
}
Way way3 = TestUtils.newWay("highway=residential", new Node(new LatLon(0, 0)),
new Node(new LatLon(-0.1, -0.1)));
way3.firstNode().put("dupe", "n".concat(Long.toString(way1.firstNode().getUniqueId())));
way3.getNodes().forEach(node -> ds1.addPrimitive(node));
ds1.addPrimitive(way3);
Node way3Node1 = way3.firstNode();
RapiDAddCommand.createConnections(ds1, Collections.singletonList(way3.firstNode()));
Assert.assertNotEquals(way3Node1, way3.firstNode());
Assert.assertEquals(way1.firstNode(), way3.firstNode());
Assert.assertFalse(ds1.containsNode(way3Node1));
}
}