1

I am using leaflet and turfjs. I am able to check whether rectangle/polygon/point is within another polygon. But I am not able to check the same for circle. I want to restrict user to draw circle inside other shape (polygon/circle/shape) etc.

Could anybody please help me to know how to do this?

This is how I check whether new shape is within polygon or not. It is working fine when new shape is rectangle or polygon. But for circle, it throws errors at for "layer.getLatLngs()".

// create parent feature
var parentContainerBoundary = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [parentCoordinates]
            }
        }
    ]
};


        var pointWithinContainer = turf.within(newShapePoints, parentContainerBoundary);
        return pointWithinContainer.features.length != newShapePoints.features.length;
5
  • 2
    How exactly are you "not able to check"? Do you receive an error? An example with some code would help a lot here. Feb 26 '16 at 19:43
  • How is your circle different from a polygon?
    – Ian Turton
    Feb 27 '16 at 15:55
  • @ericbrelsford, please check updated code. Logic here is to get list of corner points of new shape and pass these new points to turf.within function second argument as Parent shape. For circle, it's not working. Could you please let me know is there any way to check if circle is within polygon or rectangle? Thanks!
    – Balram
    Feb 29 '16 at 6:02
  • 2
    The L.Circle class does not have a .getLatLngs method, which is probably the source of the error. It is a bit hard to say exactly, since you haven't included the code that creates your newShapePoints object. Because L.Circle does not have any easy way to access the points of the circle (the built-in methods only give you the circle's center point), you may need to create a proxy object that actually produces a polygon (leaflet-geodesy is a good option here) and use turf.within on that. Feb 29 '16 at 20:31
  • @Balram It would be really helpful to see the full code for the situation that does not work (use jsfiddle.net or jsbin.com). As it stands now we have to guess at how parentCoordinates and newShapePoints are defined. Feb 29 '16 at 20:32
1

As noted in the comments, L.Circle does not have a getLatLngs method (hence your error). It does have a getLatLng method (note, no s). The latter returns the centre of the point. You need to use a polygon that approximates a circle, in order to have access to getLatLngs (i.e. the vertices of the approximate circle).

The treatment of circles as polygons with a dense array of vertices is quite common in GIS, because it's a lot easier to work with than a true circle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.