-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
101 lines (88 loc) · 3.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Feature, featureCollection, FeatureCollection, lineString, LineString, point, Point } from "@turf/helpers";
import { lonlatsToCoords } from "sharedstreets";
import { SharedStreetsGeometry, SharedStreetsIntersection, SharedStreetsReference } from "sharedstreets-types";
interface SharedStreetsGeometryGeoJSON extends Feature<LineString, SharedStreetsGeometry> {
role: "SharedStreets:Geometry";
}
interface SharedStreetsIntersectionGeoJSON extends Feature<Point, SharedStreetsIntersection> {
role: "SharedStreets:Intersection";
}
interface SharedStreetsReferenceGeoJSON extends SharedStreetsReference {
role: "SharedStreets:Reference";
}
interface SharedStreetsGeoJSON extends FeatureCollection {
references: SharedStreetsReferenceGeoJSON[];
}
/**
* Geometry
*
* @param {SharedStreetsGeometry} geom JSON SharedStreetsGeometry
* @returns {Feature<LineString, SharedStreetsGeometry>} GeoJSON LineString Feature SharedSteetsGeometry
* @example
* const line = [[110, 45], [115, 50], [120, 55]];
* const geom = sharedstreets.geometry(line);
* const geojson = sharedstreetsGeoJSON.geometry(geom)));
* geojson // => Feature<LineString, SharedStreetsGeometry>
*/
export function geometry(geom: SharedStreetsGeometry): SharedStreetsGeometryGeoJSON {
const coords = lonlatsToCoords(geom.lonlats);
const properties = geom;
// Do not include (lonlats) to GeoJSON properties
if (properties.lonlats) { delete properties.lonlats; }
// Add GeoJSON Foreign Member (role)
const line: any = lineString(coords, properties);
line.role = "SharedStreets:Geometry";
return line;
}
/**
* Intersection
*
* @param {SharedStreetsIntersection} intersect JSON SharedStreetsIntersection
* @returns {Feature<Point, SharedStreetsIntersection>} GeoJSON Point Feature SharedSteetsIntersection
* @example
* const pt = [110, 45];
* const intersect = sharedstreets.intersection(pt);
* const geojson = sharedstreetsGeoJSON.intersection(intersect)));
* geojson // => Feature<Point, SharedStreetsIntersection>
*/
export function intersection(intersect: SharedStreetsIntersection): SharedStreetsIntersectionGeoJSON {
const coords = [intersect.lon, intersect.lat];
const properties = intersect;
// Do not include (lon, lat) to GeoJSON properties
if (properties.lon) { delete properties.lon; }
if (properties.lat) { delete properties.lat; }
// Add GeoJSON Foreign Member (role)
const pt: any = point(coords, properties);
pt.role = "SharedStreets:Intersection";
return pt;
}
/**
* Reference
*
* @param {SharedStreetsReference} intersect JSON SharedStreetsReference
* @returns {SharedStreetsReference} SharedSteetsIntersection with Role
*/
export function reference(ref: SharedStreetsReference): SharedStreetsReferenceGeoJSON {
const data: any = ref;
data.role = "SharedStreets:Reference";
return data;
}
/**
* GeoJSON
*
* @param {Array<SharedStreetsGeometry>} geometries An Array of SharedStreetsGeometry
* @param {Array<SharedStreetsIntersection>} intersections An Array of SharedStreetsIntersection
* @param {Array<SharedStreetsReference>} references An Array of SharedStreetsReference
* @returns {FeatureCollection} SharedStreets GeoJSON
*/
export function geojson(
geometries: SharedStreetsGeometry[],
intersections: SharedStreetsIntersection[],
references: SharedStreetsReference[],
): SharedStreetsGeoJSON {
const data: any = featureCollection([]);
geometries.forEach((item) => data.features.push(geometry(item)));
intersections.forEach((item) => data.features.push(intersection(item)));
references.forEach((item) => data.references.push(reference(item)));
return data;
}