static void Main(string[] args) { GolfCourseRepository repo = new GolfCourseRepository(); Repository<FeatureType> fRepo = new Repository<FeatureType>(); var Kml = XDocument.Load("BridlingtonLinks.kml"); var name = Kml.Descendants().Where(x => x.Name.LocalName == "name").First().Value; var features = (from h in Kml.Descendants().Elements().Where(x => x.Name.LocalName == "Placemark") select h).ToList(); GolfCourse gc = new GolfCourse(); gc.name = name; foreach (var f in features) { Feature feature = new Feature(); var featureValid = true; int fType = 0; string fName = f.Descendants().Where(x=>x.Name.LocalName =="name").First().Value ; Hole h = null; int holeNo = 0; int count = 1; while (count < 19) { if( fName.Contains("Hole " + count.ToString())){ holeNo = count; break; } count = count + 1; } if (holeNo == 0) { featureValid = false; } else { h = gc.holes.FirstOrDefault(x=>x.holeNumber==holeNo); if (h == null) { h = new Hole() { holeNumber = holeNo, strokeIndex =0}; gc.AddHole(h); } } if( fName.Contains("Tee")){ feature.featuretype = fRepo.Get(1); } else if( fName.Contains("Middle of Green")){ feature.featuretype = fRepo.Get(5); } else if( fName.Contains("bunker")){ feature.featuretype = fRepo.Get(4); } else if( fName.Contains("water")){ feature.featuretype = fRepo.Get(6); } else{ featureValid = false; } if(featureValid) { string coordinates = f.Descendants().Where(x=>x.Name.LocalName =="coordinates").First().Value; string[] longlat = coordinates.Split(",".ToCharArray()); feature.latitude = Decimal.Parse(longlat[0]); feature.longitude = Decimal.Parse(longlat[1]); feature.hole = h; h.features.Add(feature); } } }
// // GET: /Import/ //[AcceptVerbs(HttpVerbs.Get)] //public ActionResult ImportKML() //{ //} //[AcceptVerbs(HttpVerbs.Post)] public ActionResult ImportKML(string KMLUrl) { GolfCourseRepository repo = new GolfCourseRepository(); Repository<FeatureType> fRepo = new Repository<FeatureType>(); var Kml = XDocument.Load(KMLUrl); //Server.MapPath("../kml/BridlingtonLinks.kml")); var name = Kml.Descendants().Where(x => x.Name.LocalName == "name").First().Value; var features = (from h in Kml.Descendants().Elements().Where(x => x.Name.LocalName == "Placemark") select h).ToList(); GolfCourse gc = new GolfCourse(); gc.name = name; foreach (var f in features) { Feature feature = new Feature(); var featureValid = true; int fType = 0; string fName = f.Descendants().Where(x=>x.Name.LocalName =="name").First().Value ; Hole h = null; int holeNo = 0; int count = 18; while (count > 0) { if( fName.Contains("Hole " + count.ToString())){ holeNo = count; break; } count = count - 1; } if (holeNo == 0) { featureValid = false; } else { h = gc.holes.FirstOrDefault(x=>x.holeNumber==holeNo); if (h == null) { h = new Hole() { holeNumber = holeNo, strokeIndex =0}; gc.AddHole(h); } } if( fName.Contains("Tee")){ feature.featuretypeID = 1; feature.featuretype = fRepo.Get(1); } else if( fName.Contains("Middle of Green")){ feature.featuretypeID = 5; feature.featuretype = fRepo.Get(5); } else if( fName.Contains("bunker")){ feature.featuretypeID = 4; feature.featuretype = fRepo.Get(4); } else if( fName.Contains("water")){ feature.featuretypeID = 6; feature.featuretype = fRepo.Get(6); } else{ featureValid = false; } if(featureValid) { string coordinates = f.Descendants().Where(x=>x.Name.LocalName =="coordinates").First().Value; string[] longlat = coordinates.Split(",".ToCharArray()); Random rnd = new Random(); feature.longitude = Decimal.Parse(longlat[0]); feature.latitude = Decimal.Parse(longlat[1]); feature.hole = h; // don't add multiple tees or greens or too many features in general... var teeCount = h.features.Where(x=>x.featuretypeID == 1).ToList().Count() ; var greenCount = h.features.Where(x=>x.featuretypeID == 5).ToList().Count(); var thisTypeCount = h.features.Where(x => x.featuretypeID == feature.featuretypeID).ToList().Count(); if((feature.featuretypeID ==1) ) { if (teeCount == 0) { h.features.Add(feature); } } else if ((feature.featuretypeID == 5)) { if (greenCount == 0) { h.features.Add(feature); } } else if (thisTypeCount <3) { h.features.Add(feature); } } } gc.longitude = gc.holes[0].features.Single(x => x.featuretypeID == 1).longitude; gc.latitude = gc.holes[0].features.Single(x => x.featuretypeID == 1).latitude; gc.area = GetGeoCodeData(gc.longitude, gc.latitude); repo.SaveOrUpdate(gc); return View(); }