Strange containsPoint() behavior

memesaregood2 days ago

I have this snippet:

        List<Geofence> geofences = storage.getObjects(Geofence.class, new Request(
                new Columns.All(),
                new Condition.Permission(Device.class, deviceId, Geofence.class)
        ));
        if (geofences != null) {
            System.out.println(geofences.size());
        } else {
            System.out.println("null");
        }
        if (geofences != null) {
            for (Geofence geofence : geofences) {
                GeofenceGeometry geometry = geofence.getGeometry();
                if (geometry.containsPoint(config, geofence, startTrip.getLatitude(), startTrip.getLongitude())) {
                    System.out.println("Start: " + geofence.getName());
                    trip.setStartGeofence(geofence.getName());
                } else if (geometry.containsPoint(config, geofence, endTrip.getLatitude(), endTrip.getLongitude())) {
                    trip.setEndGeofence("End: " + geofence.getName());
                }
            }
        }

For some reason, even if one of the points is indeed in the geofence, the containsPoint() method doesn't return true. For example, for the start coordinates it did return true, but for the end ones - it didn't, even though the points are mere meters away from each other and are in the same geofence. How is this possible? Perhaps I'm using the wrong method? Or using it the wrong way?
Not sure, but could this be a bug in your ray casting algorithm you're using in the implementation of the method in the GeofencePolygon class?

My data is as follows:

Start coordinates: 56.826232° 53.195482°
End coordinates: 56.826107° 53.194998°
Polygon:

POLYGON ((56.826870754105784 53.193733515583176, 56.82696721282494 53.19622797032699, 56.82478718513545 53.19651884314172, 56.82455084707689 53.19390098780934, 56.826870754105784 53.193733515583176))

Show us the map with the geofence and both points.

memesaregooda day ago

Screenshot at 2025-05-21 04-54-20.png
Screenshot at 2025-05-21 04-55-36.png

Will this do?

And can you show in Traccar that one of them didn't work?

memesaregooda day ago

Edited the snippet:

if (geometry.containsPoint(config, geofence, startTrip.getLatitude(), startTrip.getLongitude())) {
                    System.out.println(startTrip.getLatitude() + " " + startTrip.getLongitude());
                    System.out.println("Got start");
                    trip.setStartGeofence(geofence.getName());
                } else if (geometry.containsPoint(config, geofence, endTrip.getLatitude(), endTrip.getLongitude())) {
                    System.out.println(endTrip.getLatitude() + " " + endTrip.getLongitude());
                    System.out.println("Got end");
                }

Screenshot at 2025-05-21 05-06-14.png

(got start twice because two trips were detected)

Do you see this issue in Traccar itself or is it only in your custom code?

memesaregooda day ago

I.. can't seem to check? There are a few 'geofence entered' and 'geofence exited' events detected, so the method seems to work, but how do I check exactly? Is this method used somewhere else beyond processing these events? Can't say I see this issue in Traccar's original functionality.

Furthermore, my custom code also seems to work, but the method returns false on the endTrip coordinates that are clearly in the polygon. Any hints on what I'm doing not like you?

If you're using the latest Traccar, there should be a list of geofences for each position. Check route report.