UNPKG

9.29 kBtext/coffeescriptView Raw
1assert = require('chai').assert
2
3MapPLZ = require('../mapplz').MapPLZ
4mapstore = new MapPLZ
5
6describe('add point', ->
7 it('uses params: lat, lng', (done) ->
8 mapstore.add(40, -70, (err, pt) ->
9 assert.equal(pt.lat, 40)
10 assert.equal(pt.lng, -70)
11 done()
12 )
13 )
14
15 it('uses params: lat, lng, key_property', (done) ->
16 mapstore.add(40, -70, 'key', (err, pt) ->
17 assert.equal(pt.lat, 40)
18 assert.equal(pt.lng, -70)
19 assert.equal(pt.properties.property, 'key')
20 done()
21 )
22 )
23
24 it('uses params: lat, lng, properties', (done) ->
25 mapstore.add(40, -70, { hello: 'world' }, (err, pt) ->
26 assert.equal(pt.lat, 40)
27 assert.equal(pt.lng, -70)
28 assert.equal(pt.properties.hello, 'world')
29 done()
30 )
31 )
32
33 it('uses params: [lat, lng]', (done) ->
34 mapstore.add([40, -70], (err, pt) ->
35 assert.equal(pt.lat, 40)
36 assert.equal(pt.lng, -70)
37 done()
38 )
39 )
40
41 it('uses params: [lat, lng, key_property]', (done) ->
42 mapstore.add([40, -70, 'key'], (err, pt) ->
43 assert.equal(pt.lat, 40)
44 assert.equal(pt.lng, -70)
45 assert.equal(pt.properties.property, 'key')
46 done()
47 )
48 )
49
50 it('uses params: [lat, lng, properties]', (done) ->
51 mapstore.add([40, -70, { hello: 'world' }], (err, pt) ->
52 assert.equal(pt.lat, 40)
53 assert.equal(pt.lng, -70)
54 assert.equal(pt.properties.hello, 'world')
55 done()
56 )
57 )
58
59 it('uses params: { lat: 40, lng: -70 }', (done) ->
60 mapstore.add({ lat: 40, lng: -70}, (err, pt) ->
61 assert.equal(pt.lat, 40)
62 assert.equal(pt.lng, -70)
63
64 mapstore.add({ lat: 40, lng: -70, color: "#f00" }, (err, pt) ->
65 assert.equal(pt.properties.color, "#f00")
66 done()
67 )
68 )
69 )
70
71 it('uses JSON string: { "lat": 40, "lng": -70 }', (done) ->
72 mapstore.add('{ "lat": 40, "lng": -70}', (err, pt) ->
73 assert.equal(pt.lat, 40)
74 assert.equal(pt.lng, -70)
75 done()
76 )
77 )
78)
79
80describe('add line', ->
81 it('uses params: [[lat1, lng1], [lat2, lng2]]', (done) ->
82 mapstore.add([[40, -70], [22, -110]], (err, line) ->
83 assert.equal(line.type, 'line')
84 first_pt = line.path[0]
85 assert.equal(first_pt[0], 40)
86 assert.equal(first_pt[1], -70)
87 done()
88 )
89 )
90
91 it('uses params: { path: [[lat1, lng1], [lat2, lng2]] }', (done) ->
92 mapstore.add({ path: [[40, -70], [22, -110]] }, (err, line) ->
93 assert.equal(line.type, 'line')
94 first_pt = line.path[0]
95 assert.equal(first_pt[0], 40)
96 assert.equal(first_pt[1], -70)
97 done()
98 )
99 )
100
101 it('adds properties: { path: [pt1, pt], key: "x" }', (done) ->
102 mapstore.add({ path: [[40, -70], [22, -110]], key: "x" }, (err, line) ->
103 assert.equal(line.type, 'line')
104 first_pt = line.path[0]
105 assert.equal(first_pt[0], 40)
106 assert.equal(first_pt[1], -70)
107 assert.equal(line.properties.key, "x")
108 done()
109 )
110 )
111
112 it('uses JSON string: { path: [[lat1, lng1], [lat2, lng2]] }', (done) ->
113 mapstore.add('{ "path": [[40, -70], [22, -110]] }', (err, line) ->
114 assert.equal(line.type, 'line')
115 first_pt = line.path[0]
116 assert.equal(first_pt[0], 40)
117 assert.equal(first_pt[1], -70)
118 done()
119 )
120 )
121)
122
123describe('add polygon', ->
124 it('uses params: [[[lat1, lng1], [lat2, lng2], [lat3, lng3], [lat1, lng1]]]', (done) ->
125 mapstore.add([[[40, -70], [22, -110], [40, -110], [40, -70]]], (err, poly) ->
126 assert.equal(poly.type, 'polygon')
127 first_pt = poly.path[0][0]
128 assert.equal(first_pt[0], 40)
129 assert.equal(first_pt[1], -70)
130 done()
131 )
132 )
133
134 it('uses params: { path: [[[lat1, lng1], [lat2, lng2], [lat3, lng3], [lat1, lng1]]] }', (done) ->
135 mapstore.add({ path: [[[40, -70], [22, -110], [40, -110], [40, -70]]]}, (err, poly) ->
136 assert.equal(poly.type, 'polygon')
137 first_pt = poly.path[0][0]
138 assert.equal(first_pt[0], 40)
139 assert.equal(first_pt[1], -70)
140 done()
141 )
142 )
143
144 it('uses params to add properties', (done) ->
145 mapstore.add({ path: [[[40, -70], [22, -110], [40, -110], [40, -70]]], key: "x" }, (err, poly) ->
146 assert.equal(poly.type, 'polygon')
147 first_pt = poly.path[0][0]
148 assert.equal(first_pt[0], 40)
149 assert.equal(first_pt[1], -70)
150 assert.equal(poly.properties.key, "x")
151 done()
152 )
153 )
154
155 it('uses JSON string: { path: [[[lat1, lng1], [lat2, lng2], [lat3, lng3], [lat1, lng1]]] }', (done) ->
156 mapstore.add('{"path": [[[40, -70], [22, -110], [40, -110], [40, -70]]]}', (err, poly) ->
157 assert.equal(poly.type, 'polygon')
158 first_pt = poly.path[0][0]
159 assert.equal(first_pt[0], 40)
160 assert.equal(first_pt[1], -70)
161 done()
162 )
163 )
164)
165
166describe('add GeoJSON', ->
167 it('uses GeoJSON Feature', (done) ->
168 mapstore.add({ type: "Feature", geometry: { type: "Point", coordinates: [-70, 40] } }, (err, pt) ->
169 assert.equal(pt.lat, 40)
170 assert.equal(pt.lng, -70)
171 done()
172 )
173 )
174
175 it('uses GeoJSON string', (done) ->
176 mapstore.add('{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] } }', (err, pt) ->
177 assert.equal(pt.lat, 40)
178 assert.equal(pt.lng, -70)
179 done()
180 )
181 )
182
183 it('uses GeoJSON FeatureCollection', (done) ->
184 mapstore.add({ type: "FeatureCollection", features: [{ type: "Feature", geometry: { type: "Point", coordinates: [-70, 40] } }]}, (err, pts) ->
185 pt = pts[0]
186 assert.equal(pt.lat, 40)
187 assert.equal(pt.lng, -70)
188 done()
189 )
190 )
191
192 it('uses GeoJSON properties', (done) ->
193 mapstore.add({ type: "Feature", geometry: { type: "Point", coordinates: [-70, 40] }, properties: { color: "#f00" } }, (err, pt) ->
194 assert.equal(pt.lat, 40)
195 assert.equal(pt.lng, -70)
196 assert.equal(pt.properties.color, "#f00")
197
198 mapstore.add('{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-70, 40] }, "properties": { "color": "#f00" } }', (err, pt) ->
199 assert.equal(pt.lat, 40)
200 assert.equal(pt.lng, -70)
201 assert.equal(pt.properties.color, "#f00")
202 done()
203 )
204 )
205 )
206)
207
208describe('queries', ->
209 it('returns a count of points added', (done) ->
210 mapstore = new MapPLZ
211 mapstore.add(40, -70)
212 mapstore.add(40, -70)
213 mapstore.count(null, (err, count) ->
214 assert.equal(count, 2)
215 done()
216 )
217 )
218
219 it('deletes a point', (done) ->
220 mapstore = new MapPLZ
221 mapstore.add(40, -70, (err, pt) ->
222 pt.delete ->
223 mapstore.count(null, (err, count) ->
224 assert.equal(count, 0)
225 done()
226 )
227 )
228 )
229
230 it 'queries by property', (done) ->
231 mapstore = new MapPLZ
232 mapstore.add({ lat: 2, lng: 3, color: 'blue' }, (err, pt) ->
233 mapstore.add({ lat: 2, lng: 3, color: 'red' }, (err, pt2) ->
234 mapstore.query({ color: 'blue' }, (err, results) ->
235 assert.equal(results.length, 1)
236 assert.equal(results[0].properties.color, "blue")
237 done()
238 )
239 )
240 )
241
242 it 'counts by property', (done) ->
243 mapstore = new MapPLZ
244 mapstore.add({ lat: 2, lng: 3, color: 'blue' }, (err, pt) ->
245 mapstore.add({ lat: 2, lng: 3, color: 'red' }, (err, pt2) ->
246 mapstore.count({ color: 'blue' }, (err, count) ->
247 assert.equal(count, 1)
248 done()
249 )
250 )
251 )
252
253 it 'finds nearest point', (done) ->
254 mapstore = new MapPLZ
255 mapstore.add { lat: 40, lng: -70 }, (err, pt) ->
256 mapstore.add { lat: 35, lng: 110 }, (err, pt2) ->
257 mapstore.near [30, -60], 1, (err, nearest) ->
258 assert.equal(nearest.length, 1)
259 response = nearest[0]
260 assert.equal(response.lat, 40)
261 assert.equal(response.lng, -70)
262 done()
263
264 it 'finds point in polygon', (done) ->
265 mapstore = new MapPLZ
266 mapstore.add { lat: 40, lng: -70 }, (err, pt) ->
267 mapstore.add { lat: 35, lng: 110 }, (err, pt2) ->
268 mapstore.inside [[[38, -72], [38, -68], [42, -68], [42, -72], [38, -72]]], (err, within) ->
269 assert.equal(within.length, 1)
270 nearest = within[0]
271 assert.equal(nearest.lat, 40)
272 assert.equal(nearest.lng, -70)
273 done()
274)
275
276describe('strange inputs', ->
277 it 'reads WKT string', (done) ->
278 mapstore = new MapPLZ
279 mapstore.add "LINESTRING(-70 40, -110 22)", (err, line) ->
280 response = line.path[0]
281 assert.equal(response[0], 40)
282 assert.equal(response[1], -70)
283 done()
284
285 it 'reads csv string with lat and lng columns', (done) ->
286 mapstore = new MapPLZ
287 mapstore.add "lat,lng,label\n40,-70,banana", (err, pts) ->
288 response = pts[0]
289 assert.equal(response.lat, 40)
290 assert.equal(response.lng, -70)
291 done()
292
293 it 'reads csv string with WKT column', (done) ->
294 mapstore = new MapPLZ
295 mapstore.add "wkt,label\nPOINT(-70 40),banana", (err, pts) ->
296 response = pts[0]
297 assert.equal(response.lat, 40)
298 assert.equal(response.lng, -70)
299 done()
300
301 it 'reads mapplz code', (done) ->
302 mapstore = new MapPLZ
303 mapcode = "map\n"
304 mapcode += " marker\n"
305 mapcode += " [40, -70]\n"
306 mapcode += " plz\n"
307 mapcode += "plz\n"
308 mapstore.add mapcode, (err, pts) ->
309 response = pts[0]
310 assert.equal(response.lat, 40)
311 assert.equal(response.lng, -70)
312 done()
313)