public SatelliteImage Insert(SatelliteImage satelliteImage)
 {
     connection.Open();
     if(satelliteImage.AcquisitionDate == null) satelliteImage.AcquisitionDate = DateTime.Now;
     if (satelliteImage.Published == null) satelliteImage.Published = DateTime.Now.Date;
     var cmd = connection.CreateCommand() as SqlCommand;
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.CommandText = "SatelliteImage_Insert";
     
     cmd.Parameters.Add(new SqlParameter("@extent", satelliteImage.Wkt));
     cmd.Parameters.Add(new SqlParameter("@name", satelliteImage.Name));
     cmd.Parameters.Add(new SqlParameter("@provider", satelliteImage.Provider));
     cmd.Parameters.Add(new SqlParameter("@imagetype", satelliteImage.ImageType));
     cmd.Parameters.Add(new SqlParameter("@urltilecache", (object)satelliteImage.UrlTileCache?? DBNull.Value));
     cmd.Parameters.Add(new SqlParameter("@published", satelliteImage.Published));
     cmd.Parameters.Add(new SqlParameter("@acquisitiondate", satelliteImage.AcquisitionDate));
     cmd.Parameters.Add(new SqlParameter("@minlevel", satelliteImage.MinLevel));
     cmd.Parameters.Add(new SqlParameter("@maxlevel", satelliteImage.MaxLevel));
     cmd.Parameters.Add(new SqlParameter("@urlmetadata", (object)satelliteImage.UrlMetadata ?? DBNull.Value));
     cmd.Parameters.Add(new SqlParameter("@iscloudy", satelliteImage.IsCloudy));
     var idParameter = new SqlParameter("@ID", SqlDbType.Int) {Direction = ParameterDirection.Output};
     cmd.Parameters.Add(idParameter);
     cmd.ExecuteNonQuery();
     connection.Close();
     return satelliteImage;
 }
        public SatelliteLayerControl(SatelliteImage satelliteLayer, bool isAdded)
        {
            InitializeComponent();
            this.isAdded = isAdded;
            this.satelliteLayer = satelliteLayer;

            xhkAdd.IsChecked = this.isAdded;
            initialized = true;
            UpdateInfo();
        }
        public HttpResponseMessage Put(int id, SatelliteImage satelliteImage)
        {
            var satelliteImageDb = satelliteImageRepository.Get(id);

            if (satelliteImageDb != null)
            {
                satelliteImageRepository.Update(id, satelliteImage);
                return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
            }
            return new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound };
        }
        public HttpResponseMessage<SatelliteImage> Post(SatelliteImage satelliteImage, HttpRequestMessage<SatelliteImage> request)
        {
            if (satelliteImage != null)
            {
                var satelliteImageDb = satelliteImageRepository.Insert(satelliteImage);

                var response = new HttpResponseMessage<SatelliteImage>(satelliteImageDb) { StatusCode = HttpStatusCode.Created };
                return response;
            }
            return new HttpResponseMessage<SatelliteImage>(null) { StatusCode = HttpStatusCode.NotFound, };
        }
        private void AddSatelliteImagery(SatelliteImage satelliteImage)
        {
            var isAdded = true;
            var result = Current.Instance.LayerHelper.LayerCollection.FindLayer(satelliteImage.Name);
            
            var size = result.Count();
            if (size == 0 )
                isAdded = false;

            satelliteLayers.Children.Add(new SatelliteLayerControl(satelliteImage, isAdded));
        }
        void imageRequests_ImageRequestReceived(object sender, System.EventArgs e)
        {
            satelliteImages = sender as List<SatelliteImage>;
            forestLaw = satelliteImages.Where(x => x.IsForestLaw == true).FirstOrDefault();
            satelliteImages = satelliteImages.Where(x => x.IsForestLaw == false).ToList();

            if (satelliteImages != null && satelliteImages.Count != 0)
            {
                includedSatelliteImages = satelliteImages.Where(x => x.Name.IndexOf("base") > -1).ToList(); //Muestro solo 3 mas las base, el resto las mando al combo
                //MessageBox.Show("Imagen base: " + satelliteImages.Where(x => x.Name.IndexOf("base") > -1).FirstOrDefault().Name);
                includedSatelliteImages.AddRange(satelliteImages.Where(x => x.Name.IndexOf("base") == -1).OrderByDescending(x => x.Published).Take(3).ToList());
                Current.Instance.LastImageDate = includedSatelliteImages.First().Published.Value.ToString("MMMM yyyy");
                CreateTimeLine();

                if (!Current.Instance.TutorialStarted)
                {
                    var text = this.DatesGrid.Children[this.DatesGrid.ColumnDefinitions.Count - 1] as TextBlock; //Me traigo la ultima fecha del timeline
                    if (text != null)
                        ChangingOpacity(this, new ChangingOpacityEventArgs { Title = text.Text, IsCloudy = includedSatelliteImages.First().IsCloudy, IsInitial = true });
                }
            }
        }
        public SatelliteImage Insert(SatelliteImage satelliteImage)
        {
            try
            {
                connection.Open();
                if (satelliteImage.Published == null) satelliteImage.Published = DateTime.UtcNow.Date;
                var cmd = connection.CreateCommand() as SqlCommand;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SatelliteImage_Insert";

                cmd.Parameters.Add(new SqlParameter("@extent", satelliteImage.Wkt));
                cmd.Parameters.Add(new SqlParameter("@name", satelliteImage.Name));
                cmd.Parameters.Add(new SqlParameter("@urltilecache", (object)satelliteImage.UrlTileCache ?? DBNull.Value));
                cmd.Parameters.Add(new SqlParameter("@published", satelliteImage.Published));
                cmd.Parameters.Add(new SqlParameter("@minlevel", satelliteImage.MinLevel));
                cmd.Parameters.Add(new SqlParameter("@maxlevel", satelliteImage.MaxLevel));
                cmd.Parameters.Add(new SqlParameter("@iscloudy", satelliteImage.IsCloudy));
                cmd.Parameters.Add(new SqlParameter("@isForestLaw", satelliteImage.IsForestLaw));
                cmd.Parameters.Add(new SqlParameter("@regionId", satelliteImage.RegionId));
                var idParameter = new SqlParameter("@ID", SqlDbType.Int) { Direction = ParameterDirection.Output };
                cmd.Parameters.Add(idParameter);
                cmd.ExecuteNonQuery();
                connection.Close();
                return satelliteImage;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
 private void AddExtent(SatelliteImage satelliteImage)
 {
     satelliteImage.Extent = new Extent(satelliteImage.xmin, satelliteImage.ymin, satelliteImage.xmax, satelliteImage.ymax, 4326);
 }
        public SatelliteImage Update(SatelliteImage satelliteImage)
        {
            try
            {
                connection.Open();
                if (satelliteImage.Published == null) satelliteImage.Published = DateTime.UtcNow.Date;
                var cmd = connection.CreateCommand() as SqlCommand;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SatelliteImage_Update";

                cmd.Parameters.Add(new SqlParameter("@id", satelliteImage.Id));
                cmd.Parameters.Add(new SqlParameter("@extent", satelliteImage.Wkt));
                cmd.Parameters.Add(new SqlParameter("@name", satelliteImage.Name));
                cmd.Parameters.Add(new SqlParameter("@published", satelliteImage.Published));
                cmd.Parameters.Add(new SqlParameter("@minlevel", satelliteImage.MinLevel));
                cmd.Parameters.Add(new SqlParameter("@maxlevel", satelliteImage.MaxLevel));
                cmd.Parameters.Add(new SqlParameter("@iscloudy", satelliteImage.IsCloudy));
                cmd.Parameters.Add(new SqlParameter("@isForestLaw", satelliteImage.IsForestLaw));
                cmd.Parameters.Add(new SqlParameter("@regionId", satelliteImage.RegionId));
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch(Exception ex)
            {
                throw ex;
            }

            return satelliteImage;
        }
        // Name Format:  RegionId_ImageName
        public static SatelliteImage ReadXmlFileAndParseImage(string imageName)
        {
            //Paths and loading document
            var path = ConfigurationManager.AppSettings.Get("xml.greenpeaceweb.path") + ConfigurationManager.AppSettings.Get("xml.satelliteimagesregion.path");
            //TODO: Agegar el regionId al path /1/2008 /X/imageName
            var unifiedPath = path + imageName + "/tilemapresource.xml";
            string Rstring = string.Empty;
            WebRequest myWebRequest;
            WebResponse myWebResponse;

            try
            {
                myWebRequest = WebRequest.Create(unifiedPath);
                myWebResponse = myWebRequest.GetResponse();//Returns a response from an Internet resource
                Stream streamResponse = myWebResponse.GetResponseStream();//return the data stream from the internet
                StreamReader sreader = new StreamReader(streamResponse);//reads the data stream
                Rstring = sreader.ReadToEnd();//reads it to the end
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error reading file. unifiedPath: {0}, exception: {1}", unifiedPath, ex));
            }
            XDocument xDoc = XDocument.Parse(Rstring);

            //Variables declarations
            int minLevel, maxLevel, regionId = 0;
            double xMin, xMax, yMin, yMax = 0;
            string name;
            var doc = xDoc.Descendants("TileMap").ToList();

            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            //Xmin Xmax Ymin Ymax
            var xyMinMax = doc.Elements("BoundingBox").First();
            Double.TryParse(xyMinMax.Attribute("minx").Value, NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out xMin);
            Double.TryParse(xyMinMax.Attribute("maxx").Value, NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out xMax);
            Double.TryParse(xyMinMax.Attribute("miny").Value, NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out yMin);
            Double.TryParse(xyMinMax.Attribute("maxy").Value, NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out yMax);

            logger.Info(string.Format("culture: xmin - xmax - ymin - ymax:  {0} --- {1} --- {2} --- {3}", xMin, xMax, yMin, yMax));

            //Zoom Levels
            var zoomLevels = doc.Elements("TileSets").First();
            var levels = zoomLevels.Elements().ToList();
            var l1 = levels.First();
            var l2 = levels.Last();
            minLevel = Convert.ToInt32(l1.Attribute("order").Value);
            maxLevel = Convert.ToInt32(l2.Attribute("order").Value);

            //Name Data
            try
            {
                regionId = Convert.ToInt32(imageName.Split('_').FirstOrDefault());
            }
            catch
            {
                regionId = 1;
            }
            name = imageName;//.Substring(imageName.IndexOf("_") + 1);

            SatelliteImage newImage = new SatelliteImage();
            newImage.Published = DateTime.Now;
            newImage.IsCloudy = false;
            newImage.IsForestLaw = false;
            newImage.Name = name;
            newImage.xmin = xMin;
            newImage.xmax = xMax;
            newImage.ymin = yMin;
            newImage.ymax = yMax;
            newImage.Wkt = "POLYGON((" + yMin + " " + xMin + ", " + yMax + " " + xMin + ", " + yMax + " " + xMax + ", " + yMin + " " + xMax + ", " + yMin + " " + xMin + "))";
            newImage.MinLevel = minLevel;
            newImage.MaxLevel = maxLevel;
            newImage.RegionId = regionId;
            newImage.UrlTileCache = path + imageName;

            return newImage;
        }
 public void Update(int id, SatelliteImage satelliteImage)
 {
     throw new NotImplementedException();
 }