Skip to content

Commit

Permalink
Clean code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccasc committed May 25, 2021
1 parent 1c762cc commit 3315ca8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.RemovePointAction;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.TransformPointAction;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.AutoCalibratePictureAction;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.AutoCalibratePictureAction;
import org.openstreetmap.josm.plugins.piclayer.io.session.FileSessionExporter;
import org.openstreetmap.josm.plugins.piclayer.io.session.KMLSessionExporter;
import org.openstreetmap.josm.plugins.piclayer.io.session.PicLayerSessionImporter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate;
package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate;


import java.awt.event.*;
Expand All @@ -24,9 +24,9 @@
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.help.HelpBrowser;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.ObservableArrayList;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.ObservableArrayList;
import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationErrorView;
import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationWindow;
import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.ReferenceOptionView;
Expand All @@ -36,7 +36,6 @@

/**
* Class handling connection between {@link AutoCalibratePictureAction} and GUIs.
* Info at https://wiki.openstreetmap.org/wiki/User:Rebsc
*/
public class AutoCalibrateHandler {

Expand All @@ -45,10 +44,10 @@ public class AutoCalibrateHandler {
private File referenceFile;
private Layer referenceLayer;
private final AutoCalibration calibration;
private ObservableArrayList<Point2D> originPointList; // points set on picture to calibrate, scaled in LatLon
private ObservableArrayList<Point2D> referencePointList; // points of reference data, scaled in LatLon
private double distance1To2; // in meter
private double distance2To3; // in meter
private ObservableArrayList<Point2D> originPointList; // local points in LatLon
private ObservableArrayList<Point2D> referencePointList; // reference points in LatLon
private double distance1To2; // meter
private double distance2To3; // meter

public AutoCalibrateHandler() {
this.originPointList = new ObservableArrayList<>(3);
Expand Down Expand Up @@ -88,7 +87,6 @@ private static class HelpButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String topic = "Plugin/PicLayer";
// open help browser
HelpBrowser.setUrlForHelpTopic(Optional.of(topic).orElse("/"));
}
}
Expand Down Expand Up @@ -133,10 +131,8 @@ private class SelectLayerButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
mainWindow.setVisible(false);

selector = new SelectLayerView();
selector.setVisible(true);

selector.setOkButtonListener(new SelectorOkButtonListener());
selector.setCancelButtonListener(new SelectorCancelButtonListener());
}
Expand Down Expand Up @@ -229,7 +225,6 @@ private class EdgePointsButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
mainWindow.setVisible(false);
MainApplication.getLayerManager().setActiveLayer(currentPicLayer);
// switch to select mode
MovePointAction selectPointMode = new MovePointAction();
MainApplication.getMap().selectMapMode(selectPointMode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate;
package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate;

import static org.openstreetmap.josm.tools.I18n.tr;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate;
package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate;

import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
Expand All @@ -9,23 +9,22 @@
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationErrorView;
import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
import org.openstreetmap.josm.tools.Logging;


/**
* Class for image calibration.
* Info at https://wiki.openstreetmap.org/wiki/User:Rebsc
* Class for image calibration
*/
public class AutoCalibration {

private PicLayerAbstract currentLayer;
private List<Point2D> startPositions; // raw data - LatLon scale
private List<Point2D> endPositions; // raw data - LatLon scale
private double distance1To2; // in meter
private double distance2To3; // in meter
private List<Point2D> startPositions; // raw data - LatLon
private List<Point2D> endPositions; // raw data - LatLon
private double distance1To2; // meter
private double distance2To3; // meter


public AutoCalibration() {
Expand All @@ -41,11 +40,10 @@ public AutoCalibration() {
* Sets start points to end points and corrects end points by passed distances between points.
*/
public void calibrate() {
// get start / end points
// get start/end points
List<Point2D> startPointList = currentLayer.getTransformer().getOriginPoints(); // in current layer scale
List<Point2D> endPointList = correctedPoints(endPositions, distance1To2, distance2To3); // in lat/lon scale - translation follows
List<Point2D> endPointList = correctedPoints(endPositions, distance1To2, distance2To3); // in lat/lon scale

// calibrate
if (currentLayer != null && startPointList != null && endPointList != null
&& startPointList.size() == 3 && endPointList.size() == 3
&& distance1To2 != 0.0 && distance2To3 != 0.0) {
Expand All @@ -56,25 +54,18 @@ public void calibrate() {

// move all points to final state position
for (Point2D endPos : endPointList) {
// get translated start point suitable to end point
index = endPointList.indexOf(endPos);
tsPoint = startPointList.get(index);

// transform end point into current layer scale
tePoint = translatePointToCurrentScale(endPos);

// move start point to end point
currentLayer.getTransformer().updatePair(tsPoint, tePoint);
}

// check if image got too distorted after calibration, if, reset and show error.
// Input start positions (lat/lon), corrected end positions (lat/lon)
// check if image got distorted after calibration, if true reset and show error.
if (!checkCalibration(startPositions, endPointList)) {
currentLayer.getTransformer().resetCalibration();
showErrorView(CalibrationErrorView.DIMENSION_ERROR);
}
} else {
// calibration failed
showErrorView(CalibrationErrorView.CALIBRATION_ERROR);
}

Expand Down Expand Up @@ -108,7 +99,7 @@ private boolean checkCalibration(List<Point2D> list, List<Point2D> compareList)

/**
* Corrects points with given distances. Calculates new points on lines
* between given points at given distances.
* between given points with given distances.
*
* @param points need to be corrected
* @param distance12 distance between point 1 and point 2 in meter
Expand All @@ -119,20 +110,22 @@ private List<Point2D> correctedPoints(List<Point2D> points, double distance12, d
if (points != null && points.size() == 3) {
List<Point2D> correctedList = new ArrayList<>();

// get line between point1 and point2
// get line between point1 and point2, point2 and point3
GeoLine line12 = new GeoLine(points.get(0), points.get(1));
// get line between point2 and point3
GeoLine line23 = new GeoLine(points.get(1), points.get(2));

correctedList.add(points.get(0)); // anchor
correctedList.add(line12.pointOnLine(distance12)); // point on line12 at distance12 from start on
// add point 0 - anchor
correctedList.add(points.get(0));
// add point on line12 at distance12
correctedList.add(line12.pointOnLine(distance12));
// get lat/lon offset of line12 point to origin point2
double lonOffset = line12.pointOnLine(distance12).getX() - points.get(1).getX();
double latOffset = line12.pointOnLine(distance12).getY() - points.get(1).getY();
// get point on line23 and add offset - to not deform the image
// get point on line23, add offset
Point2D pointOnLine23 = line23.pointOnLine(distance23);
Point2D correctedPointOnLine23 = new Point2D.Double(pointOnLine23.getX() + lonOffset, pointOnLine23.getY() + latOffset);
correctedList.add(correctedPointOnLine23); // point on line23 at distance23 from start on corrected with offset from point on line12
// add point on line23 at distance23 corrected with offset from point on line12
correctedList.add(correctedPointOnLine23);

return correctedList;
}
Expand All @@ -147,8 +140,8 @@ private List<Point2D> correctedPoints(List<Point2D> points, double distance12, d
*/
private Point2D translatePointToCurrentScale(Point2D point) {
Point2D translatedPoint = null;
LatLon ll; // LatLon object from raw Point2D
MapViewPoint en; // MapViewPoint object from LatLon(ll) scaled in EastNorth(en)
LatLon ll; // LatLon object from raw Point2D
MapViewPoint en; // MapViewPoint object from LatLon(ll) scaled in EastNorth(en)

// put raw Point2D endPos into LatLon and transform LatLon into MapViewPoint (EastNorth)
ll = new LatLon(point.getY(), point.getX());
Expand All @@ -174,7 +167,11 @@ public void showErrorView(String msg) {
handler.getErrorView().show(msg);
}


/**
* Set current active layer
*
* @param currentLayer to set active
*/
public void setCurrentLayer(PicLayerAbstract currentLayer) {
this.currentLayer = currentLayer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils;
package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils;

import org.openstreetmap.gui.jmapviewer.OsmMercator;

Expand All @@ -24,14 +24,6 @@ public GeoLine(Point2D startPoint, Point2D endPoint) {
this.distance = getDistance();
}

public GeoLine(double startLat, double startLon, double endLat, double endLon) {
this.lat1 = startLat;
this.lon1 = startLon;
this.lat2 = endLat;
this.lon2 = endLon;
this.distance = getDistance();
}

public Point2D getStartPoint() {
return new Point2D.Double(lon1, lat1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils;
package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@

/**
* Class providing error views for calibration action.
* Info at https://wiki.openstreetmap.org/wiki/User:Rebsc
*/
public class CalibrationErrorView {

public final static String OUTLINE_FILE_ERROR = tr("Could not load outline file!");
public final static String SELECT_LAYER_ERROR = tr("Could not select layer!");
public final static String CALIBRATION_ERROR = tr("Calibration failed!");
public final static String DIMENSION_ERROR = tr("<html> Calibration failed!<br>"
+ "There must be a mistake -<br>"
+ "dimension of object differ too much from original.<br>"
+ "</html>");
private JLabel textLabel;
private final JLabel textLabel;

public CalibrationErrorView() {
textLabel = new JLabel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.ProgramArguments;
import org.openstreetmap.josm.plugins.piclayer.PicLayerPlugin;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.AutoCalibratePictureAction;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.AutoCalibratePictureAction;
import org.openstreetmap.josm.tools.I18n;
import org.openstreetmap.josm.tools.Logging;

Expand Down Expand Up @@ -312,9 +311,9 @@ private void setRefFileName() {

private void setSelectLayerButton() {
String imageName = "layerlist.png";
Image image = null;
Image image;
try {
image = ImageIO.read(getClass().getResource("/images/" + imageName));
image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName)));
} catch (Exception ex) {
Logging.error("Could not load image " + imageName + "," + ex);
return;
Expand All @@ -331,7 +330,7 @@ private void setOpenButton() {
String imageName = "open.png";
Image image;
try {
image = ImageIO.read(getClass().getResource("/images/" + imageName));
image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName)));
} catch (Exception ex) {
Logging.error("Could not load image " + imageName + "," + ex);
return;
Expand Down Expand Up @@ -632,7 +631,7 @@ private ImageIcon getCheckedIcon() {
String imageName = "checked.png";
Image image;
try {
image = ImageIO.read(getClass().getResource("/images/" + imageName));
image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName)));
} catch (Exception ex) {
Logging.error("Could not load image " + imageName + "," + ex);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.openstreetmap.josm.plugins.piclayer.actions.ResetCalibrationAction;
import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationAction;
import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationToWorldAction;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine;
import org.openstreetmap.josm.plugins.piclayer.transform.PictureTransform;
import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider;
import org.openstreetmap.josm.tools.Logging;
Expand Down Expand Up @@ -241,6 +241,10 @@ public List<ImageEntry> getImages() {
return list;
}

public PictureTransform getTransformer() {
return transformer;
}

public GeoLine getRefLine1To2() {
return refLine1To2;
}
Expand All @@ -249,10 +253,6 @@ public GeoLine getRefLine2To3() {
return refLine2To3;
}

public PictureTransform getTransformer() {
return transformer;
}

public void setDrawOriginPoints(boolean value) {
drawOriginMarkers = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

import org.openstreetmap.josm.data.coor.EastNorth;
import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.ObservableArrayList;
import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.ObservableArrayList;
import org.openstreetmap.josm.tools.Logging;

public class PictureTransform {
Expand Down

0 comments on commit 3315ca8

Please sign in to comment.