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

Last change on this file since 5028 was 4423, checked in by bastiK, 13 years ago

applied #6834 - Imagery providers slippy map enhancements: polygon support (patch by Don-vip, modified)

  • Property svn:eol-style set to native
File size: 2.5 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.tools.CheckParameterUtil;
13
14/**
15 * @author Vincent
16 *
17 */
18public 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}
Note: See TracBrowser for help on using the repository browser.