-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawing.ts
155 lines (134 loc) · 3.73 KB
/
drawing.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import mapboxgl from "mapbox-gl";
import {
RawOverpassNode,
} from "./interfaces";
import { FeatureCollection, Geometry, GeoJsonProperties, Feature, GeometryObject } from 'geojson';
import { isGreenRoad, isOrangeRoad, isRedRoad } from "./osm-selectors";
export function drawMarkerAndCard(
item: RawOverpassNode,
map: mapboxgl.Map
): mapboxgl.Marker {
const { lat, lon } = item;
let markerOptions: mapboxgl.MarkerOptions = {};
markerOptions.color = "gray";
const defaultScale = 0.5;
markerOptions.scale = defaultScale;
if (item.tags && item.tags.capacity !== undefined) {
const capacity = parseInt(item.tags.capacity);
console.log({ capacity });
let possibleScale = defaultScale + capacity / 30;
if (possibleScale > 2) {
possibleScale = 2;
}
markerOptions.scale = possibleScale;
if (item.tags && item.tags.covered === "yes") {
markerOptions.color = "green";
}
if (item.tags && item.tags.lit === "yes") {
markerOptions.color = "yellow";
}
if (item.tags && item.tags.bicycle_parking === "shed") {
markerOptions.color = "#00ec18";
}
}
const marker = new mapboxgl.Marker(markerOptions)
.setLngLat([lon, lat])
.addTo(map);
if (window.orientation !== undefined) {
marker.getElement().addEventListener("click", (e) => {
map.flyTo({
center: [lon, lat],
});
});
}
return marker;
}
export function removeMarkers(markers: mapboxgl.Marker[]): void {
markers.map((marker) => marker.remove());
}
export function removeStreetLayers(map: mapboxgl.Map): void {
try {
console.log("Removing sources...");
map.removeLayer('greenRoadsId');
map.removeLayer('redRoadsId');
map.removeLayer('orangeRoadsId');
map.removeSource('greenRoads');
map.removeSource('redRoads');
map.removeSource('orangeRoads');
} catch (e) {
console.log("not removing sources - at least one doesn't exist yet");
}
}
export function addStreetLayers(map: mapboxgl.Map, geoJson: FeatureCollection<Geometry, GeoJsonProperties>) {
/** Add below first vector layer */
const layerToAddBefore = 'SharedUse';
map.addSource('redRoads', {
type: 'geojson',
data: {
features: geoJson.features.filter(isRedRoad),
type: "FeatureCollection"
}
});
map.addSource('orangeRoads', {
type: 'geojson',
data: {
features: geoJson.features.filter(isOrangeRoad)
,
type: "FeatureCollection"
}
});
map.addSource('greenRoads', {
type: 'geojson',
data: {
features: geoJson.features.filter(isGreenRoad)
,
type: "FeatureCollection"
}
});
// Add a new layer to visualize the polygon.
map.addLayer({
'id': 'redRoadsId',
'type': 'line',
'source': 'redRoads', // reference the data source
'layout': {},
'paint': {
"line-color": "red",
"line-width": 3,
'line-opacity': 0.3
},
}, layerToAddBefore);
map.addLayer({
'id': 'orangeRoadsId',
'type': 'line',
'source': 'orangeRoads', // reference the data source
'layout': {},
'paint': {
"line-color": "orange",
"line-width": 3,
'line-opacity': 0.5
},
}, layerToAddBefore);
// Add a new layer to visualize the polygon.
map.addLayer({
'id': 'greenRoadsId',
'type': 'line',
'source': 'greenRoads', // reference the data source
'layout': {},
'paint': {
"line-color": "#00FF00",
"line-width": 7,
'line-opacity': 0.8
},
}, layerToAddBefore);
}
export function drawMarkersAndCards(
map: mapboxgl.Map,
items: RawOverpassNode[]
): mapboxgl.Marker[] {
const markers = items
.filter((item) => item.type === "node")
.map((node: RawOverpassNode) => {
return drawMarkerAndCard(node, map);
});
return markers;
}