Changeset 1169 in josm for trunk/src/org/openstreetmap/josm/data/coor
- Timestamp:
- 2008-12-23T15:07:05+01:00 (14 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/coor
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/coor/Coordinate.java
r1094 r1169 7 7 /** 8 8 * Base class of points of both coordinate systems. 9 * 10 * The variables are default package protected to allow routines in the 9 * 10 * The variables are default package protected to allow routines in the 11 11 * data package to access them directly. 12 * 13 * As the class itself is package protected too, it is not visible 14 * outside of the data package. Routines there should only use LatLon or 12 * 13 * As the class itself is package protected too, it is not visible 14 * outside of the data package. Routines there should only use LatLon or 15 15 * EastNorth. 16 16 * 17 17 * @author imi 18 */ 18 */ 19 19 abstract class Coordinate extends Point2D implements Serializable { 20 20 21 21 protected double x; 22 22 protected double y; 23 24 25 26 * 27 28 29 30 23 24 /** 25 * Construct the point with latitude / longitude values. 26 * 27 * @param x X coordinate of the point. 28 * @param y Y coordinate of the point. 29 */ 30 Coordinate(double x, double y) { 31 31 this.x = x; this.y = y; 32 33 32 } 33 34 34 public double getX() { 35 35 return x; 36 36 } 37 37 38 38 public double getY() { 39 return y; 39 return y; 40 40 } 41 41 42 42 public void setLocation (double x, double y) { 43 this.x = x; 43 this.x = x; 44 44 this.y = y; 45 45 } -
trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java
r1076 r1169 4 4 /** 5 5 * Northing, Easting of the projected coordinates. 6 * 6 * 7 7 * This class is immutable. 8 * 8 * 9 9 * @author Imi 10 10 */ 11 11 public class EastNorth extends Coordinate { 12 12 13 public EastNorth(double east, double north) { 14 super(east,north); 15 } 16 17 public double east() { 18 return x; 19 } 13 public EastNorth(double east, double north) { 14 super(east,north); 15 } 20 16 21 public double north() {22 return y;23 17 public double east() { 18 return x; 19 } 24 20 25 public EastNorth add(double dx, double dy) { 26 return new EastNorth(x+dx, y+dy); 27 } 28 29 public EastNorth interpolate(EastNorth en2, double proportion) { 30 return new EastNorth(this.x + proportion * (en2.x - this.x), 21 public double north() { 22 return y; 23 } 24 25 public EastNorth add(double dx, double dy) { 26 return new EastNorth(x+dx, y+dy); 27 } 28 29 public EastNorth interpolate(EastNorth en2, double proportion) { 30 return new EastNorth(this.x + proportion * (en2.x - this.x), 31 31 this.y + proportion * (en2.y - this.y)); 32 33 32 } 33 34 34 /** 35 * Returns the heading, in radians, that you have to use to get from 35 * Returns the heading, in radians, that you have to use to get from 36 36 * this EastNorth to another. Heading is mapped into [0, 2pi) 37 * 37 * 38 38 * @param other the "destination" position 39 * @return heading 39 * @return heading 40 40 */ 41 41 public double heading(EastNorth other) { 42 42 double hd = Math.atan2(other.east() - east(), other.north() - north()); 43 43 if(hd < 0) hd = 2 * Math.PI + hd; 44 return hd; 44 return hd; 45 45 } 46 46 47 47 public EastNorth sub(EastNorth en) { 48 48 return new EastNorth(en.east() - east(), en.north() - north()); 49 49 } 50 50 51 51 /** 52 52 * Returns an EastNorth representing the this EastNorth rotatedaround … … 54 54 * @param pivot the center of the rotation 55 55 * @param angle the angle of the rotation 56 * @return EastNorth rotated object 56 * @return EastNorth rotated object 57 57 */ 58 58 public EastNorth rotate(EastNorth pivot, double angle) { … … 65 65 return new EastNorth(nx, ny); 66 66 } 67 68 69 70 67 68 @Override public String toString() { 69 return "EastNorth[e="+x+", n="+y+"]"; 70 } 71 71 72 72 /** -
trunk/src/org/openstreetmap/josm/data/coor/LatLon.java
r1108 r1169 13 13 /** 14 14 * LatLon are unprojected latitude / longitude coordinates. 15 * 15 * 16 16 * This class is immutable. 17 * 17 * 18 18 * @author Imi 19 19 */ … … 23 23 private static DecimalFormat cDmsSecondFormatter = new DecimalFormat("00.0"); 24 24 private static DecimalFormat cDdFormatter = new DecimalFormat("###0.0000"); 25 26 /** 27 * Possible ways to display coordinates 28 */ 29 public enum CoordinateFormat { 30 DECIMAL_DEGREES {public String toString() {return tr("Decimal Degrees");}}, 31 DEGREES_MINUTES_SECONDS {public String toString() {return tr("Degrees Minutes Seconds");}}; 32 } 33 34 public static String dms(double pCoordinate) { 25 26 /** 27 * Possible ways to display coordinates 28 */ 29 public enum CoordinateFormat { 30 DECIMAL_DEGREES {public String toString() {return tr("Decimal Degrees");}}, 31 DEGREES_MINUTES_SECONDS {public String toString() {return tr("Degrees Minutes Seconds");}}; 32 } 33 34 public static String dms(double pCoordinate) { 35 35 36 36 double tAbsCoord = Math.abs(pCoordinate); … … 40 40 double tSeconds = (tTmpMinutes - tMinutes) * 60; 41 41 42 return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'" 42 return tDegree + "\u00B0" + cDmsMinuteFormatter.format(tMinutes) + "\'" 43 43 + cDmsSecondFormatter.format(tSeconds) + "\""; 44 } 44 } 45 45 46 46 public LatLon(double lat, double lon) { … … 51 51 return y; 52 52 } 53 53 54 54 public String latToString(CoordinateFormat d) { 55 55 switch(d) { … … 59 59 } 60 60 } 61 61 62 62 public double lon() { 63 63 return x; 64 64 } 65 65 66 66 public String lonToString(CoordinateFormat d) { 67 67 switch(d) { … … 71 71 } 72 72 } 73 74 73 74 75 75 76 76 /** … … 86 86 /** 87 87 * @return <code>true</code>, if the coordinate is outside the world, compared 88 89 90 91 return lat() < -Projection.MAX_LAT || lat() > Projection.MAX_LAT || 92 93 88 * by using lat/lon. 89 */ 90 public boolean isOutSideWorld() { 91 return lat() < -Projection.MAX_LAT || lat() > Projection.MAX_LAT || 92 lon() < -Projection.MAX_LON || lon() > Projection.MAX_LON; 93 } 94 94 95 /** 96 * @return <code>true</code> if this is within the given bounding box. 97 */ 98 public boolean isWithin(Bounds b) { 99 return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon(); 100 } 101 102 /** 103 * Computes the distance between this lat/lon and another point on the earth. 104 * Uses spherical law of cosines formula, not Haversine. 105 * @param other the other point. 106 * @return distance in metres. 107 */ 108 public double greatCircleDistance(LatLon other) { 109 return (Math.acos( 110 Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) + 111 Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) * 112 Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135); 113 } 114 115 /** 116 * Returns the heading, in radians, that you have to use to get from 117 * this lat/lon to another. 118 * 119 * @param other the "destination" position 120 * @return heading 121 */ 122 public double heading(LatLon other) { 123 double rv; 124 if (other.lat() == lat()) { 125 rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2); 126 } else { 127 rv = Math.atan((other.lon()-lon())/(other.lat()-lat())); 128 if (rv < 0) rv += Math.PI; 129 if (other.lon() < lon()) rv += Math.PI; 130 } 131 return rv; 132 } 95 /** 96 * @return <code>true</code> if this is within the given bounding box. 97 */ 98 public boolean isWithin(Bounds b) { 99 return lat() >= b.min.lat() && lat() <= b.max.lat() && lon() > b.min.lon() && lon() < b.max.lon(); 100 } 133 101 134 /** 135 * Returns this lat/lon pair in human-readable format. 136 * 137 * @return String in the format "lat=1.23456°, lon=2.34567°" 138 */ 139 public String toDisplayString() { 140 NumberFormat nf = NumberFormat.getInstance(); 141 nf.setMaximumFractionDigits(5); 142 return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°"; 143 } 144 145 @Override public String toString() { 146 return "LatLon[lat="+lat()+",lon="+lon()+"]"; 102 /** 103 * Computes the distance between this lat/lon and another point on the earth. 104 * Uses spherical law of cosines formula, not Haversine. 105 * @param other the other point. 106 * @return distance in metres. 107 */ 108 public double greatCircleDistance(LatLon other) { 109 return (Math.acos( 110 Math.sin(Math.toRadians(lat())) * Math.sin(Math.toRadians(other.lat())) + 111 Math.cos(Math.toRadians(lat()))*Math.cos(Math.toRadians(other.lat())) * 112 Math.cos(Math.toRadians(other.lon()-lon()))) * 6378135); 113 } 114 115 /** 116 * Returns the heading, in radians, that you have to use to get from 117 * this lat/lon to another. 118 * 119 * @param other the "destination" position 120 * @return heading 121 */ 122 public double heading(LatLon other) { 123 double rv; 124 if (other.lat() == lat()) { 125 rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2); 126 } else { 127 rv = Math.atan((other.lon()-lon())/(other.lat()-lat())); 128 if (rv < 0) rv += Math.PI; 129 if (other.lon() < lon()) rv += Math.PI; 130 } 131 return rv; 132 } 133 134 /** 135 * Returns this lat/lon pair in human-readable format. 136 * 137 * @return String in the format "lat=1.23456°, lon=2.34567°" 138 */ 139 public String toDisplayString() { 140 NumberFormat nf = NumberFormat.getInstance(); 141 nf.setMaximumFractionDigits(5); 142 return "lat=" + nf.format(lat()) + "°, lon=" + nf.format(lon()) + "°"; 143 } 144 145 @Override public String toString() { 146 return "LatLon[lat="+lat()+",lon="+lon()+"]"; 147 147 } 148 148 }
Note: See TracChangeset
for help on using the changeset viewer.