Index: README.md ================================================================== --- README.md +++ README.md @@ -1,29 +1,47 @@ #Heads I Lose -A little learning exercise in Erlang. It looks up the wind direction ([via the MetOffice Datapoint API](http://www.metoffice.gov.uk/datapoint/api)) for my cycle commute and lets me know whether I win (tail wind) or lose (head wind). +This started as a little learning exercise in Erlang, but I've been building on it to make it more useful for my cycle commute. It looks up the wind direction ([via the MetOffice Datapoint API](http://www.metoffice.gov.uk/datapoint/api)) and for a given route (a polyline from [OSRM](http://project-osrm.org/)) determines how much of the route is a headwind sidewind or tailwind. ##Usage -1. Get an [API key](http://www.metoffice.gov.uk/datapoint/support/API) and save in a file called `~/.datapoint`. -2. Compile with `erlc headsilose.erl; erlc weather_types.erl`. -3. Run `erl -run headsilose get_locations -noshell -s init stop` to get a list of locations and Ids. The optional search term cannot contain spaces. -4. Then run `erl -run headsilose headsilose -noshell -s init stop` to get the result. - -Since I wrote this to be semi-useful for me, the result returned depends on the time of the day. If it's run before 8am it looks for the 6am weather data (since data is in 3 hour periods), run between 8am and 7pm it looks for the 6pm data for going home and run after that time it looks again for the 6am data, but for the next day. - -Heading is the overall direction of travel in 16-point compass format. So a heading of N means going from south to north. I only mention this because wind directions are "directions from". +Since it has just been developed for personal use installation is not very polished. + +1. Get a MetOffice Datapoint [API key](http://www.metoffice.gov.uk/datapoint/support/API) and save in a file called `~/.datapoint`. +2. Get/install [Jiffy](https://github.com/davisp/jiffy). (I just cloned the repository, issued `make` and copied the `ebin` and `priv` directories to my `heads-I-lose` directory). +3. Within the `heads-I-lose` directory, compile with `erlc headsilose.erl; erlc weather_types.erl; erlc osrm.erl; erlc polyline.erl`. +4. Run `erl -run headsilose get_locations -noshell -s init stop` to get a list of locations and Ids. The optional search term cannot contain spaces. +5. From within the erlang shell (because I don't yet know how to pass negative numbers on the command line) run `osrm:get_route([ ,],[,])` to get a route from OSRM. You'll have to find the latitude and longitudes by [some other means](http://www.uk-postcodes.com/)) for now. This will save the route in `~/.headsilose-route`. +6. Run `erl -run headsilose headsilose -noshell` to get the result. + +Since I wrote this to be semi-useful for me, the result returned depends on the time of the day. If it's run before 8am it looks for the 6am weather data (since data is in 3 hour periods) and assumes the route is being traversed normally, run between 8am and 7pm it looks for the 6pm data for going home and therefore also traverses the saved route in reverse, and run after that time it looks again for the 6am data, but for the next day, and thus the route is back to being traversed in the normal direction. _Hint:_ I have a shell function defined as follows: - function headsilose { erl -pa /path/to/heads-I-lose/ -run headsilose headsilose XXXXXX "$@" -noshell; } + function headsilose { + erl -pa /home/simon/Code/github/atomicules/heads-I-lose /home/simon/Code/github/atomicules/heads-I-lose/ebin -run headsilose headsilose XXXXXX alternative_geometries -noshell; + } So I can just call - headsilose N + headsilose + +Which will result in something like the following being printed out: + + It's a draw + 47.1% Headwind + 51.45% Sidewind + 1.45% Tailwind + Direction: SSW + Speed: 13 mph + Gust: 29 mph + Weather type: Cloudy + Temperature: 4 deg C + +_Note1:_ Since the route is from OSRM there is the option to specify `alternative_geometries` as an argument. In theory OSRM can return more than one alternative route, but in practice (at least as far as I can tell) I think it only ever returns one alternative route so although the argument is `alternative_geometries` it is actually the first alternative that is used. If `alternative_geometries` is not specified then the default route is used. If you'd like to see visually which route is the default and which is the alternative you can see them at [Project OSRM](http://map.project-osrm.org/) as routes A (default) and B (alternative). -_Note:_ I don't use [init stop](http://erlangcentral.org/wiki/index.php?title=Running_Erlang_Code_From_The_Command_Line&oldid=2293) in the command line call as I have that in my script instead. Otherwise, if `headsilose` errors out then `init stop` will crash out (I guess because it is trying to stop something that isn't running). +_Note2:_ I don't use [init stop](http://erlangcentral.org/wiki/index.php?title=Running_Erlang_Code_From_The_Command_Line&oldid=2293) in my main command line call as I have that in my script instead. Otherwise, if `headsilose` errors out then `init stop` will crash out (I guess because it is trying to stop something that isn't running). ##Credits Various posts I've found that have helped me out: @@ -30,10 +48,20 @@ - Initial inspiration from [PragDave - A First Erlang Program](http://pragdave.pragprog.com/pragdave/2007/04/a_first_erlang_.html) - Putting initial inspiration to practice (i.e. `inets:start`) from [Andrew Locatelli Woodcock - Connecting to Cloudant from Erlang: a quick example of using HTTPS from httpc:request](http://andrewlocatelliwoodcock.com/2012/06/12/connecting-to-cloudant-from-erlang-a-quick-example-of-using-https-from-httpcrequest-17-2/) - The recursive parsing of XML from [Sam Ruby - Parsing Atom with Erlang](http://intertwingly.net/blog/2007/08/28/Parsing-Atom-with-Erlang) - Formating dates with leading zeroes from [Warren Young on Stack Overflow](http://stackoverflow.com/a/7599506/208793) - Figuring out command line arguments from [Cody on Stack Overflow](http://stackoverflow.com/a/8498073/208793) +- Figuring out [Jiffy](http://www.snip2code.com/Snippet/51463/how-to-support-chinese-in-http-request-b/) +- [Rounding numbers](http://www.codecodex.com/wiki/index.php?title=Round_a_number_to_a_specific_decimal_place#Erlang) +- [Converting between binary and decimal](http://erlangcentral.org/wiki/index.php/Converting_Between_Binary_and_Decimal) + +I did read through a [number](http://www.mathworks.com/matlabcentral/fileexchange/32341-google-maps-api-polyline-decoder) [of](http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java) [posts](http://seewah.blogspot.co.uk/2009/11/gpolyline-decoding-in-python.html)/[implementations](http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/) [of](https://github.com/Project-OSRM/osrm-frontend/blob/master/WebContent/routing/OSRM.RoutingGeometry.js) polyline decoders, but they didn't really help me in Erlang so I just worked backwards through the [specification](https://developers.google.com/maps/documentation/utilities/polylinealgorithm) and wrote my own, much less concise, version rather than porting an existing implementation. ##Todo -- Make it factor in wind speed and gusts to the win/lose decision as opposed to just direction. +_I should really put these as issues_ + - Sort the list of locations? +- Finish off implementing the list of client "supposed to"s from the OSRM API, such as checksum and hint data, but this would mean caching all previous requests made. +- Use postcodes to get the latitudes and longitudes via [UK Postcodes](http://www.uk-postcodes.com/) +- Figure out how to pass negative numbers on the command line (perhaps use [getopt](http://github.com/jcomellas/getopt)?) +- Use tuples instead of lists where it makes sense Index: headsilose.erl ================================================================== --- headsilose.erl +++ headsilose.erl @@ -1,14 +1,16 @@ -module(headsilose). --export([get_locations/0, get_locations/1, get_weather/1, headsilose/2, headsilose/1]). +-export([get_locations/0, get_locations/1, headsilose/1]). -include_lib("xmerl/include/xmerl.hrl"). -import(weather_types, [weather_type/1]). +-import(polyline, [decode/1]). +-import(osrm, [read_route/1]). %Supply a direction and location and work out if head wind or not %For now "know the location id" upfront, but ideally need to search for it at some point or present a choice. -%Initially based on: http://pragdave.pragprog.com/pragdave/2007/04/a_first_erlang_.html +%Initially based on: http://pragdave.pragprog.com/pragdave/2007/04/a_first_erlang_.html -define(BASE_URL, "http://datapoint.metoffice.gov.uk/public/data/"). -define(WXFCS_SITELIST, @@ -56,24 +58,23 @@ [ #xmlAttribute{value=ID} ] = xmerl_xpath:string("@id", Node), io:format(Location++", "++ID++"~n"), print_locations(Rest). -get_weather(Location) -> +get_weather(Location, { Date_formatted, Rep }) -> inets:start(), Key = readapikey(), URL = ?BASE_URL ++ ?WXFCS_LOCATIONID ++ Location ++ "?key=" ++ Key ++ "&res=3hourly", - Date_today = erlang:localtime(), - { Date_formatted, Rep } = date_and_rep(Date_today), try { ok, {_Status, _Headers, Body }} = httpc:request(get, {URL, []}, [{timeout, timer:seconds(10)}], []), { Xml, _Rest } = xmerl_scan:string(Body), [ [ #xmlAttribute{value=Direction} ], [ #xmlAttribute{value=Speed} ], [ #xmlAttribute{value=Gust} ], [ #xmlAttribute{value=Weather} ], - [ #xmlAttribute{value=Temperature} ] ] = lists:map(fun(X) -> + [ #xmlAttribute{value=Temperature} ] ] = lists:map( + fun(X) -> xmerl_xpath:string("//Period[@value='" ++ Date_formatted ++ "']/Rep[.='" ++ Rep ++ "']/@"++X, Xml) end, ["D", "S", "G", "W", "T"]), {Direction, Speed, Gust, Weather, Temperature} catch @@ -81,11 +82,10 @@ io:format("API Might be down~n"), Reason after maybe_quit() end. - date_and_rep(Date) -> {{_Year, _Month, _Day}, {Hours, _Minutes, _Seconds}} = Date, date_and_rep(Date, Hours). @@ -101,20 +101,21 @@ format_date(Date_to_format) -> {{Year, Month, Day}, {_Hours, _Minutes, _Seconds}} = Date_to_format, %Thanks to: http://stackoverflow.com/a/7599506/208793 - DateAsString = io_lib:format("~4..0w-~2..0w-~2..0wZ", [Year, Month, Day]), - lists:flatten(DateAsString). + Date_as_string = io_lib:format("~4..0w-~2..0w-~2..0wZ", [Year, Month, Day]), + lists:flatten(Date_as_string). nth_wrap(N, List) -> Rem = N rem (length(List)), if Rem > 0 -> lists:nth(Rem, List); Rem =:= 0 -> - 1 + %Get last of list + hd(lists:reverse(List)) end. find_next_day(Date_today) -> %get in seconds @@ -126,47 +127,178 @@ maybe_quit() -> Args = init:get_arguments(), Found = lists:keyfind(noshell, 1, Args), if Found =:= false -> dont_quit; - true -> init:stop() - end. + true -> + init:stop() + end. + + +%From: http://www.codecodex.com/wiki/index.php?title=Round_a_number_to_a_specific_decimal_place#Erlang +round(Number, Precision) -> + P = math:pow(10, Precision), + round(Number * P) / P. -%For command line usage, from http://stackoverflow.com/a/8498073/208793 -headsilose([Location, Heading]) -> - headsilose(Location, Heading). -headsilose(Location, Heading) -> - {Direction, Speed, Gust, Weather, Temperature} = get_weather(Location), - Weather_type = weather_types:weather_type(erlang:list_to_integer(Weather)), +build_list_of_wind_directions(Wind_direction) -> + %There is only one wind direction, but can build groups of directions that will count as head, side and tail winds Compass = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"], %Quicker dirtier(?) way to do below would be: http://stackoverflow.com/a/6762191/208793 - Index = length(lists:takewhile(fun(X) -> X =/= Direction end, Compass))+1, + Index = length(lists:takewhile(fun(X) -> X =/= Wind_direction end, Compass))+1, %Since heading is to direction and winds are from, opposite is -2 to +2, or to make it easier to wrap, +14 +18 %Since heading is to direction and winds are from, sidewinds are -5 to -3 and +3 to +5, or to make it easier to wrap, +14 +18 %And so on - HeadwindList = lists:seq(14,18), - SidewindList = lists:seq(3,5)++lists:seq(11,13), - TailwindList = lists:seq(6,10), - [Headwinds, Sidewinds, Tailwinds] = lists:map(fun(WindList) -> - lists:map(fun(X) -> - nth_wrap(Index+X, Compass) end, - WindList) + Headwind_list = lists:seq(14,18), + Sidewind_list = lists:seq(3,5)++lists:seq(11,13), + Tailwind_list = lists:seq(6,10), + lists:map( + fun(Wind_list) -> + lists:map( + fun(X) -> + nth_wrap(Index+X, Compass) + end, + Wind_list) + end, + [Headwind_list, Sidewind_list, Tailwind_list]). + + +convert_lats_longs_to_distance_heading([_Head1 | [ _Head2 | Rest]]) -> + %All co-ords are diff, so just ignore first two + convert_lats_longs_to_distance_heading_(Rest, []). +convert_lats_longs_to_distance_heading_([Lat | [Lon | Rest]], Distance_headings_list) -> + %Want to map through the list convert co-ords to distance and heading + Distance = math:sqrt(math:pow(Lat,2) + math:pow(Lon,2)), + Heading_signed = math:atan2(Lon, Lat), + %Need to convert heading into a 2π value + Heading = if Heading_signed < 0 -> + Heading_signed + 2*math:pi(); + true -> + Heading_signed + end, + convert_lats_longs_to_distance_heading_(Rest, [{Distance, Heading}]++Distance_headings_list); +convert_lats_longs_to_distance_heading_([], Distance_headings_list) -> + lists:reverse(Distance_headings_list). + + +journey(Distance_headings_list) -> + lists:map( + fun({Distance, Heading}) -> + Compass_direction = get_compass_direction_for(Heading), + {Distance, Compass_direction} + end, + Distance_headings_list). + + +reverse_journey(Distance_headings_list) -> + %Don't actually need a correctly ordered reverse route, as long as we have directions and distances. + lists:map( + fun({Distance, Heading}) -> + %Because can't have functions in guards + Pi = math:pi(), + Reverse_heading = if Heading < Pi -> + Heading + Pi; + Heading >= Pi -> + Heading - Pi + end, + Compass_direction = get_compass_direction_for(Reverse_heading), + {Distance, Compass_direction} end, - [HeadwindList, SidewindList, TailwindList]), - [Headwind, Sidewind, Tailwind] = lists:map(fun(Winds) -> - lists:member(Heading, Winds) end, + Distance_headings_list). + + +get_compass_direction_for(Heading) -> + %In a way this is a waste of time as could just do headwind, etc based on angles, but since already have some code, why not? + Segment = 2*math:pi()/16, + Segments = erlang:round(Heading/Segment)+1, + Compass = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"], + %Handle the case of Segments = 16. Need to wrap around. + nth_wrap(Segments, Compass). + + +head_side_or_tail_wind(Direction, [Headwinds, Sidewinds, Tailwinds]) -> + [Headwind, Sidewind, Tailwind] = lists:map( + fun(Winds) -> + lists:member(Direction, Winds) + end, [Headwinds, Sidewinds, Tailwinds]), if Headwind -> - io:format("Heads you lose!~n"); + headwind; Sidewind -> - io:format("It's a draw~n"); + sidewind; Tailwind -> - io:format("Tails you win!~n") + tailwind + end. +%Something like that? + + +%First two for command line usage +headsilose([Location]) -> + headsilose_(Location); +headsilose([Location, Route_choice]) -> + headsilose_(Location, Route_choice). +headsilose_(Location) -> + %If route choice not specified default to default! + %The other choice is "alternative_geometries", for now I think there is only ever one alternative so pick this first. + headsilose_(Location, "route_geometry"). +headsilose_(Location, Route_choice) -> + Date_today = erlang:localtime(), + { Date_formatted, Rep } = date_and_rep(Date_today), + {Direction, Speed, Gust, Weather, Temperature} = get_weather(Location, { Date_formatted, Rep }), + Weather_type = weather_types:weather_type(erlang:list_to_integer(Weather)), + [Headwinds, Sidewinds, Tailwinds] = build_list_of_wind_directions(Direction), + {_Checksum, Polyline} = osrm:read_route(Route_choice), + Polyline_decoded = polyline:decode(Polyline), + Distances_and_headings_list = convert_lats_longs_to_distance_heading(Polyline_decoded), + %A better representation than 360 or 1080 would be better now this is used here as well. + Journey = if Rep =:= "360" -> + journey(Distances_and_headings_list); + Rep =:= "1080" -> + reverse_journey(Distances_and_headings_list) + end, + %If now have a set of co-ords need to figure out distances and directions + Distances_and_wind_type_list = lists:map( + fun({Distance, Compass}) -> + Wind_type = head_side_or_tail_wind(Compass, [Headwinds, Sidewinds, Tailwinds]), + {Distance, Wind_type} + end, + Journey), + [Headwind_distances, Sidewind_distances, Tailwind_distances] = lists:map( + fun(Wind_type_filter) -> + lists:filter( + fun({_Distance, Wind_type}) -> + Wind_type == Wind_type_filter + end, + Distances_and_wind_type_list) + end, + [headwind, sidewind, tailwind]), + [Sum_of_headwind_distances, Sum_of_sidewind_distances, Sum_of_tailwind_distances] = lists:map( + fun(Wind_type) -> + lists:foldl( + fun({Distance, _Wind}, Sum) -> + Distance + Sum + end, + 0, + Wind_type) + end, + [Headwind_distances, Sidewind_distances, Tailwind_distances]), + Total_distance = Sum_of_headwind_distances + Sum_of_sidewind_distances + Sum_of_tailwind_distances, + %Determine which is worse + if (Sum_of_headwind_distances > Sum_of_sidewind_distances) and (Sum_of_headwind_distances > Sum_of_tailwind_distances) -> + io:format("Heads you lose!~n"); + (Sum_of_tailwind_distances > Sum_of_sidewind_distances) and (Sum_of_tailwind_distances > Sum_of_headwind_distances) -> + io:format("Tails you win!~n"); + (Sum_of_sidewind_distances >= Sum_of_headwind_distances) and (Sum_of_sidewind_distances >= Sum_of_tailwind_distances) -> + io:format("It's a draw~n") end, + Headwind_percent = round((Sum_of_headwind_distances/Total_distance)*100, 2), + Sidewind_percent = round((Sum_of_sidewind_distances/Total_distance)*100, 2), + Tailwind_percent = round((Sum_of_tailwind_distances/Total_distance)*100, 2), + io:format("~w% Headwind~n~w% Sidewind~n~w% Tailwind~n", [Headwind_percent, Sidewind_percent, Tailwind_percent]), +% io:format("Direction: ~s~nSpeed: ~s mph~nGust: ~s mph~nWeather type: ~s~nTemperature: ~s deg C~n", [Direction, Speed, Gust, Weather_type, Temperature]). %Need to read API key from file readapikey() -> - {_Status, KeyB} = file:read_file(os:getenv("HOME") ++ "/.datapoint"), - string:strip(erlang:binary_to_list(KeyB),right,$\n). + {_Status, Key} = file:read_file(os:getenv("HOME") ++ "/.datapoint"), + string:strip(erlang:binary_to_list(Key),right,$\n). ADDED osrm.erl Index: osrm.erl ================================================================== --- osrm.erl +++ osrm.erl @@ -0,0 +1,72 @@ +-module(osrm). +-export([get_route/1, get_route/2, read_route/1]). +-import(polyline, [decode/1]). + +%https://github.com/Project-OSRM/osrm-backend/wiki/Server-api +%For now, get weather for one location (probably good enough as relatively short distances weather wise; ultimately consider time as well?) +%To get lats and longs could also do a query for here: http://www.uk-postcodes.com/ (json again) + +-define(BASE_URL, + "http://router.project-osrm.org/"). + +-define(VIAROUTE, + "viaroute"). + +%Need hints and checksum +%First one for command line usage +%%These will be strings from the command line +get_route([Start_lat, Start_lon, Finish_lat, Finish_lon]) -> + get_route([Start_lat, Start_lon], [Finish_lat, Finish_lon]). +get_route([Start_lat, Start_lon], [Finish_lat, Finish_lon]) -> + inets:start(), + URL = ?BASE_URL ++ ?VIAROUTE ++ "?loc=" ++ Start_lat ++ "," ++ Start_lon ++ "&loc=" ++ Finish_lat ++ "," ++ Finish_lon, + %Need UA to work with OSRM API + UA = "Mozilla/5.0 (X11; NetBSD i386; rv:28.0) Gecko/20100101 Firefox/28.0", + try + %Handling timeouts: http://stackoverflow.com/a/14143762/208793 + { ok, {_Status, _Headers, Body }} = httpc:request(get, {URL, [{"User-Agent", UA}]}, [{timeout, timer:seconds(10)}], []), + %For development purposes, write this out and keep it + _Write_status = file:write_file(os:getenv("HOME") ++ "/.headsilose_route", Body), + %Need to catch file write errors as well + Body + catch + _:Reason -> + io:format("API Might be down~n"), + Reason + after + maybe_quit() + end. + + +read_route(Route_choice) -> + {_Status, Route} = file:read_file(os:getenv("HOME") ++ "/.headsilose_route"), + %Use jiffy + %http://www.snip2code.com/Snippet/51463/how-to-support-chinese-in-http-request-b/ + { Props } = jiffy:decode(Route), + Route_geometry = if Route_choice =:= "route_geometry" -> + proplists:get_value(binary:list_to_bin(Route_choice), Props); + Route_choice =:= "alternative_geometries" -> + %hd only if alternative though! + %for now I think there is only ever one alternative so that is why we pick hd + hd(proplists:get_value(binary:list_to_bin(Route_choice), Props)) + end, + %That is all for now? Because... + %And these don't seem to actually be returned, hence having to go down the route of polyline decoding + %Route_Instructions = proplists:get_value(<<"route_instructions">>, Props), + %Total_Distance = proplists:get_value(<<"total_distance">>, proplists:get_value(<<"route_summary">>, Props)), + %And need to figure out how to get nested values. I.e like xpath. Just nest the queries? Nope that doesn't work + %Like so: + {Hint_data} = proplists:get_value(<<"hint_data">>, Props), + Checksum = proplists:get_value(<<"checksum">>, Hint_data), + Route_geometry_as_string = binary:bin_to_list(Route_geometry), + {Checksum, Route_geometry_as_string}. + + +maybe_quit() -> + Args = init:get_arguments(), + Found = lists:keyfind(noshell, 1, Args), + if Found =:= false -> + dont_quit; + true -> + init:stop() + end. Index: polyline.erl ================================================================== --- polyline.erl +++ polyline.erl @@ -6,13 +6,13 @@ decode(Encoded_polyline) -> %Steps 11 back to 8 Six_bit_chunks = six_bit_chunks(Encoded_polyline), %Step 8 - List_of_groups_of_chunks = split_up_six_bits(Six_bit_chunks), + Groups_of_chunks_list = split_up_six_bits(Six_bit_chunks), %Step 8 back to 6 - Five_bit_chunks = five_bit_chunks(List_of_groups_of_chunks), + Five_bit_chunks = five_bit_chunks(Groups_of_chunks_list), %---TODO %Maybe some more of the below need splitting out into different functions or nesting in a map? %Which option to go for, a function that maps as per five_bit_chunks, or mapping functions as per below? %I don't think I can map all functions in one go because ultimately need to change number of members in groups. %I.e. following will go from groups of five to eight. @@ -51,57 +51,57 @@ end, %Step 2 back to 1 Decoded = if Last_bit =:= "1" -> -1 * Final_binary/100000; true -> - Final_binary/100000 + Final_binary/100000 end, Decoded end, Eight_bit_chunks), Results. %Step 8 - Split up six bit chunks, per the 0x20 bit -split_up_six_bits(List_of_bit_chunks) -> - split_up_six_bits_(List_of_bit_chunks, [], []). -split_up_six_bits_([Head | Tail], Group_of_bit_chunks, List_of_groups_of_bit_chunks) when [hd(Head)] == "1" -> - split_up_six_bits_(Tail, [Head]++Group_of_bit_chunks, List_of_groups_of_bit_chunks); -split_up_six_bits_([Head | Tail], Group_of_bit_chunks, List_of_groups_of_bit_chunks) when [hd(Head)] == "0" -> +split_up_six_bits(Bit_chunks_list) -> + split_up_six_bits_(Bit_chunks_list, [], []). +split_up_six_bits_([Head | Tail], Group_of_bit_chunks, Groups_of_bit_chunks_list) when [hd(Head)] == "1" -> + split_up_six_bits_(Tail, [Head]++Group_of_bit_chunks, Groups_of_bit_chunks_list); +split_up_six_bits_([Head | Tail], Group_of_bit_chunks, Groups_of_bit_chunks_list) when [hd(Head)] == "0" -> %Then need to start a new list, but after this 0 one! - split_up_six_bits_(Tail, [], [lists:reverse([Head]++Group_of_bit_chunks)]++List_of_groups_of_bit_chunks); -split_up_six_bits_([], Group_of_bit_chunks, List_of_groups_of_bit_chunks) when length(Group_of_bit_chunks) > 0 -> - split_up_six_bits_([], [], [lists:reverse(Group_of_bit_chunks)]++List_of_groups_of_bit_chunks); -split_up_six_bits_([], [], List_of_groups_of_bit_chunks) -> + split_up_six_bits_(Tail, [], [lists:reverse([Head]++Group_of_bit_chunks)]++Groups_of_bit_chunks_list); +split_up_six_bits_([], Group_of_bit_chunks, Groups_of_bit_chunks_list) when length(Group_of_bit_chunks) > 0 -> + split_up_six_bits_([], [], [lists:reverse(Group_of_bit_chunks)]++Groups_of_bit_chunks_list); +split_up_six_bits_([], [], Groups_of_bit_chunks_list) -> %TODO Might be neater to map lists:reverse over the list instead of doing above and here. - lists:reverse(List_of_groups_of_bit_chunks). + lists:reverse(Groups_of_bit_chunks_list). %Step 5 %TODO See if better way of doing this -eight_bit_chunks(List_of_five_bit_chunks) -> - Five_bit_chunk_string = lists:reverse(lists:flatten(List_of_five_bit_chunks)), +eight_bit_chunks(Five_bit_chunks_list) -> + Five_bit_chunk_string = lists:reverse(lists:flatten(Five_bit_chunks_list)), eight_bit_chunks_(Five_bit_chunk_string, []). -eight_bit_chunks_(Five_bit_chunk_string, List_of_eight_bit_chunks) when length(Five_bit_chunk_string) > 8 -> +eight_bit_chunks_(Five_bit_chunk_string, Eight_bit_chunks_list) when length(Five_bit_chunk_string) > 8 -> Eight_bit_chunk = lists:reverse(lists:sublist(Five_bit_chunk_string,1,8)), Rest_of_five_bit_chunk_string = lists:nthtail(8,Five_bit_chunk_string), - eight_bit_chunks_(Rest_of_five_bit_chunk_string, [Eight_bit_chunk]++List_of_eight_bit_chunks); -eight_bit_chunks_(Five_bit_chunk_string, List_of_eight_bit_chunks) when length(Five_bit_chunk_string) =< 8, Five_bit_chunk_string /= [] -> + eight_bit_chunks_(Rest_of_five_bit_chunk_string, [Eight_bit_chunk]++Eight_bit_chunks_list); +eight_bit_chunks_(Five_bit_chunk_string, Eight_bit_chunks_list) when length(Five_bit_chunk_string) =< 8, Five_bit_chunk_string /= [] -> Padded_bit_string = pad_to(8, lists:reverse(Five_bit_chunk_string)), - eight_bit_chunks_([], [Padded_bit_string]++List_of_eight_bit_chunks); -eight_bit_chunks_([], List_of_eight_bit_chunks) -> - List_of_eight_bit_chunks. + eight_bit_chunks_([], [Padded_bit_string]++Eight_bit_chunks_list); +eight_bit_chunks_([], Eight_bit_chunks_list) -> + Eight_bit_chunks_list. six_bit_chunks(Encoded_polyline) -> six_bit_chunks_(Encoded_polyline, []). -six_bit_chunks_([Head | Rest], List_of_chunks) -> +six_bit_chunks_([Head | Rest], Chunks_list) -> Six_bit_chunk = six_bit_chunk(Head), %Add to Reversed_chunks - six_bit_chunks_(Rest, [Six_bit_chunk]++List_of_chunks); -six_bit_chunks_([], List_of_chunks) -> - lists:reverse(List_of_chunks). + six_bit_chunks_(Rest, [Six_bit_chunk]++Chunks_list); +six_bit_chunks_([], Chunks_list) -> + lists:reverse(Chunks_list). six_bit_chunk(Ascii_bit) -> %Step 10 Shifted_bit = Ascii_bit - 63, @@ -113,11 +113,11 @@ %Well in that case, I guess that means we'd want to split, but for now pad to six and check for 0x20 elsewhere. %--- pad_to(6, Binary_chunk). -five_bit_chunks(List_of_groups_of_chunks) -> +five_bit_chunks(Groups_of_chunks_list) -> lists:map( fun(Group_of_chunks) -> %Step 7 - Un-reverse the five bit chunks lists:reverse(lists:map( fun(Chunk) -> @@ -124,24 +124,24 @@ %Step 8 - "Un-or" the 0x20 bit lists:sublist(Chunk,2,6) end, Group_of_chunks)) end, - List_of_groups_of_chunks). + Groups_of_chunks_list). %I can't figure out padding with io:format etc when printing binary numbers pad_to(Length, Binary_string) when length(Binary_string) < Length -> Padded_binary_string = "0"++Binary_string, pad_to(Length, Padded_binary_string); pad_to(Length, Binary_string) when length(Binary_string) == Length -> Binary_string. - + %bnot doesn't seem to work as I thought it would so do it very inelegantly by switching each "bit" in a string. bin_flip(Binary_number) -> - Binary_string = hd(io_lib:format("~.2B", [Binary_number])), + Binary_string = hd(io_lib:format("~.2B", [Binary_number])), bin_flip_(Binary_string, []). bin_flip_([Head | Rest], Flipped_string) -> Head_bit = hd(io_lib:format("~c",[Head])), Flipped_bit = if Head_bit =:= "0" -> "1";