Skip to content

Commit

Permalink
further optimize grid encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Jul 12, 2012
1 parent e88ed41 commit cbe4991
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 47 deletions.
56 changes: 17 additions & 39 deletions src/js_grid_utils.hpp
Expand Up @@ -149,10 +149,13 @@ static void write_features(T const& grid_type,
Local<Object>& feature_data, Local<Object>& feature_data,
std::vector<typename T::lookup_type> const& key_order) std::vector<typename T::lookup_type> const& key_order)
{ {
typename T::feature_type const& g_features = grid_type.get_grid_features();
if (g_features.size() <= 0)
{
return;
}
std::string const& key_name = grid_type.get_key(); std::string const& key_name = grid_type.get_key();
std::set<std::string> const& attributes = grid_type.property_names(); std::set<std::string> const& attributes = grid_type.property_names();
typename T::feature_type const& g_features = grid_type.get_grid_features();
bool include_key = (attributes.find(key_name) != attributes.end());
typename T::feature_type::const_iterator feat_end = g_features.end(); typename T::feature_type::const_iterator feat_end = g_features.end();
BOOST_FOREACH ( std::string const& key_item, key_order ) BOOST_FOREACH ( std::string const& key_item, key_order )
{ {
Expand All @@ -164,50 +167,25 @@ static void write_features(T const& grid_type,
typename T::feature_type::const_iterator feat_itr = g_features.find(key_item); typename T::feature_type::const_iterator feat_itr = g_features.find(key_item);
if (feat_itr == feat_end) if (feat_itr == feat_end)
{ {
std::clog << "feature not found for '" << key_item << "'\n";
continue; continue;
} }


mapnik::feature_ptr feature = feat_itr->second;
std::string join_value;
if (key_name == grid_type.key_name())
{
join_value = feat_itr->first;

}
else if (feature->has_key(key_name))
{
join_value = feature->get(key_name).to_string();
}
if (join_value.empty())
{
std::clog << "should not get here: key '" << key_name << "' not found in grid feature properties\n";
continue;
}

Local<Object> feat = Object::New();
bool found = false; bool found = false;
if (key_name == grid_type.key_name()) Local<Object> feat = Object::New();
mapnik::feature_ptr feature = feat_itr->second;
BOOST_FOREACH ( std::string const& attr, attributes )
{ {
// drop key unless requested if (attr == "__id__")
if (include_key) { {
found = true; feat->Set(String::NewSymbol(attr.c_str()),Integer::New(feature->id()));
// TODO do we need to duplicate __id__ ?
//feat->Set(String::NewSymbol(key.c_str()), String::New(join_value->c_str()) );
} }
} else if (feature->has_key(attr))
mapnik::feature_impl::iterator itr = feature->begin();
mapnik::feature_impl::iterator end = feature->end();
for ( ;itr!=end; ++itr)
{
std::string const& feat_key_name = boost::get<0>(*itr);
if (( feat_key_name == key_name && include_key ) // drop key unless requested
|| attributes.find(feat_key_name) != attributes.end())
{ {
found = true; found = true;
feat->Set(String::NewSymbol(feat_key_name.c_str()), mapnik::feature_impl::value_type const& attr_val = feature->get(attr);
boost::apply_visitor(node_mapnik::value_converter(), feat->Set(String::NewSymbol(attr.c_str()),
boost::get<1>(*itr).base())); boost::apply_visitor(node_mapnik::value_converter(),
attr_val.base()));
} }
} }


Expand Down Expand Up @@ -339,7 +317,7 @@ static void write_features(T const& grid_type,
Local<Object>& feature_data, Local<Object>& feature_data,
std::vector<typename T::lookup_type> const& key_order) std::vector<typename T::lookup_type> const& key_order)
{ {
std::string const& key = grid_type.get_key(); // get_key(); std::string const& key = grid_type.get_key();
std::set<std::string> const& attributes = grid_type.property_names(); std::set<std::string> const& attributes = grid_type.property_names();
typename T::feature_type const& g_features = grid_type.get_grid_features(); typename T::feature_type const& g_features = grid_type.get_grid_features();
typename T::feature_type::const_iterator feat_itr = g_features.begin(); typename T::feature_type::const_iterator feat_itr = g_features.begin();
Expand Down
16 changes: 8 additions & 8 deletions src/mapnik_map.cpp
Expand Up @@ -1488,16 +1488,16 @@ void Map::EIO_RenderGrid(uv_work_t* req)
// copy property names // copy property names
std::set<std::string> attributes = closure->g->get()->property_names(); std::set<std::string> attributes = closure->g->get()->property_names();


std::string join_field = closure->g->get()->get_key(); // todo - make this a static constant
if (join_field == closure->g->get()->key_name()) std::string known_id_key = "__id__";
if (attributes.find(known_id_key) != attributes.end())
{ {
// TODO - should feature.id() be a first class attribute? attributes.erase(known_id_key);
if (attributes.find(join_field) != attributes.end())
{
attributes.erase(join_field);
}
} }
else if (attributes.find(join_field) == attributes.end())
std::string join_field = closure->g->get()->get_key();
if (known_id_key != join_field &&
attributes.find(join_field) == attributes.end())
{ {
attributes.insert(join_field); attributes.insert(join_field);
} }
Expand Down

0 comments on commit cbe4991

Please sign in to comment.