Wednesday, November 24, 2010

Converting shapefile into KML

In the inaugural post in the Free GIS Tools series I presented Google Map API as a very powerful online GIS alternative. Google Map has the capacity to display spatial data in various formats but probably the most attractive option is its native support for KML/KMZ. It takes only one line of code to display complex spatial information on a Google Map – as long as it is in that format. True, not all KLM features are supported in Google Map but you can still create great maps using a single KML file. Try aus-emaps.com free map service to test how well Google Map handles KMZ files: Victorian bushfires aftermath, current earthquakes.

KML/KMZ is the only spatial data format directly supported by free Google Map and Google Earth desktop application. However, a lot of free spatial data is distributed in another very popular GIS format: shapefile (shp). In order to display that data in Google Map or Google Earth, it has to be converted to KML/KMZ format.

There are many tools available for download, or that are bundled with commercial software to do that conversion, but I found only one that really work for me. And the best thing is that it is totally free! It took quite an effort to find so, by sharing this information I hope to save you many hours of fruitless trawling through the Internet. Here are the details.


ESRI Shapefile to KML Converter by reimers.dk

Simple but effective desktop tool (it works!) available for free download. Just point to the shp file you want to convert, select attributes to include in description field and press a button to run conversion. The only limitation is that you don't have a choice in selecting colours.




Here is an example of generated output: World Borders in KMZ (1.14MB kmz file draped on Google Map).

To reduce the size of KML file for distribution or deployment just gzip it. It is easy done using Google Earth “save as” function. That is, open your KML file in Google Earth and when you save it, it will be automatically converted to a compressed gzipped format with kmz extension.

For custom conversions from shp polygons to KMZ format, such as splitting a large file with many polygons into smaller files with individual polygons only (eg. to convert a single 60MB shp file with postcodes into 2,500 individual KMZ files), or grouping of polygons into custom areas (eg. to create sales or franchise territories by amalgamation of postcodes), or converting shp polygons into a thematic map layer (eg. by assigning colours to polygons based on specific attributes, like for example electorates by political party holding the seat), you will need more advanced tools. I can help you with those conversions for a small fee. Here is a sample of a thematic KMZ file using a selection of Sydney suburbs. I also have various polygon based data in KMZ format for immediate delivery (eg. postcode boundaries, as used in aus-emaps.com postcode finder service). Don’t hesitate to ask!

Use case example: Create outlines of polygons and show in an embedded map on a website (using collection of free tools and services available from aus-emaps.com)
Steps:

1. Define vertex coordinates of custom polygon area using simple geocoder tool from aus-emaps.com (select “Point” option and click on the map, reposition markers as required, copy to Notepad).

2. Convert a list of points in csv format to shp file format using instructions provided in a recent post titled “Converting csv files into shapefile”.

3. Convert shp file into KML using tools introduced in this post.

4. Convert KML file into KMZ format using Google Earth (open KML file and “Save As”…) [optional step]

5. Upload KML/KMZ file to your server and reference its URL address in an aus-emaps.com free map service.

6. Embed the map in you web page using HTML’s iframe element.

The process is a bit convoluted, I admit. You could take a shortcut by pasting latitude and longitude coordinates directly into KML “shell” but you will need the right template for this in the first place. Using Google Map’s MyMap drawing and embedding functionality is a simpler option but you will not be able to create complex polygons (eg multi-part and/or doughnut type).

I am currently working on a public tool to generate custom polygons from administrative boundaries in a few simple steps. Coming up in 2011! But I will also have to put some though into creating a dedicated KMZ generation tool so you can do it all from a single web page... added to the list of “Things to do!”.


Related posts:
Free map service preview
Converting csv data into shapefile
Colours for thematic mapping
Free GIS Tools – Google Map
Free Address Validation Tool
Manual geocoder for 70 countries

4 comments:

Searchlight Services: Creating Community said...

I am VERY impressed by your helpfulness. In my website design, I have decided to push myself past my level of comfort, which includes using the google earth and maps apis. Your knowledge of GIS and file conversion is amazing. Keep up the great work!

Arek said...

Thanks for your kind words and I am glad to be of assistance. Good luck with your project!

Sinziana Armencea said...

This might help you in the future. I use http://kml2gpx.com for converting KML files to GPX files. It converts both ways and it's free to use. Maybe you'll need it sometime.

Nadeem Kazmi said...

Great blog my friend. Some great tips on Shapefile conversion.
Please check my blog
http://everythinghasalocation.blogspot.com/