public void SaveFinca(Zone zon, int layId)
        {
            connection.Open();
            var cmd = connection.CreateCommand() as SqlCommand;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Layer_SaveZone";

            cmd.Parameters.Add(new SqlParameter("@name", zon.Name));
            var desc = string.IsNullOrEmpty(zon.Description) ? DBNull.Value : (object)zon.Description;
            cmd.Parameters.Add(new SqlParameter("@description", desc));
            cmd.Parameters.Add(new SqlParameter("@layerId", layId));
            cmd.Parameters.Add(new SqlParameter("@param1", zon.Param1));
            var idParameter = new SqlParameter("@ID", SqlDbType.Int) { Direction = ParameterDirection.Output };
            cmd.Parameters.Add(idParameter);
            cmd.ExecuteNonQuery();
            connection.Close();
            zon.Id = (int)idParameter.Value;

        }
        public void GetLayersByName(string name, bool justBasecamps = false)
        {
            client.Authenticator = new HttpBasicAuthenticator(Current.Instance.Username, Current.Instance.Password);

            var request = new RestRequest("layers/getlayerbyname", Method.POST) { RequestFormat = DataFormat.Json };
            request.JsonSerializer = new JsonSerializer();
            Earthwatchers.Models.KmlModels.Zone zon = new Earthwatchers.Models.KmlModels.Zone();
            zon.Name = name;
            zon.Show = justBasecamps;
            request.AddBody(zon);
            //request.AddHeader("Accept-Encoding", "gzip, deflate");

            client.ExecuteAsync<Layer>(request, response =>
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                        LayerRecived(response.Data, null)
                            ));
        }
        public Layer ReadKmlFile()
        {
            //TODO: Darle acceso a una carpeta con N archivos KML.
            XDocument xDoc = XDocument.Load(ConfigurationManager.AppSettings.Get("kmlparser.kmlfile"));
            XNamespace ns = "http://www.opengis.net/kml/2.2";

            var doc = xDoc.Descendants(ns + "Document").ToList();
            var layerElement = doc.Elements(ns + "Folder").First();

            Layer layer = new Layer();
            layer.Name = "OTBN";

            //var pm = layerElement.Descendants(ns + "Placemark").First();
            //var name = pm.Element(ns + "name").Value;
            //var id = pm.Attribute("id").Value;

            var redZone = new Zone("Zona Roja", layerElement.Descendants(ns + "Folder").FirstOrDefault().Descendants(ns + "Placemark")
                                                            //.Where(pm => pm.Element(ns + "name").Value == "proteccion")
                                                            .Select(pm => new Polygon()
                                                            {
                                                                Name = pm.Attribute("id") != null ? pm.Attribute("id").Value : null,
                                                                Locations = pm.Descendants(ns + "coordinates").FirstOrDefault() != null ? pm.Descendants(ns + "coordinates").FirstOrDefault().Value.Trim()
                                                                                                .Replace("\n", "")
                                                                                                .Replace("\t", "")
                                                                                                .Split(' ')
                                                                                                .Select(x => new Location(ParseLatitude(x), ParseLongitude(x)))
                                                                                                .ToList() : null,
                                                                PolygonGeom = this.ParsePolygon(pm.Descendants(ns + "coordinates").FirstOrDefault().Value.Trim()
                                                                                               .Replace("\n", " ")
                                                                                               .Replace("\t", " ")
                                                                                               .Replace(",0 ", "|")
                                                                                               .Replace(",", " ")
                                                                                               .Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                                                            }).ToList());

            var yellowZone = new Zone("Zona Amarilla", layerElement.Descendants(ns + "Folder").Skip(1).FirstOrDefault().Descendants(ns + "Placemark")
                                                            //.Where(pm => pm.Element(ns + "name").Value == "mcb")
                                                            .Select(pm => new Polygon()
                                                            {
                                                                Name = pm.Attribute("id") != null ? pm.Attribute("id").Value : null,
                                                                Locations = pm.Descendants(ns + "coordinates").FirstOrDefault() != null ? pm.Descendants(ns + "coordinates").FirstOrDefault().Value.Trim()
                                                                                                .Replace("\n", "")
                                                                                                .Replace("\t", "")
                                                                                                .Split(' ')
                                                                                                .Select(x => new Location(ParseLatitude(x), ParseLongitude(x)))
                                                                                                .ToList() : null,
                                                                PolygonGeom = this.ParsePolygon(pm.Descendants(ns + "coordinates").FirstOrDefault().Value.Trim()
                                                                                               .Replace("\n", " ")
                                                                                               .Replace("\t", " ")
                                                                                               .Replace(",0 ", "|")
                                                                                               .Replace(",", " ")
                                                                                               .Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                                                            }).ToList());

            //var greenZone = new Zone("Zona Verde", layerElement.Descendants(ns + "Placemark")
            //                                                .Where(pm => pm.Element(ns + "name").Value == "potencial productivo")
            //                                                .Select(pm => new Polygon()
            //                                                {
            //                                                    Name = pm.Attribute("id") != null ? pm.Attribute("id").Value : null,
            //                                                    Locations = pm.Descendants(ns + "coordinates").First().Value.Trim()
            //                                                                                    .Replace("\n", "")
            //                                                                                    .Replace("\t", "")
            //                                                                                    .Split(' ')
            //                                                                                    .Select(x => new Location(ParseLatitude(x), ParseLongitude(x)))
            //                                                                                    .ToList(),
            //                                                    PolygonGeom = this.ParsePolygon(pm.Descendants(ns + "coordinates").First().Value.Trim()
            //                                                                                   .Replace("\n", " ")
            //                                                                                   .Replace("\t", " ")
            //                                                                                   .Replace(",0 ", "|")
            //                                                                                   .Replace(",", " ")
            //                                                                                   .Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
            //                                                }).ToList());
            layer.Zones = new List<Zone>();
            layer.Zones.Add(redZone);
            layer.Zones.Add(yellowZone);
            //layer.Zones.Add(greenZone);

            return layer;
        }