~ihabunek/triglav

90eeef6192112b608f633a8cfe01cf83dba9bebe — Ivan Habunek 10 months ago d258b57
Add task to load a pbf file
A lib/mix/tasks/triglav/osmosis_load.ex => lib/mix/tasks/triglav/osmosis_load.ex +14 -0
@@ 0,0 1,14 @@
defmodule Mix.Tasks.Triglav.OsmosisLoad do
  use Mix.Task

  @shortdoc "(re)Creates the Osmosis schema and loads a given pbf file"

  @impl Mix.Task
  def run([pbf_path]) do
    Application.put_env(:triglav, :minimal, true)
    {:ok, _} = Application.ensure_all_started(:triglav)

    Triglav.Import.Osmosis.create_schema()
    Triglav.Import.Osmosis.load_file(pbf_path)
  end
end

M lib/triglav/import/geofabrik.ex => lib/triglav/import/geofabrik.ex +23 -0
@@ 22,6 22,29 @@ defmodule Triglav.Import.Geofabrik do
    end
  end

  @spec file_state(String.t()) :: {:ok, map()} | {:error, term}
  def file_state(path) do
    case System.cmd("osmium", ["fileinfo", path]) do
      {out, 0} ->
        terms =
          out
          |> String.split("\n")
          |> Enum.map(&String.trim/1)
          |> Enum.map(&String.split(&1, "="))

        with [_, seq] <-
               Enum.find(terms, &(List.first(&1) == "osmosis_replication_sequence_number")),
             [_, ts] <- Enum.find(terms, &(List.first(&1) == "osmosis_replication_timestamp")),
             {sequence_number, ""} <- Integer.parse(seq),
             {:ok, timestamp, 0} <- DateTime.from_iso8601(ts) do
          {:ok, %{sequence_number: sequence_number, timestamp: timestamp}}
        end

      _ ->
        {:error, :osmium_failed}
    end
  end

  @spec local_state() :: OsmState.t() | nil
  def local_state() do
    case Repo.get(OsmState, 1) do

M lib/triglav/import/osmosis.ex => lib/triglav/import/osmosis.ex +14 -2
@@ 17,8 17,8 @@ defmodule Triglav.Import.Osmosis do
  # https://wiki.openstreetmap.org/wiki/Osmosis/PostGIS_Setup
  @scripts [
    "priv/osmosis/pgsnapshot_schema_0.6.sql",
    # "priv/osmosis/pgsnapshot_schema_0.6_action.sql",
    # "priv/osmosis/pgsnapshot_schema_0.6_changes.sql",
    "priv/osmosis/pgsnapshot_schema_0.6_action.sql",
    "priv/osmosis/pgsnapshot_schema_0.6_changes.sql",
    "priv/osmosis/pgsnapshot_schema_0.6_linestring.sql"
  ]



@@ 66,6 66,18 @@ defmodule Triglav.Import.Osmosis do
    end
  end

  def load_file(pbf_path) do
    Logger.info("Loading: #{pbf_path}")

    with {:ok, state} <- Geofabrik.file_state(pbf_path),
         :ok <- cmd("osmosis", ["--read-pbf", pbf_path, "--write-pgsql" | osmosis_db_params()]),
         {:ok, state} <- Geofabrik.save_state(state) do
      Logger.info("Loaded state: #{state.sequence_number} #{state.timestamp}")
      Logger.info("Done")
      :ok
    end
  end

  @spec update() :: :ok | {:error, term}
  def update() do
    Logger.info("Checking Geofabrik for updates...")