Skip to content

Commit

Permalink
Added road vehicle height restriction units
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Oct 11, 2020
1 parent f242152 commit fad634f
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 37 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ In addition to identifiers, `country-coder` can provide basic regional informati
- ☎️ [Telephone Calling Codes](https://en.wikipedia.org/wiki/List_of_country_calling_codes) (+44)
- 🛣 [Driving Side](https://en.wikipedia.org/wiki/Left-_and_right-hand_traffic) (right, left)
- 🚗 [Traffic Speed Unit](https://en.wikipedia.org/wiki/Speed_limit#Signage) (km/h, mph)
- 🚚 [Vehicle Height Unit](https://wiki.openstreetmap.org/wiki/Key:maxheight) (m, ft)
- 🇪🇺 [European Union Membership](https://en.wikipedia.org/wiki/Member_state_of_the_European_Union)


Expand Down Expand Up @@ -110,6 +111,7 @@ This package is kept intentionally minimal. However, if you find a bug or have a
* [isInEuropeanUnion](#isInEuropeanUnion)(query: Location | string | number): boolean
* [driveSide](#driveSide)(query: Location | string | number): string?
* [roadSpeedUnit](#roadSpeedUnit)(query: Location | string | number): string?
* [roadHeightUnit](#roadHeightUnit)(query: Location | string | number): string?
* [callingCodes](#callingCodes)(query: Location | string | number): [string]

##### Properties
Expand Down Expand Up @@ -440,7 +442,7 @@ driveSide(pointGeoJSON.geometry); // returns 'left' (Britain)


<a name="roadSpeedUnit" href="#roadSpeedUnit">#</a> <b>roadSpeedUnit</b>(query: Location | string | number): string?
[<>](https://github.com/ideditor/country-coder/blob/master/src/country-coder.ts#L498 "Source")
[<>](https://github.com/ideditor/country-coder/blob/master/src/country-coder.ts#L727 "Source")

Returns the unit of speed used on traffic signs for the given location or identifier, if found.

Expand All @@ -465,8 +467,34 @@ roadSpeedUnit(pointGeoJSON.geometry); // returns 'mph' (Britain)
```


<a name="roadHeightUnit" href="#roadHeightUnit">#</a> <b>roadHeightUnit</b>(query: Location | string | number): string?
[<>](https://github.com/ideditor/country-coder/blob/master/src/country-coder.ts#L733 "Source")

Returns the unit of length used on vehicle height restriction traffic signs for the given location or identifier, if found.

```js
roadHeightUnit([0, 51.5]); // returns 'ft' (Britain)
roadHeightUnit([6.1, 46.2]); // returns 'm' (Switzerland)
roadHeightUnit([0, 90]); // returns null (North Pole)
roadHeightUnit('EU'); // returns null
roadHeightUnit('GB'); // returns 'ft'
roadHeightUnit('GBR'); // returns 'ft'
roadHeightUnit('826'); // returns 'ft'
roadHeightUnit(826); // returns 'ft'
roadHeightUnit('Q145'); // returns 'ft'
roadHeightUnit('🇬🇧'); // returns 'ft'
roadHeightUnit('UK'); // returns 'ft'
roadHeightUnit('United Kingdom'); // returns 'ft'
roadHeightUnit('CH'); // returns 'm'

let pointGeoJSON = { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 51.5] } };
roadHeightUnit(pointGeoJSON); // returns 'ft' (Britain)
roadHeightUnit(pointGeoJSON.geometry); // returns 'ft' (Britain)
```


<a name="callingCodes" href="#callingCodes">#</a> <b>callingCodes</b>(query: Location | string | number): [string]
[<>](https://github.com/ideditor/country-coder/blob/master/src/country-coder.ts#L504 "Source")
[<>](https://github.com/ideditor/country-coder/blob/master/src/country-coder.ts#L739 "Source")

Returns the full international calling code prefix of phone numbers for the given location or identifier, if any. All prefixes have a country code, with some also including an area code separated by a space character. These are commonly formatted with a preceding plus sign (e.g. `+1 242`).

Expand Down Expand Up @@ -587,6 +615,9 @@ An object containing the attributes of a RegionFeature object.
- `roadSpeedUnit`: `string`, the speed unit used on traffic signs in this feature
- `mph`: miles per hour
- `km/h`: kilometers per hour
- `roadHeightUnit`: `string`, the length unit used on vehicle height restriction signs in this feature
- `ft`: feet and inches
- `m`: meters
- `callingCodes`: `[string]`, the international calling codes for this feature, sometimes including area codes


Expand Down
3 changes: 3 additions & 0 deletions scripts/format-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ let featureProperties = [
'isoStatus',
'driveSide',
'roadSpeedUnit',
'roadHeightUnit',
'callingCodes'
];

Expand All @@ -88,6 +89,8 @@ function validateFeature(feature) {
var name = feature.properties.nameEn;
if (feature.properties.roadSpeedUnit)
console.error(name + ' has no geometry but has roadSpeedUnit');
if (feature.properties.roadHeightUnit)
console.error(name + ' has no geometry but has roadHeightUnit');
if (feature.properties.driveSide) console.error(name + ' has no geometry but has driveSide');
if (feature.properties.callingCodes)
console.error(name + ' has no geometry but has callingCodes');
Expand Down
33 changes: 33 additions & 0 deletions src/country-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ type RegionFeatureProperties = {
// - `km/h`: kilometers per hour
roadSpeedUnit: string | undefined;

// The unit used for road vehicle height restrictions within this feature
// - `ft`: feet and inches
// - `m`: meters
roadHeightUnit: string | undefined;

// The international calling codes for this feature, sometimes including area codes
// e.g. `1`, `1 340`
callingCodes: Array<string> | undefined;
Expand Down Expand Up @@ -168,6 +173,7 @@ function loadDerivedDataAndCaches(borders) {

// must load attributes only after loading geometry features into `members`
loadRoadSpeedUnit(feature);
loadRoadHeightUnit(feature);
loadDriveSide(feature);
loadCallingCodes(feature);

Expand Down Expand Up @@ -306,6 +312,27 @@ function loadDerivedDataAndCaches(borders) {
}
}

function loadRoadHeightUnit(feature: RegionFeature) {
let props = feature.properties;
if (feature.geometry) {
// only `ft` regions are listed explicitly, else assume `m`
if (!props.roadHeightUnit) props.roadHeightUnit = 'm';
} else if (props.members) {
let vals = Array.from(
new Set(
props.members
.map(function (id) {
let member = featuresByCode[id];
if (member.geometry) return member.properties.roadHeightUnit || 'm';
})
.filter(Boolean)
)
);
// if all members have the same value then that's also the value for this feature
if (vals.length === 1) props.roadHeightUnit = vals[0];
}
}

function loadDriveSide(feature: RegionFeature) {
let props = feature.properties;
if (feature.geometry) {
Expand Down Expand Up @@ -702,6 +729,12 @@ export function roadSpeedUnit(query: Location | string | number): string | null
return (feature && feature.properties.roadSpeedUnit) || null;
}

// Returns the road vehicle height restriction unit for the feature matching `query` as a string (`ft` or `m`)
export function roadHeightUnit(query: Location | string | number): string | null {
let feature = smallestOrMatchingFeature(query);
return (feature && feature.properties.roadHeightUnit) || null;
}

// Returns the full international calling codes for phone numbers in the feature matching `query`, if any
export function callingCodes(query: Location | string | number): Array<string> {
let feature = smallestOrMatchingFeature(query);
Expand Down
Loading

0 comments on commit fad634f

Please sign in to comment.