Skip to content

Commit

Permalink
Handle aggregated attributes with no base attribute in the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Sep 17, 2024
1 parent a2adb60 commit 27a4f4e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
78 changes: 70 additions & 8 deletions attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
case op_sum:
full_values[i].s = milo::dtoa_milo(atof(full_values[i].s.c_str()) + atof(val.s.c_str()));
full_values[i].type = mvt_double;
break;
return;

case op_product:
full_values[i].s = milo::dtoa_milo(atof(full_values[i].s.c_str()) * atof(val.s.c_str()));
full_values[i].type = mvt_double;
break;
return;

case op_max: {
double existing = atof(full_values[i].s.c_str());
Expand All @@ -109,7 +109,7 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
full_values[i].s = val.s.c_str();
full_values[i].type = mvt_double;
}
break;
return;
}

case op_min: {
Expand All @@ -119,7 +119,7 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
full_values[i].s = val.s.c_str();
full_values[i].type = mvt_double;
}
break;
return;
}

case op_mean: {
Expand All @@ -131,24 +131,26 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));

full_values[i].s = milo::dtoa_milo(s.sum / s.count);
full_values[i].type = mvt_double;
} else {
state->second.sum += atof(val.s.c_str());
state->second.count += 1;

full_values[i].s = milo::dtoa_milo(state->second.sum / state->second.count);
full_values[i].type = mvt_double;
}
break;
return;
}

case op_concat:
full_values[i].s += val.s;
full_values[i].type = mvt_string;
break;
return;

case op_comma:
full_values[i].s += std::string(",") + val.s;
full_values[i].type = mvt_string;
break;
return;

case op_count: {
auto state = attribute_accum_state.find(key);
Expand All @@ -162,9 +164,69 @@ void preserve_attribute(attribute_op const &op, std::string const &key, serial_v
state->second.count += 1;
full_values[i].s = std::to_string(state->second.count);
}
break;
return;
}
}
}
}

// not found, so we are making a new value

serial_val sv;
switch (op) {
case op_sum:
case op_product:
case op_max:
case op_min:
sv.s = val.s;
sv.type = mvt_double;
break;

case op_mean: {
auto state = attribute_accum_state.find(key);
if (state == attribute_accum_state.end()) {
accum_state s;
s.sum = atof(val.s.c_str());
s.count = 1;
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));

sv.s = milo::dtoa_milo(s.sum / s.count);
} else {
state->second.sum += atof(val.s.c_str());
state->second.count += 1;

sv.s = milo::dtoa_milo(state->second.sum / state->second.count);
}
break;
}

case op_concat:
sv.s = val.s;
sv.type = mvt_string;
break;

case op_comma:
sv.s = val.s;
sv.type = mvt_string;
break;

case op_count: {
auto state = attribute_accum_state.find(key);
if (state == attribute_accum_state.end()) { // not already present
accum_state s;
s.count = 1;
attribute_accum_state.insert(std::pair<std::string, accum_state>(key, s));

sv.s = std::to_string(s.count);
} else { // already present, incrementing
state->second.count += 1;
sv.s = std::to_string(state->second.count);
}
sv.type = mvt_double;
break;
}
}

full_keys.push_back(key);
full_values.push_back(sv);
}
2 changes: 2 additions & 0 deletions tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,8 @@ void promote_attribute_prefix(std::string const &key, std::string const &prefixe

return;
}

// it does not exist, so preserve_attribute() will create it
}

// accumulate attribute values from sf onto p
Expand Down

0 comments on commit 27a4f4e

Please sign in to comment.