Merge pull request #4094 from Project-OSRM/refactor/rapidash

Replace timezone shapefile parsing with geojson parsing
This commit is contained in:
Patrick Niklaus
2017-06-02 20:01:04 +00:00
committed by GitHub
321 changed files with 54225 additions and 231 deletions
+84
View File
@@ -0,0 +1,84 @@
#ifndef OSRM_GEOJSON_VALIDATION_HPP
#define OSRM_GEOJSON_VALIDATION_HPP
#include "util/exception.hpp"
#include "util/log.hpp"
#include "rapidjson/document.h"
namespace osrm
{
namespace util
{
inline void validateCoordinate(const rapidjson::Value &coordinate)
{
if (!coordinate.IsArray())
throw osrm::util::exception("Feature geometry has a non-array coordinate.");
if (coordinate.Capacity() != 2)
{
throw osrm::util::exception(
"Feature geometry has a malformed coordinate with more than 2 values.");
}
else
{
for (rapidjson::SizeType i = 0; i < coordinate.Size(); i++)
{
if (!coordinate[i].IsNumber() && !coordinate[i].IsDouble())
throw osrm::util::exception("Feature geometry has a non-number coordinate.");
}
}
}
inline void validateFeature(const rapidjson::Value &feature)
{
if (!feature.HasMember("type"))
{
throw osrm::util::exception("Feature is missing type member.");
}
else if (!feature["type"].IsString())
{
throw osrm::util::exception("Feature has non-string type member.");
}
if (!feature.HasMember("properties"))
{
throw osrm::util::exception("Feature is missing properties member.");
}
else if (!feature.GetObject()["properties"].IsObject())
{
throw osrm::util::exception("Feature has non-object properties member.");
}
if (!feature.HasMember("geometry"))
{
throw osrm::util::exception("Feature is missing geometry member.");
}
else if (!feature.GetObject()["geometry"].IsObject())
{
throw osrm::util::exception("Feature non-object geometry member.");
}
if (!feature["geometry"].GetObject().HasMember("type"))
{
throw osrm::util::exception("Feature geometry is missing type member.");
}
else if (!feature["geometry"].GetObject()["type"].IsString())
{
throw osrm::util::exception("Feature geometry has non-string type member.");
}
if (!feature["geometry"].GetObject().HasMember("coordinates"))
{
throw osrm::util::exception("Feature geometry is missing coordinates member.");
}
else if (!feature["geometry"].GetObject()["coordinates"].IsArray())
{
throw osrm::util::exception("Feature geometry has a non-array coordinates member.");
}
const auto coord_array = feature["geometry"].GetObject()["coordinates"].GetArray();
if (coord_array.Empty())
throw osrm::util::exception("Feature geometry coordinates member is empty.");
if (!coord_array[0].GetArray()[0].IsArray())
throw osrm::util::exception("Feature geometry coordinates array has non-array outer ring.");
}
}
}
#endif // OSRM_GEOJSON_VALIDATION_HPP
+8 -6
View File
@@ -3,8 +3,12 @@
#include "util/log.hpp"
#include <boost/filesystem/path.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/optional.hpp>
#include <rapidjson/document.h>
#include <chrono>
@@ -24,21 +28,19 @@ using rtree_t =
boost::geometry::index::rtree<std::pair<box_t, size_t>, boost::geometry::index::rstar<8>>;
using local_time_t = std::pair<polygon_t, struct tm>;
bool SupportsShapefiles();
class Timezoner
{
public:
Timezoner() = default;
Timezoner(std::string tz_filename, std::time_t utc_time_now);
Timezoner(const char geojson[], std::time_t utc_time_now);
Timezoner(const boost::filesystem::path &tz_shapes_filename, std::time_t utc_time_now);
struct tm operator()(const point_t &point) const;
boost::optional<struct tm> operator()(const point_t &point) const;
private:
void LoadLocalTimesRTree(const std::string &tz_shapes_filename, std::time_t utc_time);
void LoadLocalTimesRTree(rapidjson::Document &geojson, std::time_t utc_time);
struct tm default_time;
rtree_t rtree;
std::vector<local_time_t> local_times;
};