Skip to content

v0.2.48..v0.2.49 changeset RandomTagModifier.cpp

Garret Voltz edited this page Oct 2, 2019 · 1 revision
diff --git a/hoot-core/src/main/cpp/hoot/core/visitors/RandomTagModifier.cpp b/hoot-core/src/main/cpp/hoot/core/visitors/RandomTagModifier.cpp
new file mode 100644
index 0000000..fae0b87
--- /dev/null
+++ b/hoot-core/src/main/cpp/hoot/core/visitors/RandomTagModifier.cpp
@@ -0,0 +1,100 @@
+/*
+ * This file is part of Hootenanny.
+ *
+ * Hootenanny is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * --------------------------------------------------------------------
+ *
+ * The following copyright notices are generated automatically. If you
+ * have a new notice to add, please use the format:
+ * " * @copyright Copyright ..."
+ * This will properly maintain the copyright information. DigitalGlobe
+ * copyrights will be updated automatically.
+ *
+ * @copyright Copyright (C) 2015, 2017, 2018, 2019 DigitalGlobe (http://www.digitalglobe.com/)
+ */
+#include "RandomTagModifier.h"
+
+// boost
+#include <boost/random/uniform_int.hpp>
+#include <boost/random/uniform_real.hpp>
+
+// hoot
+#include <hoot/core/util/Factory.h>
+#include <hoot/core/ops/RecursiveElementRemover.h>
+#include <hoot/core/util/Settings.h>
+#include <hoot/core/util/ConfigOptions.h>
+#include <hoot/core/util/RandomNumberUtils.h>
+#include <hoot/core/util/Log.h>
+
+// Standard
+#include <algorithm>
+
+namespace hoot
+{
+
+HOOT_FACTORY_REGISTER(ElementVisitor, RandomTagModifier)
+
+RandomTagModifier::RandomTagModifier()
+{
+  _localRng.reset(new boost::minstd_rand());
+  _rng = _localRng.get();
+}
+
+void RandomTagModifier::setConfiguration(const Settings& conf)
+{
+  ConfigOptions configOptions(conf);
+  setProbability(configOptions.getRandomTagModifierProbability());
+  const int seed = configOptions.getRandomSeed();
+  LOG_VARD(seed);
+  if (seed == -1)
+  {
+    _rng->seed(RandomNumberUtils::generateSeed());
+  }
+  else
+  {
+    _rng->seed(seed);
+  }
+  _exemptTagKeys = configOptions.getRandomTagModifierExemptTagKeys();
+  _replacementTagKeys = configOptions.getRandomTagModifierSubstitutionKeys();
+  _replacementTagValues = configOptions.getRandomTagModifierSubstitutionValues();
+}
+
+void RandomTagModifier::visit(const std::shared_ptr<Element>& e)
+{
+  boost::uniform_real<> uni(0.0, 1.0);
+
+  Tags t = e->getTags();
+  for (Tags::const_iterator it = t.constBegin(); it != t.constEnd(); ++it)
+  {
+    const QString tagKey = it.key();
+    if (uni(*_rng) <= _p && !_exemptTagKeys.contains(tagKey))
+    {
+      if (!_replacementTagKeys.contains(tagKey))
+      {
+        LOG_DEBUG("Removing tag with key: " << tagKey << " ...");
+        e->getTags().remove(tagKey);
+      }
+      else
+      {
+        const int tagIndex = _replacementTagKeys.indexOf(tagKey);
+        const QString tagValue = _replacementTagValues.at(tagIndex);
+        LOG_DEBUG("Substituting value: " << tagValue << " for tag with key: " << tagKey);
+        e->getTags().set(tagKey, tagValue);
+      }
+    }
+  }
+}
+
+}
Clone this wiki locally