geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
https://geoman.io
MIT License
2.21k stars 433 forks source link

The snap on edit mode is sometimes not perfect #715

Open alexandreDavid opened 3 years ago

alexandreDavid commented 3 years ago

When I edit a polygon with a zoom level low and I snap this polygon to another one, it's very common the created snap point is not exactly on the line / point. For me, it's a problem regarding I control if these polygons are not intersecting.

alexandreDavid commented 3 years ago
Capture d’écran 2020-12-03 à 05 43 08
Falke-Design commented 3 years ago

What is your Zoom level? map.getZoom()

alexandreDavid commented 3 years ago

I reproduce very often it might not be a problem of zoom but calculation of vertex position on the line

Falke-Design commented 3 years ago

In this code snippet you see that the calculation converts the latlng to a pixel point and then back. https://github.com/geoman-io/leaflet-geoman/blob/ff58d546ae5807a331485c16b5ac442b6bbce527/src/js/Mixins/Snapping.js#L425-L446

I didn't found a way to do this with the latlng directly, a PR from you is welcome, else we can't fix this.

antnat96 commented 2 years ago

Screen Shot 2022-06-21 at 10 55 14 AM

I know this is old, but I'm also running into a similar issue here. I'm performing geo analysis with Turf (https://turfjs.org/) to ensure that my smaller polygons do not extend outside of the bounds of a larger, outer polygon.

When I use the snapping feature and the small polygon gets snapped to the larger one, the point at which the smaller is snapped is outside the bounds of the larger polygon according both to Turf and to point-on-line-segment calculations that I accomplished.

I've done a workaround using Turf's buffer method (https://turfjs.org/docs/#buffer) where I essentially extend the outer polygon by 2 feet, like so:

` import { polygon } from '@turf/helpers' import containsHelper from '@turf/boolean-contains' import bufferHelper from '@turf/buffer'

const validate = (zoneLatLngs, sectorLatLngs) => { const zone = polygon(zoneLatLngs) const bufferedZone = bufferHelper(zone, 2, { units: 'feet' }) const sector = polygon(sectorLatLngs)

  // Zone contains sector
  if (containsHelper(bufferedZone, sector)) return true

  return false

} `

Pulling the point away from the snapped location outside of the polygon validates correctly, given a 2 foot tolerance. Hopefully this is helpful for somebody else.

rodrigore commented 2 years ago

Screen Shot 2022-06-21 at 10 55 14 AM

I know this is old, but I'm also running into a similar issue here. I'm performing geo analysis with Turf (https://turfjs.org/) to ensure that my smaller polygons do not extend outside of the bounds of a larger, outer polygon.

When I use the snapping feature and the small polygon gets snapped to the larger one, the point at which the smaller is snapped is outside the bounds of the larger polygon according both to Turf and to point-on-line-segment calculations that I accomplished.

I've done a workaround using Turf's buffer method (https://turfjs.org/docs/#buffer) where I essentially extend the outer polygon by 2 feet, like so:

` import { polygon } from '@turf/helpers' import containsHelper from '@turf/boolean-contains' import bufferHelper from '@turf/buffer'

const validate = (zoneLatLngs, sectorLatLngs) => { const zone = polygon(zoneLatLngs) const bufferedZone = bufferHelper(zone, 2, { units: 'feet' }) const sector = polygon(sectorLatLngs)

  // Zone contains sector
  if (containsHelper(bufferedZone, sector)) return true

  return false

} `

Pulling the point away from the snapped location outside of the polygon validates correctly, given a 2 foot tolerance. Hopefully this is helpful for somebody else.

Hey thanks for your reply but still not sure how do you apply your validate function into the snap function