-
Notifications
You must be signed in to change notification settings - Fork 4
/
way.m
876 lines (815 loc) · 24.4 KB
/
way.m
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
way ; Way Class
; Copyright (C) 2008 Etienne Cherdlu <[email protected]>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as
; published by the Free Software Foundation, either version 3 of the
; License, or (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Affero General Public License for more details.
;
; You should have received a copy of the GNU Affero General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
add(sWay,delete) ; Public ; Add a way
; #sWay = stream object containing way
;
n line,wayId,ndSeq,timestamp,user,uid,version,changeset
n conflict,a,currentUid,blockedByUid
;
s line=sWay("current")
;
s wayId=$$getAttribute^osmXml(line,"id")
s version=$$getAttribute^osmXml(line,"version")
s changeset=$$getAttribute^osmXml(line,"changeset")
s timestamp=$$getAttribute^osmXml(line,"timestamp")
s user=$$getAttribute^osmXml(line,"user")
i user["'" s user=$$xmlEscapeApostrophe(user)
s uid=$$getAttribute^osmXml(line,"uid")
;
;
; Don't load older versions
i $g(^waytag(wayId,"@version"))>version d q
. ;
. ; Skip the rest of the element
. i line["/>" q
. f d i line["</way>" q
. . s line=$$read^stream(.sWay)
;
; Conflict checks
s conflict=0
s currentUid=$g(^waytag(wayId,"@uid"),0)
s a=version_$c(1)_changeset_$c(1)_timestamp_$c(1)_uid
;
; Don't load forked elements
i 'conflict,$d(^waytag(wayId,"@fork")) d
. s conflict=1
. d log^conflict("way",wayId,currentUid,a,"Edited in fosm")
;
; Don't load edits from blocked users
i 'conflict,uid'="",$g(^user(uid,"osmImport"))="block" d
. s conflict=1
. s blockedByUid=$g(^user(uid,"blockedByUid"),uid)
. d log^conflict("way",wayId,blockedByUid,a,"User #"_uid_" ("_$g(^user(uid,"name"),"unknown")_") blocked by "_blockedByUid)
;
; Don't delete if current user's edits are marked to be preserved
i 'conflict,delete,currentUid'="",$g(^user(currentUid,"osmImport"))="doNotDelete" d
. s conflict=1
. s blockedByUid=$g(^user(currentUid,"blockedByUid"),currentUid)
. d log^conflict("way",wayId,blockedByUid,a,"Deletion of element belonging to #"_currentUid_" ("_$g(^user(currentUid,"name"),"unknown")_") preserved. Do not delete requested by user #"_blockedByUid)
;
; If no conflict then delete old way
i 'conflict d delete(wayId,changeset,delete)
;
; Add way attributes
d setTag(wayId,"@timestamp",timestamp,changeset,version,delete,conflict)
d setTag(wayId,"@user",user,changeset,version,delete,conflict)
d setTag(wayId,"@uid",uid,changeset,version,delete,conflict)
d setTag(wayId,"@version",version,changeset,version,delete,conflict)
d setTag(wayId,"@changeset",changeset,changeset,version,delete,conflict)
i delete d setTag(wayId,"@visible","false",changeset,version,delete,conflict)
i conflict d setTag(wayId,"@conflict","true",changeset,version,delete,conflict)
;
; Changeset by version index
i 'conflict d
. s ^wayVersion(wayId,"v",version)=changeset
. s ^wayVersion(wayId,"c",changeset,version)=""
;
; Changeset headers
s ^c(changeset)=""
s ^c(changeset,"w",wayId)=""
s ^c(changeset,"w",wayId,"v",version)=""
;
; If there are nodes and/or tags then process these
i line'["/>" d
. ;
. s ndSeq=0
. ;
. f d i line["</way>" q
. . s line=$$read^stream(.sWay)
. . i line["<nd" s ndSeq=ndSeq+1 d nd(wayId,ndSeq,line,changeset,version,delete,conflict) q
. . i line["<tag" d addTag(.sWay,wayId,changeset,version,delete,conflict) q
;
; Index the tags for this way
i 'conflict,'delete d indexTags(wayId)
;
; Create export index (don't add conflicted ways to the index)
i 'conflict s ^export($$nowZulu^date(),"w",changeset,wayId,version)=""
;
; Update user class
d add^user(uid,user)
;
d onEdit^user(uid)
;
; Update metrics (don't count conflicted ways)
i 'conflict d
. i version=1 d update^metric("osmWayCount",1)
. i delete d update^metric("osmWayCount",-1)
. d update^metric("osmWayEdits",1)
;
q
; import(sWay,delete) ; Public ; Import a way, add to changeset only at this point
; #sWay = stream object containing way
;
;n line,wayId,ndSeq,timestamp,user,uid,version,changeset
;
;s line=sWay("current")
;
;s wayId=$$getAttribute^osmXml(line,"id")
;s version=$$getAttribute^osmXml(line,"version")
;s changeset=$$getAttribute^osmXml(line,"changeset")
;
; Update - changeset
;s ^c(changeset)=""
;s ^c(changeset,"w",wayId)=""
;s ^c(changeset,"w",wayId,"v",version)=""
;
;s timestamp=$$getAttribute^osmXml(line,"timestamp")
;s user=$$getAttribute^osmXml(line,"user")
;i user["'" s user=$$xmlEscapeApostrophe(user)
;s uid=$$getAttribute^osmXml(line,"uid")
;
; Update - way attributes
;s visible="" i delete s visible="false"
;s a=version_$c(1)_changeset_$c(1)_timestamp_$c(1)_uid_$c(1)_visible_$c(1)_$c(1)
;s ^c(changeset,"w",wayId,"v",version,"a")=a
;s ^temp($j,"loadDiff","w",wayId,"a")=a
;
; If there are nodes and/or tags then process these
;i line'["/>" d
;. ;
;. s ndSeq=0
;. ;
;. f d i line["</way>" q
;. . s line=$$read^stream(.sWay)
;. . i line["<nd" s ndSeq=ndSeq+1 d importNode(wayId,ndSeq,line,changeset,version,delete) q
;. . i line["<tag" d importTag(.sWay,wayId,changeset,version,delete) q
;
; Update user class
;d add^user(uid,user)
;
;d onEdit^user(uid)
;q
; importNode(wayId,ndSeq,line,changeset,version,delete) ; Private ; Load a way node
;
;n nodeId
;
;s nodeId=$$getAttribute^osmXml(line,"ref")
;; i nodeId<0 s nodeId=$g(^temp($j,"node",nodeId)) ; Get allocated id from new element map
;
;s ^c(changeset,"w",wayId,"v",version,"n",ndSeq)=nodeId
;
;q
; importTag(sWay,wayId,changeset,version,delete) ; Private ; Load a tag and add it
;
;n k,u,v
;
;s line=sWay("current")
;i line'["/>" f d i line["/>" q
;. s line=line_$$read^stream(.sWay)
;
;s k=$$getAttribute^osmXml(line,"k") i k="" q
;
; Get internal value for the key or assign one
;s u=$g(^keyx(key))
;i u="" d
;. l +^key
;. s (u,^key)=^key+1
;. s ^key(u)=key
;. s ^keyx(key)=u
;. l -^key
;
;i $l(k)>100 s k=$e(k,1,100)_".."
;s v=$$getAttribute^osmXml(line,"v")
;i v["'" s v=$$xmlEscapeApostrophe(v) ; The planet export doesn't escape apostrophes
;i $l(v)>4000 s v=$e(v,1,4000)_".."
;
;s ^c(changeset,"w",wayId,"v",version,"u",u)=v
;
;q
; apply(changeset,wayId,version) ; Public ; Apply a way from a changeset to the active database
;
; TODO: This looks rubbish...
; Where's ^way(id,seq) get updated?
; Should update first then calculate bbox after all nodes have been added
; Should not be writing to ^e(qs,"w"...)
; Other?
;
;n qsOld,a,delete,qsNew,l
;n u,key,value,intValue
;
;s qsOld=$p($g(^way(wayId)),$c(1),1)
;
;s a=^c(changeset,"n",wayId,"v",version,"a")
;s delete=($p(a,$c(1),5)="false")
;d bbox(wayId,.bllat,.bllon,.trlat,.trlon)
;s qsNew=$$bbox^quadString(bllat,bllon,trlat,trlon)
;i qsNew="" s qsNew="*"
;s $p(a,$c(1),6)=qsNew
;
; Update - node
;i 'delete d
;. ;
;. ; If the qs key has changed then delete the old entries
;. i qsOld'="",qsOld'=qsNew d
;. . k ^e(qsOld,"w",nodeId,"a")
;. ;
;. ; Update the node with new values
;. s ^e(qsNew,"n",nodeId,"a")=$p(a,$c(1),1,6)
;
; Update - changeset by version index
;s ^wayVersion(wayId,"q")=qsNew
;s ^wayVersion(wayId,"v",version)=changeset
;s ^wayVersion(wayId,"c",changeset,version)=""
;
; Update process <tag> elements
;s u=""
;f d i u="" q
;. s u=$o(^c(changeset,"w",wayId,"v",version,"u",u)) i u="" q
;. s value=^c(changeset,"w",wayId,"v",version,"u",u)
;. d applyTag(qsOld,qsNew,wayId,u,value,changeset,version,delete)
;
; Delete all tags that are not on the new version of the way
;s u=""
;i qsOld'="" f d i u="" q
;. s u=$o(^e(qsOld,"w",wayId,"u",u)) i u="" q
;. s key=^key(u)
;. i 'delete,$d(^c(changeset,"w",wayId,"v",version,"u",u)) q
;. s value=^e(qsOld,"w",wayId,"u",u)
;. s intValue=value
;. i $l(intValue)>100 s intValue=$e(value,1,100)_".."
;. i intValue'="" k ^wayx(key,intValue,qsOld,wayId)
;. k ^wayx(key,"*",qsOld,wayId)
;. k ^e(qsOld,"w",wayId,"u",u)
;
;i delete,qsOld'="" k ^e(qsOld,"w",wayId)
;
; Create export index
;s ^export($$nowZulu^date(),"w",changeset,wayId,version)=""
;
;q
; applyTag(qsOld,qsNew,wayId,u,value,changeset,version,delete) ; TODO: Write this
;q 1/0
; addTagFromChangeset(qsOld,qsNew,nodeId,u,newValue,changeset,version,delete) ; Private ; Update (add/modify/delete) a key/value pair for a node
;
; Usage:
; d updateTag(qsOld,qsNew,nodeId,key,newValue,newChangeset,newVersion,delete)
; qsOld - qs of the old node. Null if this is a new node with no previous version
; qsNew - qs of the new node. Null if this tag is to be deleted
; nodeId - id of the node in question
; key - the tag's key
; newValue - the new value of the tag, may be null
; newChangeset - the id of the changeset for this update
; newVersion - the version number of the node being updated
; delete - 1 if the whole node is being deleted, 0 if this is an update
;
;n key,oldValue,intNewValue,intOldValue
;
;s key=^key(u)
;
;s oldValue=""
;i qsOld'="" s oldValue=$g(^e(qsOld,"n",nodeId,"u",u))
;
;s intNewValue=newValue
;i $l(newValue)>100 s intNewValue=$e(newValue,1,100)_".."
;
;s intOldValue=oldValue
;i $l(oldValue)>100 s intOldValue=$e(oldValue,1,100)_".."
;
; Delete the tag and it's indexes if the node is being deleted
;i delete d
;. ; k ^e(qsOld,"n",nodeId,"u",u) ; Don't actually need to do this becaue the whole node will be deleted anyway
;. i intOldValue'="" k ^nodex(key,intOldValue,qsOld,nodeId)
;. k ^nodex(key,"*",qsOld,nodeId)
;
; Add/Update the tag for the element
;e d
;. i (oldValue'=newValue)!(qsOld'=qsNew) d ; Optimisation, can be used when all t tags have gone
;. . i qsOld'="",qsOld'=qsNew k ^e(qsOld,"n",nodeId,"u",u)
;. . i qsNew'="" s ^e(qsNew,"n",nodeId,"u",u)=newValue
;. ;
;. ; Update the two key/value indexes
;. i (intOldValue'=intNewValue)!(qsOld'=qsNew) d
;. . i qsOld'="",intOldValue'="" k ^nodex(key,intOldValue,qsOld,nodeId)
;. . i qsNew'="",intNewValue'="" s ^nodex(key,intNewValue,qsNew,nodeId)=""
;. . i qsOld'="" k ^nodex(key,"*",qsOld,nodeId)
;. . i qsNew'="" s ^nodex(key,"*",qsNew,nodeId)=""
;
;q
addDiff(sWay,delete,changeset) ; Public ; Add a way
; #sWay = stream object containing way
;
n line,wayId,ndSeq,timestamp,user,uid,version,ok,predecessors
;
s line=sWay("current")
;
s wayId=$$getAttribute^osmXml(line,"id")
s timestamp=$$nowZulu^date()
;
; New ways
i wayId<0 d
. s oldId=wayId
. l +^id("way")
. s wayId=^id("way")+1
. s ^id("way")=wayId
. l -^id("way")
. s newId=wayId
. s version=1
. ;
. ; Add to new item map
. s ^temp($j,"way",oldId)=newId
;
; Existing ways
s ok=1
e d i 'ok q 0
. s version=$$getAttribute^osmXml(line,"version")
. ; check version match
. i $$currentVersion(wayId)'=version d error409^http("Version mismatch: Provided "_version_", server had: "_$$currentVersion(wayId)_" of Way "_wayId) s ok=0 q ; Version mismatch
. s oldId=wayId
. s newId=wayId
. s version=version+1
. d delete(wayId,changeset,delete)
;
s uid=^c(changeset,"t","@uid")
s user=^user(uid,"name")
i user["'" s user=$$xmlEscapeApostrophe(user)
;
; Way attributes
d setTag(wayId,"@timestamp",timestamp,changeset,version,delete,0)
d setTag(wayId,"@user",user,changeset,version,delete,0)
d setTag(wayId,"@uid",uid,changeset,version,delete,0)
d setTag(wayId,"@version",version,changeset,version,delete,0)
d setTag(wayId,"@changeset",changeset,changeset,version,delete,0)
d setTag(wayId,"@fork",1,changeset,version,delete,0)
i delete d setTag(wayId,"@visible","false",changeset,version,delete,0)
;
; Changeset by version index
s ^wayVersion(wayId,"v",version)=changeset
s ^wayVersion(wayId,"c",changeset,version)=""
;
; Changeset headers
s ^c(changeset)=""
s ^c(changeset,"w",wayId)=""
s ^c(changeset,"w",wayId,"v",version)=""
;
; If there are nodes and/or tags then process these
i line'["/>" d
. ;
. s ndSeq=0
. ;
. s predecessors=""
. f d i line["</way>" q
. . s line=$$read^stream(.sWay)
. . i line["<nd" s ndSeq=ndSeq+1 d nd(wayId,ndSeq,line,changeset,version,delete,0) q
. . i line["<tag" d addTag(.sWay,wayId,changeset,version,delete,0,.predecessors) q
. ;
. ; Inject a meta:predecessors tag if there are any predecessors for this version
. i predecessors'="" d
. . s k="meta:predecessors"
. . s v=$e(predecessors,2,$l(predecessors))
. . i 'delete s ^waytag(wayId,k)=v
. . s ^c(changeset,"w",wayId,"v",version,"t",k)=v
;
;
; Index the tags for this way
i 'delete d indexTags(wayId)
;
; Create export index (uses the same timestamp as the element just created)
s ^export(timestamp,"w",changeset,wayId,version)=""
;
s rSeq=$g(^response($j))+1
s ^response($j)=rSeq
;
i delete s newId="",version=""
;
s ^response($j,rSeq,"oldId")=oldId
s ^response($j,rSeq,"newId")=newId
s ^response($j,rSeq,"version")=version
s ^response($j,rSeq,"element")="way"
;
d onEdit^user(uid)
;
; Update metrics
i version=1 d update^metric("fosmWayCount",1)
i delete d update^metric("fosmWayCount",-1)
d update^metric("fosmWayEdits",1)
;
q 1
nd(wayId,ndSeq,line,changeset,version,delete,conflict) ; Private ; Load a way node
;
n nodeId
;
s nodeId=$$getAttribute^osmXml(line,"ref")
i nodeId<0 s nodeId=$g(^temp($j,"node",nodeId)) ; Get allocated id from new element map
;
; TODO: Validate that node actually exists
i nodeId="" w 1/0
;
; Write to changeset
s ^c(changeset,"w",wayId,"v",version,"n",ndSeq)=nodeId
;
; If there was no conflict then update active database as well
i 'conflict d
. i 'delete d
. . s ^way(wayId,ndSeq)=nodeId
. . s ^wayByNode(nodeId,wayId)=""
. ;
. s ^nodeVersion(nodeId,"c",changeset,"w",wayId)=""
q
addTag(sWay,wayId,changeset,version,delete,conflict,predecessors) ; Private ; Load a tag and add it
;
n k,v,i
;
s line=sWay("current")
i line'["/>" f d i line["/>" q
. s line=line_$$read^stream(.sWay)
;
s k=$$getAttribute^osmXml(line,"k") i k="" q
i $l(k)>70 s k=$e(k,1,70)_".."
s v=$$getAttribute^osmXml(line,"v")
i v["'" s v=$$xmlEscapeApostrophe(v) ; The planet export doesn't escape apostrophes
i $l(v)>4000 s v=$e(v,1,4000)_".."
;
; Swallow meta:lastEdit tag
i k="meta:lastEdit" q
;
; Process meta:id tag
; If the meta:id is the same as the element's id then swallow it
i k="meta:id",v=wayId q
;
; If the meta:id is different then it means that the way has been either merged or split
; In both cases we add the original id value to this way's predecessor list
i k="meta:id" d q
. f i=1:1:$l(v,";") s predecessorId=$p(v,";",i) i predecessorId'=wayId,predecessors'[predecessorId s predecessors=predecessors_";"_predecessorId
;
; Swallow any meta:predecessors tags as these apply only to one version
i k="meta:predecessors" q
;
; Add to changeset
s ^c(changeset,"w",wayId,"v",version,"t",k)=v
;
; Add to active dataset
i 'conflict,'delete d
. s ^waytag(wayId,k)=v
;
q
indexTags(wayId) ; Private ; Create index entries for a single way
;
n qsBox,bllat,bllon,trlat,trlon,k,v
;
; Calculate the qsRoot for this way
d bbox(wayId,.bllat,.bllon,.trlat,.trlon)
s qsBox=$$bbox^quadString(bllat,bllon,trlat,trlon)
i qsBox="" s qsBox="*"
;
s k=""
f d i k="" q
. s k=$o(^waytag(wayId,k)) i k="" q
. i k="@xapi:users" q
. i k="@version" q
. i k="@timestamp" q
. i k="@fork" q
. s v=^waytag(wayId,k)
. i v="" q
. i $l(v)>100 s v=$e(v,1,100)_".."
. s ^wayx(k,v,qsBox,wayId)=""
. s ^wayx(k,"*",qsBox,wayId)=""
;
; Add a quad string index for the way itself
s ^wayx("*","*",qsBox,wayId)=""
;
; Save the qsBox because a node might get moved independently
s ^way(wayId)=qsBox_$c(1)_bllat_$c(1)_bllon_$c(1)_trlat_$c(1)_trlon
q
getTag(wayId,tag) ; Public ; Get the value of a tag for a way
;
i wayId="" q ""
;
q $g(^waytag(wayId,tag))
setTag(wayId,k,v,changeset,version,delete,conflict) ; Private ; Add a tag
;
; Add to changeset
s ^c(changeset,"w",wayId,"v",version,"t",k)=v
;
; Add to active dataset
i 'conflict,'delete d
. s ^waytag(wayId,k)=v
;
q
xml(indent,wayId,select,meta) ; Public ; Generate xml for a way
;
n user,uid,timestamp,version,changeset
n xml
;
s user=$g(^waytag(wayId,"@user"))
s uid=$g(^waytag(wayId,"@uid"))
s timestamp=$g(^waytag(wayId,"@timestamp"))
s version=$g(^waytag(wayId,"@version"))
s changeset=$g(^waytag(wayId,"@changeset"))
;
s xml="<way"
s xml=xml_$$attribute^osmXml("id",wayId)
s xml=xml_$$attribute^osmXml("visible","true")
i timestamp'="" s xml=xml_$$attribute^osmXml("timestamp",timestamp)
i version'="" s xml=xml_$$attribute^osmXml("version",version)
i changeset'="" s xml=xml_$$attribute^osmXml("changeset",changeset)
i user'="" s xml=xml_$$attribute^osmXml("user",user)
i uid'="" s xml=xml_$$attribute^osmXml("uid",uid)
s xml=xml_">"_$c(13,10)
;
d xmlNodes(wayId,.xml)
;
; Inject meta tags
i $g(meta) d
. s xml=xml_"<tag k='meta:id' v='"_wayId_"'/>"
. s xml=xml_"<tag k='meta:lastEdit' v='"_timestamp_"'/>"
;
d xmlTags(wayId,.xml)
s xml=xml_"</way>"_$c(13,10)
;
q xml
xmlNodes(wayId,xml) ; Public ; Generate xml for a way's nodes
;
n ndSeq
;
s ndSeq=""
f d i ndSeq="" q
. s ndSeq=$o(^way(wayId,ndSeq)) i ndSeq="" q
. s xml=xml_"<nd"
. s xml=xml_$$attribute^osmXml("ref",^way(wayId,ndSeq))
. s xml=xml_"/>"_$c(13,10)
. i $l(xml)>10000 d w^write(xml) s xml=""
q
xmlTags(id,xml) ; Public ; Generate xml for a way's tags
;
n k
;
s k=""
f d i k="" q
. s k=$o(^waytag(id,k)) i k="" q
. i $e(k,1)="@" s k="@zzzzzzzzzzzzzzzzzz" q ; Skip attributes
. i $d(^waytag(id,k))#10=0 q
. s xml=xml_"<tag"
. s xml=xml_$$attribute^osmXml("k",k)
. s xml=xml_" v='"_^waytag(id,k)_"'" ; Tags are stored as escaped strings
. s xml=xml_"/>"_$c(13,10)
. i $l(xml)>10000 d w^write(xml) s xml=""
q
bbox(wayId,bllat,bllon,trlat,trlon,recalculate) ; Public ; Calculate the bbox for a way
;
; Inputs:
; recalculate - 0 = used store value if available (default). The stored value may be wrong if nodes have been moved subsequently.
; 1 = recalculate from current node locations (slow).
;
n ndSeq,nodeId,latlon,lat,lon,qsBox,way
;
s recalculate=$g(recalculate)=1
;
; Use previously calculated values if present
i 'recalculate d i bllat'="" q
. s way=$g(^way(wayId))
. s bllat=$p(way,$c(1),2)
. s bllon=$p(way,$c(1),3)
. s trlat=$p(way,$c(1),4)
. s trlon=$p(way,$c(1),5)
;
s bllat=999999,bllon=999999,trlat=-999999,trlon=-999999
s ndSeq=""
f d i ndSeq="" q
. s ndSeq=$o(^way(wayId,ndSeq)) i ndSeq="" q
. s nodeId=^way(wayId,ndSeq)
. ;
. ; Ignore if node does not exist (which is possible)
. s qsBox=$$qsBox^node(nodeId) i qsBox="" q
. s latlon=$g(^e(qsBox,"n",nodeId,"l")) i latlon="" q
. ;
. s lat=$p(latlon,$c(1),1)
. s lon=$p(latlon,$c(1),2)
. i lat>trlat s trlat=lat
. i lat<bllat s bllat=lat
. i lon>trlon s trlon=lon
. i lon<bllon s bllon=lon
;
q
delete(wayId,newChangeset,delete) ; Public ; Delete a way
;
n bllat,bllon,trlat,trlon,qsBox,ndSeq,nodeId
n oldChangeset
n key,value
;
s qsBox=$p($g(^way(wayId)),$c(1),1) i qsBox="" q
;
; Get changeset of way being deleted
s oldChangeset=$g(^waytag(wayId,"@changeset"))
;
; Delete all tags
s key=""
f d i key="" q
. s key=$o(^waytag(wayId,key)) i key="" q
. s value=^waytag(wayId,key)
. i value="" q
. i $l(value)>100 s value=$e(value,1,100)_".."
. k ^wayx(key,value,qsBox,wayId)
. k ^wayx(key,"*",qsBox,wayId)
;
k ^waytag(wayId)
;
; Delete node index
s ndSeq=""
f d i ndSeq="" q
. s ndSeq=$o(^way(wayId,ndSeq)) i ndSeq="" q
. s nodeId=^way(wayId,ndSeq)
. i nodeId="" q
. k ^wayByNode(nodeId,wayId)
. i oldChangeset'="" s ^nodeVersion(nodeId,"c",oldChangeset,"w",wayId)=newChangeset
;
; Delete way
k ^wayx("*","*",qsBox,wayId)
k ^way(wayId)
;
q
appendUser(users,user) ; Private ; Add user to list of users
;
s user=$tr(user,",","") ; Remove commas from name
;
i $$contains^string(users,user,",") q $e(users,1,100)
;
i users="" s users=user
e s users=users_","_user
q $e(users,1,100)
xmlEscapeApostrophe(string) ; Private ; Escape apostrophe
;
n out,x,c
;
s out=""
f x=1:1:$l(string) d
. s c=$e(string,x)
. i "'"[c s out=out_"'" q
. s out=out_c
q out
hasRealTag(id) ; Public ; Does this way have a real tag?
;
n hasRealTag,tag
;
s hasRealTag=0
s tag="@zzzzzzzzzzzzzz"
f d i tag="" q
. s tag=$o(^waytag(id,tag)) i tag="" q
. s hasRealTag=1,tag=""
;
q hasRealTag
versionAtChangeset(wayId,changeset,wayChangeset,wayVersion) ; Public ; Get the changeset and version that was current at a given changeset time
;
; Usage:
; d versionAtChangeset^way(wayId,changeset,.wayChangeset,.wayVersion)
; Output:
; wayChangeset - null if not found
; wayVersion - null if not found
;
s wayChangeset=changeset
s wayVersion=""
i '$d(^wayVersion(wayId,"c",wayChangeset)) s wayChangeset=$o(^wayVersion(wayId,"c",wayChangeset),-1) i wayChangeset="" q
s wayVersion=$o(^wayVersion(wayId,"c",wayChangeset,""),-1) i wayVersion="" s wayChangeset="" q
;
q
restWay(string) ; Public ; Single way query
;
n logId
;
; Get next step
s step=$p(string,SLASH,1),string=$p(string,SLASH,2,$l(string))
s wayId=step
;
; Get next step
s step=$p(string,SLASH,1),string=$p(string,SLASH,2,$l(string))
;
; Four choices here:
; / - current way only
; /full - current way plus current nodes
; /history - all versions of current way
; /#version/full - historic way plus historic nodes
s full=0
i step="" s full=0
i step="full" s full=1
i step="history" d restWayHistory(wayId) q
i step?1.n d restWay^wayVersion(wayId,step,string) q
;
s logId=$$logStart^xapi($$decode^xapi("way/"_wayId_$s(full:"/full",1:"")),"")
;
; Bad query?
i wayId'?1.n d notFound^http,logEnd^xapi(logId,0,"404 Not found") q
;
; Is it there?
i '$d(^way(wayId)) d gone^http,logEnd^xapi(logId,0,"410 Gone") q
;
d header^http("text/xml")
d xmlProlog^rest("")
;
s indent=""
d osm^xapi(indent)
;
k ^temp($j)
;
; Add all nodes that belong to this way
s ndSeq=""
i full f d i ndSeq="" q
. s ndSeq=$o(^way(wayId,ndSeq)) i ndSeq="" q
. s nodeId=^way(wayId,ndSeq)
. i $d(^temp($j,nodeId)) q
. s ^temp($j,nodeId)=""
. w $$xml^node(indent,nodeId,"node|@*|tag|")
;
w $$xml(indent,wayId,"way|@*|nd|tag|")
;
w indent,"</osm>",$c(13,10)
;
d logEnd^xapi(logId,1,"")
;
q
restWays(step,string) ; Public ; Multi way query
;
n logId,wayIds,ok,i,wayId,indent,version,changesetId
;
s logId=$$logStart^xapi($$decode^xapi(step),"")
;
s wayIds=$p(step,EQUALS,2)
;
; Validate query
s ok=1
f i=1:1:$l(wayIds,",") d i 'ok q
. s wayId=$p(wayIds,",",i)
. i wayId'?1.n d notFound^http,logEnd^xapi(logId,0,"404 Not found") s ok=0 q
. i '$d(^wayVersion(wayId)) d gone^http,logEnd^xapi(logId,0,"410 Gone") s ok=0 q
i 'ok q
;
d header^http("text/xml")
d xmlProlog^rest("")
;
s indent=""
d osm^xapi(indent)
;
; Use changeset version in case one of the selected versions has been deleted
f i=1:1:$l(wayIds,",") d
. s wayId=$p(wayIds,",",i)
. s version=$o(^wayVersion(wayId,"v",""),-1) i version="" q
. s changesetId=^wayVersion(wayId,"v",version)
. w $$xml^wayVersion(indent,wayId,changesetId,version,"way|@*|nd|tag|")
;
w indent,"</osm>",$c(13,10)
;
d logEnd^xapi(logId,i,"")
;
q
restWayHistory(wayId) ; Public ; All versions of way
;
n logId,count,indent
n version,changeset
;
s count=0
s logId=$$logStart^xapi("way/"_wayId_"/history","")
;
; Bad query?
i wayId'?1.n d notFound^http,logEnd^xapi(logId,0,"404 Not found") q
;
; Is it there?
;i '$d(^wayVersion(wayId)),$d(^way(wayId)) d q
. w "Status: 307 Moved Temporarily",!
. w "Location: ","http://api.openstreetmap.org/api/0.6/way/"_wayId_"/history",!
. w !
i '$d(^wayVersion(wayId)) d notFound^http,logEnd^xapi(logId,0,"404 Not found") q
;
d header^http("text/xml")
d xmlProlog^rest("")
;
s indent=""
d osm^xapi(indent)
;
; Iterate all versions
s version=""
f d i version="" q
. s version=$o(^wayVersion(wayId,"v",version)) i version="" q
. s changeset=^wayVersion(wayId,"v",version)
. ;
. w $$xml^wayVersion(indent,wayId,changeset,version,"way|@*|nd|tag|")
. s count=count+1
;
w indent,"</osm>",$c(13,10)
;
d logEnd^xapi(logId,count,"")
;
q
currentVersion(wayId) ; Public ; Return the current version number of a way
;
; Usage: s currentVersion=$$currentVersion^way(wayId)
; Input:
; wayId - way id, must not be null
; Output:
; currentVersion - if the way does not exists then null is returned (if the way has been deleted then the deleted version number is returned)
;
n version
;
s version=$o(^wayVersion(wayId,"v",""),-1)
i version="" s version=$g(^waytag(wayId,"@version"))
q version