Skip to content
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

Speed up hole nesting #20

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/cover
/deps
/doc
/priv
erl_crash.dump
*.ez
16 changes: 9 additions & 7 deletions lib/exshape.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ defmodule Exshape do

defp open_file(c, size), do: File.stream!(c, [], size)

defp zip(nil, nil), do: []
defp zip(nil, d), do: Dbf.read(d)
defp zip(s, nil), do: Shp.read(s)
defp zip(s, d), do: Stream.zip(Shp.read(s), Dbf.read(d))
defp zip(nil, nil, _), do: []
defp zip(nil, d, _), do: Dbf.read(d)
defp zip(s, nil, opts), do: Shp.read(s, opts)
defp zip(s, d, opts), do: Stream.zip(Shp.read(s, opts), Dbf.read(d))

defp unzip!(path, cwd, false), do: :zip.extract(to_charlist(path), cwd: cwd)
defp unzip!(path, cwd, true) do
Expand Down Expand Up @@ -78,12 +78,13 @@ defmodule Exshape do
end
open_file(Path.join(cwd, file), size)
end
})
},
opts)
end
end

@spec from_filesystem(Filesystem.t) :: [layer]
def from_filesystem(fs) do
def from_filesystem(fs, opts \\ []) do
fs.list.()
|> Enum.filter(&keep_file?/1)
|> Enum.map(fn {:zip_file, filename, _, _, _, _} -> filename end)
Expand All @@ -110,7 +111,8 @@ defmodule Exshape do
# zip up the unzipped shp and dbf components
stream = zip(
shp && fs.stream.(shp),
dbf && fs.stream.(dbf)
dbf && fs.stream.(dbf),
opts
)

{Path.basename(root), prj_contents, stream}
Expand Down
35 changes: 25 additions & 10 deletions lib/exshape/shp.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
defmodule Exshape.Shp do
require Rustler
use Rustler, otp_app: :exshape, crate: :exshape_shape, mode: :release

defmodule State do
@enforce_keys [:nest_polygon]
defstruct mode: :header,
shape_type: nil,
emit: [],
to_read: nil,
item: nil,
part_index: 0,
measures: [],
z_values: []
z_values: [],
nest_polygon: nil
end

@magic_nodata_num :math.pow(10, 38) * -1
Expand Down Expand Up @@ -111,8 +116,8 @@ defmodule Exshape.Shp do
%{s | measures: [], z_values: []}
end

defp emit(s, %Polygon{} = p) do
%{s | mode: :record_header, emit: [%{p | points: nest_polygon(p)} | s.emit]}
defp emit(%State{nest_polygon: nest_polygon} = s, %Polygon{} = p) do
%{s | mode: :record_header, emit: [%{p | points: nest_polygon.(p)} | s.emit]}
end

defp emit(s, %Polyline{} = p) do
Expand All @@ -134,9 +139,9 @@ defmodule Exshape.Shp do
%{s | mode: :record_header, emit: [polylinem | s.emit]} |> reset_unzipped
end

defp emit(s, %PolygonM{} = pm) do
defp emit(%State{nest_polygon: nest_polygon} = s, %PolygonM{} = pm) do
p = zip_measures(pm, s)
polylinem = %{p | points: nest_polygon(p)}
polylinem = %{p | points: nest_polygon.(p)}
%{s | mode: :record_header, emit: [polylinem | s.emit]} |> reset_unzipped
end

Expand All @@ -158,12 +163,12 @@ defmodule Exshape.Shp do
%{s | mode: :record_header, emit: [polylinez | s.emit]} |> reset_unzipped
end

defp emit(s, %PolygonZ{} = pz) do
defp emit(%State{nest_polygon: nest_polygon} = s, %PolygonZ{} = pz) do
p = pz
|> zip_measures(s)
|> zip_zvals(s)

polygonz = %{p | points: nest_polygon(p)}
polygonz = %{p | points: nest_polygon.(p)}
%{s | mode: :record_header, emit: [polygonz | s.emit]} |> reset_unzipped
end

Expand Down Expand Up @@ -218,8 +223,13 @@ defmodule Exshape.Shp do
parts
end

def native_nest_polygon(p) do
{:ok, r} = native_nest_polygon_impl(unflatten_parts(p))
r
end
defp native_nest_polygon_impl(_p), do: throw :nif_not_loaded

def nest_polygon(p) do
def beam_nest_polygon(p) do
{polys, holes} = unflatten_parts(p) |> Enum.split_with(&is_clockwise?/1)

Enum.reduce(holes, Enum.map(polys, fn p -> [p] end), fn hole, polys ->
Expand Down Expand Up @@ -754,8 +764,13 @@ defmodule Exshape.Shp do
|> Stream.run
```
"""
def read(byte_stream) do
Stream.transform(byte_stream, {<<>>, %State{}}, fn bin, {buf, state} ->
def read(byte_stream, opts \\ []) do
native = Keyword.get(opts, :native, true)

state = %State{
nest_polygon: if(native, do: &native_nest_polygon/1, else: &beam_nest_polygon/1)
}
Stream.transform(byte_stream, {<<>>, state}, fn bin, {buf, state} ->
case do_read(state, buf <> bin) do
{_, %State{mode: :done}} = s -> {:halt, s}
{buf, %State{emit: emit} = s} -> {Enum.reverse(emit), {buf, %{s | emit: []}}}
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Exshape.Mixfile do
# Type "mix help compile.app" for more information
def application do
# Specify extra applications you'll use from Erlang/Elixir
[extra_applications: [:logger]]
[extra_applications: [:logger, :rustler]]
end

# Dependencies can be Hex packages:
Expand All @@ -49,7 +49,8 @@ defmodule Exshape.Mixfile do
[
{:elixir_uuid, "~> 1.2"},
{:ex_doc, ">= 0.0.0", only: :dev},
{:poison, "~> 3.1", only: :test}
{:poison, "~> 3.1", only: :test},
{:rustler, "~> 0.22.0"},
]
end
end
16 changes: 9 additions & 7 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
%{
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.8.0", "1204a2f5b4f181775a0e456154830524cf2207cf4f9112215c05e0b76e4eca8b", [:mix], [{:makeup, "~> 0.5.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.2.2", "d526b23bdceb04c7ad15b33c57c4526bf5f50aaa70c7c141b4b4624555c68259", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm", "c57508ddad47dfb8038ca6de1e616e66e9b87313220ac5d9817bc4a4dc2257b9"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm", "e4d6e26434471761ed45a3545239da87af7b70904dd4442a55f87d06b137c56b"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "dc87f778d8260da0189a622f62790f6202af72f2f3dee6e78d91a18dd2fcd137"},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "259748a45dfcf5f49765a7c29c9594791c82de23e22d7a3e6e59533fe8e8935b"},
"makeup_elixir": {:hex, :makeup_elixir, "0.8.0", "1204a2f5b4f181775a0e456154830524cf2207cf4f9112215c05e0b76e4eca8b", [:mix], [{:makeup, "~> 0.5.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "393d17c5a648e3b30522b2a4743bd1dc3533e1227c8c2823ebe8c3a8e5be5913"},
"nimble_parsec": {:hex, :nimble_parsec, "0.2.2", "d526b23bdceb04c7ad15b33c57c4526bf5f50aaa70c7c141b4b4624555c68259", [:mix], [], "hexpm", "4ababf5c44164f161872704e1cfbecab3935fdebec66c72905abaad0e6e5cef6"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},
"rustler": {:hex, :rustler, "0.22.0", "e2930f9d6933e910f87526bb0a7f904e32b62a7e838a3ca4a884ee7fdfb957ed", [:mix], [{:toml, "~> 0.5.2", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "01f5989dd511ebec09be481e07d3c59773d5373c5061e09d3ebc3ef61811b49d"},
"toml": {:hex, :toml, "0.5.2", "e471388a8726d1ce51a6b32f864b8228a1eb8edc907a0edf2bb50eab9321b526", [:mix], [], "hexpm", "f1e3dabef71fb510d015fad18c0e05e7c57281001141504c6b69d94e99750a07"},
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm"},
}
1 change: 1 addition & 0 deletions native/exshape_shape/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
158 changes: 158 additions & 0 deletions native/exshape_shape/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions native/exshape_shape/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "exshape_shape"
version = "0.1.0"
authors = []
edition = "2018"

[lib]
name = "exshape_shape"
path = "src/lib.rs"
crate-type = ["dylib"]

[dependencies]
rustler = "0.22.0"
rustler_codegen = "0.22.0"
itertools = "0.10"
derivative = "2.1"
float_extras = "0.1"
Loading