예제 #1
0
        public static List <string> ReadKmlFile(int id)
        {
            var errors = new List <string>();
            var path   = ConfigurationManager.AppSettings.Get("kml.fincas.path");

            if (Directory.GetFiles(path).Count() > 1)
            {
                errors.Add("Se encontró mas de un archivo kml a procesar en la carpeta, estos archivos ya fueron eliminados. Intente nuevamente la operación");

                for (var i = 0; i <= Directory.GetFiles(path).Count(); i++)
                {
                    var filename    = Directory.GetFiles(path).FirstOrDefault();
                    var archivePath = ConfigurationManager.AppSettings.Get("kml.fincas.archive.path");
                    var archiveName = new FileInfo(filename).Name.Replace(".kml", string.Format("{0}.{1}.{2}.kml", "archive", DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss")));
                    if (!Directory.Exists(archivePath))
                    {
                        Directory.CreateDirectory(archivePath);
                    }
                    File.Move(filename, Path.Combine(archivePath, archiveName));
                }
            }
            else
            {
                BasecampRepository basecampRepo = new BasecampRepository(ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString);
                var        fincaRegionId        = (basecampRepo.GetById(id)).RegionId;
                var        FincasLayerName      = "FincasLayer" + fincaRegionId;
                var        filename             = Directory.GetFiles(path).FirstOrDefault();
                XDocument  xDoc = XDocument.Load(filename);
                XNamespace ns   = "http://www.opengis.net/kml/2.2";

                var   doc   = xDoc.Descendants(ns + "Document").ToList();
                Layer layer = new Layer();
                layer.Name  = FincasLayerName;
                layer.Zones = doc.Elements(ns + "Folder").Select(ze =>
                                                                 new Zone(ze.Element(ns + "name").Value,
                                                                          ze.Descendants(ns + "Placemark").Select(pm =>
                                                                                                                  new Polygon()
                {
                    Name      = pm.Element(ns + "name").Value,
                    Locations = pm.Descendants(ns + "coordinates").First().Value.Trim()
                                .Replace("\n", "")
                                .Replace("\t", "")
                                .Split(' ')
                                .Select(x => new Location(ParseLatitude(x), ParseLongitude(x)))
                                .ToList(),
                    PolygonGeom = ParsePolygon(pm.Descendants(ns + "coordinates").First().Value.Trim()
                                               .Replace("\n", " ")
                                               .Replace("\t", " ")
                                               .Replace(",0 ", "|")
                                               .Replace(",", " ")
                                               .Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                }).ToList(),
                                                                          ze.Element(ns + "description").Value,
                                                                          id.ToString() //Agrega el basecampId a la zona
                                                                          )).ToList();

                errors = KmlParserBasecamp.ListErrors(layer);

                if (errors.Count == 0)
                {
                    //Save finca
                    LayerRepository la = new LayerRepository(ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString);
                    la.SaveFincaFull(layer);
                    LandRepository landRepo = new LandRepository(ConfigurationManager.ConnectionStrings["EarthwatchersConnection"].ConnectionString);
                    landRepo.LoadLandBasecamp(fincaRegionId);
                }

                //Una vez finalizado mover a la carpeta Achieved
                var archivePath = ConfigurationManager.AppSettings.Get("kml.fincas.archive.path");
                var archiveName = new FileInfo(filename).Name.Replace(".kml", string.Format("{0}.{1}.{2}.kml", "archive", DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss")));
                if (!Directory.Exists(archivePath))
                {
                    Directory.CreateDirectory(archivePath);
                }
                File.Move(filename, Path.Combine(archivePath, archiveName));
            }

            return(errors);
        }