Archive for the ‘GIS’ Category

Displaying a Trail in a Google Map with KML

Tuesday, March 18th, 2008

RSSIf you like this post, then consider subscribing to my full RSS feed. Subscribe now and you will get an offer that's only available to folks that read my feed!

This is sort of Part 2 on displaying trails (line segments) in a Map.

Last year I assisted the Indianapolis Cultural Trail by mapping out the incredible cultural bike and walkways that are being constructed in Indianapolis using Google Earth. Part 1 was how to utilize Google Earth to plot your trails and export them to a KML file.

Tonight, I finally sent off a map that had been residing in my test directory over to Ian to push up to the Indy Cultural Trail site. This will allow visitors to zoom in, change to a satellite view, and interact with the map much more than a static image.

Indy Cultural Trail Map

Added to my urgency at getting this completed that I spoke with Gail Swanstrom and Brian Payne (President, Central Indiana Community Foundation) on Saturday after the Bill McKibben event. Gail and Brian are both incredible folks - kind, full of energy, and oh so patient. I couldn’t let them down.

One project down! A few more to go! When the map is posted, I’ll update this post with a link.

Add Your Site Location to your Sitemap with a KML File

Monday, October 15th, 2007

You may not know this, but Google will actually index your site’s geographic location along with your other pages. This can be done best by supplying a KML file with your coordinates in an XML format - a format that’s easy to read by programming interfaces.

Don’t let this intimidate you! It’s quite easy to build a KML file and add it to your site. In fact, I have a website that will build your KML file so you can download it, Address Fix. I added the features for downloading today!

Build a KML File the easy way:

Enter your address in Address Fix and submit. If the location on the map isn’t accurate, you can drag your marker to the exact location (pretty cool, huh?). Now you’ll see a “Download” link in the title of the KML section. When you click this, you can download the file to upload to your site later.

Download a KML File from Address Fix

I would also edit the file (just use any text editor) and add your blog’s name between the description tags. Example:

<description>The name of my site</description>

Add the KML to your Sitemap:

If you’re running WordPress, you have to be running the XML Sitemap Generator Plugin by Arne Brachhold - you won’t find a better or more essential plugin anywhere! One of the many great features of this plugin is that you can add a KML file to it. Just enter the full URL of the sitemap in the Additional Pages section:
Add KML to Sitemap

If you don’t have WordPress, you’ll find instructions at Google on how to add your KML reference to your sitemap.

That’s it! Build the KML file, upload the file to your site, and add it to your Sitemap.

PHP and MySQL: Calculating Distance

Saturday, September 15th, 2007

This month I’ve been programming quite a bit in PHP and MySQL with respect to GIS. Snooping around the net, I actually had a hard time finding some of the Geographic calculations to find the distance between two locations so I wanted to share them here.

Flight Map EuropeIf you remember ‘the old days’ of calculating a distance between two points, it was simply the hypotenuse of a triangle (A² + B² = C²).

That’s an interesting start but it doesn’t apply with Geography since the distance between lines of latitude and longitude are not an equal distance apart. As you get closer to the equator, lines of latitude get further apart. If you use some kind of simple triangulation equation, it may measure distance accurately in one location and terribly wrong in the other, because of the curvature of the Earth.

That brings up the Haversine formula, which uses trigonometry to allow for the curvature of the earth. When you’re finding the distance between 2 places on earth (as the crow flies), a straight line is really an arc. This is applicable in air flight - have you ever looked at the actual map of flights and noticed they are arched? That’s because it’s shorter to fly in an arch between two points sometimes than directly to the location.

Anyways, here’s the PHP formula for calculating the distance between two points (along with Mile vs. Kilometer conversion) rounded to two decimal places:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit) {
case ‘Mi’: break;
case ‘Km’ : $distance = $distance * 1.609344;
}return (round($distance,2));
}

It’s also possible to use MySQL to do a calculation to find all records within a specific distance. In this example, I’m going to query MyTable to find all the records that are less than or equal to variable $distance (in Miles) to my location at $latitude and $longitude:

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM `MyTable` WHERE distance <= ".$distance."

For Kilometers:

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM `MyTable` WHERE distance <= ".$distance."

I utilized similar computations in the Wild Birds Unlimited website.

A Sneak Peak at Wild Birds Unlimited

Wednesday, September 12th, 2007

With 60 hour weeks at work the last few weeks, it’s been a challenge to add another 20 or 30 on the mapping project I’m doing for Wild Birds Unlimited. Tomorrow’s a big day, though, when WBU shows the functionality off to some of its franchisees.

Wild Birds Unlimited Preview

We’ve really squeezed a lot of functionality into this site, and we hope to do much more. There’s a robust Administrative back-end where stores can even update their own information or tweak their geographic location. Some other features:

  1. GeoIP localization that determines whether or not to display in metric or standard. The GeoIP predicts your location and region and plots you on the map based on the IP address requesting the page.
  2. The custom markers were my design and are loaded, not dynamically with JavaScript, but with a KML file! This provides quicker page loads with the markers loading after. As you move on the map, Google takes care of displaying the points so I don’t have to requery the database.
  3. The information windows are a combination. If you click on the map, they are from the KML file. If you click on the locations in the sidebar, they load in a layer relative to the map.
  4. Directions are also included, if you provide more of an address than simply a state or province. This is the first time I’ve deployed the Google Directions, but it’s pretty awesome. One thing to note… I don’t actually pass the addresses to geocode, I simply add the actual latitude and longitude and mask them with a name (Here@43,-120).

Images are already enabled on the info windows, but we don’t actually have any images saved at this point. :) One step at a time. If you’d like to take a look, you can visit Wild Birds Unlimited Maps. I’d state that the software is Alpha ready to go Beta once we receive refinement requests from the client.

Special thanks to Stephen, he’s been my intern on this and has done a heck of a job. He’s moved to Germany for the school year but I look forward to continuing deploying code with him on this project. WBU is a fantastic organization and have been a joy to work with. We’re looking forward to over-delivering on this project as we hope to deploy this application for other organizations seeking a PHP Mapping application. Stephen will be my business partner… not bad for a guy still in high school!

Some additional resource have been Mike’s code examples as well as Ben, a developer with Rarebird who built a stunning implementation of Google Maps at Fanimation.