0

I have some cell sites: every cell has angle degree orientation and a range in km I fetch these cells from the db and represent them on a leaflet map with semiCircles as shown here : screenshot of my map

And I have a home address (coords) that I want to check if it is covered by (one or some) of the cells

As I couldn't find a way to check if a point(home) resides inside a semiCircle, I decided to convert my semiCircle to L.Geojson in order to use the leaflet-pip plugin

Converstion code :

let cellGeojson = {
                "features": [
                    {
                        "type": "Feature",
                        "properties": {
                            "id": cell.id,
                            "name": cell.name,
                            "coords": cell.latitude + "," + cell.longitude,
                            "lat": cell.latitude,
                            "long": cell.longitude,
                            "location": cell.location,
                            "azimuth": cell.azimuth,
                        },
                        "geometry": {
                            "type": "Point",
                            "coordinates": [cell.longitude, cell.latitude]
                        }
                    }
                ]
            }
            let cellLayer = L.geoJSON(cellGeojson, {
                pointToLayer: function (pointFeature, latlng) {
                    let rangeMarker = L.semiCircle([cell.latitude,cell.longitude], {
                        radius: cell.range,
                        startAngle: cell.angle - 30,
                        stopAngle: cell.angle + 30
                    });
                    return rangeMarker;
                },
            }).addTo(this.map)

But when I use the leafletPip.pointInLayer(home, cellLayer, [false]) it return always an empty array that means the home is not covered by any cell, I think the cause is that the semiCircle created by the pointToLayer function is just a marker and its layer is treated as a point

I've tried other leaflet plugins such as Leaflet.PointInPolygon ... but it doesn't work cuz the semiCircle is not a polygon

So I need your help to determine if the home is covered or no

and I'm sorry for the bad english

Medab
  • 63
  • 7

1 Answers1

0

I suspect you are using the plugin jieter/Leaflet-semicircle.

This plugin already implements a function _containsPoint(p) which you could call using
p = map.latLngToLayerPoint(L.latLng(yourXCoord,yourYCoord)).
The function expects a L.point, which you will have to convert to first using your maps latLngToLayerPoint() function.
Reference: https://github.com/jieter/Leaflet-semicircle/blob/master/Semicircle.js

If that doesn't work or you are using a different plugin, you could also implement this function yourself (possibly using the above-mentioned file for guidance).
This is not completly trivial, but should be possible with the following Ressources:

How-To: How to determine whether a point (X,Y) is contained within an arc section of a circle (i.e. a Pie slice)?

  1. Determine the (simplified) Radius using Pythagoras or (more accurate) Haversine Formula
  2. Find the angle between two points in a 2d-space
B. Raabe
  • 171
  • 1
  • 7
  • thanks @B. Raabe Yes, I use jieter/Leaflet-semicircle When I try to use cell._containsPoint({x:home.lat, y:home.lng} I got TypeError: p.distanceTo is not a function – Medab Apr 14 '22 at 00:08
  • Thank you so much Mr @B. Raabe for your time. I've tried that also but it return always false; so I opened the issue https://github.com/jieter/Leaflet-semicircle/issues/42 You can take a look to it there is a screenshot and more deteals. btw I'm so thankful for your help – Medab Apr 14 '22 at 19:25
  • You are welcome! I just tested it and it does appear to be broken. I don't fully understand the problem yet, but I will try to find a solution tomorrow. – B. Raabe Apr 14 '22 at 23:34
  • Ok I'll wait for you. And in the meanwhile I will continue trying to know what's wrong. – Medab Apr 15 '22 at 16:55
  • I have found it. You will have to convert your Coordinates to a L.point first. (L.point is the position an the map in pixels, not Latitude/Longitude). Your Map-Object provides a function `latLngToLayerPoint( )` for that. I will update my answer accordingly. (Full call: `cell._containsPoint(map.latLngToLayerPoint(L.latLng([51.7039,10.2169])))` – B. Raabe Apr 16 '22 at 11:19
  • 1
    That's worked fine, I just replaced the param of L.latLng() in your comment to be L.latLng(51.7039,10.2169), it receives (a, b) and no array. thank you so much B. Raabe you saved me – Medab Apr 16 '22 at 13:30