D3 mercator function NaN - javascript

I'm not too familiar with the D3 mercator projection function, and I'm getting some unexpected results. I'd like to project the following latitude and longitude (somewhere in Mexico):
var geo = [19, -99]
I define my mercator projection using d3
var projection = d3.geo.mercator();
projection(geo)
> [529.7418836818384, NaN]
which is a point obviously not on a map. What exactly am I doing wrong? Thanks!

Javascript has it's lat and long round the opposite way to the rest of the world, so what you've given D3 is a longditue of 19 and a latitude of -99. Obviously there is no latitude of -99 hence the NaN. Now if you just reverse your geo variable to
var geo = [-99, 19]
all should be good.

Related

How to get real coordinates in OpenLayers?

In OpenLayers I use the ol.source.XYZ.
So I use a own URL with the Variables {x} {y} {z} and {a-z}. I want to render some pics with another Webserver.
But I don't understand what the Variables x y and z does tell me..
I need Coordinates in lat and lon. for example: 10.1234567 and 9.1234567. Google cant tell me, whether I can convert this? Or should I just use another OpenLayers solution?
OpenLayers give me the following values for example:
x=3, y=262144, z=19
These coordinates are tile coordinates. x and y are the column and row in the tile grid, and y is the zoom level. If you need to know latitude and longitude of the tile corners, you can use
var extent = source.getTileGrid().getTileCoordExtent([z, x, y]);
This will give you the extent in the view projection. To convert to geographic coordinates, use
ol.proj.transformExtent(extent, map.getView().getProjection, 'EPSG:4326');

Coordinates to Lat Long in Leaflet

I'm here because I'm trying to create a GTA V map with Leafletjs API. In deed, I've got my tiles that are ready. But the aim is to put markers on the map by using GTA V in-game coordinates. To achieve it, I need to convert the coordinates to LatLong but after many researches, I can't find a solution.
Example :
Thanks in advance.
Your coordinates are coming from different map projections. The Leaflet ones are most probably standard WGS 84 (SRID 4326). GTA V coordinates are obviously based on a different projection.
Coordinates can be transformed from one projection to anothers. But you need to know, which projection your coordinates are coming from, to do the math.
Here is an online converter provided with some common projections, and i tried your coordinates, but had no luck in coming close with these projections.
I tried this:
var latlng = new L.latLng(-43.1731, 6.6906);
var point = L.Projection.Mercator.project(latlng);
console.log(point);
// o.Point {x: 744794.1851014761, y: -5309112.129212381}
It is not close to your GTA V coordinates, but I'm not sure it were just for example.
You can use L.latlng function
http://leafletjs.com/reference.html#latlng

Openlayers - Getting Incorrect LonLat while transform?

I have trying to get Latitude and Longitude of a Point.
But While transforming it's not returning the correct LonLat. It's big number that is not a lonlat point for sure.
I have tried for some solutions but didn't get result.
What else could be failing?
JS Code I have Tried
map = createMap("deviceMap");
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection = new OpenLayers.Projection("EPSG:900913");
map.events.register('click', map, function handleMapClick(e) {
lonLat = self.map.getLonLatFromViewPortPx(e.xy).
transform(map.getProjectionObject(), toProjection);
prompt("",lonLat);
});
Finally got answer
map.events.register('click', map, function handleMapClick(e) {
var toProjection = new OpenLayers.Projection("EPSG:4326");
var lonLat = map.getLonLatFromPixel(e.xy).
transform(map.getProjectionObject(), toProjection);
});
EPSG:4326
should refer to WGS 84,
has decimal degree values
EPSG:900913
refers to WGS84 Web Mercator
has metric values, it is x/y axis coordinate system, so "high numbers" are expected
If I understand you right, you should have in variable lonLat a high number.
"LonLat" in OpenLayers does not mean, it will be only longitude/latitude, see documentation here:
lon {Number} The x-axis coordinate in map units. If your map is in a
geographic projection, this will be the Longitude. Otherwise, it will
be the x coordinate of the map location in your map units.
lat {Number} The y-axis coordinate in map units. If your map is in a
geographic projection, this will be the Latitude. Otherwise, it will
be the y coordinate of the map location in your map units.
So if you want to get a real LonLat coordinates, you should not convert it (and use EPSG:4326) or convert it to the other coordinate system, not EPSG:900913.
By the way, OpenLayers started to use 900918 (numeric equivalent to GOOGlE), It was define by Mr. Christopher Schmidt, firstly it was not accepted by European Petroleum Survey Group (EPSG). Then EPSG changed their mind and gave them number: 3857 - WGS84 Pseudo-Mercator.

Openlayers - Projection issues when getting latitude/longitude from a point

I'm trying to get the latitude/longitude from a draggable marker with Openlayers and OSM but I can't find the good settings for the projection conversion, what I am doing wrong ?
Here is the code: http://pastie.org/2300321 (see addMarker l140 & updateTargets l153) & and a little demo test.
If you submit an address, then drag the marker, the longitude and latitude are wrong. I tested a few different projections but I'm not sure what I've to useā€¦
I think the problem is inside updateTargets method:
var point = this.feature.geometry;
var pixel = new OpenLayers.Pixel(point.x, point.y);
var coord = this.map.getLonLatFromPixel(pixel).transform(
new OpenLayers.Projection("EPSG:900913"),
new OpenLayers.Projection("EPSG:4326")
);
this.feature.geometry is already specified in lon/lat coordinates, not in pixels. So I suggest that you skip second line and do the conversion from OpenStreetMap projection to lon/lat directly on geometry object:
var coord = this.feature.geometry.transform(
new OpenLayers.Projection("EPSG:900913"),
new OpenLayers.Projection("EPSG:4326")
);

Getting Y position from Latitude on a spherical projected map

In Javascript.
I have a map of the world that is 1024 x 1024px - It is a spherical Mercator projection.
Im looking to convert long/lat to x,y for this map. I have a bunch of coordinates that I need to overlay on top of it.
Calculating longitude was very easy, and I am doing it like so:
pos.x = ((long + 180)/ 360) * 1024;
I just need to same for lat --> y
I looked around a saw a lot of reference to openlayers, but didn't see such a conversion.
To boil it down:
var mercator = function(lat, lng) {
return [lng, Math.log( (Math.sin(lat) + 1.0) / Math.cos(lat)) ] ;
};
Lat and long must be expressed in radians of course.
You can find the corresponding formulas in this wikipedia article: Mercator Projection.

Categories

Resources