devshaded / nvdb-speed-limits
Fetch Norwegian speed limits from NVDB API
Fund package maintenance!
DevShaded
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
A Laravel package for fetching speed limits from the Norwegian NVDB API.
Table of Contents
- Features
- Installation
- Configuration
- Usage
- Error Handling
- Testing
- Contributing
- Changelog
- Credits
- License
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/my-feature
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin feature/my-feature
) - 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.