Raba - Defend your code RSS 2.0
# Friday, April 10, 2009

This post inspired by Gal Ochana.

Gal is a new gifted programmer at my team, she presented a great presentation about Linq to XML - you can download code here.
The great bonus at her presentation was the Linq to Kml Sample, I would like to share this with you.

The kml itself is an XML file at our sample it looks like this:

   1: <?xml version="1.0" encoding="UTF-8"?>
   2: <kml xmlns="http://www.opengis.net/kml/2.2">
   3:     <Placemark>    
   4:         <name continent="America">America</name>    
   5:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
   6:         <Point>      
   7:             <coordinates>-100.0822035425683,37.42228990140251,0</coordinates>    
   8:         </Point>  
   9:     </Placemark>
  10:     <Placemark>    
  11:         <name continent="Africa">Africa</name>    
  12:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
  13:         <Point>      
  14:             <coordinates>20.0822035425683,20.42228990140251,0</coordinates>    
  15:         </Point>  
  16:     </Placemark>
  17:     <Placemark>    
  18:         <name continent="Asia">Asia</name>    
  19:         <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>    
  20:         <Point>      
  21:             <coordinates>42.0822035425683,33.42228990140251,0</coordinates>    
  22:         </Point>  
  23:     </Placemark>
  24:     <Placemark>    
  25:         <name continent="Europe">Europe</name>    
  26:         <description>Europe</description>    
  27:         <Point>      
  28:             <coordinates>100.0822035425683,62.42228990140251,0</coordinates>    
  29:         </Point>  
  30:     </Placemark>
  31: </kml>


You can read more about Kml here.


Screen-shot of the above kml in the ArcGis-Explorer:

You can see in the above Kml file sample the continents represented as a simple points, of course you can extend it to include polygons, poly-lines or anything else that valid within KML standard.
The only things that differs Kml from the known Xml file is the namespace that we will need to handle in our code, let's see the query itself:

   1: XNamespace ns = "http://www.opengis.net/kml/2.2"; 
   2:  
   3: XDocument kmlDoc = XDocument.Load(InputKml);
   4:  
   5: var result = from placemark in kmlDoc.Descendants(ns + "Placemark")
   6:              where (placemark.Element(ns + "name")
   7:                              .Attribute("continent").Value == 
   8:                              Continents.SelectedItem.ToString())
   9:              select placemark;

 

line 1: declare the full namespace that will be used for the attributes and Tags
line 3: loading the kml input file
line 5-9: run a simple query to select a single continent, pay attention to the namespace that we concatenate for selecting the right node.

Here you can see a screen-shot with the result of the above query using the "Asia" continent as the selected item.

Enjoy.

Friday, April 10, 2009 10:03:14 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | C#3.0 | LINQ | GIS | AGX

Comments are closed.
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Shani Raba
Sign In
Statistics
Total Posts: 145
This Year: 0
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2012, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)