Viglino / ol-ext

Cool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animations, canvas filters.
https://viglino.github.io/ol-ext/
Other
1.24k stars 463 forks source link

Splitter function can not be used when the length of the line segment is short #1073

Open JokanJan opened 3 months ago

JokanJan commented 3 months ago

PixPin_2024-06-13_18-14-28

Viglino commented 3 months ago

The issue is in the tolerance that is too low:

var splitter = new ol.interaction.Splitter({ source: vector.getSource(), tolerance: 0.001 });
jidixueqiao commented 3 months ago

The issue is in the tolerance that is too low:

var splitter = new ol.interaction.Splitter({ source: vector.getSource(), tolerance: 0.001 });

Hi bro.I copy the code of the example, change the view projection from 'default' to 'EPSG:4326', and the splitter didn't work again. This is part of the code I tested:

      // The map
      var map = new ol.Map({
        target: "map",
        view: new ol.View({
          center: [121.2738842396899, 30.179885990299272],
          //   center: ol.proj.transform(
          //     [121.2738842396899, 30.179885990299272],
          //     "EPSG:4326",
          //     "EPSG:3857"
          //   ),
          zoom: 20,
          projection: "EPSG:4326",
        }),
        layers: layers,
      });
     ...
   // Add splitter before other interaction
      var splitter = new ol.interaction.Splitter({
        source: vector.getSource(),
        tolerance: 0.001,
      });
Viglino commented 3 months ago

The tolerance must be set in the coord system. You have to adapt it to make it run.

jidixueqiao commented 3 months ago

The tolerance must be set in the coord system. You have to adapt it to make it run.

Yes I tried.And I read the code of ol-ext later,I saw the tolerance must less than the variable det (in file /src/interaction/Splitter.js function intersectSegs) and greater than the result Math.abs(d1 - d2) (in the file /src/geom/LineStringSplitAt.js).I log both result like this: image For now, how can I find a number which greater than the result Math.abs(d1 - d2) and less than the variable det?

Viglino commented 3 months ago

The issue is due to numeric instability on calculation. I've pushed a new version with 2 tolerances: tolerance and alignTolerance so you can adjust it. tolerance = alignTolerance = 1e-8 seems to work for me with EPSG:4326 but it may depends on the latitude.

jidixueqiao commented 3 months ago

The issue is due to numeric instability on calculation. I've pushed a new version with 2 tolerances: tolerance and alignTolerance so you can adjust it. tolerance = alignTolerance = 1e-8 seems to work for me with EPSG:4326 but it may depends on the latitude.

Thanks.I tried adjusting tolerance and alignTolerance many times, but this function still doesn't work properly in the coordinates I set (sometimes it splits a line into multiple segments with shorter intervals). In this case, is there any good way to quickly help me complete the setting and adjustment of these two parameters so that this function can work properly?

Viglino commented 3 months ago

To prevent digital instability you can round coordinates, here is an example that round coords to 1E10: https://codepen.io/viglino/pen/QWRmapz?editors=0010