Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LIR Request #72

Merged
merged 8 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.9.xx - xx.05.2024
- improve `Location/Address` - [PR #71](https://github.com/openTdataCH/ojp-js/pull/71)
- improve requests: allow LIR requests to use mocked XMLs, adds generic `XMLParser`, adds center min, max long/lat members for GeoPositionBBOX - [PR #72](https://github.com/openTdataCH/ojp-js/pull/72)

## 0.9.32 - 24.03.2024
- updates SDK version to `0.9.32` - [PR #70](https://github.com/openTdataCH/ojp-js/pull/70)
Expand Down
81 changes: 74 additions & 7 deletions src/location/geoposition-bbox.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { GeoPosition } from "./geoposition";

import { Polygon } from 'geojson'

export class GeoPositionBBOX {
public southWest: GeoPosition
public northEast: GeoPosition
public center: GeoPosition

public minLongitude: number
public minLatitude: number
public maxLongitude: number
public maxLatitude: number

constructor(geoPositions: GeoPosition | GeoPosition[]) {
if (!Array.isArray(geoPositions)) {
geoPositions = [geoPositions];
}

const minLongitude = Math.min.apply(null, geoPositions.map(gp => gp.longitude));
const minLatitude = Math.min.apply(null, geoPositions.map(gp => gp.latitude));
this.minLongitude = Math.min.apply(null, geoPositions.map(gp => gp.longitude));
this.minLatitude = Math.min.apply(null, geoPositions.map(gp => gp.latitude));

const maxLongitude = Math.max.apply(null, geoPositions.map(gp => gp.longitude));
const maxLatitude = Math.max.apply(null, geoPositions.map(gp => gp.latitude));
this.maxLongitude = Math.max.apply(null, geoPositions.map(gp => gp.longitude));
this.maxLatitude = Math.max.apply(null, geoPositions.map(gp => gp.latitude));

this.southWest = new GeoPosition(minLongitude, minLatitude);
this.northEast = new GeoPosition(maxLongitude, maxLatitude);
this.southWest = new GeoPosition(this.minLongitude, this.minLatitude);
this.northEast = new GeoPosition(this.maxLongitude, this.maxLatitude);

const centerX = (this.southWest.longitude + this.northEast.longitude) / 2;
const centerY = (this.southWest.latitude + this.northEast.latitude) / 2;
this.center = new GeoPosition(centerX, centerY);
}

public static initFromGeoPosition(geoPosition: GeoPosition, width_x_meters: number, width_y_meters: number): GeoPositionBBOX {
Expand Down Expand Up @@ -45,7 +57,16 @@ export class GeoPositionBBOX {

this.southWest = new GeoPosition(southWestLongitude, southWestLatitude);
this.northEast = new GeoPosition(northEastLongitude, northEastLatitude);
})
});

const centerX = (this.southWest.longitude + this.northEast.longitude) / 2;
const centerY = (this.southWest.latitude + this.northEast.latitude) / 2;
this.center = new GeoPosition(centerX, centerY);

this.minLongitude = this.southWest.longitude;
this.minLatitude = this.southWest.latitude;
this.maxLongitude = this.northEast.longitude;
this.maxLatitude = this.northEast.latitude;
}

asFeatureBBOX(): [number, number, number, number] {
Expand Down Expand Up @@ -86,4 +107,50 @@ export class GeoPositionBBOX {

return true
}

public computeWidth(): number {
const northWest = new GeoPosition(this.southWest.longitude, this.northEast.latitude);
const southEast = new GeoPosition(this.northEast.longitude, this.southWest.latitude);

const distLongitude1 = southEast.distanceFrom(this.southWest);
const distLongitude2 = this.northEast.distanceFrom(northWest);
const distance = (distLongitude1 + distLongitude2) / 2;

return distance;
}

public computeHeight(): number {
const northWest = new GeoPosition(this.southWest.longitude, this.northEast.latitude);
const southEast = new GeoPosition(this.northEast.longitude, this.southWest.latitude);

const distLatitude1 = southEast.distanceFrom(this.northEast);
const distLatitude2 = this.southWest.distanceFrom(northWest);
const distance = (distLatitude1 + distLatitude2) / 2;

return distance;
}

public asPolygon(): Polygon {
const bboxSW = this.southWest;
const bboxNW = new GeoPosition(this.southWest.longitude, this.northEast.latitude);
const bboxNE = this.northEast;
const bboxSE = new GeoPosition(this.northEast.longitude, this.southWest.latitude);

const coords: GeoJSON.Position[] = [
bboxSW.asPosition(),
bboxNW.asPosition(),
bboxNE.asPosition(),
bboxSE.asPosition(),
bboxSW.asPosition(),
];

const polygon: GeoJSON.Polygon = {
type: "Polygon",
coordinates: [
coords
]
};

return polygon;
}
}
2 changes: 1 addition & 1 deletion src/request/base-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TreeNode } from "../xml/tree-node";
import { IS_NODE_CLI } from '..';

export class BaseParser {
private rootNode: TreeNode;
protected rootNode: TreeNode;
protected currentNode: TreeNode;
protected stack: TreeNode[];

Expand Down
6 changes: 6 additions & 0 deletions src/request/base-request-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as xmlbuilder from "xmlbuilder";

import { SDK_VERSION } from "..";

export class BaseRequestParams {
protected serviceRequestNode: xmlbuilder.XMLElement;

Expand Down Expand Up @@ -27,6 +29,10 @@ export class BaseRequestParams {
return serviceRequestNode;
}

protected buildRequestorRef() {
return "OJP_JS_SDK_v" + SDK_VERSION;
}

protected buildRequestNode() {
this.serviceRequestNode = this.computeBaseServiceRequestNode();
}
Expand Down
2 changes: 2 additions & 0 deletions src/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export * from './stop-event-request/stop-event-request'
export * from './trips-request/trips-request'

export * from './types/request-info.type'

export * from './xml-parser'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
GeoRestrictionPoiOSMTag,
} from "../../types/geo-restriction.type";
import { BaseRequestParams } from "../base-request-params";
import { SDK_VERSION } from "../..";

export class LocationInformationRequestParams extends BaseRequestParams {
public locationName: string | null;
Expand Down Expand Up @@ -83,7 +82,7 @@ export class LocationInformationRequestParams extends BaseRequestParams {
const dateF = now.toISOString();
this.serviceRequestNode.ele("siri:RequestTimestamp", dateF);

this.serviceRequestNode.ele("siri:RequestorRef", "OJP_JS_SDK_v" + SDK_VERSION);
this.serviceRequestNode.ele("siri:RequestorRef", this.buildRequestorRef());

const requestNode = this.serviceRequestNode.ele(
"OJPLocationInformationRequest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export class LocationInformationRequest extends OJPBaseRequest {

public static initWithLocationName(stageConfig: StageConfig, locationName: string, geoRestrictionType: GeoRestrictionType | null = null): LocationInformationRequest {
const requestParams = LocationInformationRequestParams.initWithLocationName(locationName, geoRestrictionType);
public static initWithRequestMock(mockText: string, stageConfig: StageConfig = DEFAULT_STAGE) {
const emptyRequestParams = new LocationInformationRequestParams();
const request = new LocationInformationRequest(stageConfig, emptyRequestParams);
request.mockRequestXML = mockText;

return request;
}

const request = new LocationInformationRequest(stageConfig, requestParams);
return request;
}
Expand Down
3 changes: 1 addition & 2 deletions src/request/stop-event-request/stop-event-request-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SDK_VERSION } from "../..";
import { GeoPosition } from "../../location/geoposition"
import { StopEventType } from "../../types/stop-event-type"
import { BaseRequestParams } from '../base-request-params';
Expand Down Expand Up @@ -39,7 +38,7 @@ export class StopEventRequestParams extends BaseRequestParams {

this.serviceRequestNode.ele('siri:RequestTimestamp', dateNowF);

this.serviceRequestNode.ele("siri:RequestorRef", "OJP_JS_SDK_v" + SDK_VERSION);
this.serviceRequestNode.ele("siri:RequestorRef", this.buildRequestorRef());

const requestNode = this.serviceRequestNode.ele('OJPStopEventRequest');
requestNode.ele('siri:RequestTimestamp', dateNowF);
Expand Down
3 changes: 1 addition & 2 deletions src/request/trips-request/trips-request-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { TripModeType } from "../../types/trip-mode-type";
import { BaseRequestParams } from "../base-request-params";
import { JourneyPointType } from '../../types/journey-points';
import { Location } from "../../location/location";
import { SDK_VERSION } from "../..";
import { TripRequestBoardingType } from './trips-request'

export class TripsRequestParams extends BaseRequestParams {
Expand Down Expand Up @@ -103,7 +102,7 @@ export class TripsRequestParams extends BaseRequestParams {
const dateF = now.toISOString();
this.serviceRequestNode.ele("siri:RequestTimestamp", dateF);

this.serviceRequestNode.ele("siri:RequestorRef", "OJP_JS_SDK_v" + SDK_VERSION);
this.serviceRequestNode.ele("siri:RequestorRef", this.buildRequestorRef());

const tripRequestNode = this.serviceRequestNode.ele("OJPTripRequest");
tripRequestNode.ele("siri:RequestTimestamp", dateF);
Expand Down
38 changes: 38 additions & 0 deletions src/request/xml-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TreeNode } from "../xml/tree-node";
import { BaseParser } from "./base-parser";

type XMLParserMessage = "DONE" | "ERROR";
export type XMLParserResponse = {
message: XMLParserMessage | null
rootNode: TreeNode
}
export type XMLParserCallback = (response: XMLParserResponse) => void;

export class XMLParser extends BaseParser {
public callback: XMLParserCallback | null = null;

public parseXML(responseXMLText: string): void {
super.parseXML(responseXMLText);
}

protected onError(saxError: any): void {
console.error('ERROR: SAX parser');
console.log(saxError);

if (this.callback) {
this.callback({
message: 'ERROR',
rootNode: this.rootNode,
});
}
}

protected onEnd(): void {
if (this.callback) {
this.callback({
message: 'DONE',
rootNode: this.rootNode,
});
}
}
}