Skip to content

Instantly share code, notes, and snippets.

@jamessdixon
Created July 1, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamessdixon/b3c042a33aa87a1fdc16 to your computer and use it in GitHub Desktop.
Save jamessdixon/b3c042a33aa87a1fdc16 to your computer and use it in GitHub Desktop.
TAMGeoLocation and TheCounted Dataset
#r "../packages/FSharp.Data.2.2.2/lib/net40/FSharp.Data.dll"
open System
open System.IO
open System.Text
open FSharp.Data
[<Literal>]
let geoLocationSample = "..\Data\TAMUHttpGet.json"
type GeoLocationServiceContext = JsonProvider<geoLocationSample>
let getGeoCoordinates(streetAddress:string, city:string, state:string) =
let apiKey = "xxxxx"
let stringBuilder = new StringBuilder()
stringBuilder.Append("https://geoservices.tamu.edu/Services/Geocode/WebService/GeocoderWebServiceHttpNonParsed_V04_01.aspx") |> ignore
stringBuilder.Append("?streetAddress=") |> ignore
stringBuilder.Append(streetAddress) |> ignore
stringBuilder.Append("&city=") |> ignore
stringBuilder.Append(city) |> ignore
stringBuilder.Append("&state=") |> ignore
stringBuilder.Append(state) |> ignore
stringBuilder.Append("&apiKey=") |> ignore
stringBuilder.Append(apiKey) |> ignore
stringBuilder.Append("&version=4.01") |> ignore
stringBuilder.Append("&format=json") |> ignore
let searchUri = stringBuilder.ToString()
let searchResult = GeoLocationServiceContext.Load(searchUri)
let firstResult = searchResult.OutputGeocodes |> Seq.head
firstResult.OutputGeocode.Latitude, firstResult.OutputGeocode.Longitude, firstResult.OutputGeocode.MatchScore
[<Literal>]
let theCountedSample = "..\Data\TheCounted.csv"
type TheCountedContext = CsvProvider<theCountedSample>
let theCountedData = TheCountedContext.Load(theCountedSample)
let theCountedGeoLocated = theCountedData.Rows
|> Seq.map(fun r -> r, getGeoCoordinates(r.Streetaddress, r.City, r.State))
|> Seq.toList
|> Seq.map(fun (r,(lat,lon,ms)) -> String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15}",
r.Name,r.Age,r.Gender,r.Raceethnicity,r.Month,r.Day,r.Year, r.Streetaddress, r.City,r.State,r.Cause,r.Lawenforcementagency,r.Armed,lat,lon,ms))
let baseDirectory = __SOURCE_DIRECTORY__
let baseDirectory' = Directory.GetParent(baseDirectory)
let filePath = "Data\TheCountedWithGeo.csv"
let fullPath = Path.Combine(baseDirectory'.FullName, filePath)
File.WriteAllLines(fullPath,theCountedGeoLocated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment