Monday 4 August 2008

Why the direction of polygon coordinates matters in KML

If you use the World borders shapefile provided on the site to create a KML prism maps, you will experience a problem. The same problem is likely to occur if you extract polygon features from other shapefiles. The prism map will look like this in Google Earth:

The prisms are not colourised properly. The reason turned out to be the clockwise orientation of the polygon vertices (winding order). 3-D implementations of KML use the vertex winding order for determining the direction in which it faces. This is necessary to display the correct lighting on curved surfaces. For the prisms to be displayed properly the vertex order has to be anti-clockwise.

The prisms will be colourised properly by simply changing the direction of the coordinates.

The light has been switched back on.

The vertex order was changed by this PHP function:
function kmlReverseCoordinates($coordinates) {
  $coordinates = explode(" ", $coordinates);
  $coordinates = array_reverse($coordinates);
  $coordinates = implode(" ", $coordinates);
  return $coordinates;
}

2 comments:

Maarten said...

What a coincidence! Just last night I was struggling with this very problem and I didn't have a clue how to solve it. And today the answer was just delivered to me through my RSS feeds.
Thanks!

Maarten
www.merkador.com

Anonymous said...

I think it looks cooler with improper winding, but I suppose the project requirements come first. :)