Revision 45732

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.api/src/main/java/org/gvsig/fmap/geom/GeometryUtils.java
25 25

  
26 26
import java.awt.geom.Point2D;
27 27
import java.util.List;
28
import java.util.Locale;
28 29
import java.util.Objects;
30
import java.util.regex.Matcher;
31
import java.util.regex.Pattern;
29 32
import org.apache.commons.lang3.StringUtils;
30 33
import org.cresques.cts.IProjection;
31 34
import org.gvsig.euclidean.EuclideanLine2D;
......
887 890
        return ((b.getX()-a.getX())/(c.getX()-b.getX()) == (b.getY()-a.getY())/(c.getY()-b.getY()));
888 891
    }
889 892

  
893
    /**
894
     * Returns a formatted string using the specified format string and coordinate values.Conversion options are:
895
     * <ul>
896
     * <li><code>%[flags][width][.precision]d</code> The result is formatted as a integer number with the degress of the coordinate
897
     * <li><code>%[flags][width][.precision]m</code> The result is formatted as a integer number with the minutes of the coordinate
898
     * <li><code>%[flags][width][.precision]s</code> The result is formatted as a decimal number with the seconds of the coordinate. 
899
     * <li><code>%[flags][width][.precision]D</code> The result is formatted as a decimal number with the degress
900
     * <li><code>%-</code> The result is formatted with the sign if it is negative
901
     * <li><code>%+</code> The result is formatted with the sign 
902
     * </ul>
903
     * 
904
     * Example:
905
     * <table>
906
     * <tr><td>formatCoordinate("%-%d? %m' %s'' N", 39.89)<td>"39? 53' 24.000'' N"
907
     * <tr><td>formatCoordinate("%-%d? %m' %.0s'' N", 39.89)<td>"39? 53' 24'' N"
908
     * <tr><td>formatCoordinate("%+%d? %m' %.2s'' N (%4.4D)", 39.89)<td>"+39? 53' 24.00'' N (39.8900)"
909
     * <tr><td>formatCoordinate("%-%d? %m' N", 39.89)<td> "39? 53' N"
910
     * <tr><td>formatCoordinate("%-%d? %m' N", -39.89)<td> "-39? 53' N"
911
     * <tr><td>formatCoordinate("%-%d? %m' N (%4.4D)", 39.89)<td> "39? 53' N (39.8900)"
912
     * </table>
913
     * 
914
     * @param fmt
915
     * @param coordinate 
916
     * @return  
917
     */
918
    public static String formatCoordinate(String fmt, double coordinate) {
919
        return formatCoordinate(fmt, null, coordinate);
920
    }
921
    
922
    public static String formatCoordinate(String fmt, String NS, double coordinate) {
923

  
924
        double value = Math.abs(coordinate);
925

  
926
        int deg = (int)(value);
927
        double min = (value - deg) * 60;
928
        double secs = (min - (int) min)*60;
929
        
930
        String r = fmt;
931
        r = r.replace("%-",coordinate<0 ? "-" : "");
932
        r = r.replace("%+",coordinate<0 ? "-" : "+");
933
        if( StringUtils.isBlank(NS) || value==0) {
934
            r = r.replace("%N","");
935
        } else {
936
            r = r.replace("%N",coordinate>0 ? NS.substring(0,1)  : NS.substring(1,2) );
937
        }
938
        r = formatCoordinate_replace(r,"s","f", secs);
939
        r = formatCoordinate_replace(r,"d","d", deg);
940
        r = formatCoordinate_replace(r,"m","d", (int)min);
941
        r = formatCoordinate_replace(r,"D","f", value);        
942
        return r;
943
    }
944
    
945
    private static String formatCoordinate_replace(String fmt, String source_conversion, String target_conversion, Object value) {
946
        String s = fmt;
947
        Pattern p = Pattern.compile("%([-+#0,(]?+[0-9]*[.]*[0-9]*)" + source_conversion);
948
        Matcher macher = p.matcher(s);
949
        if (macher.find()) {
950
            String mod = macher.group(1);
951
            if (StringUtils.isBlank(mod)) {
952
                if( value instanceof Double ) {
953
                    s = fmt.replace("%" + source_conversion, String.format(Locale.ENGLISH,"%.3f", value));
954
                } else {
955
                    s = fmt.replace("%" + source_conversion, String.valueOf(value));
956
                }
957
            } else {
958
                String x = String.format(Locale.ENGLISH,"%" + mod + target_conversion, value);
959
                s = macher.replaceAll(x);
960
            }
961
            return s;
962
        }
963
        return s;
964
    }
965

  
966

  
967
//    public static void main(String[] args) throws GeometryException {
968
//        String s;
969
//        s =  GeometryUtils.formatCoordinate("%-%d? %m' %s'' N", 39.89);
970
//        System.out.println(s);
971
//        s =  GeometryUtils.formatCoordinate("%-%d? %m' %.0s'' N", 39.89);
972
//        System.out.println(s);
973
//        s =  GeometryUtils.formatCoordinate("%+%d? %m' %.2s'' N (%4.4D)", 39.89);
974
//        System.out.println(s);
975
//        s =  GeometryUtils.formatCoordinate("%-%d? %m' N", -39.89);
976
//        System.out.println(s);
977
//        s =  GeometryUtils.formatCoordinate("%-%d? %m' N (%4.4D)", 39.89);
978
//        System.out.println(s);
979
//    }
980

  
890 981
}
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/project/documents/view/toolListeners/StatusBarListener.java
33 33

  
34 34
import org.gvsig.andami.PluginServices;
35 35
import org.gvsig.andami.ui.mdiFrame.MainFrame;
36
import org.gvsig.fmap.geom.GeometryUtils;
36 37
import org.gvsig.fmap.mapcontext.MapContext;
37 38
import org.gvsig.fmap.mapcontrol.MapControl;
38 39
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
......
92 93
 */
93 94
public class StatusBarListener implements PointListener {
94 95
    
95
    private static Logger logger =
96
        LoggerFactory.getLogger(StatusBarListener.class);
97
	/**
96
    private static Logger logger = LoggerFactory.getLogger(StatusBarListener.class);
97

  
98
    private static final String DEGRESS_FORMAT = "%-%d? %m' %.0s''";
99
    
100
    /**
98 101
	 * Reference to the <code>MapControl</code> object that uses.
99 102
	 */
100 103
	private MapControl mapControl = null;
......
245 248
		return axisText;
246 249
	}
247 250

  
248
	/**
249
	 * <p>Converts a decimal value (expected latitude or longitude) in degrees, and formats it according this pattern:<br>
250
	 *  <code><b><i>S?G? M' S''</i></b></code>, having:<br>
251
	 *  <ul>
252
	 *   <li><i>S?</i> : optionally, if the value is negative, sets a "-" symbol.</li>
253
	 *   <li><i>G</i> : equivalent grades.</li>
254
	 *   <li><i>M</i> : equivalent minutes.</li>
255
	 *   <li><i>S</i> : equivalent seconds.</li>
256
	 *  </ul>
257
	 * </p>
258
	 *
259
	 * @param d the latitude or longitude value to convert
260
	 *
261
	 * @return value formatted in degrees
262
	 */
263
	private String formatDegrees(double d) {
264
		String signo = d<0 ? "-" : "";
265
		d = Math.abs(d);
266
		long grado = 0;
267
		double minuto = 0;
268
		double segundo = 0;
251
//	/**
252
//	 * <p>Converts a decimal value (expected latitude or longitude) in degrees, and formats it according this pattern:<br>
253
//	 *  <code><b><i>S?G? M' S''</i></b></code>, having:<br>
254
//	 *  <ul>
255
//	 *   <li><i>S?</i> : optionally, if the value is negative, sets a "-" symbol.</li>
256
//	 *   <li><i>G</i> : equivalent grades.</li>
257
//	 *   <li><i>M</i> : equivalent minutes.</li>
258
//	 *   <li><i>S</i> : equivalent seconds.</li>
259
//	 *  </ul>
260
//	 * </p>
261
//	 *
262
//	 * @param d the latitude or longitude value to convert
263
//	 *
264
//	 * @return value formatted in degrees
265
//	 */
266
//	private String formatDegrees(double d) {
267
////		String signo = d<0 ? "-" : "";
268
////		d = Math.abs(d);
269
////		long grado = 0;
270
////		double minuto = 0;
271
////		double segundo = 0;
272
////
273
////		grado = (long)(d);
274
////		minuto = (d - grado) * 60;
275
////		segundo = (minuto - (long) minuto)*60;
276
//////		System.out.println("Grados: " + grado);
277
//////		System.out.println("Minutos: " + minuto);
278
//////		System.out.println("Segundos: " + segundo);
279
////		return signo+grado+"? "+(long) minuto+"' "+(long)segundo+"''";
280
//                return GeometryUtils.formatCoordinate("%-%d? %m' %s''", d);
281
//	}
269 282

  
270
		grado = (long)(d);
271
		minuto = (d - grado) * 60;
272
		segundo = (minuto - (long) minuto)*60;
273
//		System.out.println("Grados: " + grado);
274
//		System.out.println("Minutos: " + minuto);
275
//		System.out.println("Segundos: " + segundo);
276
		return signo+grado+"? "+(long) minuto+"' "+(long)segundo+"''";
277
	}
278

  
279 283
	/**
280 284
	 * <p>Returns the coordinates equivalent to <code>p</code>:
281 285
	 *  <ul>
......
296 300
		String[] coords=new String[2];
297 301
		IProjection iProj = mapControl.getMapContext().getProjection();
298 302
		if (!iProj.isProjected()) {
299
			coords[0]=String.valueOf(formatDegrees(p.getX()));
300
			coords[1]=String.valueOf(formatDegrees(p.getY()));
303
			coords[0]=String.valueOf(GeometryUtils.formatCoordinate(DEGRESS_FORMAT, p.getX()));
304
			coords[1]=String.valueOf(GeometryUtils.formatCoordinate(DEGRESS_FORMAT, p.getY()));
301 305
		} else {
302 306
			double[] trans2Meter=MapContext.getDistanceTrans2Meter();
303 307
			if (PluginServices.getText(this,MapContext.getDistanceNames()[mapControl.getViewPort().getDistanceUnits()]).equals(PluginServices.getText(this,"Grados"))) {
......
305 309
				Point2D pgeo = null;
306 310
				try {
307 311
				    pgeo = iProj.toGeo(p);
308
	                coords[0]=String.valueOf(formatDegrees(pgeo.getX()));
309
	                coords[1]=String.valueOf(formatDegrees(pgeo.getY()));
312
	                coords[0]=String.valueOf(GeometryUtils.formatCoordinate(DEGRESS_FORMAT, pgeo.getX()));
313
	                coords[1]=String.valueOf(GeometryUtils.formatCoordinate(DEGRESS_FORMAT, pgeo.getY()));
310 314
				} catch (Exception exc) {
311 315
				    
312 316
				    if ((System.currentTimeMillis() - lastLogTime) > 5000) {

Also available in: Unified diff