-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: add bulk upload of shapes #986
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4586aa9
feat: basics of kml upload
meagharty 4a1721c
fix: shapes schema use :timestamptz (default is naive_datetime)
meagharty 534edcd
tests: update tests for shape bulk upload
meagharty 3bdb23b
lint: fix format
meagharty 1304c1f
test: add additional error checking to tests, update types
meagharty 1c00842
fix: fix migrations from shape table already existing via upstream br…
meagharty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,80 @@ | ||
defmodule Arrow.Shuttle.ShapeUpload do | ||
@moduledoc "schema for shape upload" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@type t :: %__MODULE__{ | ||
filename: String.t(), | ||
shapes: list(Arrow.Shuttle.Shape.t()) | ||
} | ||
|
||
embedded_schema do | ||
field :filename, :string | ||
embeds_many :shapes, Arrow.Shuttle.Shape | ||
end | ||
|
||
@doc false | ||
def changeset(shape, attrs) do | ||
shape | ||
|> cast(attrs, [:filename]) | ||
|> cast_embed(:shapes) | ||
end | ||
|
||
def parse_kml_from_file(shape_upload) do | ||
file = shape_upload["filename"] | ||
filename = file.filename | ||
|
||
with {:ok, shapes_kml} <- File.read(file.path), | ||
{:ok, shapes} <- parse_kml(shapes_kml) do | ||
{:ok, shapes} | ||
else | ||
{:error, exception} -> | ||
message = | ||
if is_atom(exception), | ||
do: :file.format_error(exception), | ||
else: Exception.message(exception) | ||
|
||
{:error, | ||
{"Failed to upload shapes from #{filename} because the provided xml was invalid", | ||
[message]}} | ||
end | ||
end | ||
|
||
@doc """ | ||
Parses a KML shape into a map | ||
""" | ||
@spec parse_kml(String.t()) :: {:ok, map} | {:error, any} | ||
def parse_kml(kml) do | ||
SAXMap.from_string(kml) | ||
end | ||
|
||
@doc """ | ||
Parses one or many Shapes from a map of the KML/XML | ||
""" | ||
@spec shapes_from_kml(map) :: {:ok, list(Arrow.Shuttle.Shape.t())} | {:error, any} | ||
def shapes_from_kml(saxy_shapes) do | ||
placemarks = saxy_shapes["kml"]["Folder"]["Placemark"] | ||
|
||
case placemarks do | ||
%{"LineString" => %{"coordinates" => coords}, "name" => name} | ||
when is_binary(coords) -> | ||
{:ok, [%{name: name, coordinates: String.split(coords)}]} | ||
|
||
%{"LineString" => %{"coordinates" => nil}, "name" => _name} -> | ||
error = | ||
{"Failed to parse shape from kml, no coordinates were found. Check your whitespace.", | ||
[inspect(placemarks)]} | ||
|
||
{:error, error} | ||
|
||
_ -> | ||
# Multiple placemarks, only capture Placemarks with LineString | ||
placemarks = Enum.filter(placemarks, fn pm -> pm["LineString"] end) | ||
|
||
{:ok, | ||
Enum.map(placemarks, fn pm -> | ||
%{name: pm["name"], coordinates: String.split(pm["LineString"]["coordinates"])} | ||
end)} | ||
end | ||
end | ||
end |
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
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
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
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
<.header> | ||
New Shapes | ||
<:subtitle>Use this form to manage shape records in your database.</:subtitle> | ||
</.header> | ||
|
||
<%= if Map.get(@shape_upload, "filename") do %> | ||
<.simple_form for={@shape_upload} action={~p"/shapes_upload"} multipart> | ||
<:actions> | ||
<.button>Back</.button> | ||
</:actions> | ||
</.simple_form> | ||
<% else %> | ||
<.simple_form :let={f} for={@shape_upload} action={~p"/shapes_upload"} multipart> | ||
<.input field={f[:filename]} type="file" label="Filename" required="true"/> | ||
<:actions> | ||
<.button>Upload File</.button> | ||
</:actions> | ||
</.simple_form> | ||
<% end %> | ||
<.back navigate={~p"/shapes"}>Back to shapes</.back> |
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
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
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
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
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
15 changes: 15 additions & 0 deletions
15
priv/repo/migrations/20240611001158_add_coordinates_to_shapes.exs
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,15 @@ | ||
defmodule Arrow.Repo.Migrations.AddCoordinatesToShapes do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:shapes) do | ||
add :coordinates, {:array, :string} | ||
end | ||
end | ||
|
||
def down do | ||
alter table(:shapes) do | ||
remove :coordinates | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -345,8 +345,9 @@ CREATE TABLE public.schema_migrations ( | |
CREATE TABLE public.shapes ( | ||
id bigint NOT NULL, | ||
name character varying(255), | ||
inserted_at timestamp(0) without time zone NOT NULL, | ||
updated_at timestamp(0) without time zone NOT NULL | ||
inserted_at timestamp with time zone NOT NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: I need to add a new migration for this change since the original scaffolding was already merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
updated_at timestamp with time zone NOT NULL, | ||
coordinates character varying(255)[] | ||
); | ||
|
||
|
||
|
@@ -753,3 +754,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20240207224211); | |
INSERT INTO public."schema_migrations" (version) VALUES (20240605185923); | ||
INSERT INTO public."schema_migrations" (version) VALUES (20240606171008); | ||
INSERT INTO public."schema_migrations" (version) VALUES (20240610185146); | ||
INSERT INTO public."schema_migrations" (version) VALUES (20240611001158); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be saved to the db as well as the kml file uploaded to s3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The designs for bulk upload for shapes is why I chose to include this in the db after all. There's versatility in having both the S3 files and the db representation of the coordinates.
It makes sense to have the kml files in S3 in terms of using the format used by other map/transit partners like Remix and Google Map (and gtfs_creator itself).
Since the ticket spec ended up calling for a bulk upload, with shape selection, shape re-naming, and coordinate consolidation, it seemed like making the backend aware (and able to later show/present) that data was worthwhile.
I see the concerns around the duplication though. What do you think?
cc @nlwstein @Whoops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is updated in #989 to drop the saving of coordinates to the database by using an embedded schema for the shapes file processing.