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

The snap on edit mode is sometimes not perfect #715

Open
alexandreDavid opened this issue Dec 3, 2020 · 6 comments
Open

The snap on edit mode is sometimes not perfect #715

alexandreDavid opened this issue Dec 3, 2020 · 6 comments

Comments

@alexandreDavid
Copy link

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
Copy link
Author

Capture d’écran 2020-12-03 à 05 43 08

@Falke-Design
Copy link
Collaborator

What is your Zoom level? map.getZoom()

@alexandreDavid
Copy link
Author

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

@Falke-Design
Copy link
Collaborator

In this code snippet you see that the calculation converts the latlng to a pixel point and then back.

_getClosestPointOnSegment(map, latlng, latlngA, latlngB) {
let maxzoom = map.getMaxZoom();
if (maxzoom === Infinity) {
maxzoom = map.getZoom();
}
const P = map.project(latlng, maxzoom);
const A = map.project(latlngA, maxzoom);
const B = map.project(latlngB, maxzoom);
const closest = L.LineUtil.closestPointOnSegment(P, A, B);
return map.unproject(closest, maxzoom);
},
_getDistanceToSegment(map, latlng, latlngA, latlngB) {
const P = map.latLngToLayerPoint(latlng);
const A = map.latLngToLayerPoint(latlngA);
const B = map.latLngToLayerPoint(latlngB);
return L.LineUtil.pointToSegmentDistance(P, A, B);
},
_getDistance(map, latlngA, latlngB) {
return map
.latLngToLayerPoint(latlngA)
.distanceTo(map.latLngToLayerPoint(latlngB));
},

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
Copy link

antnat96 commented Jun 21, 2022

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
Copy link

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

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

No branches or pull requests

5 participants