Packages
geo
0.10.1
4.1.0
4.0.1
4.0.0
3.6.0
3.5.1
3.5.0
3.4.3
3.4.2
3.4.1
3.4.0
3.3.8
3.3.7
3.3.5
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.0
3.1.0
3.0.0
2.1.0
2.0.0
1.5.0
1.4.1
1.4.0
1.3.1
1.3.0
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.18.0
0.17.0
0.16.1
0.16.0
0.15.2
0.15.1
0.15.0
0.14.0
0.13.0
0.12.0
0.11.2
0.11.1
0.11.0
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.0
0.7.2
0.7.1
0.6.1
0.5.1
Encodes and decodes WKB, WKT, and GeoJSON formats.
Current section
Files
Jump to
Current section
Files
lib/geo/utils.ex
defmodule Geo.Utils do
@moduledoc false
use Bitwise
@doc """
Turns a hex string or an integer of base 16 into its floating point
representation.
Takes an optional endian atom. Either :xdr for big endian or :ndr for little
endian. Defaults to :xdr
`
Geo.Utils.hex_to_float("40000000")
2.0
Geo.Utils.hex_to_float(0x40000000)
2.0
Geo.Utils.hex_to_float("3ff0000000000000")
1.0
Geo.Utils.hex_to_float(0x3ff0000000000000)
1.0
`
"""
def hex_to_float(hex) when is_integer(hex) or is_binary(hex) do
if is_integer(hex), do: hex = Integer.to_string(hex, 16)
case bit_size(hex) do
64 ->
<< value :: float-32 >> = << String.to_integer(hex, 16) :: integer-32 >>
value
128 ->
<< value :: float-64 >> = << String.to_integer(hex, 16) :: integer-64 >>
value
_ ->
raise ArgumentError.new message: "hex must be either 4 or 8 bytes long"
end
end
@doc """
Turns a float into a hex value. The size can either be 32 or 64.
"""
def float_to_hex(float, size) do
case size do
32 ->
<< value :: integer-32 >> = << float :: float-32 >>
value
64 ->
<< value :: integer-64 >> = << float :: float-64 >>
value
end
end
@doc """
Reverses the byte order of the given hex string.
```
Geo.Utils.reverse_byte_order("00000004")
"40000000"
Geo.Utils.reverse_byte_order("E6100000")
"000010E6"
```
"""
def reverse_byte_order(hex) do
byte_range = Enum.to_list(0..(div(String.length(hex),2)-1))
Enum.map(byte_range, fn(x) -> String.slice(hex, x*2, 2) end)
|> Enum.reverse
|> Enum.join
end
@doc """
Adds 0's to the right of hex string
"""
def pad_right(hex, size) do
if String.length(hex) == size do
hex
else
hex <> repeat("0", size - String.length(hex))
end
end
@doc """
Adds 0's to the left of hex string
"""
def pad_left(hex, size) do
if String.length(hex) == size do
hex
else
repeat("0", size - String.length(hex)) <> hex
end
end
@doc """
Repeats the char count number of times
"""
def repeat(char, count) do
Enum.map(1..count, fn(_x) -> char end) |> Enum.join
end
end