Browse Source

Refactor ewkb: typespecs moved out from .hrl file

Сергей Прохоров 7 years ago
parent
commit
783ad197b4
3 changed files with 58 additions and 66 deletions
  1. 13 61
      include/epgsql_geometry.hrl
  2. 1 2
      src/datatypes/epgsql_codec_postgis.erl
  3. 44 3
      src/ewkb.erl

+ 13 - 61
include/epgsql_geometry.hrl

@@ -1,4 +1,3 @@
--type point_type() :: '2d' | '3d' | '2dm' | '3dm'.
 
 -record(point,{
   point_type :: any(),
@@ -8,114 +7,67 @@
   m :: float() | undefined
   }).
 
--type point(PointType) :: #point{ point_type :: PointType }.
-
 -record(multi_point,{
   point_type :: any(),
-  points :: [point(point_type())]
+  points :: [ewkb:point(ewkb:point_type())]
   }).
 
--type multi_point(PointType) :: #multi_point{ point_type :: PointType }.
-
 -record(line_string,{
   point_type :: any(),
-  points :: [point(point_type())]
+  points :: [ewkb:point(ewkb:point_type())]
   }).
 
--type line_string(PointType) :: #line_string{ point_type :: PointType }.
-
 -record(multi_line_string,{
   point_type :: any(),
-  line_strings :: [line_string(point_type())]
+  line_strings :: [ewkb:line_string(ewkb:point_type())]
   }).
 
--type multi_line_string(PointType) :: #multi_line_string{ point_type :: PointType }.
-
 -record(circular_string,{
   point_type :: any(),
-  points :: [point(point_type())]
+  points :: [ewkb:point(ewkb:point_type())]
   }).
 
--type basic_string(PointType) :: #circular_string{ point_type :: PointType } | #line_string{ point_type :: PointType }.
-
 -record(compound_curve,{
   point_type :: any(),
-  lines :: [basic_string(point_type())]
+  lines :: [ewkb:basic_string(ewkb:point_type())]
   }).
 
--type curve(PointType) :: #circular_string{ point_type :: PointType } | #line_string{ point_type :: PointType } | #compound_curve{ point_type :: PointType }.
-
 -record(multi_curve,{
   point_type :: any(),
-  curves :: [curve(point_type())]
+  curves :: [ewkb:curve(ewkb:point_type())]
   }).
 
--type multi_curve(PointType) :: #multi_curve{ point_type :: PointType }.
-
 -record(polygon,{
   point_type :: any(),
-  rings :: [line_string(point_type())]
+  rings :: [ewkb:line_string(ewkb:point_type())]
   }).
 
--type polygon(PointType) :: #polygon{ point_type :: PointType }.
-
 -record(multi_polygon,{
   point_type :: any(),
-  polygons :: [polygon(point_type())]
+  polygons :: [ewkb:polygon(ewkb:point_type())]
   }).
 
--type multi_polygon(PointType) :: #multi_polygon{ point_type :: PointType }.
-
 -record(triangle,{
   point_type :: any(),
-  rings :: [line_string(point_type())]
+  rings :: [ewkb:line_string(ewkb:point_type())]
   }).
 
--type triangle(PointType) :: #triangle{ point_type :: PointType }.
-
 -record(curve_polygon,{
   point_type :: any(),
-  rings :: [curve(point_type())]
+  rings :: [ewkb:curve(ewkb:point_type())]
   }).
 
--type curve_polygon(PointType) :: #curve_polygon{ point_type :: PointType }.
-
 -record(polyhedral_surface,{
   point_type :: any(),
-  polygons :: [polygon(point_type())]
+  polygons :: [ewkb:polygon(ewkb:point_type())]
   }).
 
--type polyhedral_surface(PointType) :: #polyhedral_surface{ point_type :: PointType }.
-
--type surface(PointType) :: polygon(PointType) | curve_polygon(PointType) | polyhedral_surface(PointType).
-
 -record(multi_surface,{
   point_type :: any(),
-  surfaces :: [surface(point_type())]
+  surfaces :: [ewkb:surface(ewkb:point_type())]
   }).
 
--type multi_surface(PointType) :: #multi_surface{ point_type :: PointType }.
-
 -record(tin,{
   point_type :: any(),
-  triangles :: [triangle(point_type())]
+  triangles :: [ewkb:triangle(ewkb:point_type())]
   }).
-
--type tin(PointType) :: #tin{ point_type :: PointType }.
-
--type geometry(PointType) :: point(PointType) |
-                             line_string(PointType) |
-                             triangle(PointType) |
-                             tin(PointType) |
-                             curve(PointType) |
-                             surface(PointType) |
-                             multi_point(PointType) |
-                             multi_line_string(PointType) |
-                             multi_polygon(PointType) |
-                             multi_curve(PointType) |
-                             multi_surface(PointType) |
-                             geometry_collection(PointType).
-
--type geometry() :: geometry(point_type()).
-
--type geometry_collection(PointType) :: [geometry(PointType)].

+ 1 - 2
src/datatypes/epgsql_codec_postgis.erl

@@ -12,8 +12,7 @@
 
 -export_type([data/0]).
 
--type data() :: point().
--type point() :: {}.
+-type data() :: ewkb:geometry().
 
 init(_, _) -> [].
 

+ 44 - 3
src/ewkb.erl

@@ -1,9 +1,50 @@
 %% https://en.wikipedia.org/wiki/Well-known_text
 %% http://postgis.net/docs/manual-2.4/using_postgis_dbmanagement.html#EWKB_EWKT
 -module(ewkb).
--include("epgsql_geometry.hrl").
 -export([decode_geometry/1, encode_geometry/1]).
+-export_type([point_type/0, point/1, multi_point/1, line_string/1,
+              multi_line_string/1, basic_string/1, curve/1, multi_curve/1,
+              polygon/1, multi_polygon/1, triangle/1, curve_polygon/1,
+              polyhedral_surface/1, surface/1, multi_surface/1, tin/1,
+              geometry/1, geometry/0, geometry_collection/1, geom_type/0]).
+
+-include("epgsql_geometry.hrl").
 
+-type point_type() :: '2d' | '3d' | '2dm' | '3dm'.
+-type point(PointType) :: #point{ point_type :: PointType }.
+-type multi_point(PointType) :: #multi_point{ point_type :: PointType }.
+-type line_string(PointType) :: #line_string{ point_type :: PointType }.
+-type multi_line_string(PointType) :: #multi_line_string{ point_type :: PointType }.
+-type basic_string(PointType) :: #circular_string{ point_type :: PointType }
+                               | #line_string{ point_type :: PointType }.
+-type curve(PointType) :: #circular_string{ point_type :: PointType }
+                        | #line_string{ point_type :: PointType }
+                        | #compound_curve{ point_type :: PointType }.
+-type multi_curve(PointType) :: #multi_curve{ point_type :: PointType }.
+-type polygon(PointType) :: #polygon{ point_type :: PointType }.
+-type multi_polygon(PointType) :: #multi_polygon{ point_type :: PointType }.
+-type triangle(PointType) :: #triangle{ point_type :: PointType }.
+-type curve_polygon(PointType) :: #curve_polygon{ point_type :: PointType }.
+-type polyhedral_surface(PointType) :: #polyhedral_surface{ point_type :: PointType }.
+-type surface(PointType) :: polygon(PointType)
+                          | curve_polygon(PointType)
+                          | polyhedral_surface(PointType).
+-type multi_surface(PointType) :: #multi_surface{ point_type :: PointType }.
+-type tin(PointType) :: #tin{ point_type :: PointType }.
+-type geometry(PointType) :: point(PointType) |
+                             line_string(PointType) |
+                             triangle(PointType) |
+                             tin(PointType) |
+                             curve(PointType) |
+                             surface(PointType) |
+                             multi_point(PointType) |
+                             multi_line_string(PointType) |
+                             multi_polygon(PointType) |
+                             multi_curve(PointType) |
+                             multi_surface(PointType) |
+                             geometry_collection(PointType).
+-type geometry() :: geometry(point_type()).
+-type geometry_collection(PointType) :: [geometry(PointType)].
 
 -type geom_type() :: geometry
                    | point       %
@@ -24,11 +65,12 @@
                    | tin%
                    | triangle.%
 
-
+-spec decode_geometry(binary()) -> geometry().
 decode_geometry(Binary) ->
   {Geometry, <<>>} = decode_geometry_data(Binary),
   Geometry.
 
+-spec encode_geometry(geometry()) -> binary().
 encode_geometry(Geometry) ->
   Type = encode_type(Geometry),
   PointType = encode_point_type(Geometry),
@@ -260,4 +302,3 @@ encode_point_type('2d') -> <<0,0>>;
 encode_point_type('2dm') -> <<0,64>>;
 encode_point_type('3d') -> <<0,128>>;
 encode_point_type('3dm') -> <<0,192>>.
-