Skip to content

Commit

Permalink
clean out redundant code in src/osmdata-sc after #158
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Nov 29, 2018
1 parent 0fc6daa commit d00bc82
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 258 deletions.
33 changes: 0 additions & 33 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
207 changes: 0 additions & 207 deletions src/osmdata-sc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,82 +34,6 @@

#include <Rcpp.h>

// Note: This code uses explicit index counters within most loops which use Rcpp
// objects, because these otherwise require a
// static_cast <size_t> (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 <osmid_t> 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 <std::pair <std::string, std::string> > 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) {
Expand All @@ -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
//'
Expand Down
18 changes: 0 additions & 18 deletions src/osmdata-sc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <std::string>& get_rel_kv_id() const { return vectors.rel_kv_id; }
const std::vector <std::string>& get_rel_key() const { return vectors.rel_key; }
const std::vector <std::string>& get_rel_val() const { return vectors.rel_val; }
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d00bc82

Please sign in to comment.