-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeofilters.js
187 lines (163 loc) · 5.82 KB
/
geofilters.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { xyInBBox } from './gis.js'
import { clone } from './geojson.js'
// import simplify from 'https://esm.sh/@turf/simplify'
// util has it's own, this used locally in pathFilter
function nestedProperty(obj, path) {
if (typeof path === 'string') path = path.split('.')
return path.reduce((obj, param) => obj[param], obj)
}
// export function simplifyLineStrings(json, tolerance = 0.01, cloneJson = true) {
// if (cloneJson) json = clone(json)
// // Process each feature
// json.features.forEach(feature => {
// // Only process LineString features
// if (feature.geometry.type === 'LineString') {
// const coords = feature.geometry.coordinates
// // Skip if not enough points to simplify
// if (coords.length <= 2) {
// console.log('coords.length <= 2')
// return
// }
// // Save first and last points
// const firstPoint = [...coords[0]]
// const lastPoint = [...coords[coords.length - 1]]
// // Simplify using turf
// const simplified = simplify(feature, { tolerance })
// // Replace the simplified coordinates
// feature.geometry = simplified.geometry
// // Ensure first and last points are preserved exactly
// if (feature.geometry.coordinates.length >= 2) {
// feature.geometry.coordinates[0] = firstPoint
// feature.geometry.coordinates[
// feature.geometry.coordinates.length - 1
// ] = lastPoint
// }
// }
// })
// return json
// }
// turf.flatten()
// bin/demultline (roads has it's own)
export function flattenMultiLineStrings(json, cloneJson = true) {
if (cloneJson) json = clone(json)
const features = []
json.features.forEach(feature => {
if (feature.geometry.type === 'MultiLineString') {
feature.geometry.coordinates.forEach(coords => {
const copy = Object.assign({}, feature)
copy.geometry.type = 'LineString'
copy.geometry.coordinates = coords
features.push(copy)
})
} else {
features.push(feature)
}
})
json.features = features
return json
}
export function bboxFilter(json, bbox, cloneJson = true) {
if (cloneJson) json = clone(json)
const features = json.features.filter(feature => {
let coords = feature.geometry.coordinates
coords = coords.filter(pt => xyInBBox(bbox, pt))
// if (coords.length !== feature.geometry.coordinates.length)
// console.log(
// feature.geometry.coordinates.length,
// '->',
// coords.length
// )
feature.geometry.coordinates = coords
return coords.length >= 2
})
json.features = features
return json
}
// ========= REMIND: refactor into bin/
// AS: Used locally only, pathFilter
// MB: nmcounties npm script
// The most general filter: keeps a feature only if
// filterFcn(feature, i, features) returns true.
// The filterFcn can also modify feature properties.
// json is mutated, equals the return value (sugar)
export function featureFilter(json, filterFcn) {
json.features = json.features.filter(filterFcn)
return json
}
// Used locally only, geometryFilter & streetsFilter
// Filter by a property (path) matching one of values array.
export function pathFilter(json, featurePath, values) {
if (typeof values === 'string') values = values.split(' ')
featureFilter(json, feature => {
const value = nestedProperty(feature, featurePath)
return values.includes(value)
})
return json
}
// The above specific to geometry (Point, Polygon, LineString)
export function geometryFilter(json, values, cloneJson = true) {
if (cloneJson) json = clone(json)
return pathFilter(json, 'geometry.type', values)
}
// Reduces the feature properties to only those in the properties array.
export function propertiesFilter(json, properties, cloneJson = true) {
if (cloneJson) json = clone(json)
if (typeof properties === 'string') properties = properties.split(' ')
json.features.forEach(feature => {
const obj = {}
properties.forEach(prop => {
if (feature.properties[prop] !== undefined) {
obj[prop] = feature.properties[prop]
}
})
feature.properties = obj
})
return json
}
// AS: bin/json2roads; only used in MB.mkTiles w/ tippecanoe
// service highways could be of interest too, esp fire/emergency
// "highway": "service",
// "service": "xxx" where service is (in santafe zoom 10)
// 6 "service": "emergency_access"
// 120 "service": "alley"
// 141 "service": "drive-through"
// 1300 "service": "driveway"
// 1506 "service": "parking_aisle"
export const osmStreetTypes = [
'motorway',
'trunk',
'residential',
'primary',
'secondary',
'tertiary',
'motorway_link',
'trunk_link',
'primary_link',
'secondary_link',
'tertiary_link',
]
export const osmStreetProperties = [
'highway',
'oneway',
'name',
'tiger:name_base',
]
// A simple filter for streets.
// Filters for:
// - LineString geometry
// - highways matching streetTypes array, defaulted to above
// - properties reduced to streetProperties, defaulted to above
export function streetsFilter(
json,
streetTypes = osmStreetTypes,
streetProperties = osmStreetProperties,
cloneJson = true
) {
if (cloneJson) json = clone(json)
geometryFilter(json, 'LineString')
// see https://wiki.openstreetmap.org/wiki/Highways
// note motorway_junction's are Point geometries
pathFilter(json, 'properties.highway', streetTypes)
propertiesFilter(json, streetProperties)
return json
}