Split up your KML files with KMLSplitter

Split up your KML files with KMLSplitter

My friend who writes the Higley1000.com website is a geographer, so you can bet that his world is all about maps! In the process of researching the data for the Higley1000, he has created hundreds of maps which show the precise boundaries of the neighborhoods under consideration. Google Maps has been a great asset for him as he worked through the mounds of data, giving him the ability to easily create his maps with simple, web accessible tools.

Problem was that, as with any new tool, you don’t really appreciate how it works until you run into some problem. Since the goal in the end was to be able to share the Higley1000 maps on the web, I suggested that he should simply create a map under Google My Maps, and place all the neighborhood boundaries on that map. By sharing his map, I reasoned, all of his work could be made accessible to the world!

The Higley 1000 Neighborhoods are all created in Google Maps using polygons which trace the outlines of the given neighborhood. Each neighborhood polygon can consists of dozens upon dozens of vertices. As you add neighborhoods to the map, the time it takes for your browser to display the neighborhoods begins to take longer and longer. The delay grew until it became an impediment when using the map editor.

My first thoughts about how to share the maps had roughly followed this progression:

  1. display a Google Map at the correct latitude/longitude for the neighborhood of interest
  2. add in the Higley 1000 Neighborhoods overlay
  3. zoom to highlight the neighborhood of interest

Given my desire to minimize the delay introduced as each neighborhood is drawn, I decided to instead break down the maps that had been created into indiviual neighborhoods maps, so that no more than one polygon would have to be drawn to illustrate a neighborhood outline.

But the catch this time was that it’s hard to do anything with a map you’ve created besides add new stuff or delete something you’ve already drawn. The implication was that all the neighborhood maps that had been so laboriously created would have to be done all over again, but saved as individual maps this time. It would have been a real shame to have wasted all the effort expended so far. So I decided to write a tool that would take the original multi-neighborhood file and split it into individual neighborhood files that could be hosted on the Higley1000 web server. That’s that KMLSplitter does!

If you’ve ever wanted to get your work out of Google My Maps, you know that it’s easy enough to download the KML describing the edits you’ve made on the maps (read this post on Google if you’re not familiar with the procedure to download the actual KML instead of a network link). The resulting KML files can be split into the individual components by processing them through KMLSplitter. KMLSplitter breaks out all the placemark elements into individual files, which it names according to the placemark name which was entered on Google Maps.

KMLSplitter uses the dom4j package to parse in the KML, find the placemarks, and write out files while each contain only a single placemark. dom4j did all the hard work for me, making it easy to implement this tool. If you’d like to try it, just please bear this in mind: this was written in a hurry, as a throw-away (I only later decided to share it with you), and is a little brittle due to lack of attention to exception handling. The program only works on the subset of KML which is used by Google Maps. The program assumes that you have also entered a description for each placemark. Failure to follow these guidelines will probably result in having KMLSplitter crash and burn. If you follow the steps as shown below, though, you shouldn’t run into any problems.

You must have Java already installed on your system to use this program. KMLSplitter doesn’t need any installation, other than to simply unzip the KMLSplitter files into the directory of your choice. After you’ve done that, you can launch KMLSplitter by simply double-clicking on the KMLSplitter JAR file. KMLSplitter will show the following opening screen:

KMLSplitter Opening Screen

 

Choose the file you would like to split by using the Browse button as indicated in the following image:

Choose the file to Split

Note that you must use the browse button to select the file, because there are some things that happen under the hood when you click that button.

After choosing the file to split, KMLSplitter will parse the KML, and present the results to the user in a browsable tree. You may examine the results of parsing if you like, to verify that you are working on the correct file.

Next, you need to specify an output directory for the files created by splitting.

Choose the output Directory

When you click the Browse button, by default KMLSplitter will choose the same directory in which the original KML file is located. You are free to choose any other directory, though, as suits your needs.

You may optionally type in a prefix which will be appended to each new KML filename which is output.

Enter a filename prefix

Ok! Now you’re ready to split! Click the Split it now! button to split your KML file into the individual placemarks.

Split the KML

KMLSplitter will extract each placemark in your KML file, creating a new KML file for each placemark which contains only the single placemark. The new KML file will be named according to the prefix you have (optionally) entered plus the placemark name.

Additionally, KMLSplitter will create a text file which lists each placemark name and the corresponding filename which was created to hold the placemark KML. Also listed will be the description which was entered on the placemark.

I hope that this program will be of use to others who are using Google Maps. If there is enough interest then I may even come back and do some cleanup on the code to make it a little more robust, or perhaps post the source for the interested reader. Please feel free to leave comments for me about your experience with KMLSplitter!

Last but not least, here’s the download:

KMLSplitter downloadable zip file

Thanks for reading, and enjoy!

MirthMaker

 

 

 

 

 

Posted in Code and Hacks on Oct 20th, 2007, 1:18 pm by MirthMaker   

No comments yet. Be the first.

Leave a reply

 

Bad Behavior has blocked 23 access attempts in the last 7 days.