Showing posts with label Pie Chart. Show all posts
Showing posts with label Pie Chart. Show all posts

Wednesday, April 9, 2008

Google Earth and Charts Tutorial

This isn't really a tutorial in the proper sense of the term, but just a posted example of using a combination of KML, NetworkLink, ASP.NET generic handler (ashx), and Google Charts to create a dynamic symbolized map for Google Earth.

I started by downloading some 2000 census data in census block format, along with SF1 data, from ESRI's free download area. Then I brought it into Manifold selecting just a few census blocks for all of Fort Collins, Colorado, and created their inner centroids. I filtered some of the data that had 0 population from the SF1 table. Clearly, when/if you open this up you will find that I should have weeded a few more points, but on the other hand it shows how quick this runs. My server is pretty slow, and I tested this on a wireless internet connection that didn't have a very strong connection (plus I was streaming both Google Earth and some music). In Manifold, I related the two tables and exported it back out to a shapefile. On export I added two columns for lat and long.

In Visual Web Developer Express I created a generic handler (ashx). Instead of going into each line of code, I'll just summariz(s)e. Below is a link to a zip file where you can download both the KML file and the ASHX file. I used Sharpmap 0.9 to connect to the shapefile and read the table's contents. I just took the percentage of Males and Females (number of males / population, number of females / population) for my working statistic (truly groundbreaking stuff :)). You can see from the code that it basically just writes out the KML file, creating a Google Chart api url as it goes.

You can view the final product by opening this KML file in Google Earth. This KML file contains very little KML, and basically just has a Networklink back to the address of the handler.

If you want the files used, they are available here.

As always, any suggestions, comments, questions are much appreciated.

Here are some other examples I've found:
http://googlemapsapi.blogspot.com/2007/12/using-chart-api-in-kml-for-quick-data.html
http://www.barnabu.co.uk/interactive-spiders-and-charts/

EDIT:
Just noticed the Thematic Mapping Blog also has a post on using charts in Google Earth. Here is the link: http://blog.thematicmapping.org/2008/04/using-google-charts-with-kml.html
or with open layers http://blog.thematicmapping.org/2008/04/openlayers-and-google-chart-mashup.html

Monday, March 31, 2008

Mmmmmm Goooooogle Eaarrtth....

I have to admit, I didn't much bother with Google Earth when it first started making headlines (however many years ago that was). Sure like any Geonerd I downloaded and goofed around. "Oooh spinning globe (Wheeee!)." The imagery, of course, was stunning. It wasn't until recently that I've really found a use for it and started to delve into it. Now, I'm quite enamored with Google Earth.

The current project I'm working on is for the Department of Employment, Education, and Training....and water sports. The NT government has some good department names. I'm under the education department and there is currently a push to be able geolocate all the schools and associate this with their excellent data warehouse. The Northern Territory has some very remote communities (some still are lacking in power/electricity). I'm sure to fellow GISers it is obvious the need to be able to locate where your resources are going, and not going. A map is a simple way to do this. I'm the only GIS user working there and only on a part time basis at that, so it is important for everyone to be able to view the data when I'm not available to create a map. Their data warehouse is already running on SQL Server, and so I've been building my system on top of that (or next to). MapInfo was chosen as the GIS, and I'll describe my woes with this program later. I got into Google Earth, because another department had set up their on Google Earth server and had a site license for GE Enterprise. That's when I started to explore what we could do with it.

This is what I've come up with. I'm sure I'm not the first person to do this, but I thought I would at least share my methodology.

KML has a special section called network link. This allows the KML creator to instead of populating a file with the coordinates and geometry, to tie it to a web based server side script. This was extremely easy to do. I created a generic handler (ashx) in asp.net, and wrote a small class that created the response in the kml/xml format. The class pulled all of the coordinate information directly from the database server. Pretty cool to have the dynamic capability built into Google Earth. and only requiring a little server side scripting. Maybe 50 lines of code if that.

That's good, but it is just the location of schools. Obviously the description for the info window that pops up when the location is clicked can be populated with various data from the warehouse, but this might become a bit tedious to code. Instead I just placed a link to a profile page I had already set up. Unfortunately, you can't place an iframe in the window, because it isn't sophisticated enough to handle every scenario. No big deal.

What I really want to do is be able to display the data as a symbol. I could easily classify the symbols using some sort of nominal data stored in the database, or even some continuous data. I had originally created some maps with pie chart symbols in MapInfo showing the break-up of enrollment. I was asked if there was a way to click on the map and see what the numbers actually were. Well, this was a PDF, so the answer in that case was "no." I started to think if there was a way to do this with Google Earth, that would allow for the creation of a much more interactive environment. Using GDI+ and .NET, I was able to create a png image of a pie chart from the data in the warehouse, while programatically creating the kml file. Originally, I manually took this data and created a KMZ file. Then I started to use network links and tied my pie chart code in with my server side script. Now I have a dynamic kml file that creates pie charts on the fly as it is shown in Google Earth (images stored on the webserver). Now whenever it is opened up, it will reflect the current changes in the data warehouse. I plan on achieving the same effect with a proportional symbol. .NET has assemblies that allow me to compress data, so I could easily create a fixed kmz file based on this same method.

I would also like to do something similar using 3D. I attempted to create an extruded polygon based on the data. Unfortunately, these do not change their scale based on zoom. I looked around, but did not find anything. I'm thinking I could do this using regions. But at this point, I'm a little confused how regions work.

Anyway, it isn't difficult with a little programing to create a highly interactive map with Google Earth and ASP.NET.