Friday, April 28, 2006

Speeding Up GPolyline Loads

The GPolyline object from Google Maps API is very nice. You feed it an array of GPoints and it creates a line connecting those points. It creates an image overlay of the line on a Google server and sends it back to your map. Or if you're using IE, it will use the VML in IE to draw the line. You can tell it what color, width and opacity to set for the line.

I loaded the shapefile for the Superior trails and found that there were 475 lines with 5,772 data points. This was a pretty incredible load on the application. It takes a long time for a browser to load that much data on a page. And Google has to generate all those line images and send them back for you. So, I was trying to figure out a way to speed up the loading of the polylines. I read from some other sources that cutting down on the number of data points from the shapefile would help. But, I felt that with the hiking trails, I didn't want to lose any detail by leaving out data. Then I wondered if I couldn't use asynchronous requests (AJAX). If I could make each request for a line asynchronously, then it wouldn't seem like the application had died when you added the trails to the map. It worked really great. I built a little PHP script to be called asynchronously for each line (475 times). It would then query the database for the points for that line. It still takes a while to process that many requests, but the map does not block and wait while it's off fetching the lines.

As before, you can see it here: http://gregz.is-a-geek.net

Tuesday, April 25, 2006

Shapefile Coordinate Systems and Google Maps

While I worked for the town parks and rec. committee, I met the GIS consultant who did the town's mapping. I realized he could give me an ArcView shapfile of the trails and bike routes to put on my Google Maps mashup. I used shp2text which is a handy utility to convert shapfiles into either xml, gpx, or csv that I could then load into MySQL.

Problem. Once converted, I could tell the data points for the lines were not right. I knew what the long/lat coordinates were for points in the town and none of these matched from the shapefile dump. Looking back at my email from the GIS guy, he said the shapefile was "coordinate base is Colorado State Plane, Central Zone, NAD1983, Feet" Googling around for info on coordinate systems pointed out that of course the ArcView export was CO state. The Google Maps API expects WGS84 longitude/latitude coordinates.

Lucky for me my friend could easily produce a new shapefile export with the WGS84 coordinates. You can see an example of the GPolyline from the shapefile by clicking on "Show Bike Routes" on the map.

Friday, April 21, 2006

Hacking Google Maps Geocoding

Geocoding - the process of converting a street address to longitude/latitude. Necessary because the Google Maps API expects longitude/latitude numbers based on the WGS 84. And in order for your points to resolve correctly on the map you are using (Google/Navteq in my case), you would hope to have geocoding from that same source. And as we know Google does not offer this service.

I'm sure Navteq is holding Google to some agreement not to provide this service with their APIs. Think of it. Navteq's business is providing this data. They spend lots money with their high-tech cruiser vans traveling every road to collect the data. They charge Google big bucks for it. They don't want Google giving it away for free.

So what are your options since Google offers no geocoding service?

US Census TIGER data

You can use the geocoder.us free web service that has loaded this data for the US. I loaded the data for my county into MySQL. Hey, it's for a database course, right? So why not load the data and play with it. But what I found was the data showed a number of inaccuracies and there was quite a bit of missing data. It didn't map well onto Google.

Yahoo! Maps REST API

Yahoo! offers a REST API for gecoding. It's easy to use and open. My friend that works at Yahoo! was eager for me to use it. :) But similar problem. It works best when placing points on a Yahoo! map, not a Google Map. The points end up being off just enough.

Hack Google

Not a good idea for a commercial app (I'm sure Google might complain), but for my grad school project, I thought it might be worth exploring. And sure enough there is various scripts out there on doing just that. If you call a url like http://maps.google.com/maps?q=[your address here]&output=js (note the output=js at the end), Google returns a Javascript function with the longitude/latitude that you are looking for inside. However, just to prove that this is not an ideal solution, Google has changed the format of this output so that any of the previous hacks would no longer work. It's not a public API afterall! :) My script is shown below.




<html>
<head>
    <title>Google Geocode</title>
</head>

<body>
<h2>Google Geocode</h2>
<form name="mainform" action="">" method="GET">
    <b>Address:</b> <input type="text" size="40" name="address" value="">">
    <input type="submit" name="geo" value="Google Geocode">
</form>

<hr />
<b>Geo-location:</b>
<ul>
<?php
if (array_key_exists('geo', $_REQUEST)) {
    $result = get_long_lat($_REQUEST['address']);
    print_r("<li>Longitude: ".$result["longitude"]);
    print_r("<li>Latitude: ".$result["latitude"]);
}
?>
</ul>
</body>
</html>

<?php

function get_long_lat($q) {
  $q = urlencode($q);
  $gm = fopen('http://maps.google.com/maps?q=' . str_replace('','+',$q) . '&output=js','r');
  $tmp = stream_get_contents($gm);
  fclose($gm);
  $x = preg_replace('/.*\{center: \{lat: ([^,]*),lng: ([^}]*).*/', "|$1|$2|", trim($tmp));
  list($dmy,$lat_value, $lng_value) = explode("|",$x );
  return(array('longitude'=>$lng_value, 'latitude'=>$lat_value));

}
?>


My Superior trails map project includes an address field that uses this code to geocode the provided address and insert a marker on the map.

Thanks to Dan Moore for his insights and feedback on geocoding options.

Tuesday, April 18, 2006

Superior Google Maps Mashup

I am wrapping up a semester project for my graduate database course. The assignment was to build a dynamic website driven by a backend database. I chose to build a Google Maps mashup for my town (Superior, CO). Overlaying data about parks, trails, bike routes onto a Google Map.

I got the idea from Chicago Bike Map mashup. I'm an avid cyclist and I thought something like this would be cool for my local area. And I volunteered for the town on parks, recreation, and trails. I got to know the GIS consultant for the town and realized he would be able to provide the data I needed to overlay on Google Maps.

So I'll be posting nerdy updates here on things I've done and learned. Once I have the app hosted somewhere more permanent (currently on iBook laptop), I'll put up a pointer to it.

Update: I've got this hosted now. Go to, http://gregz.is-a-geek.net/

Monday, April 10, 2006

It Was a Moth!

Since my kids have been old enough to ask, they have wondered "Daddy, what kind of bugs do you have in your computer? Are they spiders! Or beetles! Tee Hee!" :)

Leave it to a thorough entry in the Wikipedia to answer the question. While the term bug has been around since engineers built things (Edison used the term as long ago as 1878), The first computer bug is popularly attributed to a 1947 Relay Calculator being tested at Harvard. You can see the actual bug at the Smithsonian. Here is a picture:So, the answer is "It was a moth!" that actually got stuck in the relay.

Friday, April 07, 2006

Solar Eclipse as Seen from Space

Check out this very cool photo of recent solar eclipse as captured from the International Space Station.