Skip to content

Commit

Permalink
Feature - many to many matrices (#78)
Browse files Browse the repository at this point in the history
* add distances to time filter fast

* remove deprecated functions

* 6.0.0

* add readme entries for many to many functions

* update deps
  • Loading branch information
MockusTravelTime authored Aug 29, 2024
1 parent cfcda6d commit 7d3d13e
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 180 deletions.
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,58 @@ travelTimeClient.timeFilter({
.catch((e) => console.error(e));
```

### Time Filter Many to Many Utility

This utility function leverages `travelTimeClient.timeFilter` (as described in the previous section) to efficiently construct Many-to-Many matrices in batches.


```ts
const matrix = await travelTimeClient.manyToManyMatrix({
coordsFrom: [
{ lat: 51.5055, lng: -0.0754 },
{ lat: 51.5171, lng: -0.1062 },
{ lat: 51.5309, lng: -0.1215 },
{ lat: 51.5022, lng: -0.1149 },
{ lat: 51.5144, lng: -0.1427 },
],
coordsTo: [
{ lat: 51.5055, lng: -0.0754 },
{ lat: 51.5171, lng: -0.1062 },
{ lat: 51.5309, lng: -0.1215 },
{ lat: 51.5022, lng: -0.1149 },
{ lat: 51.5144, lng: -0.1427 },
],
transportation: { type: 'driving' },
properties: ['travel_time', 'distance'],
travelTime: 1800,
leaveTime: new Date().toISOString(),
});
```

The response will be in the following format:

```json
{
"travelTimes":[
[0,1637,-1,1516,-1],
[1689,0,956,991,-1],
[-1,815,0,1193,1178],
[1550,1072,1522,0,1748],
[-1,1702,1414,1556,0]
],
"distances":[
[0,1637,-1,1516,-1],
[1689,0,956,991,-1],
[-1,815,0,1193,1178],
[1550,1072,1522,0,1748],
[-1,1702,1414,1556,0]
],
"errors":[]
}
```

The `travelTimes` and `distances` arrays are structured so that you can access specific values using `response.travelTimes[indexOfLocationFrom][indexOfLocationTo]`. Unreachable destinations are represented by `-1`. If there was an error while fetching the response, those entries will be marked as `null`. You can view all errors in the `response.errors` field.

### [Time Filter (Fast)](https://traveltime.com/docs/api/reference/time-filter-fast)
A very fast version of `time_filter()`.
However, the request parameters are much more limited.
Expand Down Expand Up @@ -369,6 +421,57 @@ travelTimeClient.timeFilterFast({
.catch((e) => console.error(e));
```

### Time Filter Fast Many to Many Utility

This utility function leverages `travelTimeClient.timeFilterFast` (as described in the previous section) to efficiently construct Many-to-Many matrices in batches.


```ts
const matrix = await travelTimeClient.manyToManyMatrixFast({
coordsFrom: [
{ lat: 51.5055, lng: -0.0754 },
{ lat: 51.5171, lng: -0.1062 },
{ lat: 51.5309, lng: -0.1215 },
{ lat: 51.5022, lng: -0.1149 },
{ lat: 51.5144, lng: -0.1427 },
],
coordsTo: [
{ lat: 51.5055, lng: -0.0754 },
{ lat: 51.5171, lng: -0.1062 },
{ lat: 51.5309, lng: -0.1215 },
{ lat: 51.5022, lng: -0.1149 },
{ lat: 51.5144, lng: -0.1427 },
],
transportation: { type: 'driving' },
properties: ['travel_time', 'distance'],
travelTime: 1800,
});
```

The response will be in the following format:

```json
{
"travelTimes":[
[0,1143,-1,1472,-1],
[1254,0,949,1080,1733],
[-1,997,0,1629,1462],
[1522,953,1285,0,1638],
[-1,-1,1249,-1,0]
],
"distances":[
[0,1143,-1,1472,-1],
[1254,0,949,1080,1733],
[-1,997,0,1629,1462],
[1522,953,1285,0,1638],
[-1,-1,1249,-1,0]
],
"errors":[]
}
```

The `travelTimes` and `distances` arrays are structured so that you can access specific values using `response.travelTimes[indexOfLocationFrom][indexOfLocationTo]`. Unreachable destinations are represented by `-1`. If there was an error while fetching the response, those entries will be marked as `null`. You can view all errors in the `response.errors` field.

### [Time Filter Fast (Proto)](https://traveltime.com/docs/api/reference/travel-time-distance-matrix-proto)
A fast version of time filter communicating using [protocol buffers](https://github.com/protocolbuffers/protobuf).

Expand Down
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "traveltime-api",
"version": "5.8.1",
"version": "6.0.0",
"description": "TravelTime API SDK for node js with TypeScript",
"main": "target/index.js",
"types": "target/index.d.ts",
Expand All @@ -19,7 +19,7 @@
"license": "MIT",
"dependencies": {
"agentkeepalive": "^4.5.0",
"axios": "^1.6.7",
"axios": "^1.7.5",
"protobufjs": "^7.3.2"
},
"devDependencies": {
Expand All @@ -39,4 +39,4 @@
"files": [
"target/**/*"
]
}
}
33 changes: 13 additions & 20 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ import { TimeMapFastResponseType, TimeMapResponseType } from '../types/timeMapRe
import { RateLimiter, RateLimitSettings } from './rateLimiter';
import {
distanceMapSimpleToRequest,
mergeTimeFilterResponses,
routesSimpleToRequest,
timeFilterFastSimpleToFullMatrix,
timeFilterFastSimpleToRequest,
timeFilterSimpleToFullMatrix,
timeFilterSimpleToRequest,
timeMapFastSimpleToRequest,
timeMapSimpleToRequest,
} from './mapper';
import { TimeFilterFastManyToManyMatrixRequest, TimeFilterManyToManyMatrixRequest } from '../types/timeFilterMatrix';
import {
timeFilterFastManyToManyMatrixResponseMapper, timeFilterFastManyToManyMatrixToRequest, timeFilterManyToManyMatrixResponseMapper, timeFilterManyToManyMatrixToRequest,
} from './matrixMapper';

type HttpMethod = 'get' | 'post'

Expand Down Expand Up @@ -259,33 +260,25 @@ export class TravelTimeClient {

timeFilter = async (body: TimeFilterRequest) => this.request<TimeFilterResponse>('/time-filter', 'post', { body });
timeFilterBatch = async (requests: TimeFilterRequest[]) => this.batch(this.timeFilter, requests);
manyToManyMatrix = async (body: TimeFilterManyToManyMatrixRequest) => {
const requests = timeFilterManyToManyMatrixToRequest(body);
const responses = await this.timeFilterBatch(requests);
return timeFilterManyToManyMatrixResponseMapper(responses, body.coordsFrom.length, body.coordsTo.length, body.properties || ['travel_time']);
};

/**
* Simplified version of timeFilter.
* Allows you to pass multiple coordinates with same params for matrixes to be made.
* @param {TimeFilterSimple} body Simplified TimeFilterRequest type. Default search type is `departure`.
*/
timeFilterSimple = async (body: TimeFilterSimple) => this.timeFilter(timeFilterSimpleToRequest(body));
/**
* Generates a data frame (full matrix).
* Travel times are calculated from each point to the remaining points passed into the function.
* @param {TimeFilterSimple} body Simplified TimeFilterRequest type. Default search type is `departure`.
*/
timeFilterFullMatrix = async (body: TimeFilterSimple) => {
const responses = await this.timeFilterBatch(timeFilterSimpleToFullMatrix(body));
return mergeTimeFilterResponses(responses);
};

timeFilterFast = async (body: TimeFilterFastRequest) => this.request<TimeFilterFastResponse>('/time-filter/fast', 'post', { body });
timeFilterFastBatch = async (requests: TimeFilterFastRequest[]) => this.batch(this.timeFilterFast, requests);
/**
* Generates a data frame (full matrix).
* Travel times are calculated from each point to the remaining points passed into the function.
* @param {TimeFilterFastSimple} body Simplified TimeFilterFastRequest type. Default search type is `departure`.
*/
timeFilterFastFullMatrix = async (body: TimeFilterFastSimple) => {
const responses = await this.timeFilterFastBatch(timeFilterFastSimpleToFullMatrix(body));
return mergeTimeFilterResponses(responses);
manyToManyMatrixFast = async (body: TimeFilterFastManyToManyMatrixRequest) => {
const requests = timeFilterFastManyToManyMatrixToRequest(body);
const responses = await this.timeFilterFastBatch(requests);
return timeFilterFastManyToManyMatrixResponseMapper(responses, body.coordsFrom.length, body.coordsTo.length, body.properties || ['travel_time']);
};

/**
Expand Down
Loading

0 comments on commit 7d3d13e

Please sign in to comment.