Weekend Scripter: Latitude, Longitude, and PowerShell

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to retrieve latitude and longitude information.

Microsoft Scripting Guy, Ed Wilson, is here. Today it is cold and dreary, so I thought it would be a nice time to play around with Windows PowerShell. One of the things I have always wanted to do is use Windows PowerShell to find the latitude and longitude for a specific address. I know there are webpages where I can look up this information—but if I ever want to do anything with latitude and longitude in a Windows PowerShell script, I had better know how to do the lookup.

So, I started hunting around for an easy-to-use web service. I found one. Well, it is not really easy-to-use, but it does have the benefit of being free. The disadvantage is that I have to provide an almost complete address. At a minimum, it requires the street address, city, and state. Also, I believe it only works for addresses in the United States, which basically leaves out over half of my readers.

Another disadvantage is the way I have to encode the question into a string. This becomes more or less a manual process. I tried to use the URLEncode method from the WebUtility class, but all that did was really munge things. I wish I could provide only a city, state, and country and get a generic latitude and longitude. I also wish there was a better way to supply these values instead of having to encode them in a string.

I will admit that I have not spent all day playing with this service, nor have I spent more than about a half hour searching for a better web service. If you find something better, let me know. I did find that Google has a pretty good API for this, but the terms say it is for displaying on a Google map, which is not what I want to do.

After I have a latitude and longitude, there are many cool things I can do. For instance, I now have a GPS receiver for my camera that writes latitude and longitude on my pictures. But before that I did not, so I could get a latitude and longitude for a specific location, and then write that information to all of my pictures in a specific folder (which are sorted by location anyway).

I can also look up weather, sunrise, and sunset information. And I can do a reverse lookup—so I can look up an address based on the latitude and longitude from a picture I have.

The hard parts about using this web service are encoding the address and finding a specific address that it is able to resolve. For example, it could not find my house in Charlotte, North Carolina in the United States. But it was able to find the White House (not in Charlotte, of course).

The first thing I need to do is to create a string that indicates the interface I want to use and the address. The address is separated by street, city, and state. When I come to a space, I use a plus sign ( + ). I am using an interface that returns the information as comma-separated values. Other interfaces are available by checking the geocoder.us website. Here is my string:

$uri = 'http://rpc.geocoder.us/service/csv?address=1+Times+Square,+New+York+NY'

Now I use the Invoke-RestMethod cmdlet, specify that I am using the Get method, and capture my results in a variable:

$csv = Invoke-RestMethod -Method Get -Uri $uri

The contents of the $csv variable are shown here:

PS C:\> $csv

40.756639,-73.986287,1 Times Sq,New York,NY,10036

The results are: Latitude, Longitude, Street, City, State, and Zip code. Obviously, I already know the street, city, and state because that is what I supplied to the call. So I can capture the first and second elements. I do this by using the Split method to split my string at the comma. I then store the results in variables. The first element (0) contains my latitude, and the second element (1) contains the longitude. This is shown here:

$lat = ($csv -split ',')[0]

$long = ($csv -split ',')[1]

So that is it. Here is the complete script:

$uri = 'http://rpc.geocoder.us/service/csv?address=1+Times+Square,+New+York+NY'

$csv = Invoke-RestMethod -Method Get -Uri $uri

$lat = ($csv -split ',')[0]

$long = ($csv -split ',')[1]

$lat, $long

The script and the output from the script are shown in the following image:

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon