March 18, 2008

Google Maps/Earth: Align Polygons in KML

The Problem
What if you want to digitize countries, counties or other area like items that have one or more borders in common... GIS applications like Google Earth, Google Maps even, let you draw polygons overlays on a map. But instead of one common border as in the real world, the borders between the polygons are defined by two different polylines that should be described by exactly the same coordinates (or: 'vertices'). When you draw the polygons by hand, this is virtually impossible. As a result, there will be areas where two or more polygons overlap or areas where there's a gap between the two bordering polygons.

The Solution
Draw the polygons by hand and run them through an algorithm that aligns the line parts of two bordering polygons by equalizing the vertices that are really close together. The resolution, or threshold, of the equalizing algorithm should depend, off course, on the resolution and precision with which the polygons where drawn.

I wrote a PHP script that does exactly that. See the embedded version of the application below, or go to the fullscreen version. It is loaded with a demonstration KML file by default. Play around with the Equalize threshold to see what happens to the polygons.


Please let me know what you think by leaving a comment. You're free to use this script for your own application, but I'd appreciate it if you tell me about it. Plain curiosity :-)
The source code can be found here.

For my other Google Maps trials, see this overview.

2 comments:

  1. In concept the script looks great, but I have been unable to successfully utilize the script using my own kml files. I've tried both local and remote kml files, using both multiple and single polygons per kml file.

    The polygon names display, but the map does not pan to the correct area, nor do the polygons show when I manually pan.

    Is this script truly a working version?

    ReplyDelete
  2. David, the script is a proof-of-concept at most. It works great for the KMLs/polygons I needed it for, but I have to admid, I haven't thoroughly tested it on all sorts of KMLs and polygons. The application is merely a showcase of a possible implementation of the algorithms in the script.

    I realize now, I never got around making the map pan/zoom to the center of the uploaded polygons. It always pans to the default location, the center of Amsterdam.

    If you manualy pan to the location of your polygons and can't find them, do you have the 'show polygons' checkbox checked?

    The script does find your polygons in the KML, but maybe it doens't find the coordinates that polygon is made of. I tailored the script to find polygon coordinates inside the KML files that are outputted by google maps. They might be of a slightly different form (within the KML standard). Can you upload the KML somewhere and post a link to it here, so I can check what's going on? Or post (a representative part of) the KML here as a comment?

    ReplyDelete