source: josm/trunk/src/org/openstreetmap/josm/data/imagery/Shape.java@ 8394

Last change on this file since 8394 was 8394, checked in by Don-vip, 9 years ago
  • global use of String.isEmpty()
  • Correctness - Method throws alternative exception from catch block without history
  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.MessageFormat;
7import java.util.ArrayList;
8import java.util.List;
9import java.util.Objects;
10
11import org.openstreetmap.gui.jmapviewer.Coordinate;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15import org.openstreetmap.josm.tools.Geometry;
16
17/**
18 * @author Vincent
19 *
20 */
21public 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}
Note: See TracBrowser for help on using the repository browser.