Pages

Friday 10 December 2010

KML creation using MATLAB

In this post, I would like to show MATLAB capabilites for handling XML file. Specifically, I will show you how to create a DOM from scratch and then build up a simple XML document. The data I will use is a excel file with longitude and latitude information of number of points along the border of my country Nepal. Using the MATLAB capabilites for handling XML file, i will show how to generate KML out of it which can be readily visualize in Google Earth.

Here is a glimpse of Excel file to show how the data is stored in the Excel.


MATLAB provides a Java method com.mathworks.xml.XMLUtils.createDocument() to create a new node. In this example, we create the top level ("root" or "document") node to be named KML then populate it with child nodes with necessary information.

Step 1) Create a structure in MATLAB to store the data in the Excel file and populate the data in that structure. MATLAB provides a simple function xlsread by which xls files can be easily read.



Step 2) Create a top level node  and other necessary child level node. Since, we want to display multiple markers in KML, we need it store multiple points in a folder. In addition, I want to also show you how to display cutom markers in KML instead of using Google's default red markers. To create node, you will use .createElement ( ) and to insert node into a  super node, you will use .appendChild ( ). Value of a node is set using .setTextContent ( ) and attributes of a node is set using .setAttribute ( ). In the example, I have stored the image location of a flag of my country Nepal in the hrefNode  and set id attributes of the node StyleNode to nepal.



Step 3) Create multiple Placemarks node and store longitude and latitude information. For this, I am using a loop to create a placemarkNode as well as necessary other nodes and populating the cordinatesNode  with longitude and latitude information from the structure created beforehand using .setTextContent ( ).


Step 4) Store the thus created xml as KML file with xmlwrite function.


Step 5) View the file thus created in a Notepad ++. By the way, if you are still using convetional Notepad that comes with windows, give a shot to Notepadd++ which is much superior to Notepad and supports many programming language.




Step 5) Visualize the KML file. Download the KML file here.






No comments:

Post a Comment