Revision 45732 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
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 |
} |
Also available in: Unified diff