Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial link #16

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ jobs:
- name: Move Profiles
if: matrix.config.mode == 'Release'
run: |
cd deps\tiles
git reset --hard HEAD
cd ..\..
mkdir dist
Copy-Item .\deps\tiles\profile dist\tiles-profiles -Recurse
mv .\build\motis.exe dist
Expand Down
4 changes: 3 additions & 1 deletion exe/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "motis/endpoints/elevators.h"
#include "motis/endpoints/footpaths.h"
#include "motis/endpoints/graph.h"
#include "motis/endpoints/initial.h"
#include "motis/endpoints/levels.h"
#include "motis/endpoints/matches.h"
#include "motis/endpoints/osr_routing.h"
Expand Down Expand Up @@ -61,7 +62,8 @@ int server(data d, config const& c) {
POST<ep::graph>(qr, "/api/graph", d);
POST<ep::update_elevator>(qr, "/api/update_elevator", d);
GET<ep::footpaths>(qr, "/api/debug/footpaths", d);
GET<ep::levels>(qr, "/api/v1/levels", d);
GET<ep::levels>(qr, "/api/v1/map/levels", d);
GET<ep::initial>(qr, "/api/v1/map/initial", d);
GET<ep::reverse_geocode>(qr, "/api/v1/reverse-geocode", d);
GET<ep::geocode>(qr, "/api/v1/geocode", d);
GET<ep::routing>(qr, "/api/v1/plan", d);
Expand Down
16 changes: 16 additions & 0 deletions include/motis/endpoints/initial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "boost/url/url.hpp"

#include "motis-api/motis-api.h"
#include "motis/fwd.h"

namespace motis::ep {

struct initial {
api::initial_response operator()(boost::urls::url_view const&) const;

nigiri::timetable const& tt_;
};

} // namespace motis::ep
11 changes: 11 additions & 0 deletions include/motis/timetable/initial_permalink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

#include "motis/fwd.h"

namespace motis {

std::string get_initial_permalink(nigiri::timetable const&);

} // namespace motis
30 changes: 29 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,35 @@ paths:
The next page is a set of itineraries departing AFTER the last itinerary in this result.
type: string

/api/v1/levels:
/api/v1/map/initial:
get:
tags:
- map
operationId: initial
summary: initial location to view the map at after loading based on where public transport should be visible
responses:
'200':
description: latitude, longitude and zoom level to set the map to
content:
application/json:
schema:
type: object
required:
- lat
- lon
- zoom
properties:
lat:
description: latitude
type: number
lon:
description: longitude
type: number
zoom:
description: zoom level
type: number

/api/v1/map/levels:
get:
tags:
- map
Expand Down
59 changes: 59 additions & 0 deletions src/endpoints/initial.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "motis/endpoints/initial.h"

#include "utl/erase_if.h"
#include "utl/to_vec.h"

#include "tiles/fixed/convert.h"
#include "tiles/fixed/fixed_geometry.h"

#include "nigiri/timetable.h"

namespace n = nigiri;

namespace motis::ep {

api::initial_response initial::operator()(boost::urls::url_view const&) const {
auto const get_quantiles = [](std::vector<double>&& coords) {
utl::erase_if(coords, [](auto const c) { return c == 0.; });
if (coords.empty()) {
return std::make_pair(0., 0.);
}
if (coords.size() < 10) {
return std::make_pair(coords.front(), coords.back());
}

std::sort(begin(coords), end(coords));
constexpr auto const kQuantile = .8;
return std::make_pair(
coords.at(static_cast<double>(coords.size()) * (1 - kQuantile)),
coords.at(static_cast<double>(coords.size()) * (kQuantile)));
};

auto const [lat_min, lat_max] = get_quantiles(utl::to_vec(
tt_.locations_.coordinates_, [](auto const& s) { return s.lat_; }));
auto const [lng_min, lng_max] = get_quantiles(utl::to_vec(
tt_.locations_.coordinates_, [](auto const& s) { return s.lng_; }));

auto const fixed0 = tiles::latlng_to_fixed({lat_min, lng_min});
auto const fixed1 = tiles::latlng_to_fixed({lat_max, lng_max});

auto const center = tiles::fixed_to_latlng(
{(fixed0.x() + fixed1.x()) / 2, (fixed0.y() + fixed1.y()) / 2});

auto const d = static_cast<unsigned>(std::max(
std::abs(fixed0.x() - fixed1.x()), std::abs(fixed0.y() - fixed1.y())));

auto zoom = 0U;
for (; zoom < (tiles::kMaxZoomLevel - 1); ++zoom) {
if (((tiles::kTileSize * 2ULL) *
(1ULL << (tiles::kMaxZoomLevel - (zoom + 1)))) < d) {
break;
}
}

return {.lat_ = center.lat_,
.lon_ = center.lng_,
.zoom_ = static_cast<double>(zoom)};
}

} // namespace motis::ep
3 changes: 3 additions & 0 deletions src/timetable/initial_permalink.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "motis/timetable/initial_permalink.h"

namespace motis {} // namespace motis
37 changes: 2 additions & 35 deletions ui/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!
Generate OpenAPI client:

```bash
# create a new project in the current directory
npm create svelte@latest

# create a new project in my-app
npm create svelte@latest my-app
npx @hey-api/openapi-ts -i ../openapi.yaml -o src/lib/openapi -c @hey-api/client-fetch
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
26 changes: 17 additions & 9 deletions ui/src/lib/map/Map.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
let ctx = $state<{ map: maplibregl.Map | undefined }>({ map: undefined });
setContext('map', ctx);

$effect(() => {
if (style != currStyle && ctx.map) {
ctx.map.setStyle(style);
}
});

let currentCenter = center;
const createMap = (container: HTMLElement) => {
map = new maplibregl.Map({ container, zoom, center, style, transformRequest });

Expand Down Expand Up @@ -75,8 +68,23 @@
};

$effect(() => {
if (center != currentCenter) {
map?.setCenter(center);
if (style != currStyle && ctx.map) {
ctx.map.setStyle(style);
}
});

let currentZoom = zoom;
$effect(() => {
if (map && zoom != currentZoom) {
map.setZoom(zoom);
currentZoom = zoom;
}
});

let currentCenter = center;
$effect(() => {
if (map && center != currentCenter) {
map.setCenter(center);
currentCenter = center;
}
});
Expand Down
12 changes: 10 additions & 2 deletions ui/src/lib/openapi/services.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts

import { createClient, createConfig, type Options } from '@hey-api/client-fetch';
import type { ReverseGeocodeData, ReverseGeocodeError, ReverseGeocodeResponse, GeocodeData, GeocodeError, GeocodeResponse, TripData, TripError, TripResponse, StoptimesData, StoptimesError, StoptimesResponse, PlanData, PlanError, PlanResponse, LevelsData, LevelsError, LevelsResponse, FootpathsData, FootpathsError, FootpathsResponse } from './types.gen';
import type { ReverseGeocodeData, ReverseGeocodeError, ReverseGeocodeResponse, GeocodeData, GeocodeError, GeocodeResponse, TripData, TripError, TripResponse, StoptimesData, StoptimesError, StoptimesResponse, PlanData, PlanError, PlanResponse, InitialError, InitialResponse, LevelsData, LevelsError, LevelsResponse, FootpathsData, FootpathsError, FootpathsResponse } from './types.gen';

export const client = createClient(createConfig());

Expand Down Expand Up @@ -45,12 +45,20 @@ export const plan = <ThrowOnError extends boolean = false>(options: Options<Plan
url: '/api/v1/plan'
}); };

/**
* initial location to view the map at after loading based on where public transport should be visible
*/
export const initial = <ThrowOnError extends boolean = false>(options?: Options<unknown, ThrowOnError>) => { return (options?.client ?? client).get<InitialResponse, InitialError, ThrowOnError>({
...options,
url: '/api/v1/map/initial'
}); };

/**
* Get all available levels for a map section
*/
export const levels = <ThrowOnError extends boolean = false>(options: Options<LevelsData, ThrowOnError>) => { return (options?.client ?? client).get<LevelsResponse, LevelsError, ThrowOnError>({
...options,
url: '/api/v1/levels'
url: '/api/v1/map/levels'
}); };

/**
Expand Down
17 changes: 17 additions & 0 deletions ui/src/lib/openapi/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,23 @@ export type PlanResponse = ({

export type PlanError = unknown;

export type InitialResponse = ({
/**
* latitude
*/
lat: number;
/**
* longitude
*/
lon: number;
/**
* zoom level
*/
zoom: number;
});

export type InitialError = unknown;

export type LevelsData = {
query: {
/**
Expand Down
16 changes: 14 additions & 2 deletions ui/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import SearchMask from './SearchMask.svelte';
import { posToLocation, type Location } from '$lib/Location';
import { Card } from '$lib/components/ui/card';
import { type Itinerary, plan, type PlanResponse, trip } from '$lib/openapi';
import { initial, type Itinerary, plan, type PlanResponse, trip } from '$lib/openapi';
import ItineraryList from './ItineraryList.svelte';
import ConnectionDetail from './ConnectionDetail.svelte';
import { Button } from '$lib/components/ui/button';
Expand All @@ -22,6 +22,7 @@
import { client } from '$lib/openapi';
import StopTimes from './StopTimes.svelte';
import { toDateTime } from '$lib/toDateTime';
import { onMount } from 'svelte';

const urlParams = browser && new URLSearchParams(window.location.search);
const hasDebug = urlParams && urlParams.has('debug');
Expand All @@ -36,11 +37,22 @@
document.documentElement.classList.add('dark');
}

let center = $state.raw<[number, number]>([8.652235, 49.876908]);
let level = $state(0);
let zoom = $state(15);
let bounds = $state<maplibregl.LngLatBoundsLike>();
let map = $state<maplibregl.Map>();

onMount(() => {
initial().then((d) => {
const r = d.data;
if (r) {
center = [r.lon, r.lat];
zoom = r.zoom;
}
});
});

let fromMarker = $state<maplibregl.Marker>();
let toMarker = $state<maplibregl.Marker>();
let from = $state<Location>({ label: '', value: {} });
Expand Down Expand Up @@ -147,7 +159,7 @@
return { url: `${client.getConfig().baseUrl}/tiles${url}` };
}
}}
center={[8.652235, 49.876908]}
{center}
class={cn('h-screen overflow-clip', theme)}
style={getStyle(theme, level)}
>
Expand Down
Loading