@@ -57,6 +57,13 @@ public class GeometryUtils {
5757 */
5858 public static final double DEFAULT_EPSILON = 0.000000000000001 ;
5959
60+ /**
61+ * Half the world distance in either direction
62+ *
63+ * @since 2.1.0
64+ */
65+ public static final double WEB_MERCATOR_HALF_WORLD_WIDTH = 20037508.342789244 ;
66+
6067 /**
6168 * Get the dimension of the Geometry, 0 for points, 1 for curves, 2 for
6269 * surfaces. If a collection, the largest dimension is returned.
@@ -178,9 +185,10 @@ public static Point getDegreesCentroid(Geometry geometry) {
178185 * Example: For WGS84 provide a max x of 180.0. Resulting x values will be
179186 * in the range: -540.0 <= x <= 540.0
180187 *
181- * Example: For web mercator provide a world width of 20037508.342789244.
182- * Resulting x values will be in the range: -60112525.028367732 <= x
183- * <= 60112525.028367732
188+ * Example: For web mercator provide a world width of
189+ * {@link GeometryUtils#WEB_MERCATOR_HALF_WORLD_WIDTH}. Resulting x values
190+ * will be in the range: -60112525.028367732 <= x <=
191+ * 60112525.028367732
184192 *
185193 * @param geometry
186194 * geometry
@@ -369,9 +377,10 @@ private static void minimize(PolyhedralSurface polyhedralSurface,
369377 * Example: For WGS84 provide a max x of 180.0. Resulting x values will be
370378 * in the range: -180.0 <= x <= 180.0.
371379 *
372- * Example: For web mercator provide a world width of 20037508.342789244.
373- * Resulting x values will be in the range: -20037508.342789244 <= x
374- * <= 20037508.342789244.
380+ * Example: For web mercator provide a world width of
381+ * {@link GeometryUtils#WEB_MERCATOR_HALF_WORLD_WIDTH}. Resulting x values
382+ * will be in the range: -20037508.342789244 <= x <=
383+ * 20037508.342789244.
375384 *
376385 * @param geometry
377386 * geometry
@@ -1149,7 +1158,7 @@ private static boolean pointOnPath(Point point, List<Point> points,
11491158 * @param line2
11501159 * second line
11511160 * @return intersection point or null if no intersection
1152- * @since 2.0.7
1161+ * @since 2.1.0
11531162 */
11541163 public static Point intersection (Line line1 , Line line2 ) {
11551164 return intersection (line1 .startPoint (), line1 .endPoint (),
@@ -1168,7 +1177,7 @@ public static Point intersection(Line line1, Line line2) {
11681177 * @param line2Point2
11691178 * second point of the second line
11701179 * @return intersection point or null if no intersection
1171- * @since 2.0.7
1180+ * @since 2.1.0
11721181 */
11731182 public static Point intersection (Point line1Point1 , Point line1Point2 ,
11741183 Point line2Point1 , Point line2Point2 ) {
@@ -1194,6 +1203,72 @@ public static Point intersection(Point line1Point1, Point line1Point2,
11941203 return intersection ;
11951204 }
11961205
1206+ /**
1207+ * Convert a point in degrees to a point in meters
1208+ *
1209+ * @param point
1210+ * point in degrees
1211+ * @return point in meters
1212+ * @since 2.1.0
1213+ */
1214+ public static Point degreesToMeters (Point point ) {
1215+ Point value = degreesToMeters (point .getX (), point .getY ());
1216+ value .setZ (point .getZ ());
1217+ value .setM (point .getM ());
1218+ return value ;
1219+ }
1220+
1221+ /**
1222+ * Convert a coordinate in degrees to a point in meters
1223+ *
1224+ * @param x
1225+ * x value in degrees
1226+ * @param y
1227+ * y value in degrees
1228+ * @return point in meters
1229+ * @since 2.1.0
1230+ */
1231+ public static Point degreesToMeters (double x , double y ) {
1232+ double xValue = x * WEB_MERCATOR_HALF_WORLD_WIDTH / 180 ;
1233+ double yValue = Math .log (Math .tan ((90 + y ) * Math .PI / 360 ))
1234+ / (Math .PI / 180 );
1235+ yValue = yValue * WEB_MERCATOR_HALF_WORLD_WIDTH / 180 ;
1236+ return new Point (xValue , yValue );
1237+ }
1238+
1239+ /**
1240+ * Convert a point in meters to a point in degrees
1241+ *
1242+ * @param point
1243+ * point in meters
1244+ * @return point in degrees
1245+ * @since 2.1.0
1246+ */
1247+ public static Point metersToDegrees (Point point ) {
1248+ Point value = metersToDegrees (point .getX (), point .getY ());
1249+ value .setZ (point .getZ ());
1250+ value .setM (point .getM ());
1251+ return value ;
1252+ }
1253+
1254+ /**
1255+ * Convert a coordinate in meters to a point in degrees
1256+ *
1257+ * @param x
1258+ * x value in meters
1259+ * @param y
1260+ * y value in meters
1261+ * @return point in degrees
1262+ * @since 2.1.0
1263+ */
1264+ public static Point metersToDegrees (double x , double y ) {
1265+ double xValue = x * 180 / WEB_MERCATOR_HALF_WORLD_WIDTH ;
1266+ double yValue = y * 180 / WEB_MERCATOR_HALF_WORLD_WIDTH ;
1267+ yValue = Math .atan (Math .exp (yValue * (Math .PI / 180 ))) / Math .PI * 360
1268+ - 90 ;
1269+ return new Point (xValue , yValue );
1270+ }
1271+
11971272 /**
11981273 * Determine if the geometries contain a Z value
11991274 *
0 commit comments