How to generate coordinates in between two known points
Destination point given distance and bearing from start point applied to your problem:
class Numeric
def to_rad
self * Math::PI / 180
end
def to_deg
self * 180 / Math::PI
end
end
include Math
R = 6371.0
def waypoint(φ1, λ1, θ, d)
φ2 = asin( sin(φ1) * cos(d/R) + cos(φ1) * sin(d/R) * cos(θ) )
λ2 = λ1 + atan2( sin(θ) * sin(d/R) * cos(φ1), cos(d/R) - sin(φ1) * sin(φ2) )
λ2 = (λ2 + 3 * Math::PI) % (2 * Math::PI) - Math::PI # normalise to -180..+180°
[φ2, λ2]
end
φ1, λ1 = -33.to_rad, -71.6.to_rad # Valparaíso
φ2, λ2 = 31.4.to_rad, 121.8.to_rad # Shanghai
d = R * acos( sin(φ1) * sin(φ2) + cos(φ1) * cos(φ2) * cos(λ2 - λ1) )
θ = atan2( sin(λ2 - λ1) * cos(φ2), cos(φ1) * sin(φ2) - sin(φ1) * cos(φ2) * cos(λ2 - λ1) )
waypoints = (0..d).step(2000).map { |d| waypoint(φ1, λ1, θ, d) }
markers = waypoints.map { |φ, λ| "#{φ.to_deg},#{λ.to_deg}" }.join("|")
puts "http://maps.googleapis.com/maps/api/staticmap?size=640x320&sensor=false&markers=#{markers}"
Generates a Google Static Maps link with the waypoints from Valparaíso to Shanghai every 2,000 km:
http://maps.googleapis.com/maps/api/staticmap?size=640x320&sensor=false&markers=-33.0,-71.60000000000002|-32.54414813683714,-93.02142653011552|-28.59922979115139,-113.43958859125276|-21.877555679819015,-131.91586675556778|-13.305784544363858,-148.5297601858932|-3.7370081151180683,-163.94988578467394|6.094273692291354,-179.03345538133888|15.493534924596633,165.33401731030006|23.70233917422386,148.3186618914762|29.83806632244171,129.34766276764626
Generate coordinates in between two known points
Consider these functions
and this fiddle:
var gis = {
/**
* All coordinates expected EPSG:4326
* @param {Array} start Expected [lon, lat]
* @param {Array} end Expected [lon, lat]
* @return {number} Distance - meter.
*/
calculateDistance: function(start, end) {
var lat1 = parseFloat(start[1]),
lon1 = parseFloat(start[0]),
lat2 = parseFloat(end[1]),
lon2 = parseFloat(end[0]);
return gis.sphericalCosinus(lat1, lon1, lat2, lon2);
},
/**
* All coordinates expected EPSG:4326
* @param {number} lat1 Start Latitude
* @param {number} lon1 Start Longitude
* @param {number} lat2 End Latitude
* @param {number} lon2 End Longitude
* @return {number} Distance - meters.
*/
sphericalCosinus: function(lat1, lon1, lat2, lon2) {
var radius = 6371e3; // meters
var dLon = gis.toRad(lon2 - lon1),
lat1 = gis.toRad(lat1),
lat2 = gis.toRad(lat2),
distance = Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(dLon)) * radius;
return distance;
},
/**
* @param {Array} coord Expected [lon, lat] EPSG:4326
* @param {number} bearing Bearing in degrees
* @param {number} distance Distance in meters
* @return {Array} Lon-lat coordinate.
*/
createCoord: function(coord, bearing, distance) {
/** http://www.movable-type.co.uk/scripts/latlong.html
* φ is latitude, λ is longitude,
* θ is the bearing (clockwise from north),
* δ is the angular distance d/R;
* d being the distance travelled, R the earth’s radius*
**/
var
radius = 6371e3, // meters
δ = Number(distance) / radius, // angular distance in radians
θ = gis.toRad(Number(bearing));
φ1 = gis.toRad(coord[1]),
λ1 = gis.toRad(coord[0]);
var φ2 = Math.asin(Math.sin(φ1)*Math.cos(δ) +
Math.cos(φ1)*Math.sin(δ)*Math.cos(θ));
var λ2 = λ1 + Math.atan2(Math.sin(θ) * Math.sin(δ)*Math.cos(φ1),
Math.cos(δ)-Math.sin(φ1)*Math.sin(φ2));
// normalise to -180..+180°
λ2 = (λ2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
return [gis.toDeg(λ2), gis.toDeg(φ2)];
},
/**
* All coordinates expected EPSG:4326
* @param {Array} start Expected [lon, lat]
* @param {Array} end Expected [lon, lat]
* @return {number} Bearing in degrees.
*/
getBearing: function(start, end){
var
startLat = gis.toRad(start[1]),
startLong = gis.toRad(start[0]),
endLat = gis.toRad(end[1]),
endLong = gis.toRad(end[0]),
dLong = endLong - startLong;
var dPhi = Math.log(Math.tan(endLat/2.0 + Math.PI/4.0) /
Math.tan(startLat/2.0 + Math.PI/4.0));
if (Math.abs(dLong) > Math.PI) {
dLong = (dLong > 0.0) ? -(2.0 * Math.PI - dLong) : (2.0 * Math.PI + dLong);
}
return (gis.toDeg(Math.atan2(dLong, dPhi)) + 360.0) % 360.0;
},
toDeg: function(n) { return n * 180 / Math.PI; },
toRad: function(n) { return n * Math.PI / 180; }
};
So if you want to calculate a new coordinate, it can be like so:
var start = [15, 38.70250];
var end = [21.54967, 38.70250];
var total_distance = gis.calculateDistance(start, end); // meters
var percent = 10;
var distance = (percent / 100) * total_distance;
var bearing = gis.getBearing(start, end);
var new_coord = gis.createCoord(icon_coord, bearing, distance);
GPS coordinates of a point between two known points
You can find all necessary information on this excellent site.
Look at section Bearing, then Destination point given distance and bearing from start point.
You might probably be interested in Intermediate point, that finds point at any fraction of the big circle arc.
How to calculate coordinates of N equidistant points along a straight line between 2 coordinates on a map?
This is how I solved it -
fun findEquidistantPoints(latLng1: LatLng, latLng2: LatLng, pointCount: Int): ArrayList<LatLng> {
if (pointCount < 0)
throw IllegalArgumentException("PointCount cannot be less than 0")
val points = ArrayList<LatLng>()
val displacement = latLng1.displacementFromInMeters(latLng2)
val distanceBetweenPoints = displacement / (pointCount + 1)
for (i in 1..pointCount) {
val t = (distanceBetweenPoints * i) / displacement
points.add(LatLng(
(1 - t) * latLng1.latitude + t * latLng2.latitude,
(1 - t) * latLng1.longitude + t * latLng2.longitude
))
}
return points
}
Get latitude and longitude points along a line between two points, by percentage
Warning : this works on a linear coordinates. As Ollie Jones mentioned, while this is a reasonable approximation for short distances (or for certain cases depending on your projection), this won't work for long distance or if you want a very accurate point at percent
The function you are looking for is pointAtPercent. Red is the start point (your center circle) and green the end point (your end circles)
var ctx = document.getElementById("myChart").getContext("2d");
function drawPoint(color, point) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(point.x, point.y, 5, 0, 2 * Math.PI, false);
ctx.fill();
}
function drawLine(point1, point2) {
ctx.strokeStyle = 'gray';
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(point1.x, point1.y);
ctx.lineTo(point2.x, point2.y);
ctx.stroke();
}
function pointAtPercent(p0, p1, percent) {
drawPoint('red', p0);
drawPoint('green', p1);
drawLine(p0, p1);
var x;
if (p0.x !== p1.x)
x = p0.x + percent * (p1.x - p0.x);
else
x = p0.x;
var y;
if (p0.y !== p1.y)
y = p0.y + percent * (p1.y - p0.y);
else
y = p0.y;
var p = {
x: x,
y: y
};
drawPoint('blue', p);
return p;
}
pointAtPercent({ x: 50, y: 25 }, { x: 200, y: 300 }, 0.2)
pointAtPercent({ x: 150, y: 25 }, { x: 300, y: 100 }, 0.6)
pointAtPercent({ x: 650, y: 300 }, { x: 100, y: 400 }, 0.4)
Fiddle - https://jsfiddle.net/goev47aL/
Coordinates of a point between two other points and know the distance from starting point and target point
Find length of AC and apply formula to x and y coordinates
H = A + (C-A)* Len(AH) / Len(AC)
It is linear interpolation. Geometrically - law of similar triangles. Hypotenuses ratio (length) is equal to cathetus ratio (coordinate)
Multiple Points Along Line between two known Geo Coords?
You can use formulae from spherical geometry as they are presented e.g. in Calculate distance, bearing and more between Latitude/Longitude points. In the following I will refer to this page.
- Calculate the bearing
theta
between the two given pointsP1
andP2
(use formula for Bearing). - Calculate the distance
d
between the two given pointsP1
andP2
(use formula for Distance). - Calculate any point
P
on the line betweenP1
andP2
, given the distanceD
fromP1
, in your caseD = d/6, D = 2d/6, ...
Use the formula under Destination point given distance and bearing from start point.
ADDED: A running JS program with source code is on jsfiddle.
Derive new coordinates increasing a distance using two known points
library(sf)
library(mapview)
library(dplyr)
library(geosphere)
# test: what are we working with here?
test_df <- data.frame(point = 0:1, lon = c(lon_0, lon_1), lat = c(lat_0, lat_1))
test_df %>% sf::st_as_sf(coords = c("lon", "lat"), crs = 4326) %>% mapview::mapview()
# initialise points
point0 <- c(lon_0, lat_0)
point1 <- c(lon_1, lat_1)
#calculate bearing 0 >> 1
bearing0_1 <- geosphere::bearing(point0, point1)
#[1] 123.9916
# Calculate new point with calulated bearing ans distance
# 250 MN = 463000.2 metres
point2 <- as.vector(geosphere::destPoint(p = point0, b = bearing0_1, d = 463000.2))
# test output
rbind(point0, point1, point2) %>% as.data.frame(col.names = c("lon", "lat")) %>%
dplyr::mutate(point = 0:2) %>%
sf::st_as_sf(coords = c(1, 2), crs = 4326) %>% mapview::mapview()
Related Topics
Heroku App Fails to Start - 'Require': No Such File to Load -- Sinatratestapp (Loaderror)
Rails 3: Call Functions Inside Controllers
Do Ruby 1.8 and 1.9 Have the Same Hash Code for a String
Ruby: Write Escaped String to Yaml
Star Rating in Ajax with Ruby on Rails
Why Is Devise Not Displaying Authentication Errors on Sign in Page
Rails: Switch Connection on Each Request But Keep a Connection Pool
How to Find Where a Ruby Method Is Declared
Rails Can't Login to Postgresql - Pg::Error - Password - Correct Info
How to Authorize a Google Service Account Without the Default Credentials File
Find Memory Leak in a Ruby on Rails Project
Using Ruby, Reading a File, Containing Name/Value Pairs into a Hash
Singleton Method VS. Class Method
Strictly Convert String to Integer (Or Nil)
Differencebetween "Rails S" and "Bundle Exec Rails S"
Are There Better Ways to Prevent 'Yield' When No Block Is Passed In
Gem::Ext::Builderror: Error: Failed to Build Gem Native Extension Bcrypt-Ruby