Showing posts with label KML. Show all posts
Showing posts with label KML. 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

Tuesday, April 8, 2008

More Fun With Goolge Earth

This is a bit of a continuation with the previous blog entry. Instead of using the networklink I wanted to create something dynamic but portable. So basically, it consists of the same process of generating a kml file and images of pie charts, only this time the kml isn't written as a response but to the server. Then the whole folder is zipped up to a kmz including all the images. To do the zipping I used an opensource solution called #ziplib (sharpziplib) which worked very easily and only required a few lines of code (below).

Of course I'm not the first or last person to do this dynamic kmz creation with ASP.NET, and a brief search will produce essentially the same process using #ziplib. Although, I don't think many people or dynamically creating graphs at the same time (could be wrong, if so please correct). I really want to use Google charts to do this, because they have some nice looking charts, but due to some proxy server issues this became more hastle than it was worth. I think if I do a tutorial or something, I would use Google charts instead. Again this has probably been done somewhere.

Here is a fix to a problem I was running into. You need to set the size of the file before adding it as an entry: http://community.sharpdevelop.net/forums/p/6986/19897.aspx#19897

On a side note. I keep delving more and more into programming. I do think it is a necessity. I can handle working with vb.net, vba, javascript, and python. Definitely more of dabler than developer. I keep telling myself that I should start to learn C# to take things to a more advanced level. A lot of these language structures look similar to c#: javascript, python, java, j#, etc. At least to me anyway. But what can I say - I love VB. As I've read elsewhere, it is such a verbose language you can read it like a book. I just think it is so easy to use, and in a lot of ways so portable with VBA, VBScript (I've used this with dos to repeat creating a directory structure over and over again), VB.NET, VB6. I can use it in so many different environments. It's hard to resist the temptation to come back to it...and so C# will have to wait some more.

Here is a copy of the code I am using:

Private Sub ZipFolder(ByVal currentFolder As String)
Dim Filenames() As String = System.IO.Directory.GetFiles(OutputPath & currentFolder & "\")
Dim s As New ZipOutputStream(System.IO.File.Create(OutputPath & currentFolder & ".kmz"))
s.SetLevel(9)
Dim buffer() As Byte
ReDim buffer(4096)
For Each file As String In Filenames
Dim entry As ZipEntry
Dim fileInfo As New System.IO.FileInfo(file)
entry = New ZipEntry(fileInfo.Name)
entry.Size = fileInfo.Length
s.PutNextEntry(entry)
Dim fs As System.IO.FileStream = fileInfo.OpenRead
ReDim buffer(fileInfo.Length)
Dim sourceBytes As Integer = 1
Do Until (sourceBytes <= 0)
sourceBytes = fs.Read(buffer, 0, fileInfo.Length)
s.Write(buffer, 0, sourceBytes)
Loop
fs.Close()
fs = Nothing
sourceBytes = Nothing
entry = Nothing
Next
s.Finish()
s.Close()
Filenames = Nothing
s = Nothing
buffer = Nothing

End Sub