Convert xyz coordinate of a tile to longitude/latitude
Asked Answered
D

1

6

I wanted to make a map using openlayers but center it a unique way. For example I have a z/x/y coordinate of 12/2045/-1362, how do I convert it to longitude/latitude? It's quite the polar opposite of this: How to get X Y Z coordinates of tile by click on Leaflet map

It's quite hard for me to get the logic of the above link and invert it. I hope someone here has an experience or a ready-made formula for this. Thanks

Later I'll this in rendering the center of my map like this:

var z = 12;
var x = 2045;
var y = -1362;

function convertXYZtoCoor(z, x, y) {
    // code here
    return [lng, lat];
}

var coor = convertXYZtoCoor(z, x, y);
var view = new ol.View({
                    center: ol.proj.transform(
                        [coor[0], coor[1]], 'EPSG:4326', 'EPSG:3857'),
                    zoom: z
            });

var map = new ol.Map({
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM()
                    })
                ],
                target: 'map',
                view: view
          });

Hope my question is understood more thanks.

Edit: Added code

Decent answered 17/8, 2015 at 9:11 Comment(8)
For me, it's not clear, are you looking for permalink?Zurek
I read the link sorry. No, you take the existing code from the link i created and just change the center value based on tile value 12/2045/-1362 by converting it to lat/lng. Is this possible?Decent
I don't think so. Let's see what others tell, in the meanwhile my guess is that you rethink your use case.Zurek
Where/How did you get this 12/2045/-1362?Zurek
openlayers.org/en/v3.5.0/examples/canvas-tiles.html?q=tiles check this one, by default the zoom level here is 10, i just zoomed it further to 12. Just edited my question further.. What my client wants to happen is that given a particular xyz tile coordinate, I would display it using openlayers which that coordinate should be the value of the ol.view so that tile is the center of the view.Decent
Look, you said "given a particular xyz tile coordinate", I ask you (I've checked the example) how do you get this info? This is dynamic, right? The ol.source.TileDebug has no method that returns this xyz info.Zurek
So what does the coordinates in that example mean? Pls enlighten me. Im no GIS expert. I just got the info there. If my info is wrong then maybe can you please help me understand what an actual xyz coordinate is..Decent
Would you provide a jsfiddle with what you have so far?Zurek
C
7
var tileExtent = function(tileCoord){
    var z = tileCoord[0];
    var x = tileCoord[1];
    var y = tileCoord[2];
    var tileGridOrigin = tileGrid.getOrigin(z);
    var tileSizeAtResolution = tileGrid.getTileSize(z) * tileGrid.getResolution(z);
    return [
        tileGridOrigin[0] + tileSizeAtResolution * x,
        tileGridOrigin[1] + tileSizeAtResolution * y,
        tileGridOrigin[0] + tileSizeAtResolution * (x + 1),
        tileGridOrigin[1] + tileSizeAtResolution * (y + 1)
    ];
}

You can test/verify at http://jsfiddle.net/eurx57s7/

Note (stolen from the ol3 example, but it applies here to): The tile coordinates are ol3 normalized tile coordinates (origin bottom left), not OSM tile coordinates (origin top left)

Cottage answered 17/8, 2015 at 17:49 Comment(5)
@AlvinLindstam could you maybe help me with this question: gis.stackexchange.com/questions/173646/… Thanks!Evolute
@Alvin Lindstam...instead of getting the results when clicking, is it possible to register produced tiles and save all the results in some sort of text file, preferably geojson: { "type": "Feature", "properties": { "tile": "10, 510, -341"}, "geometry": { "type": "Point", "coordinates": [ -58703.63772301562, 6711782.579664757 ] } }Cochineal
@Alvin Lindstam or csv:10,510,-341,-78271.51696402207,6692214.700423751,-39135.75848200917, 6731350.458905762,58703.63772301562, 6711782.579664757 (z,x,y,left down corner of tile, right above corner of tile, x_center,y_center)Cochineal
@newbie_girl: sure, but that's of topic for this question. Post a new question if you're unable to find a solution.Cottage
@AlvinLindstam Thanks for quick respond...Please take a look at this question: #40344976Cochineal

© 2022 - 2024 — McMap. All rights reserved.