comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/WKTUtils.java @ 806:2cea76f1112e

Added Javadoc in utils package. gnv-artifacts/trunk@888 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 08 Apr 2010 13:10:39 +0000
parents 9a828e5a2390
children 05bf8534a35a
comparison
equal deleted inserted replaced
805:bb7afd783321 806:2cea76f1112e
35 35
36 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer; 36 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
37 37
38 import org.apache.log4j.Logger; 38 import org.apache.log4j.Logger;
39 39
40 /**
41 * A helper class which supports some useful methods to work with wkt strings.
42 *
43 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
44 */
40 public abstract class WKTUtils { 45 public abstract class WKTUtils {
41 46
42 private static Logger log = Logger.getLogger(WKTUtils.class); 47 private static Logger log = Logger.getLogger(WKTUtils.class);
43 48
44 public static final double NAUTICAL_MILE = 1852.216d; 49 public static final double NAUTICAL_MILE = 1852.216d;
57 }; 62 };
58 63
59 public static final String DEFAULT_COORDINATE_TEMPLATE = 64 public static final String DEFAULT_COORDINATE_TEMPLATE =
60 "{0}\u00b0N {1}'' {2}\u00b0E {3}''"; 65 "{0}\u00b0N {1}'' {2}\u00b0E {3}''";
61 66
67 /**
68 * Checks the difference of two <code>Result</code>s.
69 *
70 * @param a A Result.
71 * @param b Another Result.
72 * @param indices Indices to be checked.
73 * @return true, if <i>a</i> and <i>b</i> are different - otherwise false.
74 */
62 public static boolean different(Result a, Result b, int [] indices) { 75 public static boolean different(Result a, Result b, int [] indices) {
63 for (int i = 0; i < indices.length; ++i) { 76 for (int i = 0; i < indices.length; ++i) {
64 String oa = a.getString(indices[i]); 77 String oa = a.getString(indices[i]);
65 String ob = b.getString(indices[i]); 78 String ob = b.getString(indices[i]);
66 79
77 } 90 }
78 } 91 }
79 return false; 92 return false;
80 } 93 }
81 94
95 /**
96 * Turns a wkt string into a coordinate.
97 *
98 * @param shape A wkt string.
99 * @return wkt string as coordinate.
100 */
82 public static Coordinate toCoordinate(String shape) { 101 public static Coordinate toCoordinate(String shape) {
83 try { 102 try {
84 return shape != null 103 return shape != null
85 ? ((Point)(new WKTReader().read(shape))).getCoordinate() 104 ? ((Point)(new WKTReader().read(shape))).getCoordinate()
86 : null; 105 : null;
92 log.error("cannot read WKT point", cce); 111 log.error("cannot read WKT point", cce);
93 } 112 }
94 return null; 113 return null;
95 } 114 }
96 115
116 /**
117 * Turns a wkt string into a polygon.
118 *
119 * @param shape A wkt string.
120 * @return wkt string as polygon.
121 */
97 public static Polygon toPolygon(String shape) { 122 public static Polygon toPolygon(String shape) {
98 try { 123 try {
99 return (Polygon)new WKTReader().read(shape); 124 return (Polygon)new WKTReader().read(shape);
100 } 125 }
101 catch (ParseException pe) { 126 catch (ParseException pe) {
105 log.error("cannot read WKT polygon", cce); 130 log.error("cannot read WKT polygon", cce);
106 } 131 }
107 return null; 132 return null;
108 } 133 }
109 134
135 /**
136 * Returns a distance in km.
137 *
138 * @param distance A distance.
139 * @return distance in km.
140 */
110 public static final double toKM(double distance) { 141 public static final double toKM(double distance) {
111 return (distance * NAUTICAL_MILE) / KILOMETER; 142 return (distance * NAUTICAL_MILE) / KILOMETER;
112 } 143 }
113 144
114 145
146 /**
147 * Turns a coordinate into a wkt string.
148 *
149 * @param coordinate A coordinate.
150 * @return the coordinate as wkt string.
151 */
115 public static String toWKT(Coordinate coordinate) { 152 public static String toWKT(Coordinate coordinate) {
116 StringBuilder sb = new StringBuilder("POINT("); 153 StringBuilder sb = new StringBuilder("POINT(");
117 sb.append(coordinate.x) 154 sb.append(coordinate.x)
118 .append(' ') 155 .append(' ')
119 .append(coordinate.y) 156 .append(coordinate.y)
120 .append(')'); 157 .append(')');
121 return sb.toString(); 158 return sb.toString();
122 } 159 }
160
123 161
124 public static final String indexBox( 162 public static final String indexBox(
125 java.awt.Point a, 163 java.awt.Point a,
126 java.awt.Point b, 164 java.awt.Point b,
127 String iName, 165 String iName,
358 log.error("cannot read WKT line string", cce); 396 log.error("cannot read WKT line string", cce);
359 } 397 }
360 return null; 398 return null;
361 } 399 }
362 400
401 /**
402 * Turns a wkt coordinate into a string format using the default locale of
403 * the virtual machine.
404 *
405 * @param wkt A wkt coordinate.
406 * @return A formatted coordinate.
407 */
363 public static String toText(String wkt) { 408 public static String toText(String wkt) {
364 return toText(Locale.getDefault(), wkt); 409 return toText(Locale.getDefault(), wkt);
365 } 410 }
366 411
412 /**
413 * Turns a point into a string format using the default locale of the
414 * virtual machine.
415 *
416 * @param p A point.
417 * @return A point formatted as string.
418 */
367 public static String toText(Point p) { 419 public static String toText(Point p) {
368 return toText(Locale.getDefault(), toWKT(p.getCoordinate())); 420 return toText(Locale.getDefault(), toWKT(p.getCoordinate()));
369 } 421 }
370 422
423 /**
424 * Turns a point into a string format using a given locale.
425 *
426 * @param locale A locale.
427 * @param p A point.
428 * @return a point formatted as string.
429 */
371 public static String toText(Locale locale, Point p) { 430 public static String toText(Locale locale, Point p) {
372 return toText(locale, toWKT(p.getCoordinate())); 431 return toText(locale, toWKT(p.getCoordinate()));
373 } 432 }
374 433
434 /**
435 * Turns a wkt coordiante into a formatted text using a given locale.
436 *
437 * @param locale A locale.
438 * @param wkt A wkt coordinate.
439 * @return a formatted coordinate.
440 */
375 public static String toText(Locale locale, String wkt) { 441 public static String toText(Locale locale, String wkt) {
376 String formattedCoordinate = null; 442 String formattedCoordinate = null;
377 try { 443 try {
378 Point p = (Point)new WKTReader().read(wkt); 444 Point p = (Point)new WKTReader().read(wkt);
379 double lat = p.getY(); 445 double lat = p.getY();
426 } 492 }
427 493
428 return null; 494 return null;
429 } 495 }
430 } 496 }
497 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org