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

Added MultiPointM and LinestringM #173

Open
wants to merge 2 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
14 changes: 14 additions & 0 deletions lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ defmodule Geo.JSON.Decoder do
Point,
PointZ,
LineString,
LineStringM,
LineStringZ,
Polygon,
MultiPoint,
MultiPointM,
MultiLineString,
MultiPolygon,
MultiPolygonZ,
Expand Down Expand Up @@ -116,6 +118,12 @@ defmodule Geo.JSON.Decoder do
%LineString{coordinates: coordinates, srid: get_srid(crs), properties: properties}
end

defp do_decode("LineStringM", coordinates, properties, crs) do
coordinates = Enum.map(coordinates, &List.to_tuple(&1))

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

defp do_decode("LineStringZ", coordinates, properties, crs) do
coordinates = Enum.map(coordinates, &List.to_tuple(&1))

Expand All @@ -137,6 +145,12 @@ defmodule Geo.JSON.Decoder do
%MultiPoint{coordinates: coordinates, srid: get_srid(crs), properties: properties}
end

defp do_decode("MultiPointM", coordinates, properties, crs) do
coordinates = Enum.map(coordinates, &List.to_tuple(&1))

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

defp do_decode("MultiLineString", coordinates, properties, crs) do
coordinates =
Enum.map(coordinates, fn sub_coordinates ->
Expand Down
14 changes: 14 additions & 0 deletions lib/geo/json/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ defmodule Geo.JSON.Encoder do
Point,
PointZ,
LineString,
LineStringM,
LineStringZ,
Polygon,
PolygonZ,
MultiPoint,
MultiPointM,
MultiPointZ,
MultiLineString,
MultiLineStringZ,
Expand Down Expand Up @@ -136,6 +138,12 @@ defmodule Geo.JSON.Encoder do
%{"type" => "LineString", "coordinates" => coordinates}
end

defp do_encode(%LineStringM{coordinates: coordinates}) do
coordinates = Enum.map(coordinates, &Tuple.to_list(&1))

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

defp do_encode(%LineStringZ{coordinates: coordinates}) do
coordinates = Enum.map(coordinates, &Tuple.to_list(&1))

Expand Down Expand Up @@ -166,6 +174,12 @@ defmodule Geo.JSON.Encoder do
%{"type" => "MultiPoint", "coordinates" => coordinates}
end

defp do_encode(%MultiPointM{coordinates: coordinates}) do
coordinates = Enum.map(coordinates, &Tuple.to_list(&1))

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

defp do_encode(%MultiPointZ{coordinates: coordinates}) do
coordinates = Enum.map(coordinates, &Tuple.to_list(&1))

Expand Down
8 changes: 8 additions & 0 deletions lib/geo/line_stringm.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Geo.LineStringM do
@moduledoc """
Defines the LineStringZ struct.
"""

@type t :: %__MODULE__{coordinates: [{number, number, number}], srid: integer | nil, properties: map}
defstruct coordinates: [], srid: nil, properties: %{}
end
8 changes: 8 additions & 0 deletions lib/geo/multi_pointm.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Geo.MultiPointM do
@moduledoc """
Defines the MultiPointZ struct.
"""

@type t :: %__MODULE__{coordinates: [{number, number, number}], srid: integer | nil, properties: map}
defstruct coordinates: [], srid: nil, properties: %{}
end
46 changes: 46 additions & 0 deletions lib/geo/wkb/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ defmodule Geo.WKB.Decoder do
@point_z 0x80_00_00_01
@point_zm 0xC0_00_00_01
@line_string 0x00_00_00_02
@line_string_m 0x40_00_00_02
@line_string_z 0x80_00_00_02
@polygon 0x00_00_00_03
@polygon_z 0x80_00_00_03
@multi_point 0x00_00_00_04
@multi_point_m 0x40_00_00_04
@multi_point_z 0x80_00_00_04
@multi_line_string 0x00_00_00_05
@multi_line_string_z 0x80_00_00_05
Expand All @@ -27,11 +29,13 @@ defmodule Geo.WKB.Decoder do
PointM,
PointZM,
LineString,
LineStringM,
LineStringZ,
Polygon,
PolygonZ,
GeometryCollection,
MultiPoint,
MultiPointM,
MultiPointZ,
MultiLineString,
MultiLineStringZ,
Expand Down Expand Up @@ -135,6 +139,32 @@ defmodule Geo.WKB.Decoder do
{%LineString{coordinates: coordinates, srid: srid}, rest}
end

defp do_decode(
@line_string_m,
<<count::unquote(modifier)-32, rest::bits>>,
srid,
unquote(endian)
) do
{coordinates, rest} =
Enum.map_reduce(1..count, rest, fn _,
<<x::unquote(modifier)-float-64,
y::unquote(modifier)-float-64,
z::unquote(modifier)-float-64, rest::bits>> ->
{%PointM{coordinates: coordinates}, _rest} =
do_decode(
@point_m,
<<x::unquote(modifier)-float-64, y::unquote(modifier)-float-64,
z::unquote(modifier)-float-64>>,
nil,
unquote(endian)
)

{coordinates, rest}
end)

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

defp do_decode(
@line_string_z,
<<count::unquote(modifier)-32, rest::bits>>,
Expand Down Expand Up @@ -221,6 +251,22 @@ defmodule Geo.WKB.Decoder do
{%MultiPoint{coordinates: coordinates, srid: srid}, rest}
end

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

{coordinates, rest}
end)

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

defp do_decode(
@multi_point_z,
<<count::unquote(modifier)-32, rest::bits>>,
Expand Down
27 changes: 27 additions & 0 deletions lib/geo/wkb/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ defmodule Geo.WKB.Encoder do
@point_z 0x80_00_00_01
@point_zm 0xC0_00_00_01
@line_string 0x00_00_00_02
@line_string_m 0x40_00_00_02
@line_string_z 0x80_00_00_02
@polygon 0x00_00_00_03
@polygon_z 0x80_00_00_03
@multi_point 0x00_00_00_04
@multi_point_m 0x40_00_00_04
@multi_point_z 0x80_00_00_04
@multi_line_string 0x00_00_00_05
@multi_line_string_z 0x80_00_00_05
Expand All @@ -27,10 +29,12 @@ defmodule Geo.WKB.Encoder do
PointM,
PointZM,
LineString,
LineStringM,
LineStringZ,
Polygon,
PolygonZ,
MultiPoint,
MultiPointM,
MultiPointZ,
MultiLineString,
MultiLineStringZ,
Expand Down Expand Up @@ -98,6 +102,19 @@ defmodule Geo.WKB.Encoder do
{@line_string, [<<count::unquote(modifier)-32>> | coordinates]}
end

def do_encode(%LineStringM{coordinates: coordinates}, unquote(endian_atom)) do
{coordinates, count} =
Enum.map_reduce(coordinates, 0, fn {x, y, z}, acc ->
{[
<<x::unquote(modifier)-float-64>>,
<<y::unquote(modifier)-float-64>>,
<<z::unquote(modifier)-float-64>>
], acc + 1}
end)

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

def do_encode(%LineStringZ{coordinates: coordinates}, unquote(endian_atom)) do
{coordinates, count} =
Enum.map_reduce(coordinates, 0, fn {x, y, z}, acc ->
Expand Down Expand Up @@ -141,6 +158,16 @@ defmodule Geo.WKB.Encoder do
{@multi_point, [<<count::unquote(modifier)-32>> | coordinates]}
end

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

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

def do_encode(%MultiPointZ{coordinates: coordinates}, unquote(endian_atom)) do
{coordinates, count} =
Enum.map_reduce(coordinates, 0, fn coordinate, acc ->
Expand Down
10 changes: 10 additions & 0 deletions lib/geo/wkt/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ defmodule Geo.WKT.Decoder do
PointM,
PointZM,
LineString,
LineStringM,
LineStringZ,
Polygon,
PolygonZ,
MultiPoint,
MultiPointM,
MultiPointZ,
MultiLineString,
MultiPolygon,
Expand Down Expand Up @@ -63,6 +65,10 @@ defmodule Geo.WKT.Decoder do
%Point{coordinates: create_point(coordinates), srid: srid}
end

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

defp do_decode("LINESTRINGZ" <> coordinates, srid) do
%LineStringZ{coordinates: create_line_string(coordinates), srid: srid}
end
Expand All @@ -79,6 +85,10 @@ defmodule Geo.WKT.Decoder do
%Polygon{coordinates: create_polygon(coordinates), srid: srid}
end

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

defp do_decode("MULTIPOINTZ" <> coordinates, srid) do
%MultiPointZ{coordinates: create_line_string(coordinates), srid: srid}
end
Expand Down
14 changes: 14 additions & 0 deletions lib/geo/wkt/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ defmodule Geo.WKT.Encoder do
PointM,
PointZM,
LineString,
LineStringM,
LineStringZ,
Polygon,
PolygonZ,
MultiPoint,
MultiPointM,
MultiPointZ,
MultiLineString,
MultiLineStringZ,
Expand Down Expand Up @@ -60,6 +62,12 @@ defmodule Geo.WKT.Encoder do
"LINESTRING#{coordinate_string}"
end

defp do_encode(%LineStringM{coordinates: coordinates}) do
coordinate_string = create_line_string_str(coordinates)

"LINESTRINGM#{coordinate_string}"
end

defp do_encode(%LineStringZ{coordinates: coordinates}) do
coordinate_string = create_line_string_str(coordinates)

Expand All @@ -84,6 +92,12 @@ defmodule Geo.WKT.Encoder do
"MULTIPOINT#{coordinate_string}"
end

defp do_encode(%MultiPointM{coordinates: coordinates}) do
coordinate_string = create_line_string_str(coordinates)

"MULTIPOINTM#{coordinate_string}"
end

defp do_encode(%MultiPointZ{coordinates: coordinates}) do
coordinate_string = create_line_string_str(coordinates)

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

test "GeoJson to LineStringM and back" do
json = "{ \"type\": \"LineStringM\", \"coordinates\": [ [100.0, 0.0, 50.0], [101.0, 1.0, 20.0] ]}"
exjson = Jason.decode!(json)
geom = Jason.decode!(json) |> Geo.JSON.decode!()

assert(geom.coordinates == [{100.0, 0.0, 50.0}, {101.0, 1.0, 20.0}])
new_exjson = Geo.JSON.encode!(geom)
assert(exjson == new_exjson)
end

test "GeoJson to LineStringZ and back" do
json = "{ \"type\": \"LineStringZ\", \"coordinates\": [ [100.0, 0.0, 50.0], [101.0, 1.0, 20.0] ]}"
exjson = Jason.decode!(json)
Expand Down Expand Up @@ -127,6 +137,16 @@ defmodule Geo.JSON.Test do
assert(exjson == new_exjson)
end

test "GeoJson to MultiPointM and back" do
json = "{ \"type\": \"MultiPointM\", \"coordinates\": [ [100.0, 0.0, 5], [101.0, 1.0, 50] ]}"
exjson = Jason.decode!(json)
geom = Jason.decode!(json) |> Geo.JSON.decode!()

assert(geom.coordinates == [{100.0, 0.0, 5}, {101.0, 1.0, 50}])
new_exjson = Geo.JSON.encode!(geom)
assert(exjson == new_exjson)
end

test "GeoJson to MultiLineString and back" do
json =
"{ \"type\": \"MultiLineString\", \"coordinates\": [[ [100.0, 0.0], [101.0, 1.0] ],[ [102.0, 2.0], [103.0, 3.0] ]]}"
Expand Down
Loading