Skip to content

Commit

Permalink
enable timetable only routing and geocoding (#669)
Browse files Browse the repository at this point in the history
* enable timetable only routing and geocoding

* wip

* wip

* wip

* wip

* wip

* wip

* wip
  • Loading branch information
felixguendling authored Nov 24, 2024
1 parent b2debb3 commit 513933a
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[adr]
[email protected]:triptix-tech/adr.git
branch=master
commit=bd3bb6bb8f1b5c0e066acd975767df27d5de22f8
commit=28365ed6b64b804296a09cb19cf8527247687f04
[googletest]
[email protected]:motis-project/googletest.git
branch=master
Expand Down
4 changes: 2 additions & 2 deletions .pkg.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
5061172279391244024
18178785441693657313
cista 847b27100b7e730370b810ce62206a66b0bf2d79
zlib-ng 68ab3e2d80253ec5dc3c83691d9ff70477b32cd3
boost 930f38eb0365ceb7853273e03da4d9e7787abfb9
Expand Down Expand Up @@ -46,5 +46,5 @@ reflect-cpp c54fe66de4650b60c23aadd4a06d9db4ffeda22f
FTXUI dd6a5d371fd7a3e2937bb579955003c54b727233
tg 20c0f298b8ce58de29a790290f44dca7c4ecc364
utf8proc 779b780da3b99d123133eb99707b65c7e4324cc8
adr bd3bb6bb8f1b5c0e066acd975767df27d5de22f8
adr 28365ed6b64b804296a09cb19cf8527247687f04
openapi-cpp 6b5fd40a2b552fc5656aeff56f203a34c1d9e9af
2 changes: 1 addition & 1 deletion include/motis/adr_extend_tt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace motis {

void adr_extend_tt(nigiri::timetable const&,
adr::area_database const&,
adr::area_database const*,
adr::typeahead&);

} // namespace motis
3 changes: 0 additions & 3 deletions include/motis/parse_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ namespace motis {
std::optional<osr::location> parse_location(std::string_view,
char separator = ',');

nigiri::unixtime_t get_date_time(std::optional<std::string> const& date = {},
std::optional<std::string> const& time = {});

date::sys_days parse_iso_date(std::string_view);

nigiri::routing::query cursor_to_query(std::string_view);
Expand Down
6 changes: 6 additions & 0 deletions include/motis/street_routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ using street_routing_cache_key_t = std::
using street_routing_cache_t =
hash_map<street_routing_cache_key_t, std::optional<osr::path>>;

api::Itinerary dummy_itinerary(api::Place const& from,
api::Place const& to,
api::ModeEnum,
nigiri::unixtime_t const start_time,
nigiri::unixtime_t const end_time);

api::Itinerary route(osr::ways const&,
osr::lookup const&,
gbfs::gbfs_data const*,
Expand Down
26 changes: 17 additions & 9 deletions src/adr_extend_tt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ constexpr auto const kClaszMax =
static_cast<std::underlying_type_t<n::clasz>>(n::kNumClasses);

void adr_extend_tt(nigiri::timetable const& tt,
a::area_database const& area_db,
a::area_database const* area_db,
a::typeahead& t) {
if (tt.n_locations() == 0) {
return;
Expand Down Expand Up @@ -127,6 +127,10 @@ void adr_extend_tt(nigiri::timetable const& tt,

// Add to typeahead.
auto areas = std::basic_string<a::area_idx_t>{};
auto no_areas_idx = adr::area_set_idx_t{t.area_sets_.size()};
if (area_db == nullptr) {
t.area_sets_.emplace_back(areas);
}
for (auto const [prio, l] : utl::zip(importance, place_location)) {
auto const str_idx = a::string_idx_t{t.strings_.size()};
auto const place_idx = a::place_idx_t{t.place_names_.size()};
Expand All @@ -147,14 +151,18 @@ void adr_extend_tt(nigiri::timetable const& tt,
t.string_to_type_.emplace_back(
std::initializer_list<a::location_type_t>{a::location_type_t::kPlace});

area_db.lookup(
t, a::coordinates::from_latlng(tt.locations_.coordinates_[l]), areas);
t.place_areas_.emplace_back(
utl::get_or_create(area_set_lookup, areas, [&]() {
auto const set_idx = a::area_set_idx_t{t.area_sets_.size()};
t.area_sets_.emplace_back(areas);
return set_idx;
}));
if (area_db == nullptr) {
t.place_areas_.emplace_back(no_areas_idx);
} else {
area_db->lookup(
t, a::coordinates::from_latlng(tt.locations_.coordinates_[l]), areas);
t.place_areas_.emplace_back(
utl::get_or_create(area_set_lookup, areas, [&]() {
auto const set_idx = a::area_set_idx_t{t.area_sets_.size()};
t.area_sets_.emplace_back(areas);
return set_idx;
}));
}
}

t.build_ngram_index();
Expand Down
5 changes: 0 additions & 5 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ config config::read(std::string const& s) {
}

void config::verify() const {
utl::verify(!geocoding_ || osm_,
"feature GEOCODING requires OpenStreetMap data");
utl::verify(!reverse_geocoding_ || (geocoding_ && osm_),
"feature REVERSE_GEOCODING requires OpenStreetMap data and "
"feature GEOCODING");
utl::verify(!tiles_ || osm_, "feature TILES requires OpenStreetMap data");
utl::verify(!street_routing_ || osm_,
"feature STREET_ROUTING requires OpenStreetMap data");
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ api::plan_response routing::operator()(boost::urls::url_view const& url) const {

UTL_START_TIMING(direct);
auto const [direct, fastest_direct] =
t.has_value() && !direct_modes.empty()
t.has_value() && !direct_modes.empty() && w_ && l_
? route_direct(e, gbfs.get(), from_p, to_p, direct_modes, *t,
query.wheelchair_,
std::chrono::seconds{query.maxDirectTime_})
Expand Down
103 changes: 61 additions & 42 deletions src/import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "osr/ways.h"

#include "adr/adr.h"
#include "adr/area_database.h"
#include "adr/reverse.h"
#include "adr/typeahead.h"

Expand Down Expand Up @@ -185,6 +186,10 @@ data import(config const& c, fs::path const& data_path, bool const write) {
[&]() { return c.geocoding_ || c.reverse_geocoding_; },
[]() { return true; },
[&]() {
if (!c.osm_) {
return;
}

adr::extract(*c.osm_, data_path / "adr", data_path / "adr");

// We can't use d.load_geocoder() here because
Expand All @@ -198,6 +203,10 @@ data import(config const& c, fs::path const& data_path, bool const write) {
}
},
[&]() {
if (!c.osm_) {
return;
}

// Same here, need to load base-line version for adr_extend!
d.t_ = adr::read(data_path / "adr" / "t.bin");
d.tc_ = std::make_unique<adr::cache>(d.t_->strings_.size(), 100U);
Expand Down Expand Up @@ -298,48 +307,58 @@ data import(config const& c, fs::path const& data_path, bool const write) {
},
{tt_hash, n_version()}};

auto adr_extend =
task{"adr_extend",
[&]() { return c.geocoding_ && c.timetable_.has_value(); },
[&]() { return d.tt_ && d.t_; },
[&]() {
auto const area_db = adr::area_database{
data_path / "adr", cista::mmap::protection::READ};
adr_extend_tt(*d.tt_, area_db, *d.t_);
if (write) {
cista::write(data_path / "adr" / "t_ext.bin", *d.t_);
}
d.r_.reset();
{
auto r = adr::reverse{data_path / "adr",
cista::mmap::protection::WRITE};
r.build_rtree(*d.t_);
r.write();
}
d.t_.reset();
if (c.geocoding_) {
d.load_geocoder();
}
if (c.reverse_geocoding_) {
d.load_reverse_geocoder();
}
},
[&]() {
d.t_.reset();
d.r_.reset();
if (c.geocoding_) {
d.load_geocoder();
}
if (c.reverse_geocoding_) {
d.load_reverse_geocoder();
}
},
{tt_hash,
osm_hash,
adr_version(),
n_version(),
{"geocoding", c.geocoding_},
{"reverse_geocoding", c.reverse_geocoding_}}};
auto adr_extend = task{
"adr_extend",
[&]() { return c.timetable_.has_value(); },
[&]() { return d.tt_.get() != nullptr; },
[&]() {
auto const area_db = d.t_ ? (std::optional<adr::area_database>{
std::in_place, data_path / "adr",
cista::mmap::protection::READ})
: std::nullopt;
if (!d.t_) {
d.t_ = cista::wrapped<adr::typeahead>{
cista::raw::make_unique<adr::typeahead>()};
}
adr_extend_tt(*d.tt_, area_db.has_value() ? &*area_db : nullptr, *d.t_);
if (write) {
cista::write(data_path / "adr" / "t_ext.bin", *d.t_);
}
d.r_.reset();
{
auto r =
adr::reverse{data_path / "adr", cista::mmap::protection::WRITE};
r.build_rtree(*d.t_);
r.write();
}
d.t_.reset();
if (c.geocoding_) {
d.load_geocoder();
}
if (c.reverse_geocoding_) {
d.load_reverse_geocoder();
}
},
[&]() {
if (d.t_) {
d.t_.reset();
}
if (d.r_) {
d.r_.reset();
}
if (c.geocoding_) {
d.load_geocoder();
}
if (c.reverse_geocoding_) {
d.load_reverse_geocoder();
}
},
{tt_hash,
osm_hash,
adr_version(),
n_version(),
{"geocoding", c.geocoding_},
{"reverse_geocoding", c.reverse_geocoding_}}};

auto osr_footpath = task{
"osr_footpath",
Expand Down
11 changes: 7 additions & 4 deletions src/journey_to_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ api::Itinerary journey_to_response(osr::ways const* w,
}
},
[&](n::footpath) {
append(route(*w, *l, gbfs, e, from, to, api::ModeEnum::WALK,
wheelchair, j_leg.dep_time_, j_leg.arr_time_,
gbfs_provider_idx_t::invalid(), cache, blocked_mem,
std::chrono::seconds{900}));
append(w && l
? route(*w, *l, gbfs, e, from, to, api::ModeEnum::WALK,
wheelchair, j_leg.dep_time_, j_leg.arr_time_,
gbfs_provider_idx_t::invalid(), cache,
blocked_mem, std::chrono::seconds{900})
: dummy_itinerary(from, to, api::ModeEnum::WALK,
j_leg.dep_time_, j_leg.arr_time_));
},
[&](n::routing::offset const x) {
append(route(
Expand Down
26 changes: 0 additions & 26 deletions src/parse_location.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,6 @@ std::optional<osr::location> parse_location(std::string_view s,
return osr::location{pos, osr::level_t{level}};
}

n::unixtime_t get_date_time(std::optional<std::string> const& date,
std::optional<std::string> const& time) {
if (!date.has_value()) {
utl::verify(!time.has_value(), "time without date no supported");
return std::chrono::time_point_cast<n::i32_minutes>(
std::chrono::system_clock::now());
} else {
utl::verify(time.has_value(), "date without time not supported");
auto const date_time = *date + " " + *time;

// 06-28-2024 7:06pm
// 06-28-2024 19:06
std::stringstream ss;
ss << date_time;

auto t = n::unixtime_t{};
if (date_time.contains("AM") || date_time.contains("PM")) {
ss >> date::parse("%m-%d-%Y %I:%M %p", t);
} else {
ss >> date::parse("%m-%d-%Y %H:%M", t);
}

return t;
}
}

date::sys_days parse_iso_date(std::string_view s) {
auto d = date::sys_days{};
(std::stringstream{} << s) >> date::parse("%F", d);
Expand Down
45 changes: 26 additions & 19 deletions src/street_routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ struct sharing {
};
};

api::Itinerary dummy_itinerary(api::Place const& from,
api::Place const& to,
api::ModeEnum const mode,
n::unixtime_t const start_time,
n::unixtime_t const end_time) {
auto itinerary = api::Itinerary{
.duration_ = std::chrono::duration_cast<std::chrono::seconds>(end_time -
start_time)
.count(),
.startTime_ = start_time,
.endTime_ = end_time};
auto& leg = itinerary.legs_.emplace_back(api::Leg{
.mode_ = mode,
.from_ = from,
.to_ = to,
.duration_ = std::chrono::duration_cast<std::chrono::seconds>(end_time -
start_time)
.count(),
.startTime_ = start_time,
.endTime_ = end_time});
leg.from_.departure_ = leg.startTime_;
leg.to_.arrival_ = leg.endTime_;
return itinerary;
}

api::Itinerary route(osr::ways const& w,
osr::lookup const& l,
gbfs::gbfs_data const* gbfs,
Expand Down Expand Up @@ -239,27 +264,9 @@ api::Itinerary route(osr::ways const& w,
if (!end_time.has_value()) {
return {};
}

std::cout << "ROUTING\n FROM: " << from << " \n TO: " << to
<< "\n -> CREATING DUMMY LEG\n";
auto itinerary = api::Itinerary{
.duration_ = std::chrono::duration_cast<std::chrono::seconds>(
*end_time - start_time)
.count(),
.startTime_ = start_time,
.endTime_ = *end_time};
auto& leg = itinerary.legs_.emplace_back(
api::Leg{.mode_ = mode,
.from_ = from,
.to_ = to,
.duration_ = std::chrono::duration_cast<std::chrono::seconds>(
*end_time - start_time)
.count(),
.startTime_ = start_time,
.endTime_ = *end_time});
leg.from_.departure_ = leg.startTime_;
leg.to_.arrival_ = leg.endTime_;
return itinerary;
return dummy_itinerary(from, to, mode, start_time, *end_time);
}

auto itinerary = api::Itinerary{
Expand Down
5 changes: 0 additions & 5 deletions test/read_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ TEST(motis, parse_location_no_level) {
EXPECT_EQ((osr::location{{-23.1, 45.2}, osr::level_t{0.F}}), *parsed);
}

TEST(motis, parse_date_time) {
auto const t = get_date_time("06-28-2024", "7:06 PM");
EXPECT_EQ(sys_days{2024_y / June / 28} + 19h + 6min, t);
}

TEST(motis, parse_cursor_earlier) {
auto const q = cursor_to_query("EARLIER|1720036560");

Expand Down
13 changes: 8 additions & 5 deletions ui/src/lib/AddressTypeahead.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@
return;
}
items = (
await geocode<true>({
query: { text: inputValue, language }
})
).data.map((match: Match): Location => {
const { data: matches, error } = await geocode({
query: { text: inputValue, language }
});
if (error) {
console.error('TYPEAHEAD ERROR: ', error);
return;
}
items = matches!.map((match: Match): Location => {
return {
label: getLabel(match),
value: { match, precision: GEOCODER_PRECISION }
Expand Down
Loading

0 comments on commit 513933a

Please sign in to comment.