48using google::protobuf::RepeatedPtrField;
52template <
typename MapElementInfoConstPtr>
53void ExtractIds(
const std::vector<MapElementInfoConstPtr> &items,
54 RepeatedPtrField<std::string> *ids) {
55 ids->Reserve(
static_cast<unsigned int>(items.size()));
56 for (
const auto &item : items) {
57 ids->Add()->assign(item->id().id());
61 std::sort(ids->begin(), ids->end());
64void ExtractRoadAndLaneIds(
const std::vector<LaneInfoConstPtr> &lanes,
65 RepeatedPtrField<std::string> *lane_ids,
66 RepeatedPtrField<std::string> *road_ids) {
67 lane_ids->Reserve(
static_cast<unsigned int>(lanes.size()));
68 road_ids->Reserve(
static_cast<unsigned int>(lanes.size()));
70 for (
const auto &lane : lanes) {
71 lane_ids->Add()->assign(lane->id().id());
72 if (!lane->road_id().id().empty()) {
73 road_ids->Add()->assign(lane->road_id().id());
78 std::sort(lane_ids->begin(), lane_ids->end());
79 std::sort(road_ids->begin(), road_ids->end());
84const char MapService::kMetaFileName[] =
"/metaInfo.json";
91 boost::unique_lock<boost::shared_mutex> writer_lock(mutex_);
102void MapService::UpdateOffsets() {
105 std::ifstream ifs(FLAGS_map_dir + kMetaFileName);
106 if (!ifs.is_open()) {
107 AINFO <<
"Failed to open map meta file: " << kMetaFileName;
113 for (
auto it = json.begin(); it != json.end(); ++it) {
114 auto val = it.value();
115 if (val.is_object()) {
116 auto x_offset = val.find(
"xoffset");
117 if (x_offset == val.end()) {
118 AWARN <<
"Cannot find x_offset for this map " << it.key();
122 if (!x_offset->is_number()) {
123 AWARN <<
"Expect x_offset with type 'number', but was "
124 << x_offset->type_name();
127 x_offset_ = x_offset.value();
129 auto y_offset = val.find(
"yoffset");
130 if (y_offset == val.end()) {
131 AWARN <<
"Cannot find y_offset for this map " << it.key();
135 if (!y_offset->is_number()) {
136 AWARN <<
"Expect y_offset with type 'number', but was "
137 << y_offset->type_name();
140 y_offset_ = y_offset.value();
144 AINFO <<
"Updated with map: x_offset " << x_offset_ <<
", y_offset "
148const hdmap::HDMap *MapService::HDMap()
const {
152const hdmap::HDMap *MapService::SimMap()
const {
156bool MapService::MapReady()
const {
return HDMap() && SimMap(); }
164 return (ids.ByteSizeLong() != 0);
172 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
174 std::vector<LaneInfoConstPtr> lanes;
175 if (SimMap()->GetLanes(point, radius, &lanes) != 0) {
176 AERROR <<
"Fail to get lanes from sim_map.";
178 ExtractRoadAndLaneIds(lanes, ids->mutable_lane(), ids->mutable_road());
180 std::vector<ClearAreaInfoConstPtr> clear_areas;
181 if (SimMap()->GetClearAreas(point, radius, &clear_areas) != 0) {
182 AERROR <<
"Fail to get clear areas from sim_map.";
184 ExtractIds(clear_areas, ids->mutable_clear_area());
186 std::vector<CrosswalkInfoConstPtr> crosswalks;
187 if (SimMap()->GetCrosswalks(point, radius, &crosswalks) != 0) {
188 AERROR <<
"Fail to get crosswalks from sim_map.";
190 ExtractIds(crosswalks, ids->mutable_crosswalk());
192 std::vector<JunctionInfoConstPtr> junctions;
193 if (SimMap()->GetJunctions(point, radius, &junctions) != 0) {
194 AERROR <<
"Fail to get junctions from sim_map.";
196 ExtractIds(junctions, ids->mutable_junction());
198 std::vector<PNCJunctionInfoConstPtr> pnc_junctions;
199 if (SimMap()->GetPNCJunctions(point, radius, &pnc_junctions) != 0) {
200 AERROR <<
"Fail to get pnc junctions from sim_map.";
202 ExtractIds(pnc_junctions, ids->mutable_pnc_junction());
204 std::vector<ParkingSpaceInfoConstPtr> parking_spaces;
205 if (SimMap()->GetParkingSpaces(point, radius, &parking_spaces) != 0) {
206 AERROR <<
"Fail to get parking space from sim_map.";
208 ExtractIds(parking_spaces, ids->mutable_parking_space());
210 std::vector<SpeedBumpInfoConstPtr> speed_bumps;
211 if (SimMap()->GetSpeedBumps(point, radius, &speed_bumps) != 0) {
212 AERROR <<
"Fail to get speed bump from sim_map.";
214 ExtractIds(speed_bumps, ids->mutable_speed_bump());
216 std::vector<SignalInfoConstPtr> signals;
217 if (SimMap()->GetSignals(point, radius, &signals) != 0) {
218 AERROR <<
"Failed to get signals from sim_map.";
221 ExtractIds(signals, ids->mutable_signal());
223 std::vector<StopSignInfoConstPtr> stop_signs;
224 if (SimMap()->GetStopSigns(point, radius, &stop_signs) != 0) {
225 AERROR <<
"Failed to get stop signs from sim_map.";
227 ExtractIds(stop_signs, ids->mutable_stop_sign());
229 std::vector<YieldSignInfoConstPtr> yield_signs;
230 if (SimMap()->GetYieldSigns(point, radius, &yield_signs) != 0) {
231 AERROR <<
"Failed to get yield signs from sim_map.";
233 ExtractIds(yield_signs, ids->mutable_yield());
237 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
245 for (
const auto &
id : ids.
lane()) {
249 auto lane = element->lane();
250 lane.clear_left_sample();
251 lane.clear_right_sample();
252 lane.clear_left_road_sample();
253 lane.clear_right_road_sample();
254 *result.add_lane() = lane;
262 *result.add_clear_area() = element->
clear_area();
270 *result.add_crosswalk() = element->
crosswalk();
274 for (
const auto &
id : ids.
junction()) {
278 *result.add_junction() = element->
junction();
282 for (
const auto &
id : ids.
signal()) {
286 *result.add_signal() = element->
signal();
294 *result.add_stop_sign() = element->
stop_sign();
298 for (
const auto &
id : ids.
yield()) {
302 *result.add_yield() = element->yield_sign();
306 for (
const auto &
id : ids.
road()) {
310 *result.add_road() = element->
road();
326 *result.add_speed_bump() = element->
speed_bump();
338 if (SimMap()->GetMapHeader(&map_header)) {
339 result.mutable_header()->CopyFrom(map_header);
345bool MapService::GetNearestLaneWithDistance(
346 const double x,
const double y,
348 double *nearest_l)
const {
349 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
354 static constexpr double kSearchRadius = 3.0;
356 HDMap()->GetNearestLaneWithDistance(point, kSearchRadius, nearest_lane,
357 nearest_s, nearest_l) < 0) {
358 AERROR <<
"Failed to get nearest lane!";
364bool MapService::GetNearestLane(
const double x,
const double y,
365 LaneInfoConstPtr *nearest_lane,
366 double *nearest_s,
double *nearest_l)
const {
367 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
373 HDMap()->GetNearestLane(point, nearest_lane, nearest_s, nearest_l) < 0) {
374 AERROR <<
"Failed to get nearest lane!";
380bool MapService::GetNearestLaneWithHeading(
const double x,
const double y,
381 LaneInfoConstPtr *nearest_lane,
382 double *nearest_s,
double *nearest_l,
383 const double heading)
const {
384 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
389 static constexpr double kSearchRadius = 3.0;
390 static constexpr double kMaxHeadingDiff = 1.0;
391 if (!MapReady() || HDMap()->GetNearestLaneWithHeading(
392 point, kSearchRadius, heading, kMaxHeadingDiff,
393 nearest_lane, nearest_s, nearest_l) < 0) {
394 AERROR <<
"Failed to get nearest lane with heading.";
401 std::vector<Path> *paths)
const {
402 if (!CreatePathsFromRouting(routing, paths)) {
403 AERROR <<
"Unable to get paths from routing!";
410 double *theta,
double *s)
const {
412 LaneInfoConstPtr nearest_lane;
413 if (!GetNearestLane(x, y, &nearest_lane, s, &l)) {
417 *theta = nearest_lane->Heading(*s);
424 LaneInfoConstPtr lane;
425 if (!GetNearestLane(x, y, &lane, &s, &l)) {
433 laneWayPoint->set_id(lane->id().id());
434 laneWayPoint->set_s(s);
435 auto *pose = laneWayPoint->mutable_pose();
443 const double x,
const double y,
const double heading,
446 LaneInfoConstPtr lane;
447 if (!GetNearestLaneWithHeading(x, y, &lane, &s, &l, heading)) {
455 laneWayPoint->set_id(lane->id().id());
456 laneWayPoint->set_s(s);
457 auto *pose = laneWayPoint->mutable_pose();
465 const double x,
const double y,
const std::string
id,
481 if (!lane->GetProjection({point.x(), point.y()}, &s, &l)) {
487 if (s > lane->lane().length()) {
488 s = lane->lane().length();
491 laneWayPoint->set_id(
id);
492 laneWayPoint->set_s(s);
493 auto *pose = laneWayPoint->mutable_pose();
502 LaneInfoConstPtr lane;
503 if (!GetNearestLaneWithDistance(x, y, &lane, &s, &l)) {
516 const double heading)
const {
518 LaneInfoConstPtr lane;
519 if (!GetNearestLaneWithHeading(x, y, &lane, &s, &l, heading)) {
534 <<
"Failed to construct LaneWayPoint for RoutingRequest: Expected lane "
535 << lane->id().id() <<
" to be CITY_DRIVING, but was "
536 << apollo::hdmap::Lane::LaneType_Name(lane->lane().type());
545 LaneInfoConstPtr lane;
546 if (!GetNearestLane(0.0, 0.0, &lane, &s, &l)) {
550 *start_point = lane->GetSmoothPoint(0.0);
555 std::vector<Path> *paths)
const {
556 if (routing.
road().empty()) {
560 for (
const auto &road : routing.road()) {
561 for (
const auto &passage_region : road.passage()) {
563 if (!AddPathFromPassageRegion(passage_region, paths)) {
571bool MapService::AddPathFromPassageRegion(
572 const routing::Passage &passage_region, std::vector<Path> *paths)
const {
576 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
578 std::vector<MapPathPoint> path_points;
579 for (
const auto &segment : passage_region.segment()) {
582 AERROR <<
"Failed to find lane: " << segment.id();
585 if (segment.start_s() >= segment.end_s()) {
590 path_points.insert(path_points.end(), points.begin(), points.end());
593 if (path_points.size() < 2) {
596 paths->emplace_back(Path(std::move(path_points)));
602 static std::hash<std::string> hash_function;
603 return hash_function(ids.DebugString());
607 auto *hdmap = HDMap();
608 CHECK(hdmap) <<
"Failed to get hdmap";
612 LaneInfoConstPtr lane_ptr = hdmap->GetLaneById(
id);
613 if (lane_ptr !=
nullptr) {
614 return lane_ptr->Heading(s);
620 const double y)
const {
624 static constexpr double kSearchRadius = 3.0;
625 std::vector<ParkingSpaceInfoConstPtr> parking_spaces;
628 if (HDMap()->GetParkingSpaces(point, kSearchRadius, &parking_spaces) != 0) {
629 AERROR <<
"Fail to get parking space from sim_map.";
634 for (
const auto &parking_sapce : parking_spaces) {
635 if (!parking_sapce->polygon().is_convex()) {
636 AERROR <<
"The parking space information is incorrect and is not a "
641 if (parking_sapce->polygon().IsPointIn(checked_point)) {
642 return parking_sapce->id().id();
Implements a class of 2-dimensional vectors.
bool CheckRoutingPoint(const double x, const double y) const
bool CheckRoutingPointWithHeading(const double x, const double y, const double heading) const
bool PointIsValid(const double x, const double y) const
double GetLaneHeading(const std::string &id_str, double s)
void CollectMapElementIds(const apollo::common::PointENU &point, double raidus, MapElementIds *ids) const
hdmap::Map RetrieveMapElements(const MapElementIds &ids) const
bool ReloadMap(bool force_reload)
MapService(bool use_sim_map=true)
bool GetStartPoint(apollo::common::PointENU *start_point) const
bool ConstructLaneWayPointWithLaneId(const double x, const double y, const std::string id, routing::LaneWaypoint *laneWayPoint) const
bool ConstructLaneWayPointWithHeading(const double x, const double y, const double heading, routing::LaneWaypoint *laneWayPoint) const
bool CheckRoutingPointLaneType(apollo::hdmap::LaneInfoConstPtr lane) const
bool GetPoseWithRegardToLane(const double x, const double y, double *theta, double *s) const
size_t CalculateMapHash(const MapElementIds &ids) const
bool GetPathsFromRouting(const apollo::routing::RoutingResponse &routing, std::vector< apollo::hdmap::Path > *paths) const
bool ConstructLaneWayPoint(const double x, const double y, routing::LaneWaypoint *laneWayPoint) const
The function fills out proper routing lane waypoint from the given (x,y) position.
std::string GetParkingSpaceId(const double x, const double y) const
static const HDMap * BaseMapPtr()
static const HDMap * SimMapPtr()
StopSignInfoConstPtr GetStopSignById(const Id &id) const
PNCJunctionInfoConstPtr GetPNCJunctionById(const Id &id) const
YieldSignInfoConstPtr GetYieldSignById(const Id &id) const
ClearAreaInfoConstPtr GetClearAreaById(const Id &id) const
LaneInfoConstPtr GetLaneById(const Id &id) const
RoadInfoConstPtr GetRoadById(const Id &id) const
JunctionInfoConstPtr GetJunctionById(const Id &id) const
ParkingSpaceInfoConstPtr GetParkingSpaceById(const Id &id) const
CrosswalkInfoConstPtr GetCrosswalkById(const Id &id) const
SignalInfoConstPtr GetSignalById(const Id &id) const
SpeedBumpInfoConstPtr GetSpeedBumpById(const Id &id) const
static std::vector< MapPathPoint > GetPointsFromLane(LaneInfoConstPtr lane, const double start_s, const double end_s)
std::shared_ptr< const PNCJunctionInfo > PNCJunctionInfoConstPtr
std::shared_ptr< const JunctionInfo > JunctionInfoConstPtr
apollo::hdmap::Id MakeMapId(const std::string &id)
create a Map ID given a string.
std::shared_ptr< const StopSignInfo > StopSignInfoConstPtr
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
std::shared_ptr< const ClearAreaInfo > ClearAreaInfoConstPtr
std::shared_ptr< const SignalInfo > SignalInfoConstPtr
std::shared_ptr< const SpeedBumpInfo > SpeedBumpInfoConstPtr
std::shared_ptr< const CrosswalkInfo > CrosswalkInfoConstPtr
std::shared_ptr< const YieldSignInfo > YieldSignInfoConstPtr
std::shared_ptr< const ParkingSpaceInfo > ParkingSpaceInfoConstPtr
Some string util functions.
repeated string pnc_junction
repeated string crosswalk
repeated string clear_area
repeated string stop_sign
repeated string speed_bump
repeated string parking_space
repeated Junction junction
repeated StopSign stop_sign
repeated Crosswalk crosswalk
repeated ParkingSpace parking_space
repeated SpeedBump speed_bump
repeated PNCJunction pnc_junction
repeated ClearArea clear_area
repeated apollo::routing::RoadSegment road