diff --git a/R/RcppExports.R b/R/RcppExports.R index a5b0fe0b..64b350d8 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -1,39 +1,6 @@ # Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 -#' get_osm_relations -#' -#' Return an Rcpp::List containing all OSM relations. -#' -#' @param rels Pointer to the vector of Relation objects -#' @param rel_df Pointer to return object containing the members of each -#' relation. -#' @param rel_kv Pointer to return object containing key-value pairs for each -#' relation. -#' -#' @noRd -NULL - -#' get_osm_ways -#' -#' @param edge Pointer to Rcpp::DataFrame to hold the SC::edge table -#' @param object_link_edge Pointer to Rcpp::DataFrame to hold the -#' SC::object_linkedge table -#' @param kv_df Pointer to Rcpp::DataFrame to hold key-value pairs -#' @param ways Pointer to all ways in data set -#' -#' @noRd -NULL - -#' get_osm_nodes -#' -#' @param node_df Pointer to Rcpp::DataFrame to hold nodes -#' @param kv_df Pointer to Rcpp::DataFrame to hold key-value pairs -#' @param nodes Pointer to all nodes in data set -#' -#' @noRd -NULL - #' rcpp_osmdata_sc #' #' Return OSM data in silicate (SC) format diff --git a/src/osmdata-sc.cpp b/src/osmdata-sc.cpp index c5738448..126eb09a 100644 --- a/src/osmdata-sc.cpp +++ b/src/osmdata-sc.cpp @@ -34,82 +34,6 @@ #include -// Note: This code uses explicit index counters within most loops which use Rcpp -// objects, because these otherwise require a -// static_cast (std::distance (...)). This operation copies each -// instance and can slow the loops down by several orders of magnitude! - -/************************************************************************ - ************************************************************************ - ** ** - ** 1. PRIMARY FUNCTIONS TO TRACE WAYS AND RELATIONS ** - ** ** - ************************************************************************ - ************************************************************************/ - - -//' get_osm_relations -//' -//' Return an Rcpp::List containing all OSM relations. -//' -//' @param rels Pointer to the vector of Relation objects -//' @param rel_df Pointer to return object containing the members of each -//' relation. -//' @param rel_kv Pointer to return object containing key-value pairs for each -//' relation. -//' -//' @noRd -void osm_sc::get_osm_relations (Rcpp::DataFrame &rel_df, Rcpp::DataFrame &kv_df, - const Relations &rels) -{ - std::vector ids (rels.size ()); - - size_t nrow_kv = 0, nrow_memb = 0; - for (auto itr = rels.begin (); itr != rels.end (); ++itr) - { - nrow_kv += itr->key_val.size (); - nrow_memb += itr->ways.size (); - } - - Rcpp::CharacterMatrix kv_mat (Rcpp::Dimension (nrow_kv, 3)); - Rcpp::CharacterMatrix rel_mat (Rcpp::Dimension (nrow_memb, 3)); - // rel_mat has [rel ID, member ID, member role] - - unsigned int i_rel_mat = 0, i_kv_mat = 0; // explicit loop indices - for (auto itr = rels.begin (); itr != rels.end (); ++itr) - { - Rcpp::checkUserInterrupt (); - osm_str_vec relation_ways; - std::vector > relation_kv; - trace_relation (itr, relation_ways, relation_kv); - - unsigned int i = std::distance (rels.begin (), itr); - ids [i] = itr->id; - for (auto r = relation_ways.begin (); r != relation_ways.end (); ++r) - { - rel_mat (i_rel_mat, 0) = std::to_string (itr->id); // relation ID - rel_mat (i_rel_mat, 1) = std::to_string (r->first); // ref ID of component obj - rel_mat (i_rel_mat++, 2) = r->second; // role of component - } - - for (auto k = itr->key_val.begin (); k != itr->key_val.end (); ++k) - { - kv_mat (i_kv_mat, 0) = std::to_string (itr->id); - kv_mat (i_kv_mat, 1) = k->first; - kv_mat (i_kv_mat++, 2) = k->second; - } - } - rel_df = Rcpp::DataFrame::create (Rcpp::Named ("object_") = rel_mat (Rcpp::_, 0), - Rcpp::Named ("ref") = rel_mat (Rcpp::_, 1), - Rcpp::Named ("role") = rel_mat (Rcpp::_, 2), - Rcpp::_["stringsAsFactors"] = false ); - - kv_df = Rcpp::DataFrame::create (Rcpp::Named ("object_") = kv_mat (Rcpp::_, 0), - Rcpp::Named ("key") = kv_mat (Rcpp::_, 1), - Rcpp::Named ("value") = kv_mat (Rcpp::_, 2), - Rcpp::_["stringsAsFactors"] = false ); -} - // Function to generate IDs for the edges in each way std::string random_id (size_t len) { @@ -127,137 +51,6 @@ std::string random_id (size_t len) { return str; } -//' get_osm_ways -//' -//' @param edge Pointer to Rcpp::DataFrame to hold the SC::edge table -//' @param object_link_edge Pointer to Rcpp::DataFrame to hold the -//' SC::object_linkedge table -//' @param kv_df Pointer to Rcpp::DataFrame to hold key-value pairs -//' @param ways Pointer to all ways in data set -//' -//' @noRd -void osm_sc::get_osm_ways (Rcpp::DataFrame &edge, - Rcpp::DataFrame &object_link_edge, Rcpp::DataFrame &kv_df, - const Ways &ways) -{ - const int length_ids = 10; - - Rcpp::RNGScope scope; // set random seed - - int nedges = 0, nkv = 0; - for (auto wi = ways.begin (); wi != ways.end (); ++wi) - { - if (wi->second.nodes.size () > 0) - nedges += wi->second.nodes.size () - 1; - nkv += wi->second.key_val.size (); - } - - Rcpp::CharacterMatrix edge_mat (Rcpp::Dimension (nedges, 3)); - Rcpp::CharacterMatrix object_link_edge_mat (Rcpp::Dimension (nedges, 2)); - Rcpp::CharacterMatrix kv_mat (Rcpp::Dimension (nkv, 3)); - - // TODO: Impelement these properly with std::distance - int count_w = 0, count_k = 0; - for (auto wi = ways.begin (); wi != ways.end (); ++wi) - { - Rcpp::checkUserInterrupt (); - - auto first = wi->second.nodes.begin (); - auto last = wi->second.nodes.empty () ? - wi->second.nodes.end () : std::prev (wi->second.nodes.end ()); - for (auto wj = first; wj != last; ++wj) - { - edge_mat (count_w, 0) = std::to_string (*wj); - edge_mat (count_w, 1) = std::to_string (*std::next (wj)); - std::string idj = random_id (length_ids); - edge_mat (count_w, 2) = idj; - object_link_edge_mat (count_w, 0) = idj; - object_link_edge_mat (count_w++, 1) = std::to_string (wi->first); - } - - for (auto kj = wi->second.key_val.begin (); - kj != wi->second.key_val.end (); ++kj) - { - kv_mat (count_k, 0) = std::to_string (wi->first); - kv_mat (count_k, 1) = kj->first; - kv_mat (count_k++, 2) = kj->second; - } - } - edge = Rcpp::DataFrame::create (Rcpp::Named (".vx0") = edge_mat (Rcpp::_, 0), - Rcpp::Named (".vx1") = edge_mat (Rcpp::_, 1), - Rcpp::Named ("edge_") = edge_mat (Rcpp::_, 2), - Rcpp::_["stringsAsFactors"] = false ); - - object_link_edge = Rcpp::DataFrame::create ( - Rcpp::Named ("edge_") = object_link_edge_mat (Rcpp::_, 0), - Rcpp::Named ("object_") = object_link_edge_mat (Rcpp::_, 1), - Rcpp::_["stringsAsFactors"] = false ); - - kv_df = Rcpp::DataFrame::create ( - Rcpp::Named ("object_") = kv_mat (Rcpp::_, 0), - Rcpp::Named ("key") = kv_mat (Rcpp::_, 1), - Rcpp::Named ("value") = kv_mat (Rcpp::_, 2), - Rcpp::_["stringsAsFactors"] = false ); -} - -//' get_osm_nodes -//' -//' @param node_df Pointer to Rcpp::DataFrame to hold nodes -//' @param kv_df Pointer to Rcpp::DataFrame to hold key-value pairs -//' @param nodes Pointer to all nodes in data set -//' -//' @noRd -void osm_sc::get_osm_nodes (Rcpp::DataFrame &node_df, Rcpp::DataFrame &kv_df, - const Nodes &nodes) -{ - const size_t nrow = nodes.size (); - - Rcpp::CharacterVector node_ids (nrow); - Rcpp::NumericMatrix node_mat (nrow, 2); // lon-lat - - int nkeys = 0; - for (auto ni = nodes.begin (); ni != nodes.end (); ++ni) - nkeys += ni->second.key_val.size (); - Rcpp::CharacterMatrix kv_mat (Rcpp::Dimension (nkeys, 3)); - - int i = 0, keyj = 0; - for (auto ni = nodes.begin (); ni != nodes.end (); ++ni) - { - if (i % 1000 == 0) - Rcpp::checkUserInterrupt (); - - node_ids (i) = std::to_string (ni->first); - node_mat (i, 0) = ni->second.lon; - node_mat (i++, 1) = ni->second.lat; - - for (auto kv_iter = ni->second.key_val.begin (); - kv_iter != ni->second.key_val.end (); ++kv_iter) - { - kv_mat (keyj, 0) = std::to_string (ni->first); - kv_mat (keyj, 1) = kv_iter->first; - kv_mat (keyj++, 2) = kv_iter->second; - } - } - node_df = Rcpp::DataFrame::create (Rcpp::Named ("x_") = node_mat (Rcpp::_, 0), - Rcpp::Named ("y_") = node_mat (Rcpp::_, 1), - Rcpp::Named ("vertex_") = node_ids, - Rcpp::_["stringsAsFactors"] = false ); - - kv_df = Rcpp::DataFrame::create ( - Rcpp::Named ("object_") = kv_mat (Rcpp::_, 0), - Rcpp::Named ("key") = kv_mat (Rcpp::_, 1), - Rcpp::Named ("value") = kv_mat (Rcpp::_, 2), - Rcpp::_["stringsAsFactors"] = false ); -} - - -/************************************************************************ - ************************************************************************ - ** ** - ** THE FINAL RCPP FUNCTION CALLED BY osmdata_sc ** - ** ** - ************************************************************************ - ************************************************************************/ //' rcpp_osmdata_sc //' diff --git a/src/osmdata-sc.h b/src/osmdata-sc.h index cc06fca3..bb0a1373 100644 --- a/src/osmdata-sc.h +++ b/src/osmdata-sc.h @@ -98,9 +98,6 @@ class XmlDataSC public: - double xmin = DOUBLE_MAX, xmax = -DOUBLE_MAX, - ymin = DOUBLE_MAX, ymax = -DOUBLE_MAX; - XmlDataSC (const std::string& str) { // APS empty m_nodes/m_ways/m_relations constructed here, no need to explicitly clear @@ -117,15 +114,8 @@ class XmlDataSC // APS make the dtor virtual since compiler support for "final" is limited virtual ~XmlDataSC () { - // APS m_nodes/m_ways/m_relations destructed here, no need to explicitly clear } - // Const accessors for members - double x_min() { return xmin; } - double x_max() { return xmax; } - double y_min() { return ymin; } - double y_max() { return ymax; } - const std::vector & get_rel_kv_id() const { return vectors.rel_kv_id; } const std::vector & get_rel_key() const { return vectors.rel_key; } const std::vector & get_rel_val() const { return vectors.rel_val; } @@ -353,14 +343,6 @@ inline void XmlDataSC::traverseWays (XmlNodePtr pt) { traverseNode (it); counters.nnodes++; - - /* - if (rnode.lon < xmin) xmin = rnode.lon; - if (rnode.lon > xmax) xmax = rnode.lon; - if (rnode.lat < ymin) ymin = rnode.lat; - if (rnode.lat > ymax) ymax = rnode.lat; - */ - } else if (!strcmp (it->name(), "way")) { int node_num = 0;