FP_TYPE calculate_distance_over_track(std::vector<event_point>* evt_pts_0, std::vector<event_point>* evt_pts_1) { FP_TYPE mean_dist = 0.0; int n_pts = 0; for (int e0=0; e0 < evt_pts_0->size(); e0++) { FP_TYPE lon_0 = (*evt_pts_0)[e0].svex->lon; FP_TYPE lat_0 = (*evt_pts_0)[e0].svex->lat; int evt_0 = (*evt_pts_0)[e0].timestep; for (int e1=0; e1 < evt_pts_1->size(); e1++) { int evt_1 = (*evt_pts_0)[e0].timestep; if (evt_0 == evt_1) { FP_TYPE lon_1 = (*evt_pts_1)[e1].svex->lon; FP_TYPE lat_1 = (*evt_pts_1)[e1].svex->lat; FP_TYPE dist = haversine(lon_0, lat_0, lon_1, lat_1, EARTH_R); mean_dist += dist / 1000.0; n_pts += 1; } } } if (n_pts == 0) return 2e20; else return mean_dist / n_pts; }
bool eventor::evaluate_candidate_event(steering_extremum* trk_pt, steering_extremum* cand_pt, FP_TYPE& dist, FP_TYPE& overlap) { // evaluate the candidate point against the last point in the track // calculate distance dist = haversine(trk_pt->lon, trk_pt->lat, cand_pt->lon, cand_pt->lat, EARTH_R); // calculate overlap FP_TYPE n_overlapping_labels = 0.0; for (LABEL_STORE::iterator trk_label = trk_pt->object_labels.begin(); trk_label != trk_pt->object_labels.end(); trk_label++) { if (std::find(cand_pt->object_labels.begin(), cand_pt->object_labels.end(), *trk_label) != cand_pt->object_labels.end()) { n_overlapping_labels++; } } overlap = 100 * n_overlapping_labels / trk_pt->object_labels.size(); // logic check - if percentage overlap is >= min_overlap and dist <= search radius if (dist <= sr && overlap >= min_overlap) return true; else return false; }
FP_TYPE calculate_triangle_distance(indexed_force_tri_3D* O_TRI, indexed_force_tri_3D* C_TRI) { FP_TYPE o_lon, o_lat; FP_TYPE c_lon, c_lat; cart_to_model(O_TRI->centroid(), o_lon, o_lat); cart_to_model(C_TRI->centroid(), c_lon, c_lat); FP_TYPE dist = haversine(o_lon, o_lat, c_lon, c_lat, EARTH_R); return dist / 1000.0; }
Vector2d * genVelocity(double lon1, double lon2, double lat1, double lat2, double time){ Vector2d * toReturn, * temp1, * temp2; double bearing, speed; toReturn = new Vector2d; temp1 = new Vector2d(lon1, lat1); temp2 = new Vector2d(lon2, lat2); // Speed is distance over time speed = haversine(lon1, lon2, lat1, lat2)/time; bearing = findBearing_2(temp1, temp2); toReturn->x = sin(bearing*M_PI/180.0)*speed; toReturn->y = cos(bearing*M_PI/180.0)*speed; return toReturn; }
//********************************************************************** // void CLatLongDistanceLayer::rebuild() // build //********************************************************************** void CLatLongDistanceLayer::rebuild() { try { for (int i = 0; i < iHeight; ++i) { for (int j = 0; j < iWidth; ++j) { for (int k = 0; k < iHeight; ++k) { for (int l = 0; l < iWidth; ++l) { double dLong1 = getLong(i, j); double dLat1 = getLat(i, j); double dLong2 = getLong(k, l); double dLat2 = getLat(k, l); mGrid[i][j][k][l] = haversine(dLong1, dLat1, dLong2, dLat2); } } } } } catch (string &Ex) { Ex = "CLatLongDistanceLayer.rebuild(" + getLabel() + ")->" + Ex; throw Ex; } }
LABEL tri_grid::get_triangle_for_point(vector_3D* P) { // determine which of the 20 base triangles the point is in int base_tri = -1; for (unsigned int tri=0; tri<triangles.size(); tri++) if (point_in_tri(P, triangles[tri]->get_root()->get_data())) { base_tri = tri; break; } // not found so find by distance if (base_tri == -1) { FP_TYPE min_dist = 2e20; int min_base = -1; FP_TYPE P_lon, P_lat; cart_to_model(*P, P_lon, P_lat); for (unsigned int tri=0; tri<triangles.size(); tri++) { FP_TYPE T_lon, T_lat; cart_to_model(triangles[tri]->get_root()->get_data()->centroid(), T_lon, T_lat); FP_TYPE dist = haversine(P_lon, P_lat, T_lon, T_lat, 1.0); if (dist < min_dist) { dist = min_dist; min_base = tri; } } base_tri = min_base; } // get the base node QT_TRI_NODE* current_node = get_base_tri(base_tri); bool found = false; while (not found) { if (current_node->is_leaf()) { found = true; break; } else { bool found_in_child = false; for (int i=0; i<4; i++) { QT_TRI_NODE* current_child = current_node->get_child(i); if (point_in_tri(P, current_child->get_data())) { current_node = current_child; found_in_child = true; break; } } // not found by point inclusion - find by minimum distance if (not found_in_child) { FP_TYPE min_dist = 2e20; int min_child = -1; FP_TYPE P_lon, P_lat; cart_to_model(*P, P_lon, P_lat); for (int i=0; i<4; i++) { FP_TYPE T_lon, T_lat; cart_to_model(current_node->get_child(i)->get_data()->centroid(), T_lon, T_lat); FP_TYPE dist = haversine(P_lon, P_lat, T_lon, T_lat, 1.0); if (dist < min_dist) { dist = min_dist; min_child = i; } } current_node = current_node->get_child(min_child); } } } return current_node->get_data()->get_label(); }
//' rcpp_lines_as_network //' //' Return OSM data in Simple Features format //' //' @param sf_lines An sf collection of LINESTRING objects //' @param pr Rcpp::DataFrame containing the weighting profile //' //' @return Rcpp::List objects of OSM data //' //' @noRd // [[Rcpp::export]] Rcpp::List rcpp_lines_as_network (const Rcpp::List &sf_lines, Rcpp::DataFrame pr) { std::map <std::string, float> profile; Rcpp::StringVector hw = pr [1]; Rcpp::NumericVector val = pr [2]; for (int i = 0; i != hw.size (); i ++) profile.insert (std::make_pair (std::string (hw [i]), val [i])); Rcpp::CharacterVector nms = sf_lines.attr ("names"); if (nms [nms.size () - 1] != "geometry") throw std::runtime_error ("sf_lines have no geometry component"); if (nms [0] != "osm_id") throw std::runtime_error ("sf_lines have no osm_id component"); int one_way_index = -1; int one_way_bicycle_index = -1; int highway_index = -1; for (int i = 0; i < nms.size (); i++) { if (nms [i] == "oneway") one_way_index = i; if (nms [i] == "oneway.bicycle") one_way_bicycle_index = i; if (nms [i] == "highway") highway_index = i; } Rcpp::CharacterVector ow = NULL; Rcpp::CharacterVector owb = NULL; Rcpp::CharacterVector highway = NULL; if (one_way_index >= 0) ow = sf_lines [one_way_index]; if (one_way_bicycle_index >= 0) owb = sf_lines [one_way_bicycle_index]; if (highway_index >= 0) highway = sf_lines [highway_index]; if (ow.size () > 0) { if (ow.size () == owb.size ()) { for (unsigned i = 0; i != ow.size (); ++ i) if (ow [i] == "NA" && owb [i] != "NA") ow [i] = owb [i]; } else if (owb.size () > ow.size ()) ow = owb; } Rcpp::List geoms = sf_lines [nms.size () - 1]; std::vector<bool> isOneWay (geoms.length ()); std::fill (isOneWay.begin (), isOneWay.end (), false); // Get dimension of matrix size_t nrows = 0; int ngeoms = 0; for (auto g = geoms.begin (); g != geoms.end (); ++g) { // Rcpp uses an internal proxy iterator here, NOT a direct copy Rcpp::NumericMatrix gi = (*g); int rows = gi.nrow () - 1; nrows += rows; if (ngeoms < ow.size ()) { if (!(ow [ngeoms] == "yes" || ow [ngeoms] == "-1")) { nrows += rows; isOneWay [ngeoms] = true; } } ngeoms ++; } Rcpp::NumericMatrix nmat = Rcpp::NumericMatrix (Rcpp::Dimension (nrows, 6)); Rcpp::CharacterMatrix idmat = Rcpp::CharacterMatrix (Rcpp::Dimension (nrows, 3)); nrows = 0; ngeoms = 0; int fake_id = 0; for (auto g = geoms.begin (); g != geoms.end (); ++ g) { Rcpp::NumericMatrix gi = (*g); std::string hway = std::string (highway [ngeoms]); float hw_factor = profile [hway]; if (hw_factor == 0.0) hw_factor = 1e-5; hw_factor = 1.0 / hw_factor; Rcpp::List ginames = gi.attr ("dimnames"); Rcpp::CharacterVector rnms; if (ginames.length () > 0) rnms = ginames [0]; else { rnms = Rcpp::CharacterVector (gi.nrow ()); for (int i = 0; i < gi.nrow (); i ++) rnms [i] = fake_id ++; } if (rnms.size () != gi.nrow ()) throw std::runtime_error ("geom size differs from rownames"); for (int i = 1; i < gi.nrow (); i ++) { float d = haversine (gi (i-1, 0), gi (i-1, 1), gi (i, 0), gi (i, 1)); nmat (nrows, 0) = gi (i-1, 0); nmat (nrows, 1) = gi (i-1, 1); nmat (nrows, 2) = gi (i, 0); nmat (nrows, 3) = gi (i, 1); nmat (nrows, 4) = d; nmat (nrows, 5) = d * hw_factor; idmat (nrows, 0) = rnms (i-1); idmat (nrows, 1) = rnms (i); idmat (nrows, 2) = hway; nrows ++; if (isOneWay [ngeoms]) { nmat (nrows, 0) = gi (i, 0); nmat (nrows, 1) = gi (i, 1); nmat (nrows, 2) = gi (i-1, 0); nmat (nrows, 3) = gi (i-1, 1); nmat (nrows, 4) = d; nmat (nrows, 5) = d * hw_factor; idmat (nrows, 0) = rnms (i); idmat (nrows, 1) = rnms (i-1); idmat (nrows, 2) = hway; nrows ++; } } ngeoms ++; } Rcpp::List res (2); res [0] = nmat; res [1] = idmat; return res; }
/*assuming that latitude and longitude are stored in the x and z coordinates of a point */ f64 GPSconverter::GPSdistance(glm::vec3 p, glm::vec3 q) { f64 h = haversine(q.x - p.x) + cos(p.x)*cos(q.x)*haversine(q.z - p.z); return inverseHaversine(h); }
void extrema_locator::calculate_object_intensity(int o, int t) { // object intensity is calculated as a weighted sum of the intensities of // the triangles in the object. The weight is defined as 1.0 - dist/max_dist // where dist is the distance between the lat and lon of the feature point // and max_dist is the maximum distance of all the objects // get the extremum - the lat and lon will have been set already steering_extremum* svex = ex_list.get(t, o); LABEL_STORE* object_labels = &(svex->object_labels); FP_TYPE max_dist = -1.0; // find min / max of values in the object FP_TYPE min_v = 2e20f; // minimum value in object FP_TYPE max_v = 0; // maximum value in object get_min_max_values(min_v, max_v, o, t); // loop through the triangle objects for (LABEL_STORE::iterator it_ll = object_labels->begin(); it_ll != object_labels->end(); it_ll++) { // get the triangle indexed_force_tri_3D* c_tri = tg.get_triangle(*it_ll); // get the centroid and convert to lat / lon vector_3D C = c_tri->centroid(); FP_TYPE lat, lon; cart_to_model(C, lon, lat); // calculate the distance FP_TYPE dist = haversine(svex->lon, svex->lat, lon, lat, 1.0); // check against max if (dist > max_dist) max_dist = dist; } // repeat the loop, but work out the values this time FP_TYPE sum_intensity = 0.0; FP_TYPE sum_w = 0.0; for (LABEL_STORE::iterator it_ll = object_labels->begin(); it_ll != object_labels->end(); it_ll++) { // get the triangle indexed_force_tri_3D* c_tri = tg.get_triangle(*it_ll); // get the centroid and convert to lat / lon vector_3D C = c_tri->centroid(); FP_TYPE lat, lon; cart_to_model(C, lon, lat); // calculate the distance FP_TYPE dist = haversine(svex->lon, svex->lat, lon, lat, 1.0); // now get the value from the datastore FP_TYPE val = ds.get_data(t, c_tri->get_ds_index()); // calculate the weight FP_TYPE w = 1.0; if (max_dist != 0.0) w = 1.0 - dist / max_dist; sum_intensity += w * val; sum_w += w; } if (sum_w == 0.0) svex->intensity = min_v; else svex->intensity = sum_intensity / sum_w; }