Skip to content
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

toMercator changes polygons geometry #1602

Open
Darune opened this issue Feb 28, 2019 · 9 comments · May be fixed by #1609
Open

toMercator changes polygons geometry #1602

Darune opened this issue Feb 28, 2019 · 9 comments · May be fixed by #1609

Comments

@Darune
Copy link

Darune commented Feb 28, 2019

When using toMercator on a polygon that overlap the -180 or 180 longitude, without antimeridian cutting.

The current implementation wraps every point one by one which ends up returning a wrong polygon:

Here is a polygon overlapping the -180 longitude:

code to reproduce the issue:

import { toWgs84, toMercator } from '@turf/projection';

toWgs84(toMercator({
  "type": "Feature",
  "properties": {}, 
  "geometry": {
    "type": "Polygon",
    "coordinates": [[
        [-181, 60],
        [-181, 61],
        [-179, 61],
        [-179, 60],
        [-181, 60]
    ]]
  }
}));

I think that when calling toMercator with a GeoJson, turf should not try to wrap longitude as it does.
if this is an acceptable behavior I could submit a PR

@Darune Darune changed the title toMercator change polygons geometry toMercator changes polygons geometry Feb 28, 2019
@stebogit
Copy link
Collaborator

@Darune you are right, also the fiji test, which should cover this case, does actually return a funky output that for some reason is considered OK.
Honestly I don't remember what happened here and why the issue was never really fixed properly. You are welcome to submit a PR anytime 😃

I think it would be useful also to add a set of tests like what you posted, where the double conversion should coincide with the original feature; something like:

t.deepEqual(feature, toWgs84(toMercator(feature)));

@Darune
Copy link
Author

Darune commented Mar 1, 2019

Great,

I didn't have time to handle this today, i'll do this on monday

@Darune
Copy link
Author

Darune commented Mar 5, 2019

Any specific reason to use proj4js for validation ?
Seems that the js version doesn't support the +over flag that is required to disable lon_wrap flag.

@stebogit
Copy link
Collaborator

stebogit commented Mar 5, 2019

@Darune I have no idea what you're talking about... 😅
I did not choose proj4 for any particular reason, however; at least none that I can remember.

@Darune
Copy link
Author

Darune commented Mar 5, 2019

The tests for toMercator https://github.com/Turfjs/turf/blob/master/packages/turf-projection/test.js#L31

Clearly use proj4 to validate the function output.
i'm free to change those right ? as the fidji.geojson transformed by proj4 is wrong aswell

@stebogit
Copy link
Collaborator

stebogit commented Mar 5, 2019

I have never used Mercator coordinates nor I know how to represent them directly on a map to tell if the output is right or not (i.e. if you have any suggestions or know any tools to do that I'd be glad to know 😄)

As long as it works I don't think what tool you use for validation matters. However I'm not the best person to give advice on this.
@morganherlocker @rowanwins ?

@morganherlocker
Copy link
Member

I am not familiar with the @turf/projection source, but the toMercator function lives here if you want to debug the math: https://github.com/Turfjs/turf/blob/master/packages/turf-projection/index.ts#L87-L108

@rowanwins
Copy link
Member

Yeah unfortunately the tests and the code are currently setup to run against individual coordinates rather than seeing them in context of a geometry that is crossing the anti-median, so a bit of thought needs to happen to get things behaving and tested properly.

@Darune
Copy link
Author

Darune commented Mar 14, 2020

I could retry writing tests for this, it has been stalled for way too long, but I might have time next week to digg this out of the dirt

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

Successfully merging a pull request may close this issue.

5 participants