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 | import java.util.Objects;
|
---|
10 |
|
---|
11 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
15 | import org.openstreetmap.josm.tools.Geometry;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @author Vincent
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class Shape {
|
---|
22 |
|
---|
23 | private List<Coordinate> coords = new ArrayList<>();
|
---|
24 |
|
---|
25 | public Shape(String asString, String separator) {
|
---|
26 | CheckParameterUtil.ensureParameterNotNull(asString, "asString");
|
---|
27 | String[] components = asString.split(separator);
|
---|
28 | if (components.length % 2 != 0)
|
---|
29 | throw new IllegalArgumentException(MessageFormat.format("Even number of doubles expected in string, got {0}: {1}", components.length, asString));
|
---|
30 | for (int i=0; i<components.length; i+=2) {
|
---|
31 | addPoint(components[i], components[i+1]);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Shape() {
|
---|
36 | }
|
---|
37 |
|
---|
38 | public String encodeAsString(String separator) {
|
---|
39 | StringBuilder sb = new StringBuilder();
|
---|
40 | for (Coordinate c : coords) {
|
---|
41 | if (sb.length() != 0) {
|
---|
42 | sb.append(separator);
|
---|
43 | }
|
---|
44 | sb.append(c.getLat()).append(separator).append(c.getLon());
|
---|
45 | }
|
---|
46 | return sb.toString();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public List<Coordinate> getPoints() {
|
---|
50 | return coords;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public boolean contains(LatLon latlon) {
|
---|
54 | if (latlon == null)
|
---|
55 | return false;
|
---|
56 | List<Node> nodes = new ArrayList<>(coords.size());
|
---|
57 | for (Coordinate c : coords) {
|
---|
58 | nodes.add(new Node(new LatLon(c.getLat(), c.getLon())));
|
---|
59 | }
|
---|
60 | return Geometry.nodeInsidePolygon(new Node(latlon), nodes);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void addPoint(String sLat, String sLon) {
|
---|
64 | CheckParameterUtil.ensureParameterNotNull(sLat, "sLat");
|
---|
65 | CheckParameterUtil.ensureParameterNotNull(sLon, "sLon");
|
---|
66 |
|
---|
67 | double lat, lon;
|
---|
68 |
|
---|
69 | try {
|
---|
70 | lat = Double.parseDouble(sLat);
|
---|
71 | if (!LatLon.isValidLat(lat))
|
---|
72 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", lat));
|
---|
73 | } catch (NumberFormatException e) {
|
---|
74 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLat), e);
|
---|
75 | }
|
---|
76 |
|
---|
77 | try {
|
---|
78 | lon = Double.parseDouble(sLon);
|
---|
79 | if (!LatLon.isValidLon(lon))
|
---|
80 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", lon));
|
---|
81 | } catch (NumberFormatException e) {
|
---|
82 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLon), e);
|
---|
83 | }
|
---|
84 |
|
---|
85 | coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon)));
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public int hashCode() {
|
---|
90 | int hash = 5;
|
---|
91 | hash = 47 * hash + Objects.hashCode(this.coords);
|
---|
92 | return hash;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public boolean equals(Object obj) {
|
---|
97 | if (obj == null) {
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 | if (getClass() != obj.getClass()) {
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | final Shape other = (Shape) obj;
|
---|
104 | if (!Objects.equals(this.coords, other.coords)) {
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | return true;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | }
|
---|