Skip to content

Commit

Permalink
Merge pull request #33 from naviqore/feature/NAV-160-create-gtfs-subs…
Browse files Browse the repository at this point in the history
…et-writer-app

Feature/nav 160 create gtfs subset writer app
  • Loading branch information
munterfi authored Aug 30, 2024
2 parents 4264bbc + 051bd0a commit d8619e0
Show file tree
Hide file tree
Showing 26 changed files with 520 additions and 560 deletions.
30 changes: 1 addition & 29 deletions .github/workflows/build-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest] # , clang-macos-latest
os: [windows-latest, ubuntu-latest]
include:
- os: windows-latest
compiler: msvc
- os: ubuntu-latest
compiler: gcc-linux
# - os: clang-macos-latest
# compiler: clang-macos

steps:
- name: Checkout code
Expand All @@ -33,32 +31,6 @@ jobs:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 60
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 60
- name: Cache Homebrew
if: matrix.os == 'clang-macos-latest'
uses: actions/[email protected]
with:
path: /usr/local/Homebrew
key: ${{ runner.os }}-homebrew-${{ hashFiles('**/Brewfile') }}
restore-keys: |
${{ runner.os }}-homebrew-
- name: Install Clang 14
if: matrix.os == 'clang-macos-latest'
run: |
brew update
brew install llvm@14
echo 'export PATH="/usr/local/opt/llvm@14/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/usr/local/opt/llvm@14/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/usr/local/opt/llvm@14/include"' >> ~/.zshrc
source ~/.zshrc
- name: Save Homebrew Cache
if: matrix.os == 'clang-macos-latest'
uses: actions/cache@v3
with:
path: /usr/local/Homebrew
key: ${{ runner.os }}-homebrew-${{ hashFiles('**/Brewfile') }}

- name: vcpkg installation
shell: bash
run: |
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ add_subdirectory(src/schedule/test)
add_subdirectory(src/schedule/benchmarks)
add_subdirectory(src/raptor)
add_subdirectory(src/raptor/test)
#TODO set up project in a new branch JIRA story
# add_subdirectory(gtfsRaptorConfig)
add_subdirectory(src/gtfsRaptorConfig)

include(CTest)
enable_testing()
37 changes: 10 additions & 27 deletions src/gtfsRaptorConfig/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
project(gtfsRaptorConfig)

add_executable(gtfsSubsetWriter
src/agencySubsetWriter/gtfsSubsetWriter.cpp
)

find_path(P_RANAV_CSV2_INCLUDE_DIRS "csv2/mio.hpp")

add_library(${PROJECT_NAME} SHARED )
add_library(${PROJECT_NAME} SHARED)

target_sources(${PROJECT_NAME} PRIVATE
src/GtfsToRaptorConverter.cpp
Expand All @@ -20,36 +14,23 @@ target_sources(${PROJECT_NAME} PRIVATE
include/SubRoute.h
)

target_include_directories(gtfsSubsetWriter PRIVATE ${CMAKE_SOURCE_DIR}/logging/include)
target_include_directories(gtfsSubsetWriter PRIVATE ${CMAKE_SOURCE_DIR}/gtfsRaptorConfig/src)
target_include_directories(gtfsSubsetWriter PRIVATE ${CMAKE_SOURCE_DIR}/gtfsRaptorConfig/include)
target_include_directories(gtfsSubsetWriter PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
target_include_directories(gtfsSubsetWriter PRIVATE ${P_RANAV_CSV2_INCLUDE_DIRS})

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/gtfsRaptorConfig/src)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/gtfsRaptorConfig/include) # check if public is needed
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/schedule/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/logging/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/gtfsRaptorConfig/src)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/gtfsRaptorConfig/include) # check if public is needed
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/schedule/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src/logging/include)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
target_include_directories(${PROJECT_NAME} PRIVATE src)
target_include_directories(${PROJECT_NAME} PRIVATE include)

target_link_libraries(${PROJECT_NAME} PRIVATE
logging
geometry
schedule
)

target_link_libraries(gtfsSubsetWriter PRIVATE
logging
geometry
schedule
)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

Expand All @@ -67,4 +48,6 @@ install(FILES
${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h
DESTINATION
${INCLUDE_INSTALL_DIR}
)
)

add_subdirectory(src/agencySubsetWriter)
14 changes: 5 additions & 9 deletions src/gtfsRaptorConfig/include/RoutePartitioner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
// Created by MichaelBrunner on 19/07/2024.
//

#ifndef ROUTEPARTITIONER_H
#define ROUTEPARTITIONER_H
#pragma once

#include "GtfsData.h"
#include "model/Route.h"

#include <functional>
#include <map>
#include <utility>
#include <gtfsRaptorConfig_export.h>
#include <unordered_map>
#include <vector>
#include <string>
#include "SubRoute.h"

namespace converter {
class SubRoute;

class GTFS_RAPTOR_API RoutePartitioner
{
Expand All @@ -40,5 +38,3 @@ namespace converter {

} // gtfs
// schedule

#endif //ROUTEPARTITIONER_H
16 changes: 3 additions & 13 deletions src/gtfsRaptorConfig/include/TimetableManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
// Created by MichaelBrunner on 13/06/2024.
//

#ifndef RELATIONMANAGER_H
#define RELATIONMANAGER_H
#pragma once

#include "GtfsData.h"
#include "RoutePartitioner.h"

#include <vector>
#include "model/StopTime.h"
#include "model/Trip.h"
#include "model/Stop.h"
#include "model/Route.h"

#include <iostream>
#include <gtfsRaptorConfig_export.h>
#include <ranges>

namespace converter {

class GTFS_RAPTOR_API TimetableManager
{
class GTFS_RAPTOR_API TimetableManager {
std::unique_ptr<schedule::gtfs::GtfsData> data;
std::unique_ptr<RoutePartitioner> routePartitioner;

Expand All @@ -34,7 +28,7 @@ namespace converter {

[[nodiscard]] RoutePartitioner& getRoutePartitioner();

[[nodiscard]] const RoutePartitioner& getRoutePartitioner() const ;
[[nodiscard]] const RoutePartitioner& getRoutePartitioner() const;

[[nodiscard]] const std::string& getStopNameFromStopId(std::string const& aStopId) const;

Expand All @@ -56,13 +50,9 @@ namespace converter {

[[nodiscard]] const schedule::gtfs::Trip& getTripsFromStopTimeTripId(std::string const& aTripId) const;

[[nodiscard]] std::vector<std::string> getVisitedStopIds() const;

private:
void createRelations() const;
void buildTripsToRoutesRelations() const;
void buildStopTimesToTripsAndRoutesRelations() const;
};
} // gtfs

#endif //RELATIONMANAGER_H
44 changes: 26 additions & 18 deletions src/gtfsRaptorConfig/src/RoutePartitioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
namespace converter {

RoutePartitioner::RoutePartitioner(schedule::gtfs::GtfsData* data)
: data(data) {
: data(data)
{

// auto routes = data->routes | std::views::values;
// std::vector<Route> routeList(routes.begin(), routes.end());
Expand All @@ -26,31 +27,40 @@ namespace converter {
});
}

std::vector<SubRoute> RoutePartitioner::getSubRoutes(std::string const& routeId) const {
std::vector<SubRoute> RoutePartitioner::getSubRoutes(std::string const& routeId) const
{
const auto& subRoutesForRoute = subRoutes.at(routeId);
#if defined(__cpp_lib_ranges_to_container)
return std::ranges::views::values(subRoutesForRoute) | std::ranges::to<std::vector<SubRoute>>();
#else
std::vector<SubRoute> subRoutesVector;
std::ranges::transform(subRoutesForRoute, std::back_inserter(subRoutesVector), [](const auto& pair) {
return pair.second;
});
return subRoutesVector;
#endif
}

const SubRoute& RoutePartitioner::getSubRoute(std::string const& tripId) const {
const SubRoute& RoutePartitioner::getSubRoute(std::string const& tripId) const
{
const auto route = data->routes.at(tripId);
const auto& subRoutesForRoute = subRoutes.at(route.routeId);
const auto key = generateStopSequenceKey(tripId);
return subRoutesForRoute.at(key);
}

void RoutePartitioner::processRoute(schedule::gtfs::Route const& route) {
void RoutePartitioner::processRoute(schedule::gtfs::Route const& route)
{
std::unordered_map<std::string, SubRoute> sequenceKeyToSubRoute;
sequenceKeyToSubRoute.reserve(data->routes.at(route.routeId).trips.size());

for (const auto& tripId : data->routes.at(route.routeId).trips)
{
for (const auto& tripId : data->routes.at(route.routeId).trips) {
const schedule::gtfs::Trip& trip = data->trips.at(tripId);
auto key = this->generateStopSequenceKey(tripId);

auto [it, inserted] = sequenceKeyToSubRoute.try_emplace(key,
SubRoute(std::format("{}_sr{}", route.routeId, sequenceKeyToSubRoute.size() + 1), route.routeId, key, extractStopSequence(trip)));
if (!inserted)
{
if (!inserted) {
it->second.addTrip(trip);
}
}
Expand All @@ -77,20 +87,18 @@ namespace converter {
// subRoutes.insert_or_assign(route.routeId, sequenceKeyToSubRoute);
// }

std::string RoutePartitioner::generateStopSequenceKey(const std::string& tripId) const {
std::string RoutePartitioner::generateStopSequenceKey(const std::string& tripId) const
{
const auto range = data->trips.at(tripId).stopTimes;
std::string sequenceKey;
sequenceKey.reserve(std::distance(range.begin(), range.end()) * 401); // 400 + 1 for "_"

bool first = true;
for (auto it = range.begin(); it != range.end(); ++it)
{
if (!first)
{
for (auto it = range.begin(); it != range.end(); ++it) {
if (!first) {
sequenceKey += "_";
}
else
{
else {
first = false;
}
sequenceKey += it->stopId;
Expand Down Expand Up @@ -120,13 +128,13 @@ namespace converter {
// return sequenceKey;
// }

std::vector<schedule::gtfs::Stop> RoutePartitioner::extractStopSequence(schedule::gtfs::Trip const& aTrip) const {
std::vector<schedule::gtfs::Stop> RoutePartitioner::extractStopSequence(schedule::gtfs::Trip const& aTrip) const
{
std::vector<schedule::gtfs::Stop> stops;
const auto range = data->trips.at(aTrip.tripId).stopTimes;
stops.reserve(std::distance(range.begin(), range.end()));

for (const auto& it : range)
{
for (const auto& it : range) {
stops.push_back(data->stops.at(it.stopId));
}
return stops;
Expand Down
Loading

0 comments on commit d8619e0

Please sign in to comment.