-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype de convertisseur NeTEx -> GeoJSON
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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) | ||
|> 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}) | ||
end |