New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMS layer with custom resolutions not giving the proper coordinates #31

Closed
jdduarte opened this Issue Jun 3, 2013 · 5 comments

Comments

Projects
None yet
2 participants
@jdduarte

jdduarte commented Jun 3, 2013

This is not a bug report. It's more of a cry for help

Sorry about posting this here but since I'm dealing with a custom projection I figured out this didn't belong in the LeafletJS forum.

I was a given set of tiles and an OpenLayers map. I wanted to migrate that to LeafletJS.

Although it's a simple TMS layer with the tiles generated in EPSG:900913, I figured that I have a custom set of resolutions. That's why I ended up using the Proj4Leaflet plugin.

I can see the map but zooming in/out doesn't take me to the place where the zoom was made. Also, clicking on the map doesn't return the correct coordinates.

I have set up a demo here: http://queixinhas.com/map_test/

The code is all in this file: http://queixinhas.com/map_test/app2.js

You can see the LeafletJS map and the OpenLayers map.

I thought it was a matter os providing the resolutions array and tile origin but apparently I'm missing something.

Btw, I had to comment line 116 on proj4leaflet.js file because I was getting the "Projected bounds does not match grid at zoom 0" error. This might be the cause of the problem...

Again, sorry for posting this help request here, I'm happy to move it to another place if you wish of course.

Any help would be appreciated, thanks!

@perliedman

This comment has been minimized.

Contributor

perliedman commented Jun 4, 2013

As you can see in issue #29, the error message you saw is something we're planning to resolve. On the other hand, before it is addressed, it is crucial that you do not remove that check, or at least provide bounds that actually align with the tiles on all zoom levels.

Usually, the easiest way is to simply calculate the upper bounds by rounding the value up to align with the grid. Pseudo-code/formula:

upper_y = Math.ceil(tms_upper_y / tile_size * max_tile_resolution) * tile_size * max_tile_resolution (see comment #31 (comment) below for correct formula).

where:

  • upper_y: the upper y bound you should provide to L.Proj.CRS.TMS
  • tms_upper_y: the upper y bound you have now
  • tile_size: your tile size, usually 256
  • max_tile_resolution: the highest tile resolution value you have for any zoom level.

Also note that this will only work if your zoom levels resolution's are multiples of each other.

@jdduarte

This comment has been minimized.

jdduarte commented Jun 4, 2013

Thanks for your response!

I tried that formula to adjust the upper_y of the projected bounds and I still get the same error.

Just to confirm, the projected bounds I should supply to L.Proj.CRS.TMS are in the format [left, bottom, right, top]?

@perliedman

This comment has been minimized.

Contributor

perliedman commented Jun 4, 2013

Hm, of course I made an error in the formula. Here's a working example built from your code: http://jsfiddle.net/S8FL2/

The only change I made was the TMS upper bound, which I calculated to 9691950.164.

The correct formula should be:

upper_y = tms_lower_y + Math.ceil((tms_upper_y - tms_lower_y) / (tile_size * max_tile_resolution)) * tile_size * max_tile_resolution
@jdduarte

This comment has been minimized.

jdduarte commented Jun 5, 2013

Thank you very much Per! It's working perfectly.

Hope you don't mind I wrote about this and mentioned you and Kartena here: http://jdduarte.com/stories/2013/06/06/leaflet-js-with-custom-projection/

@jdduarte jdduarte closed this Jun 5, 2013

@perliedman

This comment has been minimized.

Contributor

perliedman commented Jun 7, 2013

Nice post!

Once I get #29 sorted out, this shouldn't be a problem any longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment