Example #1
0
        /// <summary>
        /// Busca una Land para asignarle al Earthwatcher
        /// Primero busca una finca para asignarle (segun la probabilidad)
        /// Luego busca una Land que cumpla con los requisitos para ser asignada, tomando en cuenta el Basecamp asignado
        /// </summary>
        /// <param name="baseCamp"></param>
        /// <param name="geohexKey"></param>
        /// <returns></returns>
        public LandMini GetFreeLand(int regionId, string geohexKey)
        {
            //Primero asigno un basecamp
            var             basecampRepository = new BasecampRepository(connection.ConnectionString);
            List <Basecamp> basecamps          = basecampRepository.GetByBasecampRegion(regionId);
            bool            usedLand           = false;
            string          newgeohexKey       = null;

            if (basecamps != null && basecamps.Count > 0)
            {
                int    total      = basecamps.Sum(x => x.Probability);
                int    acumprob   = 0;
                Random rand       = new Random();
                int    r          = rand.Next(1, total + 1);
                int    basecampid = 0;
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }

                if (basecampid != 0)
                {
                    var sql = "Earthwatcher_GetFreeLand";
                    connection.Open();
                    var cmd = connection.CreateCommand() as SqlCommand;
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                    //cmd.Parameters.AddWithValue("@TutorLandId", Configuration.getTutorLandIdByRegion(regionId));
                    cmd.Parameters.AddWithValue("@GeoHexKey", geohexKey);
                    cmd.Parameters.AddWithValue("@RegionId", regionId);

                    var reader    = cmd.ExecuteReader();
                    var hasResult = reader.Read();
                    int landId    = 0;
                    if (hasResult)
                    {
                        landId       = reader.GetInt32(0);
                        newgeohexKey = reader.GetString(2);
                        usedLand     = reader.GetBoolean(3);
                    }
                    connection.Close();
                    return(new LandMini {
                        GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId
                    });
                }
            }

            return(null);
        }
        public LandMini GetFreeLandByEarthwatcherId(string baseCamp, int earthwatcherId)
        {
            //Primero asigno un basecamp
            var basecampRepository = new BasecampRepository(connection.ConnectionString);
            List<Basecamp> basecamps = basecampRepository.GetByBasecamp(baseCamp);
            bool usedLand = false;
            string newgeohexKey = null;
            if (basecamps != null && basecamps.Count > 0)
            {
                int total = basecamps.Sum(x => x.Probability);
                int acumprob = 0;
                Random rand = new Random();
                int r = rand.Next(1, total + 1);
                int basecampid = 0;
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }

                if (basecampid != 0)
                {
                    try
                    {
                        var sql = "Earthwatcher_GetFreeLandByEarthwatcherId";
                        connection.Open();
                        var cmd = connection.CreateCommand() as SqlCommand;
                        cmd.CommandText = sql;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                        cmd.Parameters.AddWithValue("@TutorLandId", Configuration.TutorLandId);
                        cmd.Parameters.AddWithValue("@EarthwatcherId", earthwatcherId);

                        var reader = cmd.ExecuteReader();
                        var hasResult = reader.Read();
                        int landId = 0;
                        if (hasResult)
                        {
                            landId = reader.GetInt32(0);
                            newgeohexKey = reader.GetString(2);
                            usedLand = reader.GetBoolean(3);
                        }
                        connection.Close();
                        return new LandMini { GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId };
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Ha ocurrido una excepcion :", ex); //TODO: HANDLER DE ESTA EXCEPTION
                    }
                }

            }

            return null;
        }
        public LandMini GetFreeLandByEarthwatcherIdAndPlayingRegion(int earthwatcherId, int playingRegion)
        {
            //Primero asigno un basecamp
            var basecampRepository = new BasecampRepository(connection.ConnectionString);
            List<Basecamp> basecamps = basecampRepository.GetByBasecampRegion(playingRegion);

            bool usedLand = false;
            string newgeohexKey = null;
            int basecampid = 0;

            //Si el pais tiene basecamps eligo segun la probabilidad
            if (basecamps != null && basecamps.Count > 0)
            {
                int total = basecamps.Sum(x => x.Probability);
                int acumprob = 0;
                Random rand = new Random();
                int r = rand.Next(1, total + 1);
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }
            }

            //Busco una land nueva para el usuario, dependiendo del pais, el basecamp, y el hot point de la parcela.
            try
            {
                var sql = "Earthwatcher_GetFreeLandByEarthwatcherId";
                connection.Open();
                var cmd = connection.CreateCommand() as SqlCommand;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                //cmd.Parameters.AddWithValue("@TutorLandId", Configuration.getTutorLandIdByRegion(playingRegion));
                cmd.Parameters.AddWithValue("@EarthwatcherId", earthwatcherId);
                cmd.Parameters.AddWithValue("@PlayingRegion", playingRegion);

                var reader = cmd.ExecuteReader();
                var hasResult = reader.Read();
                int landId = 0;
                if (hasResult)
                {
                    landId = reader.GetInt32(0);
                    newgeohexKey = reader.GetString(2);
                    usedLand = reader.GetBoolean(3);
                }
                connection.Close();
                return new LandMini { GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId };
            }
            catch (Exception ex)
            {
                //throw new Exception("Ha ocurrido una excepcion :", ex); //TODO: HANDLER DE ESTA EXCEPTION
                return null;
            }
        }
        /// <summary>
        /// Busca una Land para asignarle al Earthwatcher
        /// Primero busca una finca para asignarle (segun la probabilidad)
        /// Luego busca una Land que cumpla con los requisitos para ser asignada, tomando en cuenta el Basecamp asignado
        /// </summary>
        /// <param name="baseCamp"></param>
        /// <param name="geohexKey"></param>
        /// <returns></returns>
        public LandMini GetFreeLand(int regionId, string geohexKey)
        {
            //Primero asigno un basecamp
            var basecampRepository = new BasecampRepository(connection.ConnectionString);
            List<Basecamp> basecamps = basecampRepository.GetByBasecampRegion(regionId);
            bool usedLand = false;
            string newgeohexKey = null;
            if (basecamps != null && basecamps.Count > 0)
            {
                int total = basecamps.Sum(x => x.Probability);
                int acumprob = 0;
                Random rand = new Random();
                int r = rand.Next(1, total + 1);
                int basecampid = 0;
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }

                if (basecampid != 0)
                {
                    var sql = "Earthwatcher_GetFreeLand";
                    connection.Open();
                    var cmd = connection.CreateCommand() as SqlCommand;
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                    //cmd.Parameters.AddWithValue("@TutorLandId", Configuration.getTutorLandIdByRegion(regionId));
                    cmd.Parameters.AddWithValue("@GeoHexKey", geohexKey);
                    cmd.Parameters.AddWithValue("@RegionId", regionId);

                    var reader = cmd.ExecuteReader();
                    var hasResult = reader.Read();
                    int landId = 0;
                    if (hasResult)
                    {
                        landId = reader.GetInt32(0);
                        newgeohexKey = reader.GetString(2);
                        usedLand = reader.GetBoolean(3);
                    }
                    connection.Close();
                    return new LandMini { GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId };
                }

            }

            return null;
        }
        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;
        }
        public LandMini GetFreeLandByEarthwatcherId(string baseCamp, int earthwatcherId)
        {
            //Primero asigno un basecamp
            var             basecampRepository = new BasecampRepository(connection.ConnectionString);
            List <Basecamp> basecamps          = basecampRepository.GetByBasecamp(baseCamp);
            bool            usedLand           = false;
            string          newgeohexKey       = null;

            if (basecamps != null && basecamps.Count > 0)
            {
                int    total      = basecamps.Sum(x => x.Probability);
                int    acumprob   = 0;
                Random rand       = new Random();
                int    r          = rand.Next(1, total + 1);
                int    basecampid = 0;
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }

                if (basecampid != 0)
                {
                    try
                    {
                        var sql = "Earthwatcher_GetFreeLandByEarthwatcherId";
                        connection.Open();
                        var cmd = connection.CreateCommand() as SqlCommand;
                        cmd.CommandText = sql;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                        cmd.Parameters.AddWithValue("@TutorLandId", Configuration.TutorLandId);
                        cmd.Parameters.AddWithValue("@EarthwatcherId", earthwatcherId);

                        var reader    = cmd.ExecuteReader();
                        var hasResult = reader.Read();
                        int landId    = 0;
                        if (hasResult)
                        {
                            landId       = reader.GetInt32(0);
                            newgeohexKey = reader.GetString(2);
                            usedLand     = reader.GetBoolean(3);
                        }
                        connection.Close();
                        return(new LandMini {
                            GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId
                        });
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Ha ocurrido una excepcion :", ex); //TODO: HANDLER DE ESTA EXCEPTION
                    }
                }
            }

            return(null);
        }
Example #7
0
        public LandMini GetFreeLandByEarthwatcherIdAndPlayingRegion(int earthwatcherId, int playingRegion)
        {
            //Primero asigno un basecamp
            var             basecampRepository = new BasecampRepository(connection.ConnectionString);
            List <Basecamp> basecamps          = basecampRepository.GetByBasecampRegion(playingRegion);

            bool   usedLand     = false;
            string newgeohexKey = null;
            int    basecampid   = 0;

            //Si el pais tiene basecamps eligo segun la probabilidad
            if (basecamps != null && basecamps.Count > 0)
            {
                int    total    = basecamps.Sum(x => x.Probability);
                int    acumprob = 0;
                Random rand     = new Random();
                int    r        = rand.Next(1, total + 1);
                foreach (var basecamp in basecamps)
                {
                    acumprob += basecamp.Probability;
                    if (r < acumprob)
                    {
                        basecampid = basecamp.Id;
                        break;
                    }
                }
            }

            //Busco una land nueva para el usuario, dependiendo del pais, el basecamp, y el hot point de la parcela.
            try
            {
                var sql = "Earthwatcher_GetFreeLandByEarthwatcherId";
                connection.Open();
                var cmd = connection.CreateCommand() as SqlCommand;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@BasecampDetailId", basecampid);
                //cmd.Parameters.AddWithValue("@TutorLandId", Configuration.getTutorLandIdByRegion(playingRegion));
                cmd.Parameters.AddWithValue("@EarthwatcherId", earthwatcherId);
                cmd.Parameters.AddWithValue("@PlayingRegion", playingRegion);

                var reader    = cmd.ExecuteReader();
                var hasResult = reader.Read();
                int landId    = 0;
                if (hasResult)
                {
                    landId       = reader.GetInt32(0);
                    newgeohexKey = reader.GetString(2);
                    usedLand     = reader.GetBoolean(3);
                }
                connection.Close();
                return(new LandMini {
                    GeohexKey = newgeohexKey, IsUsed = usedLand, Id = landId
                });
            }
            catch (Exception ex)
            {
                //throw new Exception("Ha ocurrido una excepcion :", ex); //TODO: HANDLER DE ESTA EXCEPTION
                return(null);
            }
        }