Skip to content

Commit

Permalink
Prototype de convertisseur NeTEx -> GeoJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitfred committed Oct 2, 2024
1 parent 28390d9 commit afe2fe9
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions apps/transport/lib/netex/geojson_converter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defmodule Transport.NeTEx.GeoJSONConverter do
@moduledoc """
A first converter to extract stop places and display it as a GeoJSON.
"""

@doc """
Reads a NeTEx zip archive stored on disk, and returns a GeoJSON.
"""
@spec convert(binary()) :: binary()
def convert(zip_file_name) do
{:ok, result} =
zip_file_name
|> Transport.NeTEx.read_all_stop_places()
|> Enum.reduce([], &append_relevant_stop_places/2)
|> Enum.map(&to_geojson_feature/1)
|> to_geojson_feature_collection()
|> Jason.encode()

result
end

@spec append_relevant_stop_places({binary(), list()}, list()) :: list()
defp append_relevant_stop_places(per_file, acc) do
acc ++ keep_stop_places_with_location(per_file)
end

@spec keep_stop_places_with_location({binary(), list()}) :: list()
defp keep_stop_places_with_location({_filename, stop_places}) do
Enum.filter(stop_places, &has_location?/1)
end

@spec has_location?(map()) :: boolean()
defp has_location?(stop_place) do
Map.has_key?(stop_place, :latitude) && Map.has_key?(stop_place, :longitude)
end

defp to_geojson_feature(%{latitude: latitude, longitude: longitude, name: name}) do
%{
type: "Feature",
geometry: %{
type: "Point",
coordinates: [longitude, latitude]
},
properties: %{
name: name
}
}
end

defp to_geojson_feature(%{id: id, latitude: latitude, longitude: longitude}),
do: to_geojson_feature(%{latitude: latitude, longitude: longitude, name: id})

defp to_geojson_feature_collection(features) do
%{
type: "FeatureCollection",
features: features
}
end
end

0 comments on commit afe2fe9

Please sign in to comment.