8 #include <boost/optional.hpp>
9 #include <nlohmann/json.hpp>
22 const std::string& file,
bool coord_frame_fail_silently) {
26 std::locale::global(std::locale::classic());
28 tinyxml2::XMLDocument doc;
29 doc.LoadFile(file.c_str());
30 auto* p_parcel = doc.RootElement();
32 if (p_parcel ==
nullptr) {
33 throw std::invalid_argument(
"File not found");
38 auto e_result = p_parcel->Attribute(
"id");
39 if (e_result !=
nullptr) {
43 auto* p_field = p_parcel->FirstChildElement(
"Field");
45 auto* p_polygon = p_field\
46 ->FirstChildElement(
"geometry")\
47 ->FirstChildElement(
"gml:Polygon");
49 std::string coord_sys = p_polygon->Attribute(
"srsName");
51 std::string p_coords = std::string(
52 p_polygon->FirstChildElement(
"gml:outerBoundaryIs")\
53 ->FirstChildElement(
"gml:LinearRing")\
54 ->FirstChildElement(
"gml:coordinates")\
56 auto findAndReplaceAll =
57 [](std::string& data, std::string toSearch, std::string replaceStr) {
58 size_t pos = data.find(toSearch);
59 while (pos != std::string::npos) {
60 data.replace(pos, toSearch.size(), replaceStr);
61 pos = data.find(toSearch, pos + replaceStr.size());
65 findAndReplaceAll(p_coords,
",",
";");
66 findAndReplaceAll(p_coords,
" ",
", ");
67 findAndReplaceAll(p_coords,
";",
" ");
68 p_coords =
"POLYGON ((" + p_coords +
"))";
69 OGRGeometry* new_geom{};
71 OGRGeometryFactory::createFromWkt(p_coords.c_str(), spt_ref.get(), &new_geom);
74 field.setCRS(coord_sys);
75 OGRGeometryFactory::destroyGeometry(new_geom);
84 }
else if (
ps.size() == 2) {
91 auto jsonToF2CRing = [] (
const json& json_ring) {
93 for (
auto&&
ps : json_ring) {
99 auto json_rings = imported_cell[
"geometry"][
"coordinates"];
100 for (
int i = 0; i < json_rings.size(); ++i) {
101 cell.addRing(jsonToF2CRing(json_rings[i]));
108 std::ifstream f(file);
109 json imported_field = json::parse(f);
111 for (
auto&& imported_cell : imported_field[
"features"]) {
114 imported_cell[
"properties"][
"Name"]));
120 std::ifstream f(file);
121 json imported_field = json::parse(f);
126 std::ifstream f(file);
127 json imported_swaths = json::parse(f);
129 for (
auto&& imported_swath : imported_swaths[
"features"]) {
131 for (
auto&&
ps : imported_swath[
"geometry"][
"coordinates"]) {
135 imported_swath[
"properties"][
"width"],
136 imported_swath[
"properties"][
"path_id"]);
142 std::ifstream f(file);
143 json imported_strips = json::parse(f);
145 for (
auto&& imported_strip : imported_strips[
"features"]) {
147 strip.
setName(imported_strip[
"properties"][
"crop_id"]);
149 strips.emplace_back(strip);