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

Add MultiLineStringZM #225

Open
wants to merge 1 commit 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A collection of GIS functions. Handles conversions to and from well-known text (
* MultiPointZ
* MultiLineString
* MultiLineStringZ
* MultiLineStringZM
* MultiPolygon
* MultiPolygonZ
* GeometryCollection
Expand Down
1 change: 1 addition & 0 deletions lib/geo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Geo do
| Geo.MultiPointZ.t()
| Geo.MultiLineString.t()
| Geo.MultiLineStringZ.t()
| Geo.MultiLineStringZM.t()
| Geo.MultiPolygon.t()
| Geo.MultiPolygonZ.t()
| Geo.GeometryCollection.t()
Expand Down
10 changes: 10 additions & 0 deletions lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Geo.JSON.Decoder do
MultiPoint,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ,
GeometryCollection
Expand Down Expand Up @@ -175,6 +176,15 @@ defmodule Geo.JSON.Decoder do
%MultiLineStringZ{coordinates: coordinates, srid: get_srid(crs), properties: properties}
end

defp do_decode("MultiLineStringZM", coordinates, properties, crs) do
coordinates =
Enum.map(coordinates, fn sub_coordinates ->
Enum.map(sub_coordinates, &List.to_tuple(&1))
end)

%MultiLineStringZM{coordinates: coordinates, srid: get_srid(crs), properties: properties}
end

defp do_decode("MultiPolygon", coordinates, properties, crs) do
coordinates =
Enum.map(coordinates, fn sub_coordinates ->
Expand Down
10 changes: 10 additions & 0 deletions lib/geo/json/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Geo.JSON.Encoder do
MultiPointZ,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ,
GeometryCollection
Expand Down Expand Up @@ -202,6 +203,15 @@ defmodule Geo.JSON.Encoder do
%{"type" => "MultiLineStringZ", "coordinates" => coordinates}
end

defp do_encode(%MultiLineStringZM{coordinates: coordinates}) do
coordinates =
Enum.map(coordinates, fn sub_coordinates ->
Enum.map(sub_coordinates, &Tuple.to_list(&1))
end)

%{"type" => "MultiLineStringZM", "coordinates" => coordinates}
end

defp do_encode(%MultiPolygon{coordinates: coordinates}) do
coordinates =
Enum.map(coordinates, fn sub_coordinates ->
Expand Down
12 changes: 12 additions & 0 deletions lib/geo/multi_line_stringzm.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Geo.MultiLineStringZM do
@moduledoc """
Defines the MultiLineStringZM struct.
"""

@type t :: %__MODULE__{
coordinates: [[{number, number, number, number}]],
srid: integer | nil,
properties: map
}
defstruct coordinates: [], srid: nil, properties: %{}
end
18 changes: 18 additions & 0 deletions lib/geo/wkb/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Geo.WKB.Decoder do
@multi_point_z 0x80_00_00_04
@multi_line_string 0x00_00_00_05
@multi_line_string_z 0x80_00_00_05
@multi_line_string_zm 0xC0_00_00_05
@multi_polygon 0x00_00_00_06
@multi_polygon_z 0x80_00_00_06
@geometry_collection 0x00_00_00_07
Expand All @@ -38,6 +39,7 @@ defmodule Geo.WKB.Decoder do
MultiPointZ,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ
}
Expand Down Expand Up @@ -299,6 +301,22 @@ defmodule Geo.WKB.Decoder do
{%MultiLineStringZ{coordinates: coordinates, srid: srid}, rest}
end

defp do_decode(
@multi_line_string_zm,
<<count::unquote(modifier)-32, rest::bits>>,
srid,
unquote(endian)
) do
{coordinates, rest} =
Enum.map_reduce(List.duplicate(1, count), rest, fn _, <<rest::bits>> ->
{%LineStringZM{coordinates: coordinates}, rest} = decode(rest)

{coordinates, rest}
end)

{%MultiLineStringZM{coordinates: coordinates, srid: srid}, rest}
end

defp do_decode(
@multi_polygon,
<<count::unquote(modifier)-32, rest::bits>>,
Expand Down
12 changes: 12 additions & 0 deletions lib/geo/wkb/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Geo.WKB.Encoder do
@multi_point_z 0x80_00_00_04
@multi_line_string 0x00_00_00_05
@multi_line_string_z 0x80_00_00_05
@multi_line_string_zm 0xC0_00_00_05
@multi_polygon 0x00_00_00_06
@multi_polygon_z 0x80_00_00_06
@geometry_collection 0x00_00_00_07
Expand All @@ -35,6 +36,7 @@ defmodule Geo.WKB.Encoder do
MultiPointZ,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ,
GeometryCollection
Expand Down Expand Up @@ -194,6 +196,16 @@ defmodule Geo.WKB.Encoder do
{@multi_line_string_z, [<<count::unquote(modifier)-32>> | coordinates]}
end

def do_encode(%MultiLineStringZM{coordinates: coordinates}, unquote(endian_atom)) do
{coordinates, count} =
Enum.map_reduce(coordinates, 0, fn coordinate, acc ->
geom = encode!(%LineStringZM{coordinates: coordinate}, unquote(endian_atom))
{geom, acc + 1}
end)

{@multi_line_string_zm, [<<count::unquote(modifier)-32>> | coordinates]}
end

def do_encode(%MultiPolygon{coordinates: coordinates}, unquote(endian_atom)) do
{coordinates, count} =
Enum.map_reduce(coordinates, 0, fn coordinate, acc ->
Expand Down
18 changes: 14 additions & 4 deletions lib/geo/wkt/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Geo.WKT.Decoder do
MultiPoint,
MultiPointZ,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ,
GeometryCollection
Expand Down Expand Up @@ -88,14 +90,22 @@ defmodule Geo.WKT.Decoder do
%MultiPointZ{coordinates: create_line_string(coordinates), srid: srid}
end

defp do_decode("MULTILINESTRING" <> coordinates, srid) do
%MultiLineString{coordinates: create_polygon(coordinates), srid: srid}
end

defp do_decode("MULTIPOINT" <> coordinates, srid) do
%MultiPoint{coordinates: create_line_string(coordinates), srid: srid}
end

defp do_decode("MULTILINESTRINGZM" <> coordinates, srid) do
%MultiLineStringZM{coordinates: create_polygon(coordinates), srid: srid}
end

defp do_decode("MULTILINESTRINGZ" <> coordinates, srid) do
%MultiLineStringZ{coordinates: create_polygon(coordinates), srid: srid}
end

defp do_decode("MULTILINESTRING" <> coordinates, srid) do
%MultiLineString{coordinates: create_polygon(coordinates), srid: srid}
end

defp do_decode("MULTIPOLYGONZ" <> coordinates, srid) do
%MultiPolygonZ{coordinates: create_multi_polygon(coordinates), srid: srid}
end
Expand Down
7 changes: 7 additions & 0 deletions lib/geo/wkt/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Geo.WKT.Encoder do
MultiPointZ,
MultiLineString,
MultiLineStringZ,
MultiLineStringZM,
MultiPolygon,
MultiPolygonZ,
GeometryCollection
Expand Down Expand Up @@ -109,6 +110,12 @@ defmodule Geo.WKT.Encoder do
"MULTILINESTRINGZ#{coordinate_string}"
end

defp do_encode(%MultiLineStringZM{coordinates: coordinates}) do
coordinate_string = create_polygon_str(coordinates)

"MULTILINESTRINGZM#{coordinate_string}"
end

defp do_encode(%MultiPolygon{coordinates: coordinates}) do
coordinate_string = create_multi_polygon_str(coordinates)

Expand Down
18 changes: 18 additions & 0 deletions test/geo/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,24 @@ defmodule Geo.JSON.Test do
assert_geojson_equal(exjson, new_exjson)
end

test "GeoJson to MultiLineStringZM and back" do
json =
"{ \"type\": \"MultiLineStringZM\", \"coordinates\": [[ [100.0, 0.0, 50.0, 1], [101.0, 1.0, 51.0, 2] ],[ [102.0, 2.0, 52.0, 3], [103.0, 3.0, 53.0, 4] ]]}"

exjson = Jason.decode!(json)
geom = Jason.decode!(json) |> Geo.JSON.decode!()

assert(
geom.coordinates == [
[{100.0, 0.0, 50.0, 1}, {101.0, 1.0, 51.0, 2}],
[{102.0, 2.0, 52.0, 3}, {103.0, 3.0, 53.0, 4}]
]
)

new_exjson = Geo.JSON.encode!(geom)
assert_geojson_equal(exjson, new_exjson)
end

test "GeoJson to MultiPolygon and back" do
json =
"{ \"type\": \"MultiPolygon\", \"coordinates\": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]]}"
Expand Down
46 changes: 46 additions & 0 deletions test/geo/wkb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,52 @@ defmodule Geo.WKB.Test do
)
end

test "Decode WKB to MultiLineStringZM" do
geom =
Geo.WKB.decode!(
"00C00000050000000200C0000002000000024024000000000000402400000000000040240000000000003FF0000000000000403400000000000040340000000000004034000000000000400000000000000000C000000200000002402E000000000000402E000000000000402E0000000000004008000000000000403E000000000000402E00000000000040240000000000004010000000000000"
)

expected_coords = [[{10, 10, 10, 1}, {20, 20, 20, 2}], [{15, 15, 15, 3}, {30, 15, 10, 4}]]

assert(geom.coordinates == expected_coords)
end

test "Encode MultiLineStringZM to WKB" do
geom = %Geo.MultiLineStringZM{
coordinates: [[{10, 10, 10, 1}, {20, 20, 20, 2}], [{15, 15, 15, 3}, {30, 15, 10, 4}]]
}

assert(
Geo.WKB.encode!(geom) ==
"00C00000050000000200C0000002000000024024000000000000402400000000000040240000000000003FF0000000000000403400000000000040340000000000004034000000000000400000000000000000C000000200000002402E000000000000402E000000000000402E0000000000004008000000000000403E000000000000402E00000000000040240000000000004010000000000000"
)
end

test "Decode EWKB to MultiLineStringZM" do
geom =
Geo.WKB.decode!(
"01050000E0E61000000200000001020000C002000000000000000000244000000000000024400000000000002440000000000000F03F000000000000344000000000000034400000000000003440000000000000004001020000C0020000000000000000002E400000000000002E400000000000002E4000000000000008400000000000003E400000000000002E4000000000000024400000000000001040"
)

expected_coords = [[{10, 10, 10, 1}, {20, 20, 20, 2}], [{15, 15, 15, 3}, {30, 15, 10, 4}]]

assert(geom.coordinates == expected_coords)
assert(geom.srid == 4326)
end

test "Encode MultiLineStringZM to EWKB" do
geom = %Geo.MultiLineStringZM{
coordinates: [[{10, 10, 10, 1}, {20, 20, 20, 2}], [{15, 15, 15, 3}, {30, 15, 10, 4}]],
srid: 4326
}

assert(
Geo.WKB.encode!(geom, :ndr) ==
"01050000E0E61000000200000001020000C002000000000000000000244000000000000024400000000000002440000000000000F03F000000000000344000000000000034400000000000003440000000000000004001020000C0020000000000000000002E400000000000002E400000000000002E4000000000000008400000000000003E400000000000002E4000000000000024400000000000001040"
)
end

test "Decode WKB to MultiLineStringZ" do
geom =
Geo.WKB.decode!(
Expand Down
Loading