-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathRouteDataObject.java
1102 lines (1012 loc) · 32.2 KB
/
RouteDataObject.java
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package net.osmand.binary;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule;
import net.osmand.data.LatLon;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import net.osmand.util.TransliterationHelper;
import org.apache.commons.logging.Log;
import java.text.MessageFormat;
import java.util.Arrays;
import gnu.trove.map.hash.TIntObjectHashMap;
public class RouteDataObject {
/*private */static final int RESTRICTION_SHIFT = 3;
/*private */static final int RESTRICTION_MASK = 7;
public static int HEIGHT_UNDEFINED = -80000;
public final RouteRegion region;
// all these arrays supposed to be immutable!
// These fields accessible from C++
public int[] types;
public int[] pointsX;
public int[] pointsY;
public long[] restrictions;
public long[] restrictionsVia;
public int[][] pointTypes;
public String[][] pointNames;
public int[][] pointNameTypes;
public long id;
public TIntObjectHashMap<String> names;
public final static float NONE_MAX_SPEED = 40f;
public int[] nameIds;
// mixed array [0, height, cumulative_distance height, cumulative_distance, height, ...] - length is length(points)*2
public float[] heightDistanceArray = null;
public float heightByCurrentLocation;
private static final Log LOG = PlatformUtil.getLog(RouteDataObject.class);
public RouteDataObject(RouteRegion region) {
this.region = region;
}
public RouteDataObject(RouteRegion region, int[] nameIds, String[] nameValues) {
this.region = region;
this.nameIds = nameIds;
if (nameIds.length > 0) {
names = new TIntObjectHashMap<String>();
}
for (int i = 0; i < nameIds.length; i++) {
names.put(nameIds[i], nameValues[i]);
}
}
public RouteDataObject(RouteDataObject copy) {
this.region = copy.region;
this.pointsX = copy.pointsX;
this.pointsY = copy.pointsY;
this.types = copy.types;
this.names = copy.names;
this.nameIds = copy.nameIds;
this.restrictions = copy.restrictions;
this.restrictionsVia = copy.restrictionsVia;
this.pointTypes = copy.pointTypes;
this.pointNames = copy.pointNames;
this.pointNameTypes = copy.pointNameTypes;
this.id = copy.id;
}
public boolean compareRoute(RouteDataObject thatObj) {
if (this.id == thatObj.id
&& Arrays.equals(this.pointsX, thatObj.pointsX)
&& Arrays.equals(this.pointsY, thatObj.pointsY)) {
if (this.region == null) {
throw new IllegalStateException("Illegal routing object: " + id);
}
if (thatObj.region == null) {
throw new IllegalStateException("Illegal routing object: " + thatObj.id);
}
boolean equals = true;
equals = equals && Arrays.equals(this.restrictions, thatObj.restrictions);
equals = equals && Arrays.equals(this.restrictionsVia, thatObj.restrictionsVia);
if (equals) {
if (this.types == null || thatObj.types == null) {
equals = this.types == thatObj.types;
} else if (types.length != thatObj.types.length) {
equals = false;
} else {
for (int i = 0; i < this.types.length && equals; i++) {
String thisTag = region.routeEncodingRules.get(types[i]).getTag();
String thisValue = region.routeEncodingRules.get(types[i]).getValue();
String thatTag = thatObj.region.routeEncodingRules.get(thatObj.types[i]).getTag();
String thatValue = thatObj.region.routeEncodingRules.get(thatObj.types[i]).getValue();
equals = (thisTag.equals(thatTag) && thisValue.equals(thatValue));
}
}
}
if (equals) {
if (this.nameIds == null || thatObj.nameIds == null) {
equals = this.nameIds == thatObj.nameIds;
} else if (nameIds.length != thatObj.nameIds.length) {
equals = false;
} else {
for (int i = 0; i < this.nameIds.length && equals; i++) {
String thisTag = region.routeEncodingRules.get(nameIds[i]).getTag();
String thisValue = names.get(nameIds[i]);
String thatTag = thatObj.region.routeEncodingRules.get(thatObj.nameIds[i]).getTag();
String thatValue = thatObj.names.get(thatObj.nameIds[i]);
equals = (Algorithms.objectEquals(thisTag, thatTag) && Algorithms.objectEquals(thisValue, thatValue));
}
}
}
if (equals) {
if (this.pointTypes == null || thatObj.pointTypes == null) {
equals = this.pointTypes == thatObj.pointTypes;
} else if (pointTypes.length != thatObj.pointTypes.length) {
equals = false;
} else {
for (int i = 0; i < this.pointTypes.length && equals; i++) {
if (this.pointTypes[i] == null || thatObj.pointTypes[i] == null) {
equals = this.pointTypes[i] == thatObj.pointTypes[i];
} else if (pointTypes[i].length != thatObj.pointTypes[i].length) {
equals = false;
} else {
for (int j = 0; j < this.pointTypes[i].length && equals; j++) {
String thisTag = region.routeEncodingRules.get(pointTypes[i][j]).getTag();
String thisValue = region.routeEncodingRules.get(pointTypes[i][j]).getValue();
String thatTag = thatObj.region.routeEncodingRules.get(thatObj.pointTypes[i][j]).getTag();
String thatValue = thatObj.region.routeEncodingRules.get(thatObj.pointTypes[i][j]).getValue();
equals = (Algorithms.objectEquals(thisTag, thatTag) && Algorithms.objectEquals(thisValue, thatValue));
}
}
}
}
}
if (equals) {
if (this.pointNameTypes == null || thatObj.pointNameTypes == null) {
equals = this.pointNameTypes == thatObj.pointNameTypes;
} else if (pointNameTypes.length != thatObj.pointNameTypes.length) {
equals = false;
} else {
for (int i = 0; i < this.pointNameTypes.length && equals; i++) {
if (this.pointNameTypes[i] == null || thatObj.pointNameTypes[i] == null) {
equals = this.pointNameTypes[i] == thatObj.pointNameTypes[i];
} else if (pointNameTypes[i].length != thatObj.pointNameTypes[i].length) {
equals = false;
} else {
for (int j = 0; j < this.pointNameTypes[i].length && equals; j++) {
String thisTag = region.routeEncodingRules.get(pointNameTypes[i][j]).getTag();
String thisValue = pointNames[i][j];
String thatTag = thatObj.region.routeEncodingRules.get(thatObj.pointNameTypes[i][j]).getTag();
String thatValue = thatObj.pointNames[i][j];
equals = (Algorithms.objectEquals(thisTag, thatTag) && Algorithms.objectEquals(thisValue, thatValue));
}
}
}
}
}
return equals;
}
return false;
}
public float[] calculateHeightArray() {
return calculateHeightArray(null);
}
public float[] calculateHeightArray(LatLon currentLocation) {
if (heightDistanceArray != null) {
return heightDistanceArray;
}
int startHeight = Algorithms.parseIntSilently(getValue("osmand_ele_start"), HEIGHT_UNDEFINED);
int endHeight = Algorithms.parseIntSilently(getValue("osmand_ele_end"), startHeight);
if (startHeight == HEIGHT_UNDEFINED) {
heightDistanceArray = new float[0];
return heightDistanceArray;
}
heightDistanceArray = new float[2 * getPointsLength()];
double plon = 0;
double plat = 0;
float prevHeight = heightByCurrentLocation = startHeight;
double prevDistance = 0;
for (int k = 0; k < getPointsLength(); k++) {
double lon = MapUtils.get31LongitudeX(getPoint31XTile(k));
double lat = MapUtils.get31LatitudeY(getPoint31YTile(k));
if (k > 0) {
double dd = MapUtils.getDistance(plat, plon, lat, lon);
float height = HEIGHT_UNDEFINED;
if (k == getPointsLength() - 1) {
height = endHeight;
} else {
String asc = getValue(k, "osmand_ele_asc");
if (asc != null && asc.length() > 0) {
height = (prevHeight + Float.parseFloat(asc));
} else {
String desc = getValue(k, "osmand_ele_desc");
if (desc != null && desc.length() > 0) {
height = (prevHeight - Float.parseFloat(desc));
}
}
}
heightDistanceArray[2 * k] = (float) dd;
heightDistanceArray[2 * k + 1] = height;
if (currentLocation != null) {
double distance = MapUtils.getDistance(currentLocation, lat, lon);
if (height != HEIGHT_UNDEFINED && distance < prevDistance) {
prevDistance = distance;
heightByCurrentLocation = height;
}
}
if (height != HEIGHT_UNDEFINED) {
// interpolate undefined
double totalDistance = dd;
int startUndefined = k;
while (startUndefined - 1 >= 0 && heightDistanceArray[2 * (startUndefined - 1) + 1] == HEIGHT_UNDEFINED) {
startUndefined--;
totalDistance += heightDistanceArray[2 * (startUndefined)];
}
if (totalDistance > 0) {
double angle = (height - prevHeight) / totalDistance;
for (int j = startUndefined; j < k; j++) {
heightDistanceArray[2 * j + 1] = (float) ((heightDistanceArray[2 * j] * angle) + heightDistanceArray[2 * j - 1]);
}
}
prevHeight = height;
}
} else {
heightDistanceArray[0] = 0;
heightDistanceArray[1] = startHeight;
}
plat = lat;
plon = lon;
if (currentLocation != null) {
prevDistance = MapUtils.getDistance(currentLocation, plat, plon);
}
}
return heightDistanceArray;
}
public long getId() {
return id;
}
public String getName() {
if (names != null) {
return names.get(region.nameTypeRule);
}
return null;
}
public String getName(String lang) {
return getName(lang, false);
}
public String getName(String lang, boolean transliterate) {
if (names != null) {
if (Algorithms.isEmpty(lang)) {
return names.get(region.nameTypeRule);
}
int[] kt = names.keys();
for (int i = 0; i < kt.length; i++) {
int k = kt[i];
if (region.routeEncodingRules.size() > k) {
if (("name:" + lang).equals(region.routeEncodingRules.get(k).getTag())) {
return names.get(k);
}
}
}
String nmDef = names.get(region.nameTypeRule);
if (transliterate && nmDef != null && nmDef.length() > 0) {
return TransliterationHelper.transliterate(nmDef);
}
return nmDef;
}
return null;
}
public int[] getNameIds() {
return nameIds;
}
public TIntObjectHashMap<String> getNames() {
return names;
}
public String getRef(String lang, boolean transliterate, boolean direction) {
//if (getDestinationRef(direction) != null) {
// return getDestinationRef(direction);
//}
if (names != null) {
if (Algorithms.isEmpty(lang)) {
return names.get(region.refTypeRule);
}
int[] kt = names.keys();
for (int i = 0; i < kt.length; i++) {
int k = kt[i];
if (region.routeEncodingRules.size() > k) {
if (("ref:" + lang).equals(region.routeEncodingRules.get(k).getTag())) {
return names.get(k);
}
}
}
String refDefault = names.get(region.refTypeRule);
if (transliterate && refDefault != null && refDefault.length() > 0) {
return TransliterationHelper.transliterate(refDefault);
}
return refDefault;
}
return null;
}
public String getDestinationRef(boolean direction) {
if (names != null) {
int[] kt = names.keys();
String refTag = (direction == true) ? "destination:ref:forward" : "destination:ref:backward";
String refTagDefault = "destination:ref";
String refDefault = null;
for (int i = 0; i < kt.length; i++) {
int k = kt[i];
if (region.routeEncodingRules.size() > k) {
if (refTag.equals(region.routeEncodingRules.get(k).getTag())) {
return names.get(k);
}
if (refTagDefault.equals(region.routeEncodingRules.get(k).getTag())) {
refDefault = names.get(k);
}
}
}
if (refDefault != null) {
return refDefault;
}
//return names.get(region.refTypeRule);
}
return null;
}
public String getDestinationName(String lang, boolean transliterate, boolean direction) {
//Issue #3289: Treat destination:ref like a destination, not like a ref
String destRef = ((getDestinationRef(direction) == null) || getDestinationRef(direction).equals(getRef(lang, transliterate, direction))) ? "" : getDestinationRef(direction);
String destRef1 = Algorithms.isEmpty(destRef) ? "" : destRef + ", ";
if (names != null) {
int[] kt = names.keys();
// Issue #3181: Parse destination keys in this order:
// destination:lang:XX:forward/backward
// destination:forward/backward
// destination:lang:XX
// destination
String destinationTagLangFB = "destination:lang:XX";
if (!Algorithms.isEmpty(lang)) {
destinationTagLangFB = (direction == true) ? "destination:lang:" + lang + ":forward" : "destination:lang:" + lang + ":backward";
}
String destinationTagFB = (direction == true) ? "destination:forward" : "destination:backward";
String destinationTagLang = "destination:lang:XX";
if (!Algorithms.isEmpty(lang)) {
destinationTagLang = "destination:lang:" + lang;
}
String destinationTagDefault = "destination";
String destinationDefault = null;
for (int i = 0; i < kt.length; i++) {
int k = kt[i];
if (region.routeEncodingRules.size() > k) {
if (!Algorithms.isEmpty(lang) && destinationTagLangFB.equals(region.routeEncodingRules.get(k).getTag())) {
return destRef1 + ((transliterate) ? TransliterationHelper.transliterate(names.get(k)) : names.get(k));
}
if (destinationTagFB.equals(region.routeEncodingRules.get(k).getTag())) {
return destRef1 + ((transliterate) ? TransliterationHelper.transliterate(names.get(k)) : names.get(k));
}
if (!Algorithms.isEmpty(lang) && destinationTagLang.equals(region.routeEncodingRules.get(k).getTag())) {
return destRef1 + ((transliterate) ? TransliterationHelper.transliterate(names.get(k)) : names.get(k));
}
if (destinationTagDefault.equals(region.routeEncodingRules.get(k).getTag())) {
destinationDefault = names.get(k);
}
}
}
if (destinationDefault != null) {
return destRef1 + ((transliterate) ? TransliterationHelper.transliterate(destinationDefault) : destinationDefault);
}
}
return Algorithms.isEmpty(destRef) ? null : destRef;
}
public int getPoint31XTile(int i) {
return pointsX[i];
}
public int getPoint31YTile(int i) {
return pointsY[i];
}
public int getPointsLength() {
return pointsX.length;
}
public int getRestrictionLength() {
return restrictions == null ? 0 : restrictions.length;
}
public int getRestrictionType(int i) {
return (int) (restrictions[i] & RESTRICTION_MASK);
}
public RestrictionInfo getRestrictionInfo(int k) {
RestrictionInfo ri = new RestrictionInfo();
ri.toWay = getRestrictionId(k);
ri.type = getRestrictionType(k);
if (restrictionsVia != null && k < restrictionsVia.length) {
ri.viaWay = restrictionsVia[k];
}
return ri;
}
public long getRestrictionVia(int i) {
if (restrictionsVia != null && restrictionsVia.length > i) {
return restrictionsVia[i];
}
return 0;
}
public long getRestrictionId(int i) {
return restrictions[i] >> RESTRICTION_SHIFT;
}
public boolean hasPointTypes() {
return pointTypes != null;
}
public boolean hasPointNames() {
return pointNames != null;
}
public void insert(int pos, int x31, int y31) {
int[] opointsX = pointsX;
int[] opointsY = pointsY;
int[][] opointTypes = pointTypes;
String[][] opointNames = pointNames;
int[][] opointNameTypes = pointNameTypes;
pointsX = new int[pointsX.length + 1];
pointsY = new int[pointsY.length + 1];
boolean insTypes = this.pointTypes != null && this.pointTypes.length > pos;
boolean insNames = this.pointNames != null && this.pointNames.length > pos;
if (insTypes) {
pointTypes = new int[opointTypes.length + 1][];
}
if (insNames) {
pointNames = new String[opointNames.length + 1][];
pointNameTypes = new int[opointNameTypes.length + 1][];
}
int i = 0;
for (; i < pos; i++) {
pointsX[i] = opointsX[i];
pointsY[i] = opointsY[i];
if (insTypes) {
pointTypes[i] = opointTypes[i];
}
if (insNames) {
pointNames[i] = opointNames[i];
pointNameTypes[i] = opointNameTypes[i];
}
}
pointsX[i] = x31;
pointsY[i] = y31;
if (insTypes) {
pointTypes[i] = null;
}
if (insNames) {
pointNames[i] = null;
pointNameTypes[i] = null;
}
for (i = i + 1; i < pointsX.length; i++) {
pointsX[i] = opointsX[i - 1];
pointsY[i] = opointsY[i - 1];
if (insTypes && i < pointTypes.length) {
pointTypes[i] = opointTypes[i - 1];
}
if (insNames && i < pointNames.length) {
pointNames[i] = opointNames[i - 1];
}
if (insNames && i < pointNameTypes.length) {
pointNameTypes[i] = opointNameTypes[i - 1];
}
}
}
public String[] getPointNames(int ind) {
if (pointNames == null || ind >= pointNames.length) {
return null;
}
return pointNames[ind];
}
public int[] getPointNameTypes(int ind) {
if (pointNameTypes == null || ind >= pointNameTypes.length) {
return null;
}
return pointNameTypes[ind];
}
public int[] getPointTypes(int ind) {
if (pointTypes == null || ind >= pointTypes.length) {
return null;
}
return pointTypes[ind];
}
public int[] getTypes() {
return types;
}
public void processConditionalTags(long conditionalTime) {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r != null && r.conditional()) {
int vl = r.conditionalValue(conditionalTime);
if (vl != 0) {
RouteTypeRule rtr = region.quickGetEncodingRule(vl);
String nonCondTag = rtr.getTag();
int ks;
for (ks = 0; ks < types.length; ks++) {
RouteTypeRule toReplace = region.quickGetEncodingRule(types[ks]);
if (toReplace != null && toReplace.getTag().equals(nonCondTag)) {
break;
}
}
if (ks == types.length) {
int[] ntypes = new int[types.length + 1];
System.arraycopy(types, 0, ntypes, 0, types.length);
types = ntypes;
}
types[ks] = vl;
}
}
}
if (pointTypes != null) {
for (int i = 0; i < pointTypes.length; i++) {
if (pointTypes[i] != null) {
int[] pTypes = pointTypes[i];
int pSz = pTypes.length;
if (pSz > 0) {
for (int j = 0; j < pSz; j++) {
RouteTypeRule r = region.quickGetEncodingRule(pTypes[j]);
if (r != null && r.conditional()) {
int vl = r.conditionalValue(conditionalTime);
if (vl != 0) {
RouteTypeRule rtr = region.quickGetEncodingRule(vl);
String nonCondTag = rtr.getTag();
int ks;
for (ks = 0; ks < pointTypes[i].length; ks++) {
RouteTypeRule toReplace = region.quickGetEncodingRule(pointTypes[i][j]);
if (toReplace != null && toReplace.getTag().contentEquals(nonCondTag)) {
break;
}
}
if (ks == pTypes.length) {
int[] ntypes = new int[pTypes.length + 1];
System.arraycopy(pTypes, 0, ntypes, 0, pTypes.length);
pTypes = ntypes;
}
pTypes[ks] = vl;
}
}
}
}
pointTypes[i] = pTypes;
}
}
}
}
public float getMaximumSpeed(boolean direction) {
int sz = types.length;
float maxSpeed = 0;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
float mx = r.maxSpeed();
if (mx > 0) {
if (r.isForward() != 0) {
if ((r.isForward() == 1) != direction) {
continue;
} else {
// priority over default
maxSpeed = mx;
break;
}
} else {
maxSpeed = mx;
}
}
}
return maxSpeed;
}
public static float parseSpeed(String v, float def) {
if (v.equals("none")) {
return RouteDataObject.NONE_MAX_SPEED;
} else {
int i = Algorithms.findFirstNumberEndIndex(v);
if (i > 0) {
float f = Float.parseFloat(v.substring(0, i));
f /= 3.6; // km/h -> m/s
if (v.contains("mph")) {
f *= 1.6;
}
return f;
}
}
return def;
}
public static float parseLength(String v, float def) {
float f = 0;
// 14'10" 14 - inches, 10 feet
int i = Algorithms.findFirstNumberEndIndex(v);
if (i > 0) {
f += Float.parseFloat(v.substring(0, i));
String pref = v.substring(i, v.length()).trim();
float add = 0;
for (int ik = 0; ik < pref.length(); ik++) {
if (Algorithms.isDigit(pref.charAt(ik)) || pref.charAt(ik) == '.' || pref.charAt(ik) == '-') {
int first = Algorithms.findFirstNumberEndIndex(pref.substring(ik));
if (first != -1) {
add = parseLength(pref.substring(ik), 0);
pref = pref.substring(0, ik);
}
break;
}
}
if (pref.contains("km")) {
f *= 1000;
}
if (pref.contains("\"") || pref.contains("in")) {
f *= 0.0254;
} else if (pref.contains("\'") || pref.contains("ft") || pref.contains("feet")) {
// foot to meters
f *= 0.3048;
} else if (pref.contains("cm")) {
f *= 0.01;
} else if (pref.contains("mile")) {
f *= 1609.34f;
}
return f + add;
}
return def;
}
public static float parseWeightInTon(String v, float def) {
int i = Algorithms.findFirstNumberEndIndex(v);
if (i > 0) {
float f = Float.parseFloat(v.substring(0, i));
if (v.contains("\"") || v.contains("lbs")) {
// lbs -> kg -> ton
f = (f * 0.4535f) / 1000f;
}
return f;
}
return def;
}
public boolean loop() {
return pointsX[0] == pointsX[pointsX.length - 1] && pointsY[0] == pointsY[pointsY.length - 1];
}
public boolean platform() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r.getTag().equals("railway") && r.getValue().equals("platform")) {
return true;
}
if (r.getTag().equals("public_transport") && r.getValue().equals("platform")) {
return true;
}
}
return false;
}
public boolean roundabout() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r.roundabout()) {
return true;
} else if (r.onewayDirection() != 0 && loop()) {
return true;
}
}
return false;
}
public boolean tunnel() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r.getTag().equals("tunnel") && r.getValue().equals("yes")) {
return true;
}
if (r.getTag().equals("layer") && r.getValue().equals("-1")) {
return true;
}
}
return false;
}
public boolean isExitPoint() {
if (pointTypes != null) {
int ptSz = pointTypes.length;
for (int i = 0; i < ptSz; i++) {
int[] point = pointTypes[i];
if (point != null) {
int pSz = point.length;
for (int j = 0; j < pSz; j++) {
if (region.routeEncodingRules.get(point[j]).getValue().equals("motorway_junction")) {
return true;
}
}
}
}
}
return false;
}
// public boolean isMotorWayLink() {
// int sz = types.length;
// for (int i = 0; i < sz; i++) {
// RouteTypeRule r = region.quickGetEncodingRule(types[i]);
// if (r.getTag().equals("highway") && r.getValue().equals("motorway_link")) {
// return true;
// }
// }
// return false;
// }
public String getExitName() {
if (pointNames != null && pointNameTypes != null) {
int pnSz = pointNames.length;
for (int i = 0; i < pnSz; i++) {
String[] point = pointNames[i];
if (point != null) {
int pSz = point.length;
for (int j = 0; j < pSz; j++) {
if (pointNameTypes[i][j] == region.nameTypeRule) {
return point[j];
}
}
}
}
}
return null;
}
public String getExitRef() {
if (pointNames != null && pointNameTypes != null) {
int pnSz = pointNames.length;
for (int i = 0; i < pnSz; i++) {
String[] point = pointNames[i];
if (point != null) {
int pSz = point.length;
for (int j = 0; j < pSz; j++) {
if (pointNameTypes[i][j] == region.refTypeRule) {
return point[j];
}
}
}
}
}
return null;
}
public int getOneway() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r.onewayDirection() != 0) {
return r.onewayDirection();
} else if (r.roundabout()) {
return 1;
}
}
return 0;
}
public String getRoute() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if ("route".equals(r.getTag())) {
return r.getValue();
}
}
return null;
}
public String getHighway() {
return getHighway(types, region);
}
public boolean hasPrivateAccess() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if ("motorcar".equals(r.getTag())
|| "motor_vehicle".equals(r.getTag())
|| "vehicle".equals(r.getTag())
|| "access".equals(r.getTag())) {
if (r.getValue().equals("private")) {
return true;
}
}
}
return false;
}
public String getValue(String tag) {
for (int i = 0; i < types.length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r.getTag().equals(tag)) {
return r.getValue();
}
}
if (nameIds != null) {
for (int i = 0; i < nameIds.length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(nameIds[i]);
if (r.getTag().equals(tag)) {
return names.get(nameIds[i]);
}
}
}
return null;
}
public String getValue(int pnt, String tag) {
if (pointTypes != null && pnt < pointTypes.length && pointTypes[pnt] != null) {
for (int i = 0; i < pointTypes[pnt].length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pointTypes[pnt][i]);
if (r.getTag().equals(tag)) {
return r.getValue();
}
}
}
if (pointNameTypes != null && pnt < pointNameTypes.length && pointNameTypes[pnt] != null) {
for (int i = 0; i < pointNameTypes[pnt].length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pointNameTypes[pnt][i]);
if (r.getTag().equals(tag)) {
return pointNames[pnt][i];
}
}
}
return null;
}
public static String getHighway(int[] types, RouteRegion region) {
String highway = null;
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
highway = r.highwayRoad();
if (highway != null) {
break;
}
}
return highway;
}
public int getLanes() {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
int ln = r.lanes();
if (ln > 0) {
return ln;
}
}
return -1;
}
public double directionRoute(int startPoint, boolean plus) {
// same goes to C++
// Victor : the problem to put more than 5 meters that BinaryRoutePlanner will treat
// 2 consequent Turn Right as UT and here 2 points will have same turn angle
// So it should be fix in both places
return directionRoute(startPoint, plus, 5);
}
public boolean bearingVsRouteDirection(Location loc) {
boolean direction = true;
if (loc != null && loc.hasBearing()) {
double diff = MapUtils.alignAngleDifference(directionRoute(0, true) - loc.getBearing() / 180f * Math.PI);
direction = Math.abs(diff) < Math.PI / 2f;
}
return direction;
}
public boolean isRoadDeleted() {
int[] pt = getTypes();
int sz = pt.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pt[i]);
if ("osmand_change".equals(r.getTag()) && "delete".equals(r.getValue())) {
return true;
}
}
return false;
}
public boolean isStopApplicable(boolean direction, int intId, int startPointInd, int endPointInd) {
int[] pt = getPointTypes(intId);
int sz = pt.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pt[i]);
// Evaluate direction tag if present
if (r.getTag().equals("direction")) {
String dv = r.getValue();
if ((dv.equals("forward") && direction == true) || (dv.equals("backward") && direction == false)) {
return true;
} else if ((dv.equals("forward") && direction == false) || (dv.equals("backward") && direction == true)) {
return false;
}
}
// Tagging stop=all should be ok anyway, usually tagged on intersection node itself, so not needed here
//if (r.getTag().equals("stop") && r.getValue().equals("all")) {
// return true;
//}
}
// Heuristic fallback: Distance analysis for STOP with no recognized directional tagging:
// Mask STOPs closer to the start than to the end of the routing segment if it is within 50m of start, but do not mask STOPs mapped directly on start/end (likely intersection node)
double d2Start = distance(startPointInd, intId);
double d2End = distance(intId, endPointInd);
if ((d2Start < d2End) && d2Start != 0 && d2End != 0 && d2Start < 50) {
return false;
}
// No directional info detected
return true;
}
public double distance(int startPoint, int endPoint) {
if (startPoint > endPoint) {
int k = endPoint;
endPoint = startPoint;
startPoint = k;
}
double d = 0;
for (int k = startPoint; k < endPoint && k < getPointsLength() - 1; k++) {
int x = getPoint31XTile(k);
int y = getPoint31YTile(k);
int kx = getPoint31XTile(k + 1);
int ky = getPoint31YTile(k + 1);
d += simplifyDistance(kx, ky, x, y);
}
return d;
}
// Gives route direction of EAST degrees from NORTH ]-PI, PI]
public double directionRoute(int startPoint, boolean plus, float dist) {
int x = getPoint31XTile(startPoint);
int y = getPoint31YTile(startPoint);
int nx = startPoint;
int px = x;
int py = y;
double total = 0;
do {
if (plus) {
nx++;
if (nx >= getPointsLength()) {
break;
}
} else {
nx--;
if (nx < 0) {
break;
}
}
px = getPoint31XTile(nx);
py = getPoint31YTile(nx);
// translate into meters
total += simplifyDistance(x, y, px, py);
} while (total < dist);
return -Math.atan2(x - px, y - py);
}
private double simplifyDistance(int x, int y, int px, int py) {