Skip to content

Commit

Permalink
Ignore virtual stations/stations with invalid coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti committed Nov 25, 2024
1 parent 2c07663 commit 82486f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/transport/lib/jobs/geo_data/gbfs_stations_to_geo_data.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Transport.Jobs.GBFSStationsToGeoData do
@moduledoc """
Job in charge of importing GBFS stations data (docks for bikes usually) to the `geo_data` table.
It ignores virtual stations and stations with invalid coordinates.
"""
use Oban.Worker, max_attempts: 3
import Ecto.Query
Expand Down Expand Up @@ -33,7 +35,9 @@ defmodule Transport.Jobs.GBFSStationsToGeoData do
{:feed_exists, true} <- {:feed_exists, not is_nil(feed_url)},
{:ok, %HTTPoison.Response{status_code: 200, body: body}} <- http_client().get(feed_url),
{:ok, json} <- Jason.decode(body) do
Enum.map(json["data"]["stations"], fn station ->
json["data"]["stations"]
|> Enum.reject(&(virtual_station?(&1) or missing_coordinates?(&1)))
|> Enum.map(fn station ->
%{
geo_data_import_id: geo_data_import_id,
geom: %Geo.Point{coordinates: {station["lon"], station["lat"]}, srid: 4326},
Expand All @@ -48,6 +52,15 @@ defmodule Transport.Jobs.GBFSStationsToGeoData do
end
end

defp virtual_station?(%{"is_virtual_station" => true}), do: true
defp virtual_station?(%{}), do: false

defp missing_coordinates?(%{"lat" => lat, "lon" => lon}) do
is_nil(lon) or is_nil(lat)
end

defp missing_coordinates?(%{}), do: true

# From GBFS 1.1 until 2.3
defp station_name(%{"name" => name}) when is_binary(name), do: name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ defmodule Transport.Jobs.GBFSStationsToGeoDataTest do
"text" => "Gare"
}
]
},
# Ignored: virtual station
%{
"capacity" => 10,
"is_virtual_station" => true,
"lat" => 2,
"lon" => 1,
"name" => [
%{
"language" => "fr",
"text" => "Bistrot"
}
]
},
# Ignored: latitude is nil
%{
"capacity" => 10,
"lat" => nil,
"lon" => 1,
"name" => [
%{
"language" => "fr",
"text" => "Pub"
}
]
},
# Ignored: no coordinates
%{
"capacity" => 10,
"name" => [
%{
"language" => "fr",
"text" => "Bar"
}
]
}
]
}
Expand Down

0 comments on commit 82486f7

Please sign in to comment.