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