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 |
|
---|
10 | import org.openstreetmap.gui.jmapviewer.Coordinate;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * @author Vincent
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class Shape {
|
---|
19 |
|
---|
20 | private List<Coordinate> coords = new ArrayList<Coordinate>();
|
---|
21 |
|
---|
22 | public Shape(String asString, String separator) throws IllegalArgumentException {
|
---|
23 | CheckParameterUtil.ensureParameterNotNull(asString, "asString");
|
---|
24 | String[] components = asString.split(separator);
|
---|
25 | if (components.length % 2 != 0)
|
---|
26 | throw new IllegalArgumentException(MessageFormat.format("Even number of doubles excpected in string, got {0}: {1}", components.length, asString));
|
---|
27 | for (int i=0; i<components.length; i+=2) {
|
---|
28 | addPoint(components[i], components[i+1]);
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public Shape() {
|
---|
33 | }
|
---|
34 |
|
---|
35 | public String encodeAsString(String separator) {
|
---|
36 | StringBuffer sb = new StringBuffer();
|
---|
37 | for (Coordinate c : coords) {
|
---|
38 | if (sb.length() != 0) {
|
---|
39 | sb.append(separator);
|
---|
40 | }
|
---|
41 | sb.append(c.getLat()).append(separator).append(c.getLon());
|
---|
42 | }
|
---|
43 | return sb.toString();
|
---|
44 | }
|
---|
45 |
|
---|
46 | public List<Coordinate> getPoints() {
|
---|
47 | return coords;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void addPoint(String sLat, String sLon) throws IllegalArgumentException {
|
---|
51 | CheckParameterUtil.ensureParameterNotNull(sLat, "sLat");
|
---|
52 | CheckParameterUtil.ensureParameterNotNull(sLon, "sLon");
|
---|
53 |
|
---|
54 | double lat, lon;
|
---|
55 |
|
---|
56 | try {
|
---|
57 | lat = Double.parseDouble(sLat);
|
---|
58 | if (!LatLon.isValidLat(lat))
|
---|
59 | throw new IllegalArgumentException(tr("Illegal latitude value ''{0}''", lat));
|
---|
60 | } catch (NumberFormatException e) {
|
---|
61 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLat));
|
---|
62 | }
|
---|
63 |
|
---|
64 | try {
|
---|
65 | lon = Double.parseDouble(sLon);
|
---|
66 | if (!LatLon.isValidLon(lon))
|
---|
67 | throw new IllegalArgumentException(tr("Illegal longitude value ''{0}''", lon));
|
---|
68 | } catch (NumberFormatException e) {
|
---|
69 | throw new IllegalArgumentException(MessageFormat.format("Illegal double value ''{0}''", sLon));
|
---|
70 | }
|
---|
71 |
|
---|
72 | coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon)));
|
---|
73 | }
|
---|
74 | }
|
---|