I have written some code which offset a certain latitude and longitude with a distance in centimeters. What I am trying to do is based on a coordinate and a vector create two new points along the line described by the vector.
I believe I have succeeded in doing this but the results I am getting are a bit odd. I am not very experienced with map coordinates and projections so I am posting here to see if my results 'look' right.
The following link shows an image I have of two lines:
One extends mostly along the longitude and the other mostly along the latitude. Both lines should have a length of 1 meter. However the red line is noticeably longer than the green line.
The lines are located around longitude 6.080695759673799 and latitude 52.501029191616425 (The Netherlands). I know with the projection I am using (Web Mercator I believe) landmasses become stretched along the poles. Is this what I am seeing with the red line? Is the red line stretched because we are closer to the poles than we are at the equator?
I use a combination of two functions to offset the latitude and longitude. I found the code in this forum post: https://sharpmap.codeplex.com/discussions/395118 I've then rewritten the code to Javascript like this:
function toMercator(latLong)
{
var x = latLong.y * 20037508.34 / 180;
var y = Math.log(Math.tan((90 + latLong.x) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return {x: x, y: y};
};
function toLatLong(mercator)
{
var lon = (mercator.x / 20037508.34) * 180;
var lat = (mercator.y / 20037508.34) * 180;
lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
return {
x: lat,
y: lon
};
};
To offset the point I do this:
var lat = 2;
var long = 5;
var xOffsetCM = 50;
var yOffsetCM = 50;
var mercator = toMercator({x: lat, y: long});
// Add offset
mercator.x += normalizedVector.x * (50 / 100); // divide offset by 100 to get meters
mercator.y += normalizedVector.y * (50 / 100);
var latLong = toLatLong(mercator);
After the last function call latLong should contain the new coordinate.