Ejemplo n.º 1
0
        public static void ValidateParentAndShapes(Progress Progress,
                                                   Dictionary <string, int> items, string file, string codeColumn, string iParent, bool skipOrphans = false)
        {
            medea.context.Data.Session.Ping();
            List <NetTopologySuite.Features.Feature> features;
            bool validateGeo = true;

            if (file.EndsWith(".shp", System.StringComparison.InvariantCultureIgnoreCase))
            {
                features = ShapeFile.ReadShapefile(file);
            }
            else if (file.EndsWith(".dbf", System.StringComparison.InvariantCultureIgnoreCase))
            {
                features    = ShapeFile.ReadDbasefile(file);
                validateGeo = false;
            }
            else
            {
                throw new Exception("Invalid extension.");
            }
            ValidateFeatures(Progress, items, codeColumn, iParent, features, validateGeo, skipOrphans);
            medea.context.Data.Session.Ping();
        }
Ejemplo n.º 2
0
        private void SaveItems(Dictionary <string, int> ci)
        {
            if (!fileAdded)
            {
                return;
            }

            Progress.Caption = "Guardando ítems";

            //Trae todos los items de la geografía padre para asociar.
            current.GeographyItems.Clear();
            Progress.Total = 0;
            var features = ShapeFile.ReadShapefile(Basename + ".shp");

            if (dbfMissingFilename != null)
            {
                var featuresMissing = ShapeFile.ReadDbasefile(dbfMissingFilename);
                features.AddRange(featuresMissing);
            }
            Progress.Total = features.Count;
            Dictionary <string, bool> done = new Dictionary <string, bool>();

            int n = 0;
            List <GeographyItem> l = new List <GeographyItem>();

            foreach (var feature in features)
            {
                GeographyItem item = new GeographyItem();
                if (iHousehold != "")
                {
                    item.Households = ParseOrZero(iHousehold, feature);
                }
                if (iChildren != "")
                {
                    item.Children = ParseOrZero(iChildren, feature);
                }
                if (iPopulation != "")
                {
                    item.Population = ParseOrZero(iPopulation, feature);
                }
                if (iUrbanity != "")
                {
                    item.Urbanity = UrbanityEnumFromInt(ParseOrZero(iUrbanity, feature));
                }
                else
                {
                    item.Urbanity = UrbanityEnum.None;
                }
                item.Code = feature.Attributes[iCode].ToString();
                if (iCaption != "")
                {
                    item.Caption = feature.Attributes[iCaption].ToString();
                }

                if (ci != null)
                {
                    var parent = feature.Attributes[iParent].ToString();
                    item.Parent = new GeographyItem(ci[parent]);
                }

                item.Geometry = (Geometry)feature.Geometry;
                if (feature.Geometry != null)
                {
                    item.AreaM2   = Projections.CalculateM2Area(feature.Geometry);
                    item.Centroid = (Point)feature.Geometry.Centroid;

                    Simplifications.FillSimplifiedGeometries(item.Geometry, item);
                }
                else
                {
                    throw new Exception("La geometría no puede ser nula.");
                }

                item.Geography = current;
                if (done.ContainsKey(item.Code) == false)
                {
                    l.Add(item);
                    done[item.Code] = true;
                    if (n % 100 == 99)
                    {
                        string sql = InsertGenerator.FromList(l);
                        context.Data.Session.SqlActions.ExecuteNonQuery(sql);
                        l.Clear();
                    }
                    n++;
                }
                Progress.Increment();
            }
            if (l.Count > 0)
            {
                string sql = InsertGenerator.FromList(l);
                context.Data.Session.SqlActions.ExecuteNonQuery(sql);
            }
            string updateAverage = "UPDATE `geography` SET geo_area_avg_m2=(select avg(gei_area_m2) from "
                                   + "geography_item where gei_geography_id = geo_id) WHERE geo_id = " + current.Id.Value.ToString();

            context.Data.Session.SqlActions.ExecuteNonQuery(updateAverage);
            // esta formula es cualquier cosa, que fitea más o menos 6 como maxzoom de provincias y 11 como maxzoom de departamentos.
            //string updateMaxZoom = "update geography set geo_max_zoom = truncate (16-(power(geo_area_avg_m2, .25) / 60),0) "
            //		+ "WHERE geo_id = " + current.Id.Value.ToString();
            //context.Data.Session.SqlActions.ExecuteNonQuery(updateAverage);
        }