Geocoding Using Texas A&M Service and F#

Geocoding is the technique of taking an address and turning it in to a geocordinate (latitude and longitude).  There are plenty of geocoding services out there (notably Google and Bing) but I decided to try a lesser know (though apparently just as good) service from Texas A&M found here.

It took about 30 seconds to create an account with the only downer is that they only give you 2,500 calls (and it is not clear if that is per day/month/forever).  In any event, the documentation was easy enough to work though and since they seem to offer the data in a variety of formats, I picked the json type provider.  Also, their documentation starts off with POST examples, which a bit harder to mange using a TP, but they do support GETs with the parameters in the query string.

I fired up Visual Studio and started a new FSharp project.  The first thing I did was pull down their sample json result which I saved as a local file to the project folder (I also put a sample XML in there in case the json provider didn’t work out.  Ironically, I would not get the XmlProvider working but the Json one worked like a champ):



I then added in some code to make the request.  A majority of the code is creating the query string:

1 #r "../packages/FSharp.Data.2.2.2/lib/net40/FSharp.Data.dll" 2 3 open System.IO 4 open System.Text 5 open FSharp.Data 6 7 [<Literal>] 8 let sample = "C:\Users\Dixon\Desktop\SampleApp_CSharp\ChickenSoftware.Geolocation.Solution\Data\TAMUHttpGet.json" 9 10 type Context = JsonProvider<sample> 11 12 let streetAddress = "904 Strathorn Drive" 13 let city = "Cary" 14 let state = "NC" 15 let zip = "27519" 16 let apiKey = "XXXXXXX" 17 18 let stringBuilder = new StringBuilder() 19 stringBuilder.Append("") |> ignore 20 stringBuilder.Append("?streetAddress=") |> ignore 21 stringBuilder.Append(streetAddress) |> ignore 22 stringBuilder.Append("&city=") |> ignore 23 stringBuilder.Append(city) |> ignore 24 stringBuilder.Append("&state=") |> ignore 25 stringBuilder.Append(state) |> ignore 26 stringBuilder.Append("&zip=") |> ignore 27 stringBuilder.Append(zip) |> ignore 28 stringBuilder.Append("&apiKey=") |> ignore 29 stringBuilder.Append(apiKey) |> ignore 30 stringBuilder.Append("&version=4.01") |> ignore 31 stringBuilder.Append("&format=json") |> ignore 32 33 let searchUri = stringBuilder.ToString() 34 let searchResult = Context.Load(searchUri) 35 36 let firstResult = searchResult.OutputGeocodes |> Seq.head 37 firstResult.OutputGeocode.Latitude 38 firstResult.OutputGeocode.Longitude 39 firstResult.OutputGeocode.MatchScore 40 41 42 43 44

And sure enough: data that is correct


FSharp made it stupid simple to consume this service and the only real gotchas I found were in the documentation itself:

1) The MatchScore is a decimal but the json sample has it as “100” so it was inferred as an int.  I replaced the value as “98.4023668639053” to force the correct type

2) The documentation’s formats are listed as this


But since they had samples in json below, I just added in

1 stringBuilder.Append("&format=json") |> ignore

and it worked fine.

You can see the gist here.


One Response to Geocoding Using Texas A&M Service and F#

  1. Pingback: F# Weekly #24, 2015 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: