Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.IO;
- using System.Xml.Linq;
- namespace CountryTest
- {
- public class Kml
- {
- public List<Country> Countries { get; private set; }
- public Kml(String kml)
- {
- this.Countries = new List<Country>();
- XElement xElement = XElement.Parse(kml);
- var placemarks = xElement.Elements().First().Elements(XName.Get("{http://earth.google.com/kml/2.2}Placemark"));
- foreach (var p in placemarks)
- {
- var extendedData = p.Element(XName.Get("{http://earth.google.com/kml/2.2}ExtendedData")).Elements();
- string name = "";
- foreach (var dataElement in extendedData)
- {
- var a = dataElement.Attribute(XName.Get("name")).Value;
- if (a == "NAME")
- {
- name = dataElement.Element(XName.Get("{http://earth.google.com/kml/2.2}value")).Value.Replace("<![CDATA[", "").Replace("]]>", "");
- }
- }
- var multiGeometry = p.Element(XName.Get("{http://earth.google.com/kml/2.2}MultiGeometry")).Elements().ToList();
- List<Polygon> outerPolygons = new List<Polygon>();
- List<Polygon> innerPolygons = new List<Polygon>();
- var n = multiGeometry.First().Name;
- if (multiGeometry.First().Name == XName.Get("{http://earth.google.com/kml/2.2}Polygon"))
- {
- var polygonString = multiGeometry.Elements().First().Elements().First().Elements().First().Value.Replace("\n","");
- outerPolygons.Add(new Polygon(polygonString));
- }
- else
- {
- var polygons = multiGeometry.Elements();
- foreach (var polygon in polygons)
- {
- var type = polygon.Elements().First().Name;
- if (type == XName.Get("{http://earth.google.com/kml/2.2}outerBoundaryIs"))
- {
- var polygonString = polygon.Elements().First().Value.Replace("\n", "");
- outerPolygons.Add(new Polygon(polygonString));
- }
- else if (type == XName.Get("{http://earth.google.com/kml/2.2}innerBoundaryIs"))
- {
- var polygonString = polygon.Elements().First().Value.Replace("\n", "");
- innerPolygons.Add(new Polygon(polygonString));
- }
- }
- }
- Countries.Add(new Country(name, outerPolygons, innerPolygons));
- }
- }
- public String GetCountryByCoordinates(double x, double y)
- {
- foreach (Country c in Countries)
- if (c.ContainsPoint(x, y))
- return c.Name;
- return "";
- }
- }
- public class Country
- {
- public String Name { get; private set; }
- List<Polygon> _outerPolygons;
- List<Polygon> _innerPolygons;
- public Country(String name, List<Polygon> outerPolygons, List<Polygon> innerPolygons)
- {
- this.Name = name;
- _outerPolygons = outerPolygons;
- _innerPolygons = innerPolygons;
- }
- public bool ContainsPoint(double x, double y)
- {
- foreach (Polygon inner in _innerPolygons)
- if (inner.ContainsPoint(x, y))
- return false;
- foreach (Polygon outer in _outerPolygons)
- if (outer.ContainsPoint(x, y))
- return true;
- return false;
- }
- }
- public class Polygon
- {
- List<Tuple<double, double>> _coordinates;
- public Polygon(String kmlPolygonString)
- {
- _coordinates = new List<Tuple<double, double>>();
- string[] points = kmlPolygonString.Split(' ');
- foreach (string s in points)
- {
- string[] parts = s.Split(',');
- double x = double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture);
- double y = double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture);
- _coordinates.Add(new Tuple<double, double>(x, y));
- }
- }
- //modified from source: http://stackoverflow.com/a/2922778/1020861
- public bool ContainsPoint(double x, double y)
- {
- int i, j;
- bool c = false;
- for (i = 0, j = _coordinates.Count - 1; i < _coordinates.Count; j = i++)
- {
- if (((_coordinates[i].Item2 > y) != (_coordinates[j].Item2 > y)) &&
- (x < (_coordinates[j].Item1 - _coordinates[i].Item1) * (y - _coordinates[i].Item2) / (_coordinates[j].Item2 - _coordinates[i].Item2) + _coordinates[i].Item1))
- c = !c;
- }
- return c;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement