[4423] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.imagery;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.text.MessageFormat;
|
---|
| 7 | import java.util.ArrayList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 |
|
---|
| 10 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
| 11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[5381] | 12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[4423] | 13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[5381] | 14 | import org.openstreetmap.josm.tools.Geometry;
|
---|
[4423] | 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @author Vincent
|
---|
| 18 | *
|
---|
| 19 | */
|
---|
| 20 | public class Shape {
|
---|
| 21 |
|
---|
| 22 | private List<Coordinate> coords = new ArrayList<Coordinate>();
|
---|
[6069] | 23 |
|
---|
[4423] | 24 | public Shape(String asString, String separator) throws IllegalArgumentException {
|
---|
| 25 | CheckParameterUtil.ensureParameterNotNull(asString, "asString");
|
---|
| 26 | String[] components = asString.split(separator);
|
---|
| 27 | if (components.length % 2 != 0)
|
---|
[5381] | 28 | throw new IllegalArgumentException(MessageFormat.format("Even number of doubles expected in string, got {0}: {1}", components.length, asString));
|
---|
[4423] | 29 | for (int i=0; i<components.length; i+=2) {
|
---|
| 30 | addPoint(components[i], components[i+1]);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public Shape() {
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public String encodeAsString(String separator) {
|
---|
[6822] | 38 | StringBuilder sb = new StringBuilder();
|
---|
[4423] | 39 | for (Coordinate c : coords) {
|
---|
| 40 | if (sb.length() != 0) {
|
---|
| 41 | sb.append(separator);
|
---|
| 42 | }
|
---|
| 43 | sb.append(c.getLat()).append(separator).append(c.getLon());
|
---|
| 44 | }
|
---|
| 45 | return sb.toString();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public List<Coordinate> getPoints() {
|
---|
| 49 | return coords;
|
---|
| 50 | }
|
---|
[6069] | 51 |
|
---|
[5381] | 52 | public boolean contains(LatLon latlon) {
|
---|
| 53 | if (latlon == null)
|
---|
| 54 | return false;
|
---|
| 55 | List<Node> nodes = new ArrayList<Node>(coords.size());
|
---|
| 56 | for (Coordinate c : coords) {
|
---|
| 57 | nodes.add(new Node(new LatLon(c.getLat(), c.getLon())));
|
---|
| 58 | }
|
---|
| 59 | return Geometry.nodeInsidePolygon(new Node(latlon), nodes);
|
---|
| 60 | }
|
---|
[4423] | 61 |
|
---|
| 62 | public void addPoint(String sLat, String sLon) throws IllegalArgumentException {
|
---|
| 63 | CheckParameterUtil.ensureParameterNotNull(sLat, "sLat");
|
---|
| 64 | CheckParameterUtil.ensureParameterNotNull(sLon, "sLon");
|
---|
| 65 |
|
---|
| 66 | double lat, lon;
|
---|
[6069] | 67 |
|
---|
[4423] | 68 | try {
|
---|
| 69 | lat = Double.parseDouble(sLat);
|
---|
| 70 | if (!LatLon.isValidLat(lat))
|
---|
| 71 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", lat));
|
---|
| 72 | } catch (NumberFormatException e) {
|
---|
| 73 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLat));
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | try {
|
---|
| 77 | lon = Double.parseDouble(sLon);
|
---|
| 78 | if (!LatLon.isValidLon(lon))
|
---|
| 79 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", lon));
|
---|
| 80 | } catch (NumberFormatException e) {
|
---|
| 81 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLon));
|
---|
| 82 | }
|
---|
[6069] | 83 |
|
---|
[4423] | 84 | coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon)));
|
---|
| 85 | }
|
---|
| 86 | }
|
---|