forked from ggrise/wkb_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwkb_parser.erl
More file actions
139 lines (119 loc) · 4.86 KB
/
Copy pathwkb_parser.erl
File metadata and controls
139 lines (119 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
%% @author Gabriel Grise <ggrise@ggri.se>
%% @copyright 2007 Gabriel Grise
-module(wkb_parser).
-author('ggrise@ggri.se').
-export([parse/1, parse_hex/1]).
get_type(Type) ->
EType = Type band (bnot (16#80000000 bor 16#20000000)),
case EType of
1 ->
'Point';
2 ->
'LineString';
3 ->
'Polygon';
4 ->
'MultiPoint';
5 ->
'MultiLineString';
6 ->
'MultiPolygon';
7 ->
'GeometryCollection'
end.
to_num(A) when A >= 65 andalso A =< 70 ->
A-55;
to_num(A) when A >= 48 andalso A =< 57 ->
A-48.
hex_to_binary([], Acc) ->
list_to_binary(lists:reverse(Acc));
hex_to_binary([H,L|Hex], Acc) ->
hex_to_binary(Hex, [to_num(H) * 16 + to_num(L) | Acc]).
%% @spec parse_hex(string()) -> {integer(), Geometry}
%% @doc Return the Geometry with SRID if present
parse_hex(Wkb) ->
Bin = hex_to_binary(string:to_upper(Wkb), []),
parse(Bin).
%% @spec parse(binary()) -> {integer(), Geometry}
%% @doc Return the Geometry with SRID if present
parse(Wkb) ->
{Srid, {Geom, _}} = parse_geometry(Wkb),
{Srid, Geom}.
get_has_z(Type) ->
(Type band 16#80000000) =/= 0.
get_has_srid(Type) ->
(Type band 16#20000000) =/= 0.
parse_geometry(<<0, Type:32/unsigned-integer-big, Geom/binary>>) ->
HasZ = get_has_z(Type),
case get_has_srid(Type) of
true ->
<<Srid:32/integer-big, NGeom/binary>> = Geom,
{Srid, parse_geometry(big, HasZ, get_type(Type), NGeom)};
_ ->
{none, parse_geometry(big, HasZ, get_type(Type), Geom)}
end;
parse_geometry(<<1, Type:32/unsigned-integer-little, Geom/binary>>) ->
HasZ = get_has_z(Type),
case get_has_srid(Type) of
true ->
<<Srid:32/integer-little, NGeom/binary>> = Geom,
{Srid, parse_geometry(little, HasZ, get_type(Type), NGeom)};
_ ->
{none, parse_geometry(little, HasZ, get_type(Type), Geom)}
end.
%WKBPoint
parse_geometry(big, false, 'Point', <<X:64/float-big, Y:64/float-big, R/binary>>) ->
{{'Point', {X, Y}}, R};
parse_geometry(little, false, 'Point', <<X:64/float-little, Y:64/float-little, R/binary>>) ->
{{'Point', {X, Y}}, R};
parse_geometry(big, true, 'Point', <<X:64/float-big, Y:64/float-big, Z:64/float-big, R/binary>>) ->
{{'Point', {X, Y, Z}}, R};
parse_geometry(little, true, 'Point', <<X:64/float-little, Y:64/float-little, Z:64/float-little, R/binary>>) ->
{{'Point', {X, Y, Z}}, R};
%WKBLineString
parse_geometry(big, HasZ, 'LineString', <<NumPoints:32/unsigned-integer-big, Points/binary>>) ->
{LS, R} = parse_linestring(big, HasZ, NumPoints, Points, []),
{{'LineString', LS}, R};
parse_geometry(little, HasZ, 'LineString', <<NumPoints:32/unsigned-integer-little, Points/binary>>) ->
{LS, R} = parse_linestring(little, HasZ, NumPoints, Points, []),
{{'LineString', LS}, R};
%WKBPolygon
parse_geometry(big, HasZ, 'Polygon', <<NumRings:32/unsigned-integer-big, Rings/binary>>) ->
parse_polygon(big, HasZ, NumRings, Rings, []);
parse_geometry(little, HasZ, 'Polygon', <<NumRings:32/unsigned-integer-little, Rings/binary>>) ->
parse_polygon(little, HasZ, NumRings, Rings, []);
parse_geometry(big, HasZ, Type, <<Num:32/unsigned-integer-big, Geoms/binary>>) ->
{G, R} = parse_multi(HasZ, Num, Geoms, []),
{{Type, G}, R};
parse_geometry(little, HasZ, Type, <<Num:32/unsigned-integer-little, Geoms/binary>>) ->
{G, R} = parse_multi(HasZ, Num, Geoms, []),
{{Type, G}, R}.
parse_linestring(_, _, 0, Remain, Acc) ->
{lists:reverse(Acc), Remain};
parse_linestring(_, _, _, <<>>, Acc) ->
{lists:reverse(Acc), <<>>};
parse_linestring(little, false, Count, <<X:64/float-little, Y:64/float-little, Points/binary>>, Acc) ->
parse_linestring(little, false, Count-1, Points, [{X, Y} | Acc]);
parse_linestring(big, false, Count, <<X:64/float-big, Y:64/float-big, Points/binary>>, Acc) ->
parse_linestring(big, false, Count-1, Points, [{X, Y} | Acc]);
parse_linestring(little, true, Count, <<X:64/float-little, Y:64/float-little, Z:64/float-little, Points/binary>>, Acc) ->
parse_linestring(little, true, Count-1, Points, [{X, Y, Z} | Acc]);
parse_linestring(big, true, Count, <<X:64/float-big, Y:64/float-big, Z:64/float-little, Points/binary>>, Acc) ->
parse_linestring(big, true, Count-1, Points, [{X, Y, Z} | Acc]).
parse_polygon(_, _,0, R, Acc) ->
{{get_type(3), lists:reverse(Acc)}, R};
parse_polygon(_, _, _, <<>>, Acc) ->
{{get_type(3), lists:reverse(Acc)}, <<>>};
parse_polygon(little, Z, NumRings, <<NumPoints:32/unsigned-integer-little, Ring/binary>>, Acc) ->
{Line, Remain} = parse_linestring(little, Z, NumPoints, Ring, []),
parse_polygon(little, Z, NumRings-1, Remain, [Line|Acc]);
parse_polygon(big, Z, NumRings, <<NumPoints:32/unsigned-integer-big, Ring/binary>>, Acc) ->
{Line, Remain} = parse_linestring(big, Z, NumPoints, Ring, []),
parse_polygon(big, Z, NumRings-1, Remain, [Line|Acc]).
parse_multi(_, 0, R, Acc) ->
{lists:reverse(Acc), R};
parse_multi(_, _, <<>>, Acc) ->
{lists:reverse(Acc), <<>>};
parse_multi(Z, Num, Geoms, Acc) ->
{_Srid, {Geom, Remain}} = parse_geometry(Geoms),
parse_multi(Z, Num-1, Remain, [Geom|Acc]).