Skip to content

Commit

Permalink
Carte d'exploration : affiche les stations GBFS
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Nov 26, 2024
1 parent a0a52d1 commit 4a76056
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 91 deletions.
27 changes: 25 additions & 2 deletions apps/transport/client/javascripts/explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ function getTooltip ({ object, layer }) {
return { html: `<strong>Parking relais</strong><br>${object.properties.nom}<br>Capacité : ${object.properties.nb_pr} places` }
} else if (layer.id === 'zfe-layer') {
return { html: '<strong>Zone à Faible Émission</strong>' }
} else if (layer.id === 'gbfs_stations-layer') {
return {
html: `<strong>Station GBFS</strong><br>
${object.properties.name}<br>
Capacité&nbsp;: ${object.properties.capacity}`
}
} else if (layer.id === 'irve-layer') {
return {
html: `<strong>Infrastructure de recharge</strong><br>
Expand All @@ -76,14 +82,15 @@ function getTooltip ({ object, layer }) {
}
}
// internal dictionary were all layers are stored
const layers = { gtfsrt: {}, bnlc: undefined, parkings_relais: undefined, zfe: undefined }
const layers = { gtfsrt: {}, bnlc: undefined, parkings_relais: undefined, zfe: undefined, gbfs_stations: undefined }

function getLayers (layers) {
const layersArray = Object.values(layers.gtfsrt)
layersArray.push(layers.bnlc)
layersArray.push(layers.parkings_relais)
layersArray.push(layers.zfe)
layersArray.push(layers.irve)
layersArray.push(layers.gbfs_stations)
return layersArray
}

Expand Down Expand Up @@ -154,6 +161,17 @@ document.getElementById('irve-check').addEventListener('change', (event) => {
}
})

// Handle GBFS stations toggle
document.getElementById('gbfs_stations-check').addEventListener('change', (event) => {
if (event.currentTarget.checked) {
trackEvent('gbfs-stations')
fetch('/api/geo-query?data=gbfs_stations')
.then(data => updateGBFSStationsLayer(data.json()))
} else {
updateGBFSStationsLayer(null)
}
})

function updateBNLCLayer (geojson) {
layers.bnlc = createPointsLayer(geojson, 'bnlc-layer')
deckGLLayer.setProps({ layers: getLayers(layers) })
Expand All @@ -170,6 +188,10 @@ function updateIRVELayer (geojson) {
layers.irve = createPointsLayer(geojson, 'irve-layer')
deckGLLayer.setProps({ layers: getLayers(layers) })
}
function updateGBFSStationsLayer (geojson) {
layers.gbfs_stations = createPointsLayer(geojson, 'gbfs_stations-layer')
deckGLLayer.setProps({ layers: getLayers(layers) })
}

function trackEvent (layer) {
// https://matomo.org/faq/reports/implement-event-tracking-with-matomo/#how-to-set-up-matomo-event-tracking-with-javascript
Expand All @@ -184,7 +206,8 @@ function createPointsLayer (geojson, id) {
'bnlc-layer': [255, 174, 0, 100],
'parkings_relais-layer': [0, 33, 70, 100],
'zfe-layer': [52, 8, 143, 100],
'irve-layer': [245, 40, 145, 100]
'irve-layer': [245, 40, 145, 100],
'gbfs_stations-layer': [60, 115, 168, 100]
}[id]

return new GeoJsonLayer({
Expand Down
3 changes: 3 additions & 0 deletions apps/transport/client/stylesheets/components/_explore.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@
#irve-check:checked {
background-color: rgb(245, 40, 145);
}
#gbfs_stations-check:checked {
background-color: rgb(60, 115, 168);
}
}
14 changes: 0 additions & 14 deletions apps/transport/lib/db/geo_data/geo_data_import.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule DB.GeoDataImport do
"""
use Ecto.Schema
use TypedEctoSchema
import Ecto.Query

typed_schema "geo_data_import" do
belongs_to(:resource_history, DB.ResourceHistory)
Expand All @@ -13,17 +12,4 @@ defmodule DB.GeoDataImport do

timestamps(type: :utc_datetime_usec)
end

@doc """
takes a dataset_id as input, return the latest geo_data_import done for that dataset
"""
def dataset_latest_geo_data_import(dataset_id) do
DB.ResourceHistory
|> join(:inner, [rh], g in DB.GeoDataImport, on: rh.id == g.resource_history_id)
|> where([rh, _g], fragment("(payload->>'dataset_id')::bigint") == ^dataset_id)
|> order_by([rh, _g], desc: rh.inserted_at)
|> limit(1)
|> select([_rh, g], g)
|> DB.Repo.one()
end
end
4 changes: 2 additions & 2 deletions apps/transport/lib/jobs/geo_data/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ defmodule Transport.Jobs.BaseGeoData do
# For a static resource, associated to a consolidated dataset (BNLC, BNZFE, IRVE etc)
# We rely on the relevant `DB.ResourceHistory` to determine if we should replace the data.
def import_replace_data(slug, prepare_data_for_insert_fn) when slug in @consolidated_datasets_slugs do
%DB.Resource{id: resource_id, dataset_id: dataset_id} = Transport.ConsolidatedDataset.resource(slug)
%DB.Resource{id: resource_id} = Transport.ConsolidatedDataset.resource(slug)
latest_resource_history = DB.ResourceHistory.latest_resource_history(resource_id)
current_geo_data_import = DB.GeoDataImport.dataset_latest_geo_data_import(dataset_id)
current_geo_data_import = DB.Repo.get_by(DB.GeoDataImport, slug: slug)

if needs_import?(latest_resource_history, current_geo_data_import) do
Logger.info("New content detected...update content")
Expand Down
3 changes: 1 addition & 2 deletions apps/transport/lib/transport/stats_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ defmodule Transport.StatsHandler do
end

def count_geo_data_lines(feature) do
Transport.ConsolidatedDataset.dataset(feature).id
|> DB.GeoDataImport.dataset_latest_geo_data_import()
DB.Repo.get_by(DB.GeoDataImport, slug: feature)
|> DB.GeoData.count_lines_for_geo_data_import()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ defmodule TransportWeb.API.GeoQueryController do
use TransportWeb, :controller
import Ecto.Query

def index(%Plug.Conn{} = conn, %{"data" => slug}) do
feature_atom = slug |> String.to_atom()
@possible_slugs Ecto.Enum.dump_values(DB.GeoDataImport, :slug)

if feature_atom in Transport.ConsolidatedDataset.geo_data_datasets() do
dataset = Transport.ConsolidatedDataset.dataset(feature_atom)
def index(%Plug.Conn{} = conn, %{"data" => slug}) when slug in @possible_slugs do
case DB.Repo.get_by(DB.GeoDataImport, slug: slug) do
nil ->
render_404(conn)

get_geojson = fn ->
dataset
|> Map.fetch!(:id)
|> DB.GeoDataImport.dataset_latest_geo_data_import()
|> transform_geojson(feature_atom)
end

geojson = Transport.Cache.fetch("#{slug}_data", get_geojson, :timer.hours(1))
conn |> json(geojson)
else
render_404(conn)
%DB.GeoDataImport{} = geo_data_import ->
get_geojson = fn -> transform_geojson(geo_data_import, String.to_existing_atom(slug)) end
geojson = Transport.Cache.fetch("#{slug}_data", get_geojson, :timer.hours(1))
conn |> json(geojson)
end
end

Expand Down Expand Up @@ -61,4 +55,14 @@ defmodule TransportWeb.API.GeoQueryController do

DB.GeoData.geo_data_as_geojson(geo_data_import, add_fields)
end

def transform_geojson(%DB.GeoDataImport{} = geo_data_import, :gbfs_stations) do
add_fields = fn query ->
from(g in query,
select_merge: %{capacity: fragment("payload->>'capacity'"), name: fragment("payload->>'name'")}
)
end

DB.GeoData.geo_data_as_geojson(geo_data_import, add_fields)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<% parcs_relais_link = dataset_path(@conn, :details, @parkings_relais_dataset.slug) %>
<% zfe_link = dataset_path(@conn, :details, @zfe_dataset.slug) %>
<% irve_link = dataset_path(@conn, :details, @irve_dataset.slug) %>
<% bike_scooter_sharing_datasets_link = dataset_path(@conn, :index, type: "bike-scooter-sharing") %>
<% car_motorbike_sharing_datasets_link = dataset_path(@conn, :index, type: "car-motorbike-sharing") %>
<div class="container dataset-page-top">
<h2><%= dgettext("explore", "Transport Explore") %></h2>

Expand Down Expand Up @@ -78,6 +80,20 @@
) %>
</details>
</div>
<div class="checkbox-explore pb-24">
<input id="gbfs_stations-check" name="gbfs_stations" type="checkbox" value="true" />
<details>
<summary>
<%= dgettext("explore", "GBFS stations") %>
</summary>
<%= raw(
dgettext("explore", "gbfs-stations-explanation",
bike_scooter_sharing_datasets_link: bike_scooter_sharing_datasets_link,
car_motorbike_sharing_datasets_link: car_motorbike_sharing_datasets_link
)
) %>
</details>
</div>
</div>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions apps/transport/priv/gettext/en/LC_MESSAGES/explore.po
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ msgstr "Consolidated national map of charging stations static %{link}."
#, elixir-autogen, elixir-format
msgid "IRVE map"
msgstr "Electric Vehicule Charging Infrastructure database"

#, elixir-autogen, elixir-format
msgid "GBFS stations"
msgstr "Stations for shared vehicles"

#, elixir-autogen, elixir-format
msgid "gbfs-stations-explanation"
msgstr "This map consolidates all stations for shared vehicles. It contains <a href=\"%{bike_scooter_sharing_datasets_link}\">bikes and scooters</a> and <a href=\"%{car_motorbike_sharing_datasets_link}\">cars and motorbikes</a>."
8 changes: 8 additions & 0 deletions apps/transport/priv/gettext/explore.pot
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "IRVE map"
msgstr ""

#, elixir-autogen, elixir-format
msgid "GBFS stations"
msgstr ""

#, elixir-autogen, elixir-format
msgid "gbfs-stations-explanation"
msgstr ""
8 changes: 8 additions & 0 deletions apps/transport/priv/gettext/fr/LC_MESSAGES/explore.po
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ msgstr "Carte du %{link} consolidé des IRVE (données statiques)."
#, elixir-autogen, elixir-format
msgid "IRVE map"
msgstr "Infrastructures de Recharge des Véhicules Électriques (IRVE)"

#, elixir-autogen, elixir-format
msgid "GBFS stations"
msgstr "Stations pour véhicules partagés"

#, elixir-autogen, elixir-format
msgid "gbfs-stations-explanation"
msgstr "Cette carte rassemble toutes les stations pour les véhicules partagés. Elle contient les données des <a href=\"%{bike_scooter_sharing_datasets_link}\">vélos et trotinettes</a> et <a href=\"%{car_motorbike_sharing_datasets_link}\">voitures et scooters</a> en libre-service."
34 changes: 0 additions & 34 deletions apps/transport/test/db/geo_data_import_test.exs

This file was deleted.

2 changes: 1 addition & 1 deletion apps/transport/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ defmodule DB.Factory do

def insert_imported_irve_geo_data(dataset_id) do
%{id: resource_history_id} = insert(:resource_history, %{payload: %{"dataset_id" => dataset_id}})
%{id: geo_data_import_id} = insert(:geo_data_import, %{resource_history_id: resource_history_id})
%{id: geo_data_import_id} = insert(:geo_data_import, %{slug: :irve, resource_history_id: resource_history_id})

insert(:geo_data, %{
geo_data_import_id: geo_data_import_id,
Expand Down
Loading

0 comments on commit 4a76056

Please sign in to comment.