Skip to content

Commit

Permalink
Add intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 13, 2018
1 parent 960eb4c commit 9c503a0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
@@ -1,9 +1,13 @@
# Changelog

## 2018-02-11 - 0.7.0
## 2018-02-12

- Add new method `intersection`

## 2018-02-11

- Convert tests to Typescript
- Add new method `geometry`
- Convert tests to Typescript

## 2018-02-08 - 0.6.0

Expand Down
56 changes: 48 additions & 8 deletions index.ts
Expand Up @@ -5,7 +5,7 @@ import { getCoord, getCoords, getGeom } from "@turf/invariant";
import lineOffset from "@turf/line-offset";
import BigNumber from "bignumber.js";
import { createHash } from "crypto";
import { LocationReference, SharedStreetsGeometry } from "sharedstreets-types";
import { LocationReference, SharedStreetsGeometry, SharedStreetsIntersection } from "sharedstreets-types";
import { isArray } from "util";

/**
Expand Down Expand Up @@ -132,14 +132,17 @@ export function geometry(line: Feature<LineString> | LineString | number[][], op
const backReferenceId = referenceId([toIntersection, fromIntersection], formOfWay);

// Save Results
const id = geometryId(line);
const lonlats = coordsToLonlats(coords);

return {
backReferenceId,
forwardReferenceId,
id,
fromIntersectionId,
id: geometryId(line),
lonlats: coordsToLonlats(coords),
roadClass,
toIntersectionId,
forwardReferenceId,
backReferenceId,
roadClass,
lonlats,
};
}

Expand All @@ -163,8 +166,45 @@ export function intersectionId(pt: number[] | Feature<Point> | Point): string {
* @private
*/
export function intersectionMessage(pt: number[] | Feature<Point> | Point): string {
const [x, y] = getCoord(pt);
return `Intersection ${round(x)} ${round(y)}`;
const [lon, lat] = getCoord(pt);
return `Intersection ${round(lon)} ${round(lat)}`;
}

/**
* Intersection
*
* @param {Feature<Point>|Array<number>} pt Point location reference as a GeoJSON Point or an Array of numbers <longitude, latitude>.
* @param {Object} [options={}] Optional parameters
* @param {string} [options.nodeId] Define NodeId for Intersection
* @returns {string} SharedStreets Intersection
* @example
* const intersection = sharedstreets.intersection([110, 45]);
* intersection.id // => "71f34691f182a467137b3d37265cb3b6"
*/
export function intersection(pt: number[] | Feature<Point> | Point, options: {
nodeId?: number,
inboundReferences?: LocationReference[],
outboundReferencesIds?: LocationReference[],
} = {}): SharedStreetsIntersection {
// Default params
const inboundReferences = options.inboundReferences || [];
const outboundReferences = options.outboundReferencesIds || [];
const nodeId = options.nodeId;

// Main
const [lon, lat] = getCoord(pt);
const id = intersectionId(pt);
const inboundReferenceIds = inboundReferences.map((ref) => ref.intersectionId);
const outboundReferenceIds = outboundReferences.map((ref) => ref.intersectionId);

return {
id,
nodeId,
lon,
lat,
inboundReferenceIds,
outboundReferenceIds,
};
}

/**
Expand Down
23 changes: 18 additions & 5 deletions test.ts
Expand Up @@ -161,13 +161,26 @@ test("sharedstreets -- geometry", (t) => {
const line = [[110, 45], [115, 50], [120, 55]];
const geom = sharedstreets.geometry(line);
t.deepEqual(geom, {
backReferenceId: "197cb60a06518f4d616fea25b5e81266",
forwardReferenceId: "79aad7fa7e4fec23ff2c97d0b33086ce",
fromIntersectionId: "71f34691f182a467137b3d37265cb3b6",
id: "ce9c0ec1472c0a8bab3190ab075e9b21",
lonlats: [ 110, 45, 115, 50, 120, 55 ],
roadClass: "Other",
fromIntersectionId: "71f34691f182a467137b3d37265cb3b6",
toIntersectionId: "42286ee839cf23904b00acfc7d13a2e2",
forwardReferenceId: "79aad7fa7e4fec23ff2c97d0b33086ce",
backReferenceId: "197cb60a06518f4d616fea25b5e81266",
roadClass: "Other",
lonlats: [ 110, 45, 115, 50, 120, 55 ],
});
t.end();
});

test("sharedstreets -- intersection", (t) => {
const intersect = sharedstreets.intersection([110, 45]);
t.deepEqual(intersect, {
id: "71f34691f182a467137b3d37265cb3b6",
inboundReferenceIds: [],
lat: 45,
lon: 110,
nodeId: undefined,
outboundReferenceIds: [],
});
t.end();
});
3 changes: 2 additions & 1 deletion tslint.json
Expand Up @@ -5,7 +5,8 @@
],
"jsRules": {},
"rules": {
"max-line-length": [false]
"max-line-length": [false],
"object-literal-sort-keys": [false]
},
"rulesDirectory": []
}

0 comments on commit 9c503a0

Please sign in to comment.