devshaded/nvdb-speed-limits

Fetch Norwegian speed limits from NVDB API

v1.0.1 2025-07-01 14:57 UTC

This package is auto-updated.

Last update: 2025-07-01 20:40:09 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel package for fetching speed limits from the Norwegian NVDB API.

Table of Contents

Features

  • Fetches speed limits from the Norwegian NVDB API for given coordinates
  • Supports searching with an expanding radius if no speed limit is found initially
  • Batch lookup for multiple coordinates
  • Configurable API endpoint, search radius, and coordinate bounds
  • Designed for Laravel, but can be used in any PHP project

Installation

Install the package via Composer:

composer require devshaded/nvdb-speed-limits

Publish the config file (optional):

php artisan vendor:publish --tag="nvdb-speed-limits-config"

Configuration

The configuration file allows you to customize API settings, search parameters, and coordinate bounds. Example:

return [
    'api' => [
        'base_url' => 'https://nvdbapiles-v3.atlas.vegvesen.no',
        'timeout' => 30,
        'headers' => [
            'accept' => 'application/vnd.vegvesen.nvdb-v3-rev1+json',
            'X-Client' => 'LaravelNvdbSpeedLimits/1.0',
        ],
    ],
    'search' => [
        'default_radius' => 0.0001, // ~11 meters
        'max_radius' => 0.005,      // ~550 meters
        'radius_multiplier' => 3,   // For expanding search
    ],
    'bounds' => [
        'latitude' => [57, 72],
        'longitude' => [4, 32],
    ],
];

Usage

Import the facade or use the class directly:

use DevShaded\NvdbSpeedLimits\Facades\NvdbSpeedLimits;
// or
use DevShaded\NvdbSpeedLimits\NvdbSpeedLimits;

Get Speed Limit for a Coordinate

$result = NvdbSpeedLimits::getSpeedLimit(59.9139, 10.7522);

if ($result->found) {
    echo "Speed limit: " . $result->recommended['speed'] . " km/h";
} else {
    echo "No speed limit found.";
}

Optional: Specify a Search Radius

$result = NvdbSpeedLimits::getSpeedLimit(59.9139, 10.7522, 100); // 100 meters

Get Speed Limit with Expanded Search

This method will automatically expand the search radius until a speed limit is found or the maximum radius is reached.

$result = NvdbSpeedLimits::getSpeedLimitWithExpandedSearch(59.9139, 10.7522);

if ($result->found) {
    echo "Speed limit: " . $result->recommended['speed'] . " km/h";
}

Get Speed Limits for Multiple Coordinates

You can pass an array of coordinates (with lat/lng or latitude/longitude keys):

$coordinates = [
    ['lat' => 59.9139, 'lng' => 10.7522],
    ['latitude' => 60.3913, 'longitude' => 5.3221],
    ['lat' => 61.1234, 'lng' => 11.5678],
];

$results = NvdbSpeedLimits::getSpeedLimitsForCoordinates($coordinates);

foreach ($results as $item) {
    if ($item['error']) {
        echo "Error for coordinate (" . json_encode($item['coordinate']) . "): " . $item['error'] . "\n";
    } else {
        echo "Speed limit at (" . $item['coordinate']['lat'] . ", " . $item['coordinate']['lng'] . "): " . $item['result']->recommended['speed'] . " km/h\n";
    }
}

Error Handling

  • If invalid coordinates are provided, an InvalidCoordinatesException will be thrown.
  • For batch lookups, errors are included in the result array for each coordinate.
  • Always check the found property on the result object to determine if a speed limit was found.

Testing

Run the tests with:

composer test

Contributing

Contributions are welcome! Please open issues or submit pull requests for improvements or bug fixes.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Create a new Pull Request

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.