Skip to content

OSM Legal Speed is a Java SDK which help determine the legal max speed for most of the countries of the world, with respect to regional legal restrictions, based on OSM tags.

License

Notifications You must be signed in to change notification settings

chargetrip/osm-legal-speed

Repository files navigation

Intro

OSM Legal Speed is a Java SDK which help determine the legal max speed for most of the countries of the world, with respect to regional legal restrictions, based on OSM tags.

This project is highly inspired by OSM Legal Default Speeds. Under the hood, we are using the JSON file generated by its parser (legal_default_speeds.json).

Getting started

OSM Legal Speed is a complete package to determine the legal speed based on a set of GPS coordinates and a vehicle type. Also, its providing support for different situation, including time based speed restrictions.

package com.chargetrip.example;

import com.chargetrip.osmLegalSpeed.types.VehicleType;
import com.chargetrip.osmLegalSpeed.types.Options;

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    Map<String, String> tags = new HashMap<>();
    tags.put("type", "route");
    tags.put("route", "road");
    tags.put("highway", "motorway");

    try {
      Options options = new Options();
      options.latitude = 52.3727598;
      options.longitude = 4.8936041;
      options.datetime = LocalDateTime.now().withHour(12).withMinute(0);

      LegalSpeed legalSpeed = new LegalSpeed();

      // How to get a country with region code
      String countryWithRegion = legalSpeed.getCountryWithRegion(options.latitude, options.longitude);
      System.out.println("Country with region: " + countryWithRegion);

      // How to search for max speed tags
      LegalSpeed.SearchResult searchResult = legalSpeed.searchSpeedLimits(tags, countryWithRegion, true);
      System.out.println("SearchResult: ");
      System.out.println("- certitude: " + searchResult.certitude);
      System.out.println("- tags: " + searchResult.speedType);

      // How to get the legal speed for all vehicles
      System.out.println("Legal speeds:");
      for (VehicleType vehicle : VehicleType.values()) {
        options.vehicle = vehicle;
        System.out.println("- " + vehicle + ": " + legalSpeed.getSpeedLimit(tags, options));
      }
    } catch (IOException | ParseException e) {
      e.printStackTrace();
    }
  }
}

This example will output:

Country with region: NL
SearchResult: 
- certitude: Exact
- tags: motorway: {maxspeed:motorhome:conditional=80 @ (maxweightrating>3.5), maxspeed:conditional=100 @ (06:00-19:00); 90 @ (trailer); 80 @ (maxweightrating>3.5 AND trailer), maxspeed:hgv=80, maxspeed=130, minspeed=60, maxspeed:bus=80, maxspeed:coach=100}
Legal speeds:
- Car: 100.0
- Bus: 80.0
- MiniBus: 100.0
- SchoolBus: 100.0
- TruckBus: 100.0
- Coach: 100.0
- Goods: 100.0
- Hazmat: 100.0
- Hgv: 80.0
- Motorcycle: 100.0
- Tricycle: 100.0
- MotorHome: 100.0

Internals

The SDK is using legal_default_speeds.json rules to determine the legal max speed and countries.json to determine country and/or region (is necessary) based on GPS coordinates. The legal rules file was generated using this parser based on this wiki page. Once more rules will be added to this page or changed, we will parse it again and build a new set of rules.

The countries.json file contains definitions only for countries and regions which are present in legal_default_speeds.json. Every time we update legal_default_speeds.json, we also check to see if we need to adjust countries.json.

Alternative (which inspired us)

  • OSM Legal Default Speeds - Kotlin multiplatform library that provides information about legal default speed limits. Runs on JVM, native and JavaScript.
SDK OSM legal speed tags Country with region Speed based on GPS
OSM Legal Speed
OSM Legal Default Speeds x x

Sponsors

Chargetrip logo

About

OSM Legal Speed is a Java SDK which help determine the legal max speed for most of the countries of the world, with respect to regional legal restrictions, based on OSM tags.

Resources

License

Stars

Watchers

Forks

Packages

No packages published