Skip to content

Commit

Permalink
Merge pull request #34 from openTdataCH/feature/improve-location
Browse files Browse the repository at this point in the history
Improve coords handling in Location
  • Loading branch information
vasile authored Oct 28, 2023
2 parents 44729df + 7b86fe2 commit cb5a0e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/location/geoposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 16 additions & 9 deletions src/location/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -231,7 +238,7 @@ export class Location {
return this.locationName;
}

if (this.geoPosition) {
if (includeLiteralCoords && this.geoPosition) {
return this.geoPosition.asLatLngString();
}

Expand Down

0 comments on commit cb5a0e0

Please sign in to comment.