/
osm_query_node.cpp
1942 lines (1667 loc) · 70.4 KB
/
osm_query_node.cpp
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
/***************************************************************************
* *
* IRALab - Informatics & Robotics for Automation Laboratory *
* Universita' degli Studi Milano - Bicocca, DISCO *
* Building U14, viale Sarca 336, 20126, Milano, Italy *
* *
* Authors: Dario Limongi, Augusto Ballardini, Sergio Cattaneo *
* Maintainer: augusto.ballardini@disco.unimib.it *
* *
***************************************************************************/
#define OSMIUM_WITH_PBF_INPUT
#define OSMIUM_WITH_XML_INPUT
#include <ros/ros.h>
#include <ros/publisher.h>
#include <visualization_msgs/MarkerArray.h>
#include <ros/package.h>
#include <geometry_msgs/Point.h>
#include <tf/tf.h>
#include <osmium.hpp>
#include <osmium/input/xml.hpp>
#include <osmium/handler/coordinates_for_ways.hpp>
#include <osmium/storage/objectstore.hpp>
#include <osmium/storage/byid/sparse_table.hpp>
#include <osmium/storage/byid/mmap_file.hpp>
#include <osmium/handler/coordinates_for_ways.hpp>
#include <osmium/multipolygon/assembler.hpp>
#include <osmium/geometry/multipolygon.hpp>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <std_srvs/Empty.h>
#include <ira_open_street_map/get_distance_from_xy.h>
#include <ira_open_street_map/ecef_2_lla.h>
#include <ira_open_street_map/get_closest_way_distance_utm.h>
#include <ira_open_street_map/get_node_coordinates.h>
#include <ira_open_street_map/getDistanceFromLaneCenter.h>
#include <ira_open_street_map/getHighwayInfo.h>
#include <ira_open_street_map/latlon_2_xy.h>
#include <ira_open_street_map/lla_2_ecef.h>
#include <ira_open_street_map/snap_particle_xy.h>
#include <ira_open_street_map/way_direction.h>
#include <ira_open_street_map/xy_2_latlon.h>
#include <ira_open_street_map/oneway.h>
using namespace std;
typedef Osmium::Storage::ById::SparseTable<Osmium::OSM::Position> storage_sparsetable_t;
typedef Osmium::Storage::ById::MmapFile<Osmium::OSM::Position> storage_mmap_t;
typedef Osmium::Handler::CoordinatesForWays<storage_sparsetable_t, storage_mmap_t> cfw_handler_t;
/**
* @brief The ObjectHandler class
* With this class we handle the Osmium Library
*/
class ObjectHandler : public Osmium::Handler::Base
{
public:
typedef std::set<shared_ptr<Osmium::OSM::Node const> > nodeset;
typedef std::set<shared_ptr<Osmium::OSM::Way const> > wayset;
typedef std::set<shared_ptr<Osmium::OSM::Relation const> > relationset;
storage_sparsetable_t store_pos;
storage_mmap_t store_neg;
cfw_handler_t* handler_cfw;
nodeset m_nodes;
wayset m_ways;
relationset m_relations;
ObjectHandler() : m_nodes(), m_ways(), m_relations()
{
handler_cfw = new cfw_handler_t(store_pos, store_neg);
}
void init(Osmium::OSM::Meta& meta)
{
handler_cfw->init(meta);
}
/**
* Insert shared_ptr of Node into object store.
*/
void node(const shared_ptr<Osmium::OSM::Node const>& node)
{
handler_cfw->node(node);
m_nodes.insert(node);
}
void after_nodes()
{
handler_cfw->after_nodes();
}
/**
* Insert shared_ptr of Way into object store.
*/
void way(const shared_ptr<Osmium::OSM::Way>& way)
{
handler_cfw->way(way);
m_ways.insert(way);
}
/**
* Insert shared_ptr of Relation into object store.
*/
void relation(const shared_ptr<Osmium::OSM::Relation const>& relation)
{
m_relations.insert(relation);
}
/**
* Remove all nodes from object store.
*/
void clear_nodes()
{
m_nodes.clear();
}
/**
* Remove all ways from object store.
*/
void clear_ways()
{
m_ways.clear();
}
/**
* Remove all relations from object store.
*/
void clear_relations()
{
m_relations.clear();
}
/**
* Remove all objects from object store.
*/
void clear()
{
clear_nodes();
clear_ways();
clear_relations();
}
};
/* Structs ---------------------------------------------------------------------------*/
/// Combination of ObjectStore and CoordinateForWays handlers
ObjectHandler oh;
/// Coordinates struct
struct Coordinates
{
float latitude;
float longitude;
float altitude;
};
/// Cartesian coordinates struct
struct Xy
{
double x;
double y;
};
/// Response struct for Way direction calculator
struct Way_dir_struct
{
// Direction quaternion
float q_w;
float q_x;
float q_y;
float q_z;
// Direction yaw
float yaw_deg;
float yaw_rad;
// Create two particles with opposite direction
bool opposite_direction;
// Error flags
bool way_not_found;
bool not_a_way;
};
/* Functions ---------------------------------------------------------------------------*/
bool isLeft (Xy osmA, Xy osmB, Xy queryPoint);
bool ecef_2_lla (ira_open_street_map::ecef_2_lla::Request& req, ira_open_street_map::ecef_2_lla::Response& resp);
Coordinates ecef2lla_helper (float x, float y, float z);
bool get_closest_way_distance_utm (ira_open_street_map::get_closest_way_distance_utm::Request& req, ira_open_street_map::get_closest_way_distance_utm::Response& resp);
bool get_distance_from_xy (ira_open_street_map::get_distance_from_xy::Request& req, ira_open_street_map::get_distance_from_xy::Response& resp);
double inline get_distance_helper (double x1, double y1, double x2, double y2);
bool get_node_coordinates (ira_open_street_map::get_node_coordinates::Request& req, ira_open_street_map::get_node_coordinates::Response& resp);
ROS_DEPRECATED bool getDistanceFromLaneCenter (ira_open_street_map::getDistanceFromLaneCenter::Request& req, ira_open_street_map::getDistanceFromLaneCenter::Response& resp);
bool getHighwayInfo (ira_open_street_map::getHighwayInfo::Request& req, ira_open_street_map::getHighwayInfo::Response& resp);
bool getOneWayInfo (ira_open_street_map::oneway::Request& req, ira_open_street_map::oneway::Response& resp);
bool latlon_2_xy (ira_open_street_map::latlon_2_xy::Request& req, ira_open_street_map::latlon_2_xy::Response& resp);
Xy latlon2xy_helper (double lat, double lngd);
bool lla_2_ecef (ira_open_street_map::lla_2_ecef::Request& req, ira_open_street_map::lla_2_ecef::Response& resp);
double lla_distance (Coordinates& c1, Coordinates& c2);
geometry_msgs::Point lla2ecef_helper (double lat, double lon, double alt);
void load_waylist ();
void load_buildinglist ();
Xy snap_particle_helper (Xy& A, Xy& B, Xy& C);
bool snap_particle_xy (ira_open_street_map::snap_particle_xy::Request& req, ira_open_street_map::snap_particle_xy::Response& resp);
Way_dir_struct way_direction_helper (const boost::shared_ptr<Osmium::OSM::Way const>& way, Xy& A, Xy& B);
Way_dir_struct way_direction_helper (double way_id);
bool way_direction (ira_open_street_map::way_direction::Request& req, ira_open_street_map::way_direction::Response& resp);
bool xy_2_latlon (ira_open_street_map::xy_2_latlon::Request& req, ira_open_street_map::xy_2_latlon::Response& resp);
Coordinates xy2latlon_helper (double x, double y, double utmz, bool southern);
double xyz_distance (geometry_msgs::Point& p1, geometry_msgs::Point& p2);
// How to get UTM from Longitude:
// UTM = 1.0 + floor((lngd + 180.0) / 6.0);
//
// (italy -> UTM = 32, southern = false)
Coordinates xy2latlon_helper(double x, double y, double utmz, bool southern)
{
// WGS 84 datum
double eqRad = 6378137.0;
double flat = 298.2572236;
// constants used in calculations:
double a = eqRad; // equatorial radius in meters
double f = 1.0 / flat; // polar flattening
double b = a * (1.0 - f); // polar radius
double e = sqrt(1.0 - (pow(b, 2) / pow(a, 2))); // eccentricity
double k0 = 0.9996;
double k = 1;
double drad = M_PI / 180.0;
double esq = (1.0 - (b / a) * (b / a));
double e0sq = e * e / (1.0 - e * e);
double zcm = 3.0 + 6.0 * (utmz - 1.0) - 180.0; // Central meridian of zone
double e1 = (1 - sqrt(1 - e * e) ) / (1.0 + sqrt(1 - e * e));
double M0 = 0.0;
double M = 0.0;
if (!southern)
M = M0 + y / k0; // Arc length along standard meridian.
else
M = M0 + (y - 10000000.0) / k;
double mu = M / (a * (1.0 - esq * (1.0 / 4.0 + esq * (3.0 / 64.0 + 5.0 * esq / 256.0))));
double phi1 = mu + e1 * (3.0 / 2.0 - 27.0 * e1 * e1 / 32.0) * sin(2.0 * mu) + e1 * e1 * (21.0 / 16.0 - 55.0 * e1 * e1 / 32.0) * sin(4.0 * mu); //Footprint Latitude
phi1 = phi1 + e1 * e1 * e1 * (sin(6.0 * mu) * 151.0 / 96.0 + e1 * sin(8.0 * mu) * 1097.0 / 512.0);
double C1 = e0sq * cos(phi1) * cos(phi1);
double T1 = tan(phi1) * tan(phi1);
double N1 = a / sqrt(1.0f - pow(e * sin(phi1), 2.0f));
double R1 = N1 * (1.0f - pow(e, 2.0f)) / (1.0f - pow(e * sin(phi1), 2.0f));
double D = (x - 500000.0) / (N1 * k0);
double phi = (D * D) * (1.0 / 2.0 - D * D * (5.0 + 3.0 * T1 + 10.0 * C1 - 4.0 * C1 * C1 - 9.0 * e0sq) / 24.0);
phi = phi + pow(D, 6.0) * (61.0 + 90.0 * T1 + 298.0 * C1 + 45.0 * T1 * T1 - 252.0 * e0sq - 3.0 * C1 * C1) / 720.0;
phi = phi1 - (N1 * tan(phi1) / R1) * phi;
double lat = floor(1000000.0 * phi / drad) / 1000000.0;
double lng = D * (1.0 + D * D * ((-1.0 - 2.0 * T1 - C1) / 6.0 + D * D * (5.0 - 2.0 * C1 + 28.0 * T1 - 3.0 * C1 * C1 + 8.0 * e0sq + 24.0 * T1 * T1) / 120.0)) / cos(phi1);
double lngd = zcm + lng / drad;
Coordinates coords = { lat, lngd };
return coords;
}
// Conversion between geographic and UTM coordinates
// Adapted from:
// http://www.uwgb.edu/dutchs/UsefulData/ConvertUTMNoOZ.HTM
Xy latlon2xy_helper(double lat, double lngd)
{
// WGS 84 datum
double eqRad = 6378137.0;
double flat = 298.2572236;
// constants used in calculations:
double a = eqRad; // equatorial radius in meters
double f = 1.0 / flat; // polar flattening
double b = a * (1.0 - f); // polar radius
double e = sqrt(1.0 - (pow(b, 2) / pow(a, 2))); // eccentricity
double k0 = 0.9996;
double drad = M_PI / 180.0;
double phi = lat * drad; // convert latitude to radians
double utmz = 1.0 + floor((lngd + 180.0) / 6.0); // longitude to utm zone
double zcm = 3.0 + 6.0 * (utmz - 1.0) - 180.0; // central meridian of a zone
double esq = (1.0 - (b / a) * (b / a));
double e0sq = e * e / (1.0 - e * e);
double M = 0.0;
double M0 = 0.0;
double N = a / sqrt(1.0 - pow(e * sin(phi), 2));
double T = pow(tan(phi), 2);
double C = e0sq * pow(cos(phi), 2);
double A = (lngd - zcm) * drad * cos(phi);
// calculate M (USGS style)
M = phi * (1.0 - esq * (1.0 / 4.0 + esq * (3.0 / 64.0 + 5.0 * esq / 256.0)));
M = M - sin(2.0 * phi) * (esq * (3.0 / 8.0 + esq * (3.0 / 32.0 + 45.0 * esq / 1024.0)));
M = M + sin(4.0 * phi) * (esq * esq * (15.0 / 256.0 + esq * 45.0 / 1024.0));
M = M - sin(6.0 * phi) * (esq * esq * esq * (35.0 / 3072.0));
M = M * a; // Arc length along standard meridian
// now we are ready to calculate the UTM values...
// first the easting (relative to CM)
double x = k0 * N * A * (1.0 + A * A * ((1.0 - T + C) / 6.0 + A * A * (5.0 - 18.0 * T + T * T + 72.0 * C - 58.0 * e0sq) / 120.0));
x = x + 500000.0; // standard easting
// now the northing (from the equator)
double y = k0 * (M - M0 + N * tan(phi) * (A * A * (1.0 / 2.0 + A * A * ((5.0 - T + 9.0 * C + 4.0 * C * C) / 24.0 + A * A * (61.0 - 58.0 * T + T * T + 600.0 * C - 330.0 * e0sq) / 720.0))));
if (y < 0)
{
y = 10000000.0 + y; // add in false northing if south of the equator
}
//double easting = round(10.0 * x) / 10.0;
//double northing = round(10.0 * y) / 10.0;
double easting = x;
double northing = y;
Xy coords;
coords.x = easting;
coords.y = northing;
return coords;
}
/**
* LLA2ECEF - convert latitude, longitude, and altitude to
* earth-centered, earth-fixed (ECEF) cartesian
*
* USAGE:
* [x,y,z] = lla2ecef(lat,lon,alt)
*
* x = ECEF X-coordinate (m)
* y = ECEF Y-coordinate (m)
* z = ECEF Z-coordinate (m)
* lat = geodetic latitude (radians)
* lon = longitude (radians)
* alt = height above WGS84 ellipsoid (m)
*
* Notes: This function assumes the WGS84 model.
* Latitude is customary geodetic (not geocentric).
*
* Source: "Department of Defense World Geodetic System 1984"
* Page 4-4
* National Imagery and Mapping Agency
* Last updated June, 2004
* NIMA TR8350.2
*
* Michael Kleder, July 2005
* (C++ porting by Dario Limongi)
*/
geometry_msgs::Point lla2ecef_helper(double lat, double lon, double alt)
{
geometry_msgs::Point point;
// WGS84 ellipsoid constants:
double a = 6378137; // (m) at the equator
double e = 0.081819190842622; // eccentricity
// intermediate calculation
// (prime vertical radius of curvature)
double N = a / sqrt(1 - (e * e) * (sin(lat) * sin(lat)));
// results:
point.x = (N + alt) * cos(lat) * cos(lon);
point.y = (N + alt) * cos(lat) * sin(lon);
point.z = ((1 - (e * e)) * N + alt) * sin(lat);
return point;
}
/**
* ECEF2LLA - convert earth-centered earth-fixed (ECEF)
* cartesian coordinates to latitude, longitude,
* and altitude
*
* USAGE:
* [lat,lon,alt] = ecef2lla(x,y,z)
*
* lat = geodetic latitude (radians)
* lon = longitude (radians)
* alt = height above WGS84 ellipsoid (m)
* x = ECEF X-coordinate (m)
* y = ECEF Y-coordinate (m)
* z = ECEF Z-coordinate (m)
*
* Notes: (1) This function assumes the WGS84 model.
* (2) Latitude is customary geodetic (not geocentric).
* (3) Inputs may be scalars, vectors, or matrices of the same
* size and shape. Outputs will have that same size and shape.
* (4) Tested but no warranty; use at your own risk.
* (5) Michael Kleder, April 2006
* (C++ porting by Dario Limongi)
*/
Coordinates ecef2lla_helper(float x, float y, float z)
{
Coordinates coords;
// WGS84 ellipsoid constants:
float a = 6378137; // (m) at the equator
float e = 0.081819190842622; // eccentricity
// calculations:
float b = sqrt( (a * a) * (1 - e * e ));
float ep = sqrt( (a * a - b * b ) / (b * b));
float p = sqrt( x * x + y * y );
float th = atan2(a * z, b * p);
float lon = atan2(y, x);
float lat = atan2( (z + ep * ep * b * sin(th) * sin(th) * sin(th)), (p - e * e * a * cos(th) * cos(th) * cos(th)) );
float N = a / sqrt( 1 - e * e * sin(lat) * sin(lat));
float alt = p / cos(lat) - N;
// return lon in range [0,2*pi)
lon = lon - (M_PI * 2) * floor( lon / (M_PI * 2) );
coords.latitude = lat;
coords.longitude = lon;
coords.altitude = alt;
return coords;
}
/**
* @brief get_distance_helper
* @param x1 1st point, x coordinate
* @param y1 1st point, y coordinate
* @param x2 2nd point, x coordinate
* @param y2 2nd point, y coordinate
* @return value of the distance between the two points
*/
double inline get_distance_helper(double x1, double y1, double x2, double y2)
{
double dx = x2 - x1;
double dy = y2 - y1;
return sqrt(fabs(dx * dx + dy * dy));
}
/**
* @brief snapxy2way_outbounds_helper
* @param A 1st node of the way
* @param B 2nd node of the way
* @param C hypothesis position
* @return the position of the orthogonal projection of C in the segment defined by A and B
*
* Snaps the particle position to the way segment defined by A and B using
* the orthogonal projection
*/
Xy snap_particle_helper(Xy& A, Xy& B, Xy& C)
{
// Snapped point to segment A,B
Xy D;
// vector from A to B
Xy AB = {B.x - A.x, B.y - A.y };
// squared distance from A to B
double AB_squared = AB.x * AB.x + AB.y * AB.y;
if (AB_squared == 0)
{
// A and B are the same point
D.x = A.x;
D.y = A.y;
}
else
{
// vector from A to p
Xy Ap = {C.x - A.x, C.y - A.y};
// Consider the line extending the segment, parameterized as A + t (B - A)
// We find projection of point p onto the line.
// It falls where t = [(p-A) . (B-A)] / |B-A|^2
double t = (Ap.x * AB.x + Ap.y * AB.y) / AB_squared;
if (t < 0.0)
{
// "Before" A on the line, just return A
// q = A;
D.x = A.x;
D.y = A.y;
}
else if (t > 1.0)
{
// "After" B on the line, just return B
// q = B;
D.x = B.x;
D.y = B.y;
}
else
{
// projection lines "inbetween" A and B on the line
D.x = t * AB.x + A.x;
D.y = t * AB.y + A.y;
}
}
return D;
}
/**
* @brief way_direction_helper
* @param way this is closest way to the current hypothesis
* @param A min_dist_node1, following the order of the elements in the way, this is the 1st element
* @param B min_dist_node2, following the order of the elements in the way, this is the 2nd element
* @return
*
* Given a way (and two XY points) return, as a service response, the direction of the hypotheses,
* based on the two points that creates the segment (A and B points). It also
* considers if the way is oneway or not.
*
*/
Way_dir_struct way_direction_helper(const boost::shared_ptr<Osmium::OSM::Way const>& way, Xy& A, Xy& B)
{
// Init response
Way_dir_struct response;
response.not_a_way = false;
response.way_not_found = false;
response.q_w = 0;
response.q_x = 0;
response.q_y = 0;
response.q_z = 0;
response.yaw_deg = 0;
response.yaw_rad = 0;
response.opposite_direction = 0;
double theta = 0; // Angle between A an B;
bool opposite_particles = false; // This tells if we should create 2 particles with opposite directions
// Find tags
// const char* lanes = way->tags().get_value_by_key("lanes");
const char* one_way = way->tags().get_value_by_key("oneway");
// Get angle between A and B (radians)
theta = atan2(B.y - A.y, B.x - A.x);
// ONEWAY TAG FOUND:
if ( one_way )
{
if (strcmp(one_way, "yes") == 0 || strcmp(one_way, "true") == 0 || strcmp(one_way, "1") == 0)
{
opposite_particles = false;
}
else if (strcmp(one_way, "no") == 0 || strcmp(one_way, "false") == 0 || strcmp(one_way, "0") == 0)
{
// If street has multiple lanes, then we should create 2 particles with opposite direction
opposite_particles = true;
}
else if (strcmp(one_way, "-1") == 0 || strcmp(one_way, "reverse") == 0)
{
// Get angle between B and A (radians)
theta = atan2(A.y - B.y, A.x - B.x);
opposite_particles = false;
}
}
// NO TAGS FOUND!
else
{
// Create 2 particles, since we don't know anything about this way
opposite_particles = true;
}
// Create quaternion from angle and put it inside response
// Normalize angle if it is negative:
if (theta < 0)
{
theta += 2 * M_PI;
}
tf::Quaternion q = tf::createQuaternionFromYaw(theta);
// Set response quaternion value
response.q_w = q.getW();
response.q_x = q.getX();
response.q_y = q.getY();
response.q_z = q.getZ();
// Set response yaw value
response.yaw_deg = theta * 180.0f / M_PI;
response.yaw_rad = theta;
// Set opposite_direction flag
response.opposite_direction = opposite_particles;
ROS_DEBUG_STREAM(" Direction calculated, Way ID: " << boost::lexical_cast<std::string>(way->id()));
ROS_DEBUG_STREAM(" Theta: " << theta);
ROS_DEBUG_STREAM(" Degrees: " << response.yaw_deg);
ROS_DEBUG_STREAM(" Quaternion: " << q.getX() << " " << q.getY() << " " << q.getZ() << " " << q.getW() << "\n");
return response;
}
/**
* @brief way_direction_helper
* @param way_id
* @return
*
* Given a way id, find that way and create a RESPONSE with it,
* calculating the orientation using the FIRST two nodes.
*/
Way_dir_struct way_direction_helper(double way_id)
{
// Init response
Way_dir_struct response;
response.not_a_way = false;
response.way_not_found = false;
response.q_w = 0;
response.q_x = 0;
response.q_y = 0;
response.q_z = 0;
response.yaw_deg = 0;
response.yaw_rad = 0;
double theta = 0; // Angle between A an B;
bool opposite_particles = false; // This tells if we should create 2 particles with opposite directions
bool found = false; // This flag tells if we found they Way inside our map or not
// Cycle through every stored way
for (std::set<shared_ptr<Osmium::OSM::Way const> >::iterator way_itr = oh.m_ways.begin(); way_itr != oh.m_ways.end(); way_itr++)
{
if ( (*way_itr)->id() == way_id )
{
ROS_DEBUG_STREAM("SERVICE way_direction:");
// way id found, check if it's a street
const char* highway = (*way_itr)->tags().get_value_by_key("highway");
if (highway)
{
// Get way node list
Osmium::OSM::WayNodeList waylist = (*way_itr)->nodes();
// const char* lanes = (*way_itr)->tags().get_value_by_key("lanes");
const char* one_way = (*way_itr)->tags().get_value_by_key("oneway");
// Get first and second node UTM coords
Xy A = latlon2xy_helper(waylist.begin()->position().lat(), waylist.begin()->position().lon());
Xy B = latlon2xy_helper((++waylist.begin())->position().lat(), (++waylist.begin())->position().lon());
theta = atan2(B.y - A.y, B.x - A.x);
// ONEWAY TAG FOUND:
if ( one_way )
{
if (strcmp(one_way, "yes") == 0 || strcmp(one_way, "true") == 0 || strcmp(one_way, "1") == 0)
{
opposite_particles = false;
}
else if (strcmp(one_way, "no") == 0 || strcmp(one_way, "false") == 0 || strcmp(one_way, "0") == 0)
{
// If street has multiple lanes, then we should create 2 particles with opposite direction
opposite_particles = true;
}
else if (strcmp(one_way, "-1") == 0 || strcmp(one_way, "reverse") == 0)
{
// Get angle between B and A (radians)
theta = atan2(A.y - B.y, A.x - B.x);
opposite_particles = false;
}
}
// NO TAGS FOUND!
else
{
// Create 2 particles, since we don't know anything about this way
opposite_particles = true;
}
}
else
{
ROS_DEBUG_STREAM(" Given ID is not a Way");
response.not_a_way = true;
return response;
}
// Way was found, exit FOR loop
found = true;
break;
}
}
if (found)
{
// Create quaternion from angle and put it inside response
// Normalize angle if it is negative:
if (theta < 0)
{
theta += 2 * M_PI;
}
tf::Quaternion q = tf::createQuaternionFromYaw(theta);
// Set response quaternion value
response.q_w = q.getW();
response.q_x = q.getX();
response.q_y = q.getY();
response.q_z = q.getZ();
// Set response yaw value
response.yaw_deg = theta * 180.0f / M_PI;
response.yaw_rad = theta;
// Set opposite_direction flag
response.opposite_direction = opposite_particles;
// ROS_DEBUG_STREAM(" Direction calculated, Way ID: " << boost::lexical_cast<std::string>(way_id));
// ROS_DEBUG_STREAM(" Theta: " << theta);
// ROS_DEBUG_STREAM(" Degrees: " << response.yaw_deg);
// ROS_DEBUG_STREAM(" Quaternion: " << q.getX() << " " << q.getY() << " " << q.getZ()<< " " << q.getW());
// ROS_DEBUG_STREAM(" Opposite_particles: " << opposite_particles);
return response;
}
else
{
// Given ID was not found inside our Map
ROS_DEBUG_STREAM(" Given ID was not found inside our Map");
response.way_not_found = true;
return response;
}
}
/**
* @brief oneWay returns true if the way_id has the tag OneWay, false otherwise
* @param way_id, inside the request(req.way_id)
* @return boolean: true if oneway tag found, false otherwise
*
* Returns true if oneway tag found. If returns true, check the response;
*
*/
bool getOneWayInfo(ira_open_street_map::oneway::Request& req, ira_open_street_map::oneway::Response& resp)
{
resp.oneway = false; //initialize response to false, default value if wayid is not found
// Cycle through every stored way
for (std::set<shared_ptr<Osmium::OSM::Way const> >::iterator way_itr = oh.m_ways.begin(); way_itr != oh.m_ways.end(); way_itr++)
{
if ( (*way_itr)->id() == req.way_id )
{
ROS_DEBUG_STREAM("SERVICE oneWay");
// way id found, check if it's a street
const char* highway = (*way_itr)->tags().get_value_by_key("highway");
if (highway)
{
const char* one_way = (*way_itr)->tags().get_value_by_key("oneway");
// ONEWAY TAG FOUND:
if ( one_way )
{
if (strcmp(one_way, "yes") == 0 || strcmp(one_way, "true") == 0 || strcmp(one_way, "1") == 0)
{
resp.oneway = true;
}
else if (strcmp(one_way, "no") == 0 || strcmp(one_way, "false") == 0 || strcmp(one_way, "0") == 0)
{
resp.oneway = false;
}
else if (strcmp(one_way, "-1") == 0 || strcmp(one_way, "reverse") == 0)
{
resp.oneway = true;
}
return true;
}
// NO ONEWAY TAG FOUND!
else
{
// in this case, the oneway tag is not present. return false
resp.oneway = false;
return false;
}
}
else
{
ROS_DEBUG_STREAM("Given ID is not a Way!");
resp.oneway = false;
return false;
}
}
}
return false;
}
double xyz_distance(geometry_msgs::Point& p1, geometry_msgs::Point& p2)
{
// calculate difference between points
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
double dz = p2.z - p1.z;
// If the points are so close that the curvature of the Earth doesn't
// matter, then we can simply calculate the stright-line distance:
return sqrt(dx * dx + dy * dy + dz * dz);
}
double lla_distance(Coordinates& c1, Coordinates& c2)
{
// convert LLA to ECEF
geometry_msgs::Point p1 = lla2ecef_helper(c1.latitude, c1.longitude, c1.altitude);
geometry_msgs::Point p2 = lla2ecef_helper(c2.latitude, c2.longitude, c2.altitude);
// calculate difference between points
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
double dz = p2.z - p1.z;
// If the points are so close that the curvature of the Earth doesn't
// matter, then we can simply calculate the stright-line distance:
return sqrt(dx * dx + dy * dy + dz * dz);
}
/**
* @brief snap_particle_xy XY in global map frame! (Northing/Easting)
* @param req
* @param resp
* @return
*/
bool snap_particle_xy(ira_open_street_map::snap_particle_xy::Request& req, ira_open_street_map::snap_particle_xy::Response& resp)
{
// (1) Calculate distance between particle and every map Way (keep only ways that are below 'max_distance_radius')
vector<shared_ptr<Osmium::OSM::Way const> > way_vector; /// keep OSM way stored
for (std::set<shared_ptr<Osmium::OSM::Way const> >::iterator way_itr = oh.m_ways.begin(); way_itr != oh.m_ways.end(); way_itr++)
{
// Use only ways with Key:Highway
const char* highway = (*way_itr)->tags().get_value_by_key("highway");
if (!highway)
continue;
// Get way-node list
Osmium::OSM::WayNodeList waylist = (*way_itr)->nodes();
for (Osmium::OSM::WayNodeList::iterator node_list_itr = waylist.begin(); node_list_itr != waylist.end(); node_list_itr++ )
{
// Get way-node coordinates
double lat = node_list_itr->position().lat();
double lon = node_list_itr->position().lon();
Xy way_node_xy = latlon2xy_helper(lat, lon);
// Calculate distance from particle
double distance = get_distance_helper(req.x, req.y, way_node_xy.x, way_node_xy.y);
// Check if it's below max_distance_radius
if (distance <= req.max_distance_radius)
{
// Store way ID+
way_vector.push_back(*way_itr);
break;
}
}
}
if (way_vector.size() == 0)
{
ROS_ERROR_STREAM(" No map nodes found next to particle. Distance radius: " << req.max_distance_radius << " m");
return false;
}
// // Print kept Ways ID
// ROS_DEBUG_STREAM("PRINT Way ID vector");
// for(vector<shared_ptr<Osmium::OSM::Way const> >::iterator way_itr = way_vector.begin(); way_itr != way_vector.end(); way_itr++){
// ROS_DEBUG_STREAM(" Way ID: " << boost::lexical_cast<std::string>((*way_itr)->id()) );
// }
// (2) For every kept way, calculate shortest distance between particle and every way's segments (a way segment is defined by 2 nodes)
// init values
double min_distance = 999999999;
shared_ptr<Osmium::OSM::Way const> min_distance_way;
Xy snapped_xy;
Xy particle = {req.x, req.y};
Xy min_dist_node1;
Xy min_dist_node2;
int64 way_id = 0;
// start cycle
for (vector<shared_ptr<Osmium::OSM::Way const> >::iterator way_itr = way_vector.begin(); way_itr != way_vector.end(); way_itr++)
{
// Use only ways with Key:Highway
// WARNING: this should be useless since we added only ways with Key:highway in the phase
const char* highway = (*way_itr)->tags().get_value_by_key("highway");
if (!highway)
continue;
Osmium::OSM::WayNodeList way_n_list = (*way_itr)->nodes();
for (Osmium::OSM::WayNodeList::iterator node_list_itr = way_n_list.begin(); node_list_itr != --way_n_list.end(); node_list_itr++ )
{
// extract 1st node coordinates
double lat = node_list_itr->position().lat();
double lon = node_list_itr->position().lon();
Xy node_1 = latlon2xy_helper(lat, lon);
// extract 2nd node coordinates
Osmium::OSM::WayNodeList::iterator it2 = node_list_itr;
it2++;
double lat2 = it2->position().lat();
double lon2 = it2->position().lon();
Xy node_2 = latlon2xy_helper(lat2, lon2);
// snap particle (on the way=highway segment defined by node_1 and node_2)
Xy snap = snap_particle_helper(node_1, node_2, particle);
// calculate distance from particle to snapped way segment
double dist = get_distance_helper(snap.x, snap.y, particle.x, particle.y);
if (dist < min_distance)
{
// update min distance
min_distance = dist;
min_distance_way = *way_itr;
snapped_xy = snap;
min_dist_node1 = node_1;
min_dist_node2 = node_2;
way_id = (*way_itr)->id();
}
// break if it's last way segment
if ( it2 == way_n_list.end() )
{
break;
}
}
}
// Get snapped particle direction using the min_distance segment of the nearest way=highway element
Way_dir_struct dir = way_direction_helper(min_distance_way, min_dist_node1, min_dist_node2);
// Set response values
resp.snapped_x = snapped_xy.x;
resp.snapped_y = snapped_xy.y;
resp.way_dir_rad = dir.yaw_rad;
resp.way_dir_degrees = dir.yaw_deg;
resp.way_dir_quat_w = dir.q_w;
resp.way_dir_quat_x = dir.q_x;
resp.way_dir_quat_y = dir.q_y;
resp.way_dir_quat_z = dir.q_z;
resp.way_dir_opposite_particles = dir.opposite_direction;
resp.distance_from_way = min_distance;
resp.way_id = way_id;
resp.isLeft = isLeft(min_dist_node1, min_dist_node2, particle); // #538 checks if we're on the left of right w.r.t the WAY
// #538 std::cout<< "LEFT/RIGHT: " << isLeft(min_dist_node1, min_dist_node2, particle) << std::endl;
return true;
}
/**
* @brief isLeft This routine checks on which side of a segment created by two points
* a queryPoint is. This is related to task #538
* @param osmA the first point of the orientated segment, usually northing/easting
* @param osmB the second point of the orientated segment, usually northing/easting
* @param queryPoint the query point, position of the particle, usually northing/easting
* @return true if the queryPoint is on the LEFT, false if it is on the RIGHT.
*/
inline bool isLeft(Xy osmA, Xy osmB, Xy queryPoint)
{
return ((osmB.x - osmA.x) * (queryPoint.y - osmA.y) - (osmB.y - osmA.y) * (queryPoint.x - osmA.x)) > 0;
}
/**
* @brief way_direction
* 1. Is there Oneway tag?
* YES:
* oneway = yes : calculate way direction with atan2 using Way node list sorting
* oneway = no : calculate way direction with atan2 using Way node list sorting and place 2 particles with opposite directions
* oneway = -1 : calculate way direction with atan2 using Way node list sorting and reverse the direction
* NO: Go to (2)
*
* 2. Is there Lanes tag?
* YES:
* lanes = 1 : calculate way direction with atan2 using Way node list sorting
* lanes > 1 : calculate way direction with atan2 using Way node list sorting and place 2 particles with opposite directions
* NO: Go to (3)
*
* 3. calculate way direction with atan2 using Way node list sorting and place 2 particles with opposite directions
*
* @param req
* @param resp
* @return
*/
bool way_direction(ira_open_street_map::way_direction::Request& req, ira_open_street_map::way_direction::Response& resp)