Search code examples
pythongoogle-mapsgpsgoogle-places-apiosrm

Determine if GPS coordinates fall on a road


I'm trying to determine whether a point is on a road/highway/freeway etc or in a building through a Python script. For some inputs they'll contain the user's velocity which means in some contexts, it'll be obvious given the device velocity. However, for instances when the user's instantaneous velocity is small it could be due to traffic, being stopped at lights etc.

I want to be able to input a pair of inputs, eg. -33.852592, 151.210663 and receive a boolean result as to whether the user's coordinates fall on a road.

I've come across Google places and OSRM but haven't found a solution from these yet. Are there any other options I'm missing?


Solution

  • One option would be do do a DirectionsService.route request with the origin and destination set to the input point. That will return a 0 length route from the closest location on a road (within reason). If there are no results, the point isn't on the road. If you get a result, you can calculate the distance between your input and the point returned to make an informed decision about whether the coordinates are on the road or not.

    Note that the GPS device itself may not be but so accurate.

    proof of concept fiddle (with your example point)

    screenshot of map showing result

    code snippet:

    var geocoder;
    var map;
    
    function initialize() {
      var testPoint = {
        lat: -33.852592,
        lng: 151.210663
      };
      var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: testPoint,
          zoom: 22,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      var marker = new google.maps.Marker({
        position: testPoint,
        map: map,
        icon: {
          url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
          size: new google.maps.Size(7, 7),
          anchor: new google.maps.Point(3.5, 3.5)
        }
      });
    
      var directionsService = new google.maps.DirectionsService();
      directionsService.route({
        origin: testPoint,
        destination: testPoint,
        travelMode: "DRIVING"
      }, function(result, status) {
        if (status == 'OK') {
          var marker = new google.maps.Marker({
            position: result.routes[0].legs[0].steps[0].start_location,
            map: map,
            icon: {
              url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
              size: new google.maps.Size(7, 7),
              anchor: new google.maps.Point(3.5, 3.5)
            }
          });
          var distance = google.maps.geometry.spherical.computeDistanceBetween(result.routes[0].legs[0].steps[0].start_location, marker.getPosition());
          if (distance < 10)
            document.getElementById('info').innerHTML = "distance=" + distance + "m on road";
          else
            document.getElementById('info').innerHTML = "distance=" + distance + "m not on road";
        } else alert("status=" + status);
      });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    
    #map_canvas {
      height: 90%;
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
    <div id="info"></div>
    <div id="map_canvas"></div>