summaryrefslogtreecommitdiffstats
path: root/server/src/datex2.cpp
blob: 4c8000b93bcda8f1b709be4b287d9642764136ca (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
module;

#include <boost/geometry/algorithms/is_empty.hpp>
#include <boost/geometry/srs/epsg.hpp>
#include <boost/geometry/srs/transformation.hpp>

#include <pugixml.hpp>

module routemon:datex2$impl;

import :datex2;

using namespace std::literals::string_view_literals;

namespace routemon::datex2 {

auto parse_timestamp(char const* in) -> std::optional<time::timestamp>
{
  auto res = time::timestamp{};
  auto is = std::istringstream{in};
  is >> std::chrono::parse("%Y-%m-%dT%H:%M:%SZ", res);
  return is.fail() ? std::nullopt : std::make_optional(res);
}

auto loader::add_location_from_xml(
    road_closure& rc, pugi::xml_node const& loc_xml) -> void
{
  auto loc_xml_type = std::string_view{loc_xml.attribute("xsi:type").value()};
  if (loc_xml_type == "loc:ItineraryByIndexedLocations")
  {
    for (auto const loc_cont_xml :
         loc_xml.children("loc:locationContainedInItinerary"))
    {
      add_location_from_xml(rc, loc_cont_xml.child("loc:location"));
    }
  }
  else if (loc_xml_type == "loc:LinearLocation"
           || loc_xml_type == "loc:SingleRoadLinearLocation")
  {
    auto const& loc_gml_xml = loc_xml.child("loc:gmlLineString");
    if (!loc_gml_xml)
      return;

    auto const srs_name =
        std::string_view{loc_gml_xml.attribute("srsName").value()};
    if (srs_name != "WGS 84"sv)
    {
      warnings_.insert(
          std::format("don't now how to handle the CRS {}", srs_name));
      return;
    }
    auto const pos_list_str =
        std::string_view{loc_gml_xml.child_value("loc:posList")};
    // lat1 long1 lat2 long2 ... lat(n-1) long(n-1) latn longn

    auto ls = std::make_shared<geo::linestring>();

    auto lat_set = false;
    auto lat = 0.0;
    for (auto const lat_or_long_str : std::views::split(pos_list_str, " "sv))
    {
      auto mlat_or_long = util::parse_double(std::string_view{lat_or_long_str});
      if (!mlat_or_long)
      {
        warnings_.insert(
            std::format(
                "failed to parse coordinate {:?}",
                std::string_view{lat_or_long_str}));
        return;
      }

      if (!lat_set)
      {
        lat = *mlat_or_long;
        lat_set = true;
      }
      else
      {
        bgeo::append(*ls, geo::point{*mlat_or_long, lat});
        lat = 0;
        lat_set = false;
      }
    }

    if (bgeo::is_empty(*ls))
    {
      warnings_.emplace("empty line string in data set");
      return;
    }

    rc.relevant_line_strings.push_back(ls);
  }
  else if (loc_xml_type == "loc:PointLocation")
  {
    auto const& coords_xml =
        loc_xml.child("loc:pointByCoordinates").child("loc:pointCoordinates");
    if (!coords_xml)
      return;

    auto mlat = util::parse_double(coords_xml.child_value("loc:latitude"));
    auto mlon = util::parse_double(coords_xml.child_value("loc:longitude"));
    if (!mlat || !mlon)
    {
      warnings_.emplace("failed to parse PointLocation coordinates");
      return;
    }

    // Vaag genoeg zegt NDW dat het hier om WGS 84 gaat:
    //   https://docs.ndw.nu/en/dataformaten/datex2-v3/elementen/locationreferencing/pointCoordinates/
    // maar heeft het UML-model van DATEX II v3 het over ETRS 89:
    //   https://docs.datex2.eu/_static/data/v3.7/umlmodel/html/EARoot/EA3/EA3/EA5/EA676.htm

    auto const coords_etrs89 = geo::point{*mlon, *mlat};
    auto coords_wgs84 = geo::point{};
    etrs89_to_wgs84_.forward(coords_etrs89, coords_wgs84);

    rc.relevant_points.push_back(coords_wgs84);
  }
  else
  {
    warnings_.insert(
        std::format(
            "don't know how to hande location of type {}, ignoring",
            loc_xml.attribute("xsi:type").value()));
    return;
  }
}

auto loader::handle_road_or_carriageway_or_lane_management(
    pugi::xml_node const& record_xml, std::weak_ptr<situation> parent)
    -> std::optional<std::shared_ptr<road_closure>>
{
  auto const type = std::string_view{
    record_xml.child("sit:roadOrCarriagewayOrLaneManagementType").child_value()
  };
  if (type != "carriagewayClosures" && type != "roadClosed")
    // TODO: checken of er nog andere types fietsers de doorgang zouden
    // kunnen blokkeren?
    return std::nullopt;

  auto const& restricted_vehicle_types_xml =
      record_xml.child("sit:forVehiclesWithCharacteristicsOf");
  bool likely_restriction_for_bikes = restricted_vehicle_types_xml.empty();
  for (auto const vehicle_type_xml :
       restricted_vehicle_types_xml.children("com:vehicleType"))
  {
    auto vehicle_type = std::string_view{vehicle_type_xml.child_value()};
    if (vehicle_type == "anyVehicle" || vehicle_type == "bicycle"
        || vehicle_type == "unknown" || vehicle_type == "other")
    {
      likely_restriction_for_bikes = true;
    }
  }
  if (!likely_restriction_for_bikes)
    return std::nullopt;

  //---- Check if within the defined validity period

  auto validity = std::optional<time::period_seq>{};
  auto const& validity_xml = record_xml.child("sit:validity");
  if (validity_xml
      && validity_xml.child_value("com:validityStatus")
             == "definedByValidityTimeSpec"sv)
  {
    auto const& validity_spec_xml =
        validity_xml.child("com:validityTimeSpecification");

    auto valid_periods = std::vector<time::period>{};
    auto exception_periods = std::vector<time::period>{};

    // TODO: com:overallEndTime may be missing (according to the DATEX
    // II v3 data model)
    auto const overall_start_time =
        parse_timestamp(validity_spec_xml.child_value("com:overallStartTime"));
    auto const overall_end_time =
        parse_timestamp(validity_spec_xml.child_value("com:overallEndTime"));
    if (overall_start_time && overall_end_time
        && *overall_start_time < *overall_end_time)
    {
      valid_periods.emplace_back(*overall_start_time, *overall_end_time);

      for (auto const valid_period_xml :
           validity_xml.children("com:validPeriod"))
      {
        auto const start_of_period =
            parse_timestamp(valid_period_xml.child_value("com:startOfPeriod"));
        auto const end_of_period =
            parse_timestamp(valid_period_xml.child_value("com:endOfPeriod"));
        if (start_of_period && end_of_period
            && *start_of_period < *end_of_period)
        {
          valid_periods.emplace_back(*start_of_period, *end_of_period);
        }
      }
      for (auto const exception_period_xml :
           validity_xml.children("com:exceptionPeriod"))
      {
        auto const start_of_period = parse_timestamp(
            exception_period_xml.child_value("com:startOfPeriod"));
        auto const end_of_period = parse_timestamp(
            exception_period_xml.child_value("com:endOfPeriod"));
        if (start_of_period && end_of_period
            && *start_of_period < *end_of_period)
        {
          exception_periods.emplace_back(*start_of_period, *end_of_period);
        }
      }

      validity =
          time::period_seq{valid_periods.begin(), valid_periods.end()}.except(
              time::period_seq{
                exception_periods.begin(), exception_periods.end()
              });
    }
    else
    {
      warnings_.insert(
          std::format(
              "invalid overall start / end time (start time: {}, end "
              "time: {})",
              validity_spec_xml.child_value("com:overallStartTime"),
              validity_spec_xml.child_value("com:overallEndTime")));
      return std::nullopt;
    }
  }

  //---- Try to extract the location info

  auto rc = std::make_shared<road_closure>(std::move(parent), validity);
  add_location_from_xml(*rc, record_xml.child("sit:locationReference"));
  return rc;
}

auto loader::load_situation_publication(std::string const& filename)
    -> situation_publication
{
  auto doc = pugi::xml_document{};
  if (auto result = doc.load_file(filename.c_str()); !result)
  {
    throw std::runtime_error{result.description()};
  }
  auto payload_xml = doc.child("mc:messageContainer").child("mc:payload");
  auto mpublication_time =
      parse_timestamp(payload_xml.child_value("com:publicationTime"));
  if (!mpublication_time)
    throw std::runtime_error{
      "provided publication does not name publication time"
    };

  auto situations = std::vector<std::shared_ptr<situation>>{};
  for (auto const sit_xml : payload_xml.children("sit:situation"))
  {
    auto id = std::string_view{sit_xml.attribute("id").value()};

    auto const sit = std::make_shared<situation>(std::string{id});
    situations.push_back(sit);

    auto const& header_info_xml = sit_xml.child("sit:headerInformation");
    if (header_info_xml.child_value("com:informationStatus") != "real"sv)
      continue;

    for (auto const record_xml : sit_xml.children("sit:situationRecord"))
    {
      auto const record_type =
          std::string_view{record_xml.attribute("xsi:type").value()};
      auto const primary_record_types = std::unordered_set<std::string_view>{
        "sit:Roadworks",
        /* { */ "sit:MaintenanceWorks",
        /* | */ "sit:ConstructionWorks",
        /* } */
        "sit:Obstruction",
        /* { */ "sit:EnvironmentalObstruction",
        /* | */ "sit:GeneralObstruction",
        /* | */ "sit:InfrastructureDamageObstruction",
        /* } */
        "sit:Activity",
        /* { */ "sit:PublicEvent",
        /* } */
      };

      if (record_type == "sit:RoadOrCarriagewayOrLaneManagement")
      {
        if (auto rc =
                handle_road_or_carriageway_or_lane_management(record_xml, sit))
        {
          sit->road_closures.push_back(*rc);
        }
      }
      else if (primary_record_types.contains(record_type))
      {
        for (auto const comment_xml :
             record_xml.children("sit:generalPublicComment"))
        {
          //            if
          //            (comment_xml.child_value("sit:commentType")
          //            == "internalNote"sv) {
          auto candidate = std::optional<
              std::pair<std::string_view, std::string_view>
          >{}; // (text, language)
          for (auto const comment_value_xml : comment_xml.child("sit:comment")
                                                  .child("com:values")
                                                  .children("com:value"))
          {
            if (!candidate
                || comment_value_xml.attribute("lang").value() == "nl"sv
                || (candidate->second != "nl"sv
                    && comment_value_xml.attribute("lang").value() == "nl"sv))
            {
              candidate = std::make_pair(
                  comment_value_xml.child_value(),
                  comment_value_xml.attribute("lang").value());
            }
          }
          if (candidate)
          {
            auto already_present = false;
            for (auto const& comment : sit->comments)
              already_present = already_present || comment == candidate->first;
            if (!already_present)
            {
              sit->comments.emplace_back(candidate->first);
            }
          }
          //            }
        }

        if (auto const location_ref_xml =
                record_xml.child("sit:locationReference"))
        {
          if (location_ref_xml.attribute("xsi:type").value()
              == "loc:PointLocation"sv)
          {
            if (auto const coords_xml =
                    location_ref_xml.child("loc:pointByCoordinates")
                        .child("loc:pointCoordinates"))
            {
              auto const mlat =
                  util::parse_double(coords_xml.child_value("loc:latitude"));
              auto const mlon =
                  util::parse_double(coords_xml.child_value("loc:longitude"));
              if (mlat && mlon)
              {
                // Vaag genoeg zegt NDW dat het hier om WGS
                // 84 gaat:
                //   https://docs.ndw.nu/en/dataformaten/datex2-v3/elementen/locationreferencing/pointCoordinates/
                // maar heeft het UML-model van DATEX II v3
                // het over ETRS 89:
                //   https://docs.datex2.eu/_static/data/v3.7/umlmodel/html/EARoot/EA3/EA3/EA5/EA676.htm

                auto const coords_etrs89 = geo::point{*mlon, *mlat};
                auto coords_wgs84 = geo::point{};
                etrs89_to_wgs84_.forward(coords_etrs89, coords_wgs84);

                if (!sit->location)
                {
                  sit->location = coords_wgs84;
                }
              }
            }
          }
        }
      }
    }
  }

  return {
    .publication_time = *mpublication_time,
    .situations = situations,
  };
}

auto loader::warnings() const -> std::multiset<std::string> const&
{
  return warnings_;
}

} // namespace routemon::datex2