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 final 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}",
|
---|
30 | components.length, asString));
|
---|
31 | for (int i = 0; i < components.length; i += 2) {
|
---|
32 | addPoint(components[i], components[i+1]);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new empty {@code Shape}.
|
---|
38 | */
|
---|
39 | public Shape() {
|
---|
40 | // shape contents can be set later with addPoint()
|
---|
41 | }
|
---|
42 |
|
---|
43 | public String encodeAsString(String separator) {
|
---|
44 | StringBuilder sb = new StringBuilder();
|
---|
45 | for (Coordinate c : coords) {
|
---|
46 | if (sb.length() != 0) {
|
---|
47 | sb.append(separator);
|
---|
48 | }
|
---|
49 | sb.append(c.getLat()).append(separator).append(c.getLon());
|
---|
50 | }
|
---|
51 | return sb.toString();
|
---|
52 | }
|
---|
53 |
|
---|
54 | public List<Coordinate> getPoints() {
|
---|
55 | return coords;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public boolean contains(LatLon latlon) {
|
---|
59 | if (latlon == null)
|
---|
60 | return false;
|
---|
61 | List<Node> nodes = new ArrayList<>(coords.size());
|
---|
62 | for (Coordinate c : coords) {
|
---|
63 | nodes.add(new Node(new LatLon(c.getLat(), c.getLon())));
|
---|
64 | }
|
---|
65 | return Geometry.nodeInsidePolygon(new Node(latlon), nodes);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void addPoint(String sLat, String sLon) {
|
---|
69 | CheckParameterUtil.ensureParameterNotNull(sLat, "sLat");
|
---|
70 | CheckParameterUtil.ensureParameterNotNull(sLon, "sLon");
|
---|
71 |
|
---|
72 | double lat, lon;
|
---|
73 |
|
---|
74 | try {
|
---|
75 | lat = Double.parseDouble(sLat);
|
---|
76 | if (!LatLon.isValidLat(lat))
|
---|
77 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", lat));
|
---|
78 | } catch (NumberFormatException e) {
|
---|
79 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLat), e);
|
---|
80 | }
|
---|
81 |
|
---|
82 | try {
|
---|
83 | lon = Double.parseDouble(sLon);
|
---|
84 | if (!LatLon.isValidLon(lon))
|
---|
85 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", lon));
|
---|
86 | } catch (NumberFormatException e) {
|
---|
87 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLon), e);
|
---|
88 | }
|
---|
89 |
|
---|
90 | coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon)));
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public int hashCode() {
|
---|
95 | return Objects.hash(coords);
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean equals(Object obj) {
|
---|
100 | if (this == obj) return true;
|
---|
101 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
102 | Shape shape = (Shape) obj;
|
---|
103 | return Objects.equals(coords, shape.coords);
|
---|
104 | }
|
---|
105 | }
|
---|