maplibre / maplibre-gl-js

MapLibre GL JS - Interactive vector tile maps in the browser
https://maplibre.org/maplibre-gl-js/docs/
Other
6.61k stars 712 forks source link

Rendering of self-intersect polygon is inconsistant & not good form #1830

Open Jeromearsene opened 2 years ago

Jeromearsene commented 2 years ago

maplibre-gl-js version: 2.3.0

Problem description

For self-intersect polygons, we have 2 problems:

ezgif-3-fb8c6b854b

Capture d’écran 2022-11-09 à 15 10 38

Steps to Trigger Behavior

  1. Draw a polygon with self intersection.

    Example:

    {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "id": "dd2857b6-aae2-4437-91bb-766b79993b58",
                "fillColor": "#00796B",
                "stroke": "#00796B",
                "color": "#00796B",
                "weight": 2,
                "opacity": 0.8,
                "dashArray": null,
                "fillOpacity": 0.35
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            2.47217,
                            47.52403
                        ],
                        [
                            2.40693,
                            47.49713
                        ],
                        [
                            2.44607,
                            47.43679
                        ],
                        [
                            2.57104,
                            47.48553
                        ],
                        [
                            2.39251,
                            47.44793
                        ],
                        [
                            2.47217,
                            47.52403
                        ]
                    ]
                ]
            }
        }
    ]
    }

Link to Demonstration

JSfiddle example

Expected Behavior

  1. Like to Leaflet (exemple here), keep the same render, no matter the zoom.
  2. Have a good form:
Capture d’écran 2022-11-09 à 15 09 44

Actual Behavior

  1. Render changed depend to zoom.
  2. The form is not good:
Capture d’écran 2022-11-09 à 15 10 38
HarelM commented 2 years ago

Can you check older versions? I don't think this issue was introduced recently. But in general, self intersecting polygon is not a valid polygon when checking the geojson spec so I would consider fixing the data and not the rendering as an initial solution.

Jeromearsene commented 2 years ago

Can you check older versions? I don't think this issue was introduced recently. But in general, self intersecting polygon is not a valid polygon when checking the geojson spec so I would consider fixing the data and not the rendering as an initial solution.

Thanks for you quick answer !

I also think that the problem is not recent.

Geojson is agnostic to coordinates from Features ¹. I can format Geojson with Turf. By example, I can use unkinkPolygon to format Geojson features:

Capture d’écran 2022-11-09 à 16 11 31

But they are some "problems":

Sources

  1. https://github.com/mclaeysb/geojson-polygon-self-intersections & https://github.com/mapbox/mapbox-gl-draw/issues/319#issuecomment-225695148
HarelM commented 2 years ago

Why not use a multipolygon in this case?

Jeromearsene commented 1 year ago

Why not use a multipolygon in this case?

Sometimes I've not the choice. If I get a Geojson with this format, I want have possibility to display correctly, without manual changes. With Leaflet, I didn't ask myself the question. Ideally, I wish I didn't have to worry about it 😅

HarelM commented 1 year ago

Ideally I agree. You have two options here: fix the geometry, or send a PR to fix the code here. I'll let you decide which is simpler. 😀

Jeromearsene commented 1 year ago

Ideally I agree. You have two options here: fix the geometry, or send a PR to fix the code here. I'll let you decide which is simpler. 😀

Yeah, for the moment I will fix the geometry with Turf.

Thanks

tiokmu1731 commented 3 months ago

First, I thank everyone participating in maplibre developing project.

But in general, self intersecting polygon is not a valid polygon when checking the geojson spec so I would consider fixing the data and not the rendering as an initial solution.

I also consider fixing the self intersecting is the first step towards solution. But I now found the case that

I've also reported this case at the geojson-vt project. Please refer "here" for more information.

In this case, I hope maplibre will display the form that is not self intersecting, becase the original geojson is as well. Is it technologically difficult to fix maplibre and realize it?

The code blow is how I got the self intersecting WKT using the geojson that is not self intersecting. (ZoomLevel=10, Latitude=28.4441, Longitude=129.6784) I attached the WKT I got by this code as well.

A20-070402_46_AmamiIslands_kasari_10_28.4441_129.6784.txt

map.addSource('amami', {
    type: 'geojson',
    data: './geojson/A20-070402_46_AmamiIslands_kasari.json',
},);

map.addLayer({
    id: 'amami_polygon',
    type: 'fill',
    source: 'amami',
    paint: {
        'fill-color': "rgb(255,0,0)",
        'fill-opacity': 0.5,
        'fill-outline-color': "rgb(0,0,255)",
        'fill-antialias': true,
    },
});

// get WKT
map.on('click', 'amami_polygon', function (e) {
    const coordinates = e.features[0].geometry.coordinates[0];
    let cood = "POLYGON((";
    for (let i = 0; i < coordinates.length; i++) {
        cood+= coordinates[i][0] + (' ') + coordinates[i][1] + ',';
    }
    cood += "))";
    console.log(cood);
    }
);