Ignore:
Timestamp:
2021-08-01T22:03:28+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21172 - Add method to create geometric distances from a point (patch by taylor.smock)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r17141 r18109  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
     3
     4import static org.openstreetmap.josm.data.projection.Ellipsoid.WGS84;
    35
    46import java.awt.geom.Area;
     
    2628import org.openstreetmap.josm.data.coor.EastNorth;
    2729import org.openstreetmap.josm.data.coor.ILatLon;
     30import org.openstreetmap.josm.data.coor.LatLon;
    2831import org.openstreetmap.josm.data.osm.BBox;
    2932import org.openstreetmap.josm.data.osm.DataSet;
     
    15571560
    15581561    /**
     1562     * Create a new LatLon at a specified distance. Currently uses WGS84, but may change randomly in the future.
     1563     * This does not currently attempt to be hugely accurate. The actual location may be off
     1564     * depending upon the distance and the elevation, but should be within 0.0002 meters.
     1565     *
     1566     * @param original The originating point
     1567     * @param angle The angle (from true north) in radians
     1568     * @param offset The distance to the new point in the current projection's units
     1569     * @return The location at the specified angle and distance from the originating point
     1570     * @since 18109
     1571     */
     1572    public static ILatLon getLatLonFrom(final ILatLon original, final double angle, final double offset) {
     1573        final double meterOffset = ProjectionRegistry.getProjection().getMetersPerUnit() * offset;
     1574        final double radianLat = Math.toRadians(original.lat());
     1575        final double radianLon = Math.toRadians(original.lon());
     1576        final double angularDistance = meterOffset / WGS84.a;
     1577        final double lat = Math.asin(Math.sin(radianLat) * Math.cos(angularDistance)
     1578                + Math.cos(radianLat) * Math.sin(angularDistance) * Math.cos(angle));
     1579        final double lon = radianLon + Math.atan2(Math.sin(angle) * Math.sin(angularDistance) * Math.cos(radianLat),
     1580                Math.cos(angularDistance) - Math.sin(radianLat) * Math.sin(lat));
     1581        return new LatLon(Math.toDegrees(lat), Math.toDegrees(lon));
     1582    }
     1583
     1584    /**
    15591585     * Calculate closest distance between a line segment s1-s2 and a point p
    15601586     * @param s1 start of segment
Note: See TracChangeset for help on using the changeset viewer.