osrm-backend/include/util/geojson_validation.hpp

83 lines
2.6 KiB
C++
Raw Normal View History

2017-05-30 12:47:02 -04:00
#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
{
2017-06-02 14:19:44 -04:00
inline void validateCoordinate(const rapidjson::Value &coordinate)
2017-05-30 12:47:02 -04:00
{
if (!coordinate.IsArray())
throw osrm::util::exception("Feature geometry has a non-array coordinate.");
if (coordinate.Capacity() != 2)
{
2017-06-01 12:41:25 -04:00
throw osrm::util::exception(
"Feature geometry has a malformed coordinate with more than 2 values.");
}
else
{
2017-05-30 12:47:02 -04:00
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.");
}
}
}
2017-06-02 14:19:44 -04:00
inline void validateFeature(const rapidjson::Value &feature)
2017-05-30 12:47:02 -04:00
{
if (!feature.HasMember("type"))
{
throw osrm::util::exception("Feature is missing type member.");
2017-06-01 12:41:25 -04:00
}
else if (!feature["type"].IsString())
2017-05-30 12:47:02 -04:00
{
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.");
2017-06-01 12:41:25 -04:00
}
else if (!feature["geometry"].GetObject()["type"].IsString())
{
2017-05-30 12:47:02 -04:00
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.");
2017-06-01 12:41:25 -04:00
}
else if (!feature["geometry"].GetObject()["coordinates"].IsArray())
{
2017-05-30 12:47:02 -04:00
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.");
}
} // namespace util
} // namespace osrm
2017-05-30 12:47:02 -04:00
#endif // OSRM_GEOJSON_VALIDATION_HPP