Updated to use bounding box from service 75/83575/1
authorDuane Gearhart <duane@mapzen.com>
Thu, 11 Aug 2016 20:07:45 +0000 (16:07 -0400)
committerDuane Gearhart <duane@mapzen.com>
Thu, 11 Aug 2016 20:07:45 +0000 (16:07 -0400)
Change-Id: I1aeb1cecda91d8917757196ffe99b4f01b7f2947

src/mapzen/mapzen_jsonparser.cpp

index db22997..d0cd2c8 100644 (file)
@@ -362,32 +362,21 @@ static void __process_shape(std::vector<coords_s>& decoded_shape, GList **shapeP
        MAP_DEBUG(">>>>> END __process_shape");
 }
 
-// TODO - bounding box was added to service - therefore, remove method when we read from updated service
-static void __create_bounding_box(std::vector<coords_s>& pts, rectangle_s& bounding_box)
+static void __set_bounding_box(rapidjson::Value::ConstMemberIterator& summary, rectangle_s& bounding_box)
 {
-       MAP_DEBUG(">>>>> START __create_bounding_box");
-       auto p = pts.begin();
-       double min_lon = p->longitude;
-       double max_lon = min_lon;
-       double min_lat = p->latitude;
-       double max_lat = min_lat;
-       p++;
-       for (; p < pts.end(); p++) {
-               if (p->longitude < min_lon)
-                       min_lon = p->longitude;
-               else if (p->longitude > max_lon)
-                       max_lon = p->longitude;
-               if (p->latitude < min_lat)
-                       min_lat = p->latitude;
-               else if (p->latitude > max_lat)
-                       max_lat = p->latitude;
+       rapidjson::Value::ConstMemberIterator min_lat = summary->value.FindMember("min_lat");
+       rapidjson::Value::ConstMemberIterator min_lon = summary->value.FindMember("min_lon");
+       rapidjson::Value::ConstMemberIterator max_lat = summary->value.FindMember("max_lat");
+       rapidjson::Value::ConstMemberIterator max_lon = summary->value.FindMember("max_lon");
+       if ((min_lat != summary->value.MemberEnd())
+                       && (min_lon != summary->value.MemberEnd())
+                       && (max_lat != summary->value.MemberEnd())
+                       && (max_lon != summary->value.MemberEnd())) {
+               bounding_box.top_left.latitude = max_lat->value.GetDouble();
+               bounding_box.top_left.longitude = min_lon->value.GetDouble();
+               bounding_box.bottom_right.latitude = min_lat->value.GetDouble();
+               bounding_box.bottom_right.longitude = max_lon->value.GetDouble();
        }
-       bounding_box.top_left.latitude = max_lat;
-       bounding_box.top_left.longitude = min_lon;
-       bounding_box.bottom_right.latitude = min_lat;
-       bounding_box.bottom_right.longitude = max_lon;
-
-       MAP_DEBUG(">>>>> END __create_bounding_box");
 }
 
 static void __parse_maneuvers(rapidjson::Value::ConstMemberIterator maneuvers, mapzen_route_segment *segment_resp, std::vector<coords_s>& segment_shape)
@@ -576,28 +565,33 @@ static void __parse_route_response(char *response, int size, int *status, mapzen
        (*routeResp)->distance_unit = __route_unit;
        MAP_DEBUG(">>>>> PROCESS __parse_route_response: distance_unit=%d", (*routeResp)->distance_unit);
 
-       // Set trip distance and time
+       // Process trip summary
        rapidjson::Value::ConstMemberIterator trip_summary = trip->value.FindMember("summary");
        if (trip_summary != trip->value.MemberEnd()) {
+               // Set trip distance
                rapidjson::Value::ConstMemberIterator trip_distance = trip_summary->value.FindMember("length");
                if (trip_distance != trip_summary->value.MemberEnd()) {
                        (*routeResp)->distance = trip_distance->value.GetDouble();
                        MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip length : %f", (*routeResp)->distance);
                }
+
+               // Set trip time
                rapidjson::Value::ConstMemberIterator trip_time = trip_summary->value.FindMember("time");
                if (trip_time != trip_summary->value.MemberEnd()) {
                        (*routeResp)->time = trip_time->value.GetInt();
                        MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip time: %d", trip_time->value.GetInt());
                }
+
+               // Set trip bounding box
+               __set_bounding_box(trip_summary, (*routeResp)->bounding_box);
+               MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip_bounding_box.top_left =%f,%f", (*routeResp)->bounding_box.top_left.latitude, (*routeResp)->bounding_box.top_left.longitude);
+               MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip_bounding_box.bottom_right =%f,%f", (*routeResp)->bounding_box.bottom_right.latitude, (*routeResp)->bounding_box.bottom_right.longitude);
        }
 
        // Type
        (*routeResp)->type = __route_type;
        MAP_DEBUG(">>>>> PROCESS __parse_route_response: route_type=%d", (*routeResp)->type);
 
-       // Trip bounding box points
-       std::vector<coords_s> trip_bounding_box_candidates;
-
        ///////////////////////////////////////////////////////////////////////////
        // Process segments (legs)
        rapidjson::Value::MemberIterator legs = trip->value.FindMember("legs");
@@ -627,19 +621,27 @@ static void __parse_route_response(char *response, int size, int *status, mapzen
                segment_resp->destination = segment_destination;
                MAP_DEBUG(">>>>> PROCESS __parse_route_response: segment_destination: %f,%f", segment_destination.latitude, segment_destination.longitude);
 
-               // Set segment distance & time
+               // Process segment summary
                rapidjson::Value::ConstMemberIterator leg_summary = leg->FindMember("summary");
                if (leg_summary != leg->MemberEnd()) {
+                       // Set segment distance
                        rapidjson::Value::ConstMemberIterator leg_distance = leg_summary->value.FindMember("length");
                        if (leg_distance != leg_summary->value.MemberEnd()) {
                                segment_resp->distance = leg_distance->value.GetDouble();
                                MAP_DEBUG(">>>>> PROCESS __parse_route_response: leg length=%f", segment_resp->distance);
                        }
+
+                       // Set segment time
                        rapidjson::Value::ConstMemberIterator leg_time = leg_summary->value.FindMember("time");
                        if (leg_time != leg_summary->value.MemberEnd()) {
                                segment_resp->time = leg_time->value.GetInt();
                                MAP_DEBUG(">>>>> PROCESS __parse_route_response: leg time=%d", segment_resp->time);
                        }
+
+                       // Set segment bounding box
+                       __set_bounding_box(leg_summary, segment_resp->bounding_box);
+                       MAP_DEBUG(">>>>> PROCESS __parse_route_response: segment_bounding_box.top_left =%f,%f", segment_resp->bounding_box.top_left.latitude, segment_resp->bounding_box.top_left.longitude);
+                       MAP_DEBUG(">>>>> PROCESS __parse_route_response: segment_bounding_box.bottom_right =%f,%f", segment_resp->bounding_box.bottom_right.latitude, segment_resp->bounding_box.bottom_right.longitude);
                }
 
                // Get segment encoded shape and decode
@@ -658,18 +660,6 @@ static void __parse_route_response(char *response, int size, int *status, mapzen
                __process_shape(decoded_shape, &(segment_resp->shapePoints));
                MAP_DEBUG(">>>>> PROCESS __parse_route_response: POST __process_shape");
 
-               // Set segment bounding box
-               // TODO - update to read from service
-               rectangle_s segment_bounding_box;
-               __create_bounding_box(decoded_shape, segment_bounding_box);
-               segment_resp->bounding_box = segment_bounding_box;
-               MAP_DEBUG(">>>>> PROCESS __parse_route_response: segment_bounding_box.top_left =%f,%f", segment_resp->bounding_box.top_left.latitude, segment_resp->bounding_box.top_left.longitude);
-               MAP_DEBUG(">>>>> PROCESS __parse_route_response: segment_bounding_box.bottom_right =%f,%f", segment_resp->bounding_box.bottom_right.latitude, segment_resp->bounding_box.bottom_right.longitude);
-
-               // Add segment bounding box points to the trip bounding box candidates
-               trip_bounding_box_candidates.push_back(segment_bounding_box.top_left);
-               trip_bounding_box_candidates.push_back(segment_bounding_box.bottom_right);
-
                rapidjson::Value::ConstMemberIterator maneuvers = leg->FindMember("maneuvers");
                if (maneuvers == leg->MemberEnd() || !maneuvers->value.IsArray())
                        return;
@@ -692,14 +682,6 @@ static void __parse_route_response(char *response, int size, int *status, mapzen
        } // end process each leg
        ///////////////////////////////////////////////////////////////////////////
 
-       // Trip Bounding box
-       // TODO - update to read from service
-       rectangle_s trip_bounding_box;
-       __create_bounding_box(trip_bounding_box_candidates, trip_bounding_box);
-       (*routeResp)->bounding_box = trip_bounding_box;
-       MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip_bounding_box.top_left =%f,%f", (*routeResp)->bounding_box.top_left.latitude, (*routeResp)->bounding_box.top_left.longitude);
-       MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip_bounding_box.bottom_right =%f,%f", (*routeResp)->bounding_box.bottom_right.latitude, (*routeResp)->bounding_box.bottom_right.longitude);
-
        MAP_DEBUG(">>>>> END __parse_route_response");
 }