diff --git a/CHANGELOG.md b/CHANGELOG.md index 415e985..ab716b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## 0.9.20 - 28.10.2023 +- improve handling of coordinates in `Location` objects - [PR #34](https://github.com/openTdataCH/ojp-js/pull/34) + ## 0.9.19 - 15.10.2023 - adds [SIRI-SX](https://opentransportdata.swiss/en/siri-sx/) to `StopPoint` - [PR #32](https://github.com/openTdataCH/ojp-js/pull/32) diff --git a/README.md b/README.md index 0401210..91b0d40 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The OJP Javascript SDK is the client used for communication with [OJP APIs](http ``` "dependencies": { - "ojp-sdk": "0.9.19" + "ojp-sdk": "0.9.20" } ``` diff --git a/package.json b/package.json index a1249a5..13f6e04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ojp-sdk", - "version": "0.9.19", + "version": "0.9.20", "description": "OJP (Open Journey Planner) Javascript SDK", "main": "lib/index.js", "type": "module", diff --git a/src/location/geoposition.ts b/src/location/geoposition.ts index cc8a38c..e78759a 100644 --- a/src/location/geoposition.ts +++ b/src/location/geoposition.ts @@ -8,9 +8,9 @@ export class GeoPosition { public properties: GeoJSON.GeoJsonProperties | null constructor(longitude: number, latitude: number) { - this.longitude = longitude - this.latitude = latitude - this.properties = null + this.longitude = parseFloat(longitude.toFixed(6)); + this.latitude = parseFloat(latitude.toFixed(6)); + this.properties = null; } public static initFromContextNode(contextNode: Node): GeoPosition | null { diff --git a/src/location/location.ts b/src/location/location.ts index 206be97..46cd1fc 100644 --- a/src/location/location.ts +++ b/src/location/location.ts @@ -15,6 +15,8 @@ interface NearbyLocation { // TODO - long term: subclass from Location? export type LocationType = 'stop' | 'address' | 'poi' | 'topographicPlace' +const literalCoordsRegexp = /^([0-9\.]+?),([0-9\.]+?)$/; + export class Location { public address: Address | null public stopPointRef: string | null @@ -101,7 +103,7 @@ export class Location { public static initWithLngLat(longitude: number, latitude: number): Location { const location = new Location() - location.geoPosition = new GeoPosition(longitude, latitude) + location.geoPosition = new GeoPosition(longitude, latitude); return location } @@ -138,9 +140,13 @@ export class Location { } public static initFromLiteralCoords(inputS: string): Location | null { - const inputLiteralCoords = inputS.trim().replace(/[^0-9\.,]/g, ''); + let inputLiteralCoords = inputS.trim(); + // strip: parantheses (groups) + inputLiteralCoords = inputLiteralCoords.replace(/\(.+?\)/g, ''); + // strip: characters NOT IN [0..9 , .] + inputLiteralCoords = inputLiteralCoords.replace(/[^0-9\.,]/g, ''); - const inputMatches = inputLiteralCoords.match(/^([0-9\.]+?),([0-9\.]+?)$/); + const inputMatches = inputLiteralCoords.match(literalCoordsRegexp); if (inputMatches === null) { return null } @@ -152,15 +158,16 @@ export class Location { longitude = parseFloat(inputMatches[2]) latitude = parseFloat(inputMatches[1]) } - const location = Location.initWithLngLat(longitude, latitude) - const locationName = inputS.trim().replace(/(\(?[0-9\.]*\s?,\s?[0-9\.]*\)?)/, '').trim(); - if (locationName !== '') { + // Match the content inside the () + const locationNameMatches = inputS.trim().match(/\(([^\)]*)\)?/); + if (locationNameMatches !== null) { + const locationName = locationNameMatches[1]; location.locationName = locationName; } - + return location } @@ -214,7 +221,7 @@ export class Location { return feature } - public computeLocationName(): string | null { + public computeLocationName(includeLiteralCoords: boolean = true): string | null { if (this.stopPlace) { return this.stopPlace.stopPlaceName; } @@ -231,7 +238,7 @@ export class Location { return this.locationName; } - if (this.geoPosition) { + if (includeLiteralCoords && this.geoPosition) { return this.geoPosition.asLatLngString(); }