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

Last change on this file since 5381 was 5381, checked in by Don-vip, 12 years ago

fix #7910 - Wrong suggested imagery layer providers (use of shapes when defined)

  • Property svn:eol-style set to native
File size: 3.0 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;
9
10import org.openstreetmap.gui.jmapviewer.Coordinate;
11import org.openstreetmap.josm.data.coor.LatLon;
12import org.openstreetmap.josm.data.osm.Node;
13import org.openstreetmap.josm.tools.CheckParameterUtil;
14import org.openstreetmap.josm.tools.Geometry;
15
16/**
17 * @author Vincent
18 *
19 */
20public class Shape {
21
22 private List<Coordinate> coords = new ArrayList<Coordinate>();
23
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)
28 throw new IllegalArgumentException(MessageFormat.format("Even number of doubles expected in string, got {0}: {1}", components.length, asString));
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) {
38 StringBuffer sb = new StringBuffer();
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 }
51
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 }
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;
67
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 }
83
84 coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon)));
85 }
86}
Note: See TracBrowser for help on using the repository browser.