# Road Segments API

## Introduction

The Road Segments API from NextBillion.ai provides users with detailed information on all road segments within a specified geographic area. The service provides users with a list of all road segments, their shapes, midpoints, and associated driving attributes, including maximum speed and direction of travel. Users can define either a circular or bounding box area by providing simple inputs to receive this information.

POST

https://api.nextbillion.io/map/segments?key={your_api_key}



### Request Parameters

| Name | Required | Format and Usage | Description |
|------|----------|------------------|-------------|
| `key` | Yes | Type: `string`<br>Format: 32 character alphanumeric string<br>Example: key=API_KEY | A key is a unique identifier that is required to authenticate a request to the API. |

### Request Body

| Field | Type | Description |
|-------|------|-------------|
| `type` | string | Indicate the type of area to retrieve the road segments it contains. Only a "circle" or a "bbox" can be specified. |
| `circle` | object | An object to collect the details of a circular area. Please note that `circle` attribute is effective only when `type = circle`. |
| `circle.center` | object | Specify the coordinates of the center of the circular area. |
| `circle.center.lat` | number | Latitude of the circle's center location. |
| `circle.center.lon` | number | Longitude of the circle's center location. |
| `circle.radius` | integer | Specify the radius of the circle, in meters, of the circular area. |
| `bbox` | object | An object to collect details of a bounding box's boundaries. Please note that `bbox` attribute is effective only when `type = bbox`. |
| `bbox.max_lat` | number | Specify the maximum latitude of the bounding box area. |
| `bbox.max_lon` | number | Specify the maximum longitude of the bounding box area. |
| `bbox.min_lat` | number | Specify the minimum latitude of the bounding box area. |
| `bbox.min_lon` | number | Specify the minimum longitude of the bounding box area. |

### Response Schema

| Field | Type | Description |
|-------|------|-------------|
| `status` | string | A string indicating the state of the response. On successful responses, the value will be `Ok` otherwise it will be `Failed` . |
| `message` | string | Displays indicative error messages in case of a failed request or operation. See the [API Error Codes](#api-error-codes) section below for more information. |
| `segments` | array of object | An array of objects returning the details of individual segments contained within the specified area. Each object represents a single segment. |
| `segments[].segment_id` | integer | Returns the unique ID of the road segment returned. |
| `segments[].shape` | string | Returns the encoded geometry of the road segment in polyline format. |
| `segments[].mid_point` | object | Returns the location coordinates of the mid-point of the given road segment. |
| `segments[].mid_point.lat` | number | Latitude of the mid-point location. |
| `segments[].mid_point.lon` | number | Longitude of the mid-point location. |
| `segments[].attributes` | object | An object returning attributes associated with the given road segment. Each attribute is represented as a "key : value" pair. |
| `segments[].attributes.way_name` | string | Returns the name or reference of the road. |
| `segments[].attributes.road_type` | string | Returns the road classifications such as `motorway`, `primary`, `residential`, `service`, or `ferry`. |
| `segments[].attributes.motorcar:forward` | boolean | Whether cars are permitted to travel in the forward direction. Here forward means following the coordinate order of the decoded segment shape. |
| `segments[].attributes.motorcar:backward` | boolean | Whether cars are permitted to travel in the backward direction. Here backward means going against the coordinate order of the decoded segment shape. |
| `segments[].attributes.length` | number | Returns the length of the road segment, in meters |
| `segments[].attributes.incident` | boolean | Whether or not the segment is marked as closed in the traffic data. However, it is not a general indicator of every incident. |
| `segments[].attributes.is_heavy_vehicle_route` | boolean | Whether or not the segment is part of a heavy-vehicle route or a dedicated freight network. |
| `segments[].attributes.truck_route_type` | string | If the road segment is part of a dedicate freight network or a heavy-vehicle route, this field returns the applicable designated truck classes, separated by \|. Possible values are `rigid_truck`, `semi_trailer`, `b_double`, `road_train`, or `generic_truck`. |
| `segments[].attributes.truck:forward` | boolean | Whether trucks are permitted to travel in the forward direction. Please note that:<br>*   Here forward means following the coordinate order of the decoded segment shape.<br>*   This flags describe mapped access, and should not treated as valid clearance for a particular truck’s dimensions or cargo. |
| `segments[].attributes.truck:backward` | boolean | Whether trucks are permitted to travel in the backward direction. Please note that:<br>*   Here backward means going against the coordinate order of the decoded segment shape.<br>*   This flags describe mapped access, and should not treated as valid clearance for a particular truck’s dimensions or cargo. |
| `segments[].attributes.speed_limit` | number | Returns the general speed limit recorded in the base map, in km/h. A value of `0` means unavailable and `-1` means unlimited. |
| `segments[].attributes.truck_speed_limit` | number | Returns the truck-specific speed limit recorded in the base map, in km/h. A value of `0` means unavailable and `-1` means unlimited. |
| `segments[].attributes.hazmat_type` | string | Returns the prohibited hazardous-cargo categories for the given road segment. Multiple values are separated by a `\|`. Possible values are `general`, `explosive`, `harmful_to_water`, or `circumstantial`. |

### Sample Request

```bash
curl --location 'https://api.nextbillion.io/map/segments?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
    "type": "circle",
    "circle": {
        "center": {
            "lat": 1.3132272656379491,
            "lon": 103.82278560368337
        },
        "radius": 100
    }
}'
```

### Sample Response

```json
{
    "status": "Ok",
    "message": "",
    "segments": [
        {
            "segment_id": 641457318,
            "shape": "sq_G}wtxR@B@D@BBJ",
            "mid_point": {
                "lat": 1.31368,
                "lon": 103.82218
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 16,
                "motorcar:backward": false,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "service",
                "speed_limit": 0,
                "truck:backward": false,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": ""
            }
        },
        {
            "segment_id": 41890609,
            "shape": "kj_Gk{txROT",
            "mid_point": {
                "lat": 1.3126200000000001,
                "lon": 103.82266999999999
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 15,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 40,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": "Fernhill Close"
            }
        },
        {
            "segment_id": 691859080,
            "shape": "el_G{~txRMd@",
            "mid_point": {
                "lat": 1.3129,
                "lon": 103.82315
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 22,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 40,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": "Fernhill Road"
            }
        },
        {
            "segment_id": 691859080,
            "shape": "_l_Gk_uxRABCJ",
            "mid_point": {
                "lat": 1.31281,
                "lon": 103.82339999999999
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 9,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 40,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": "Fernhill Road"
            }
        },
        {
            "segment_id": 933776994,
            "shape": "on_Gs_uxRK\\",
            "mid_point": {
                "lat": 1.3132599999999999,
                "lon": 103.82330999999999
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 18,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 0,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": ""
            }
        },
        {
            "segment_id": 641457318,
            "shape": "wq_GcxtxRBD",
            "mid_point": {
                "lat": 1.3137,
                "lon": 103.82223
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 3,
                "motorcar:backward": false,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "service",
                "speed_limit": 0,
                "truck:backward": false,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": ""
            }
        },
        {
            "segment_id": 691859077,
            "shape": "mn_G}_uxRlAP",
            "mid_point": {
                "lat": 1.3128000000000002,
                "lon": 103.82342
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 45,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 40,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": "Fernhill Road"
            }
        },
        {
            "segment_id": 762223277,
            "shape": "iq_GcwtxRTN",
            "mid_point": {
                "lat": 1.31354,
                "lon": 103.82202000000001
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 15,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "service",
                "speed_limit": 0,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": ""
            }
        },
        {
            "segment_id": 933776994,
            "shape": "mn_G}_uxRAH",
            "mid_point": {
                "lat": 1.3132000000000001,
                "lon": 103.82346
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 5,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 0,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": ""
            }
        },
        {
            "segment_id": 765596018,
            "shape": "wq_GcxtxRDE",
            "mid_point": {
                "lat": 1.31369,
                "lon": 103.82229
            },
            "attributes": {
                "hazmat_type": "",
                "incident": false,
                "is_heavy_vehicle_route": false,
                "length": 4,
                "motorcar:backward": true,
                "motorcar:forward": true,
                "region": "SGP|",
                "road_type": "residential",
                "speed_limit": 50,
                "truck:backward": true,
                "truck:forward": true,
                "truck_route_type": "",
                "truck_speed_limit": 0,
                "way_name": "Dalvey Road"
            }
        }
    ]
}
```

## API Query Limits

1. The maximum value that can be specified for the `radius` is 50000m (50 km).
2. The maximum diagonal length of the configured bounding box should be less than 100,000m (100km).
3. NextBillion.ai allows a maximum rate limit of 6000 queries per minute or 100 queries/second for continuous requests.
   *Note*: We can increase the quota if needed on request. Contact [support@nextbillion.ai](mailto:support@nextbillion.ai) for more details.

## API Error Codes

| Response code | Description | Additional notes |
| --- | --- | --- |
| 200 | Normal success case. | Normal success case. |
| 400 | Input validation failed. | There is a missing or an invalid parameter or a parameter with an invalid value type is added to the request. |
| 401 | APIKEY not supplied or invalid | This error occurs when the wrong API key is passed in the request or the key is missing altogether. |
| 403 | APIKEY is valid but does not have access to requested resources | You might be querying for a geographical region which is not valid for your account or requesting a service which is not enabled for you. |
| 404 | Requested host/path not found | This error occurs when a malformed hostname is used. |
| 422 | Could not process the request | A feasible solution could not be generated for the given set of locations or parameter configuration. |
| 429 | Too many requests | QPM reached or API request count quota reached. |
| 500 | Internal Service error. | There was an internal issue with NextBillion.ai services. You can reach out to [support@nextbillion.ai](mailto:support@nextbillion.ai) for an explanation. |
