コード例 #1
0
        /// <summary>
        /// Gets the values from the raster.  If MaxSampleCount is less than the
        /// number of cells, then it randomly samples the raster with MaxSampleCount
        /// values.  Otherwise it gets all the values in the raster.
        /// </summary>
        /// <param name="raster">The raster to sample</param>
        public void GetValues(IRaster raster)
        {
            Values = raster.GetRandomValues(EditorSettings.MaxSampleCount);
            var keepers = Values.Where(val => val != raster.NoDataValue).ToList();

            Values = keepers;
            Statistics.Calculate(Values, raster.Minimum, raster.Maximum);
        }
コード例 #2
0
        /// <summary>
        /// Gets the values from the raster.  If MaxSampleCount is less than the
        /// number of cells, then it randomly samples the raster with MaxSampleCount
        /// values.  Otherwise it gets all the values in the raster.
        /// </summary>
        /// <param name="raster">The raster to sample</param>
        public void GetValues(IRaster raster)
        {
            Values = raster.GetRandomValues(EditorSettings.MaxSampleCount);
            List <double> keepers = new List <double>();

            foreach (double val in Values)
            {
                if (val != raster.NoDataValue)
                {
                    keepers.Add(val);
                }
            }
            Values = keepers;
            Statistics.Calculate(Values, raster.Minimum, raster.Maximum);
        }
コード例 #3
0
        /// <summary>
        /// Before attempting to create categories using a color ramp, this must be calculated
        /// to updated the cache of values that govern statistics and break placement.
        /// </summary>
        /// <param name="table">The data Table to use.</param>
        public void GetValues(DataTable table)
        {
            Values = new List <double>();
            string normField = EditorSettings.NormField;
            string fieldName = EditorSettings.FieldName;

            if (!string.IsNullOrEmpty(EditorSettings.ExcludeExpression))
            {
                DataRow[] rows = table.Select("NOT (" + EditorSettings.ExcludeExpression + ")");
                foreach (DataRow row in rows)
                {
                    if (rows.Length < EditorSettings.MaxSampleCount)
                    {
                        double val;
                        if (!double.TryParse(row[fieldName].ToString(), out val))
                        {
                            continue;
                        }
                        if (double.IsNaN(val))
                        {
                            continue;
                        }

                        if (normField != null)
                        {
                            double norm;
                            if (!double.TryParse(row[normField].ToString(), out norm) || double.IsNaN(val))
                            {
                                continue;
                            }

                            Values.Add(val / norm);
                            continue;
                        }

                        Values.Add(val);
                    }
                    else
                    {
                        Dictionary <int, double> randomValues = new();
                        int count = EditorSettings.MaxSampleCount;
                        int max   = rows.Length;

                        // Specified seed is required for consistently recreating the break values
                        Random rnd = new(9999);

                        for (int i = 0; i < count; i++)
                        {
                            double val;
                            double norm = 1;
                            int    index;
                            bool   failed = false;
                            do
                            {
                                index = rnd.Next(max);
                                if (!double.TryParse(rows[index][fieldName].ToString(), out val))
                                {
                                    failed = true;
                                }
                                if (normField == null)
                                {
                                    continue;
                                }

                                if (!double.TryParse(rows[index][normField].ToString(), out norm))
                                {
                                    failed = true;
                                }
                            }while (randomValues.ContainsKey(index) || double.IsNaN(val) || failed);

                            if (normField != null)
                            {
                                Values.Add(val / norm);
                            }
                            else
                            {
                                Values.Add(val);
                            }

                            randomValues.Add(index, val);
                        }
                    }
                }

                Values.Sort();
                Statistics.Calculate(Values);
                return;
            }

            if (table.Rows.Count < EditorSettings.MaxSampleCount)
            {
                // Simply grab all the values
                foreach (DataRow row in table.Rows)
                {
                    double val;
                    if (!double.TryParse(row[fieldName].ToString(), out val))
                    {
                        continue;
                    }
                    if (double.IsNaN(val))
                    {
                        continue;
                    }

                    if (normField == null)
                    {
                        Values.Add(val);
                        continue;
                    }

                    double norm;
                    if (!double.TryParse(row[normField].ToString(), out norm) || double.IsNaN(val))
                    {
                        continue;
                    }

                    Values.Add(val / norm);
                }
            }
            else
            {
                // Grab random samples
                Dictionary <int, double> randomValues = new();
                int count = EditorSettings.MaxSampleCount;
                int max   = table.Rows.Count;

                // Specified seed is required for consistently recreating the break values
                Random rnd = new(9999);
                for (int i = 0; i < count; i++)
                {
                    double val;
                    double norm = 1;
                    int    index;
                    bool   failed = false;
                    do
                    {
                        index = rnd.Next(max);
                        if (!double.TryParse(table.Rows[index][fieldName].ToString(), out val))
                        {
                            failed = true;
                        }
                        if (normField == null)
                        {
                            continue;
                        }

                        if (!double.TryParse(table.Rows[index][normField].ToString(), out norm))
                        {
                            failed = true;
                        }
                    }while (randomValues.ContainsKey(index) || double.IsNaN(val) || failed);

                    if (normField != null)
                    {
                        Values.Add(val / norm);
                    }
                    else
                    {
                        Values.Add(val);
                    }

                    randomValues.Add(index, val);
                }
            }

            Values.Sort();
        }
コード例 #4
0
        /// <summary>
        /// Gets the values from a file based data source rather than an in memory object.
        /// </summary>
        /// <param name="source">Source to get the values from.</param>
        /// <param name="progressHandler">The progress handler.</param>
        public void GetValues(IAttributeSource source, ICancelProgressHandler progressHandler)
        {
            int pageSize = 100000;

            Values = new List <double>();
            string normField = EditorSettings.NormField;
            string fieldName = EditorSettings.FieldName;

            if (source.NumRows() < EditorSettings.MaxSampleCount)
            {
                int numPages = (int)Math.Ceiling((double)source.NumRows() / pageSize);
                for (int ipage = 0; ipage < numPages; ipage++)
                {
                    int       numRows = (ipage == numPages - 1) ? source.NumRows() % pageSize : pageSize;
                    DataTable table   = source.GetAttributes(ipage * pageSize, numRows);
                    if (!string.IsNullOrEmpty(EditorSettings.ExcludeExpression))
                    {
                        DataRow[] rows = table.Select("NOT (" + EditorSettings.ExcludeExpression + ")");
                        foreach (DataRow row in rows)
                        {
                            double val;
                            if (!double.TryParse(row[fieldName].ToString(), out val))
                            {
                                continue;
                            }
                            if (double.IsNaN(val))
                            {
                                continue;
                            }

                            if (normField != null)
                            {
                                double norm;
                                if (!double.TryParse(row[normField].ToString(), out norm) || double.IsNaN(val))
                                {
                                    continue;
                                }

                                Values.Add(val / norm);
                                continue;
                            }

                            Values.Add(val);
                        }
                    }
                    else
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            double val;
                            if (!double.TryParse(row[fieldName].ToString(), out val))
                            {
                                continue;
                            }
                            if (double.IsNaN(val))
                            {
                                continue;
                            }

                            if (normField != null)
                            {
                                double norm;
                                if (!double.TryParse(row[normField].ToString(), out norm) || double.IsNaN(val))
                                {
                                    continue;
                                }

                                Values.Add(val / norm);
                                continue;
                            }

                            Values.Add(val);
                        }
                    }
                }
            }
            else
            {
                Dictionary <int, double> randomValues = new();
                pageSize = 10000;
                int count = EditorSettings.MaxSampleCount;

                // Specified seed is required for consistently recreating the break values
                Random         rnd          = new(9999);
                AttributePager ap           = new(source, pageSize);
                int            countPerPage = count / ap.NumPages();
                ProgressMeter  pm           = new(progressHandler, "Sampling " + count + " random values", count);
                for (int iPage = 0; iPage < ap.NumPages(); iPage++)
                {
                    for (int i = 0; i < countPerPage; i++)
                    {
                        double val;
                        double norm = 1;
                        int    index;
                        bool   failed = false;
                        do
                        {
                            index = rnd.Next(ap.StartIndex, ap.StartIndex + pageSize);
                            DataRow dr = ap.Row(index);
                            if (!double.TryParse(dr[fieldName].ToString(), out val))
                            {
                                failed = true;
                            }
                            if (normField == null)
                            {
                                continue;
                            }

                            if (!double.TryParse(dr[normField].ToString(), out norm))
                            {
                                failed = true;
                            }
                        }while (randomValues.ContainsKey(index) || double.IsNaN(val) || failed);

                        if (normField != null)
                        {
                            Values.Add(val / norm);
                        }
                        else
                        {
                            Values.Add(val);
                        }

                        randomValues.Add(index, val);
                        pm.CurrentValue = i + (iPage * countPerPage);
                    }

                    if (progressHandler != null && progressHandler.Cancel)
                    {
                        break;
                    }
                }
            }

            Values.Sort();
            Statistics.Calculate(Values);
        }