ewkb.erl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. -module(ewkb).
  2. -include("epgsql_geometry.hrl").
  3. -export([decode_geometry/1, encode_geometry/1]).
  4. -type geom_type() :: geometry
  5. | point %
  6. | line_string%
  7. | polygon%
  8. | multi_point%
  9. | multi_line_string%
  10. | multi_polygon%
  11. | geometry_collection%
  12. | circular_string%
  13. | compound_curve%
  14. | curve_polygon%
  15. | multi_curve%
  16. | multi_surface%
  17. | curve%
  18. | surface%
  19. | polyhedral_surface%
  20. | tin%
  21. | triangle.%
  22. decode_geometry(Binary) ->
  23. {Geometry, <<>>} = decode_geometry_data(Binary),
  24. Geometry.
  25. encode_geometry(Geometry) ->
  26. Type = encode_type(Geometry),
  27. PointType = encode_point_type(Geometry),
  28. Data = encode_geometry_data(Geometry),
  29. <<1, Type/binary, PointType/binary, Data/binary>>.
  30. encode_geometry_data(#point{ point_type = '2d', x = X, y = Y }) ->
  31. Xbin = encode_float64(X),
  32. Ybin = encode_float64(Y),
  33. <<Xbin/binary, Ybin/binary>>;
  34. encode_geometry_data(#point{ point_type = '2dm', x = X, y = Y, m = M }) ->
  35. Xbin = encode_float64(X),
  36. Ybin = encode_float64(Y),
  37. Mbin = encode_float64(M),
  38. <<Xbin/binary, Ybin/binary, Mbin/binary>>;
  39. encode_geometry_data(#point{ point_type = '3d', x = X, y = Y, z = Z }) ->
  40. Xbin = encode_float64(X),
  41. Ybin = encode_float64(Y),
  42. Zbin = encode_float64(Z),
  43. <<Xbin/binary, Ybin/binary, Zbin/binary>>;
  44. encode_geometry_data(#point{ point_type = '3dm', x = X, y = Y, z = Z, m = M }) ->
  45. Xbin = encode_float64(X),
  46. Ybin = encode_float64(Y),
  47. Zbin = encode_float64(Z),
  48. Mbin = encode_float64(M),
  49. <<Xbin/binary, Ybin/binary, Zbin/binary, Mbin/binary>>;
  50. encode_geometry_data({SimpleCollection, _, Data})
  51. when SimpleCollection == line_string;
  52. SimpleCollection == circular_string;
  53. SimpleCollection == polygon;
  54. SimpleCollection == triangle ->
  55. encode_collection(Data);
  56. encode_geometry_data({TypedCollection, _, Data})
  57. when
  58. TypedCollection == multi_point;
  59. TypedCollection == multi_line_string;
  60. TypedCollection == multi_curve;
  61. TypedCollection == multi_polygon;
  62. TypedCollection == multi_surface;
  63. TypedCollection == compound_curve;
  64. TypedCollection == curve_polygon;
  65. TypedCollection == geometry_collection;
  66. TypedCollection == polyhedral_surface;
  67. TypedCollection == tin ->
  68. encode_typed_collection(Data).
  69. encode_collection(Collection) when is_list(Collection) ->
  70. Length = length(Collection),
  71. LengthBin = encode_int32(Length),
  72. CollectionBin = lists:foldl(
  73. fun(Element, Acc) ->
  74. ElementBin = encode_geometry_data(Element),
  75. <<Acc/binary, ElementBin/binary>>
  76. end,
  77. <<>>,
  78. Collection),
  79. <<LengthBin/binary, CollectionBin/binary>>.
  80. encode_typed_collection(Collection) when is_list(Collection) ->
  81. Length = length(Collection),
  82. LengthBin = encode_int32(Length),
  83. CollectionBin = lists:foldl(
  84. fun(Element, Acc) ->
  85. ElementBin = encode_geometry(Element),
  86. <<Acc/binary, ElementBin/binary>>
  87. end,
  88. <<>>,
  89. Collection),
  90. <<LengthBin/binary, CollectionBin/binary>>.
  91. encode_int32(Int) when is_integer(Int) ->
  92. <<Int:1/little-integer-unit:32>>.
  93. encode_float64(Int) when is_number(Int) ->
  94. <<Int:1/little-float-unit:64>>.
  95. -spec decode_geometry_data(binary()) -> {geometry(), binary()}.
  96. decode_geometry_data(Binary) ->
  97. <<1, TypeCode:2/binary, SubtypeCode:2/binary, Data/binary>> = Binary,
  98. Type = decode_type(TypeCode),
  99. Subtype = decode_point_type(SubtypeCode),
  100. decode_geometry_data(Type, Subtype, Data).
  101. -spec decode_geometry_data(geom_type(), point_type(), binary()) -> {geometry(), binary()}.
  102. decode_geometry_data(curve, _, _) -> error({curve, not_supported});
  103. decode_geometry_data(surface, _, _) -> error({surface, not_supported});
  104. decode_geometry_data(geometry, _, _) -> error({geometry, not_supported});
  105. decode_geometry_data(point, PointType, Data) ->
  106. decode_point(PointType, Data);
  107. decode_geometry_data(LineType, PointType, Data)
  108. when LineType == line_string;
  109. LineType == circular_string ->
  110. {Points, Rest} = decode_collection(point, PointType, Data),
  111. {{LineType, PointType, Points}, Rest};
  112. decode_geometry_data(polygon, PointType, Data) ->
  113. {Lines, Rest} = decode_collection(line_string, PointType, Data),
  114. {#polygon{ point_type = PointType, rings = Lines }, Rest};
  115. decode_geometry_data(triangle, PointType, Data) ->
  116. {#polygon{ rings = Rings }, Rest} = decode_geometry_data(polygon, PointType, Data),
  117. {#triangle{ point_type = PointType, rings = Rings }, Rest};
  118. decode_geometry_data(Collection, PointType, Data)
  119. when
  120. Collection == multi_point;
  121. Collection == multi_line_string;
  122. Collection == multi_curve;
  123. Collection == multi_polygon;
  124. Collection == multi_surface;
  125. Collection == compound_curve;
  126. Collection == curve_polygon;
  127. Collection == geometry_collection;
  128. Collection == polyhedral_surface;
  129. Collection == tin ->
  130. {Lines, Rest} = decode_typed_collection(Data),
  131. {{Collection, PointType, Lines}, Rest}.
  132. -spec decode_collection(geom_type(), point_type(), binary()) -> {[geometry()], binary()}.
  133. decode_collection(Type, PointType, Data) ->
  134. {Length, CountRest} = decode_int32(Data),
  135. lists:foldl(
  136. fun(_, {Geoms, Rest}) ->
  137. {Geom, R} = decode_geometry_data(Type, PointType, Rest),
  138. {Geoms ++ [Geom], R}
  139. end,
  140. {[], CountRest},
  141. lists:seq(1, Length)).
  142. -spec decode_typed_collection(binary()) -> {[geometry()], binary()}.
  143. decode_typed_collection(Data) ->
  144. {Length, CountRest} = decode_int32(Data),
  145. lists:foldl(
  146. fun(_, {Geoms, Rest}) ->
  147. {Geom, R} = decode_geometry_data(Rest),
  148. {Geoms ++ [Geom], R}
  149. end,
  150. {[], CountRest},
  151. lists:seq(1, Length)).
  152. -spec decode_int32(binary()) -> {integer(), binary()}.
  153. decode_int32(<<Hex:4/binary, Rest/binary>>) ->
  154. <<Int:1/little-integer-unit:32>> = Hex,
  155. {Int, Rest}.
  156. -spec decode_float64(binary()) -> {float(), binary()}.
  157. decode_float64(<<Hex:8/binary, Rest/binary>>) ->
  158. <<Float:1/little-float-unit:64>> = Hex,
  159. {Float, Rest}.
  160. decode_point(PointType, Data) ->
  161. {Values, Rest} = lists:foldl(
  162. fun(_, {Values, Rest}) ->
  163. {Value, R} = decode_float64(Rest),
  164. {Values ++ [Value], R}
  165. end,
  166. {[], Data},
  167. lists:seq(1, point_size(PointType))),
  168. Point = case {PointType, Values} of
  169. {'2d', [X,Y]} ->
  170. #point{ point_type = PointType, x = X, y = Y };
  171. {'2dm', [X,Y,M]} ->
  172. #point{ point_type = PointType, x = X, y = Y, m = M };
  173. {'3d', [X,Y,Z]} ->
  174. #point{ point_type = PointType, x = X, y = Y, z = Z };
  175. {'3dm', [X,Y,Z,M]} ->
  176. #point{ point_type = PointType, x = X, y = Y, z = Z, m = M }
  177. end,
  178. {Point, Rest}.
  179. -spec point_size(point_type()) -> 2..4.
  180. point_size('2d') -> 2;
  181. point_size('2dm') -> 3;
  182. point_size('3d') -> 3;
  183. point_size('3dm') -> 4.
  184. -spec decode_type(binary()) -> geom_type().
  185. decode_type(<<0,0>>) -> geometry;
  186. decode_type(<<1,0>>) -> point;
  187. decode_type(<<2,0>>) -> line_string;
  188. decode_type(<<3,0>>) -> polygon;
  189. decode_type(<<4,0>>) -> multi_point;
  190. decode_type(<<5,0>>) -> multi_line_string;
  191. decode_type(<<6,0>>) -> multi_polygon;
  192. decode_type(<<7,0>>) -> geometry_collection;
  193. decode_type(<<8,0>>) -> circular_string;
  194. decode_type(<<9,0>>) -> compound_curve;
  195. decode_type(<<10,0>>) -> curve_polygon;
  196. decode_type(<<11,0>>) -> multi_curve;
  197. decode_type(<<12,0>>) -> multi_surface;
  198. decode_type(<<13,0>>) -> curve;
  199. decode_type(<<14,0>>) -> surface;
  200. decode_type(<<15,0>>) -> polyhedral_surface;
  201. decode_type(<<16,0>>) -> tin;
  202. decode_type(<<17,0>>) -> triangle.
  203. -spec encode_type(geometry() | geom_type()) -> binary().
  204. encode_type(Geometry) when is_tuple(Geometry) ->
  205. encode_type(element(1, Geometry));
  206. encode_type(geometry) -> <<00, 0>>;
  207. encode_type(point) -> <<01, 0>>;
  208. encode_type(line_string) -> <<02, 0>>;
  209. encode_type(polygon) -> <<03, 0>>;
  210. encode_type(multi_point) -> <<04, 0>>;
  211. encode_type(multi_line_string) -> <<05, 0>>;
  212. encode_type(multi_polygon) -> <<06, 0>>;
  213. encode_type(geometry_collection) -> <<07, 0>>;
  214. encode_type(circular_string) -> <<08, 0>>;
  215. encode_type(compound_curve) -> <<09, 0>>;
  216. encode_type(curve_polygon) -> <<10, 0>>;
  217. encode_type(multi_curve) -> <<11, 0>>;
  218. encode_type(multi_surface) -> <<12, 0>>;
  219. encode_type(curve) -> <<13, 0>>;
  220. encode_type(surface) -> <<14, 0>>;
  221. encode_type(polyhedral_surface) -> <<15, 0>>;
  222. encode_type(tin) -> <<16, 0>>;
  223. encode_type(triangle) -> <<17, 0>>.
  224. -spec decode_point_type(binary()) -> point_type().
  225. decode_point_type(<<0,0>>) -> '2d';
  226. decode_point_type(<<0, 64>>) -> '2dm';
  227. decode_point_type(<<0, 128>>) -> '3d';
  228. decode_point_type(<<0, 192>>) -> '3dm'.
  229. -spec encode_point_type(geometry() | point_type()) -> binary().
  230. encode_point_type(Geometry) when is_tuple(Geometry) ->
  231. encode_point_type(element(2, Geometry));
  232. encode_point_type('2d') -> <<0,0>>;
  233. encode_point_type('2dm') -> <<0,64>>;
  234. encode_point_type('3d') -> <<0,128>>;
  235. encode_point_type('3dm') -> <<0,192>>.