Пример #1
0
        private Dictionary <string, SortedDictionary <string, double> > Stat <T>(IRasterDataProvider raster, string templateName, Dictionary <string, Func <T, bool> > multiFilter, bool weight, float weightZoom, string vectorTemplate) where T : struct, IConvertible
        {
            if (raster == null)
            {
                return(null);
            }
            if (raster == null)
            {
                return(null);
            }
            _perAreas = AreaCountHelper.CalcArea(raster.CoordEnvelope.Center.X, raster.CoordEnvelope.Center.Y, raster.ResolutionX, raster.ResolutionY) * Math.Pow(10, -6);
            if (string.IsNullOrWhiteSpace(templateName))
            {
                return(StatRaster(raster, multiFilter, weight, weightZoom));
            }
            string tempFile = FindRasterFullname(templateName);

            if (templateName == "China_LandRaster")
            {
                return(StatByRasterTemplate <T>(raster, tempFile, multiFilter, weight, weightZoom));
            }
            else
            {
                Dictionary <string, SortedDictionary <string, double> > statResult = StatByRasterTemplate <T>(raster, tempFile, multiFilter, weight, weightZoom, vectorTemplate);
                //SortedDictionary<string, double> newDic = ToSSXLevel(statResult);   //县级统计,向上推至省市级。
                return(statResult);
            }
        }
Пример #2
0
        public StatResultItem[] CountByVector(IRasterDataProvider raster, Dictionary <string, int[]> aoi, Func <T, bool> filter)
        {
            if (filter == null)
            {
                return(null);
            }
            if (aoi == null || aoi.Count == 0)
            {
                return(null);
            }
            double lon   = raster.CoordEnvelope.Center.X;
            double lat   = raster.CoordEnvelope.Center.Y;
            int    count = 0;
            List <StatResultItem> items = new List <StatResultItem>();
            IRasterOperator <T>   oper  = new RasterOperator <T>();

            foreach (string fea in aoi.Keys)
            {
                if (aoi[fea] == null)
                {
                    count = 0;
                }
                else
                {
                    count = oper.Count(raster, aoi[fea], filter);
                }
                StatResultItem it = new StatResultItem();
                it.Name = fea;
                double d = AreaCountHelper.CalcArea(lon, lat, raster.ResolutionX, raster.ResolutionY) * count * Math.Pow(10, -6);
                it.Value = Math.Round(d, 3);
                items.Add(it);
            }
            return(items != null?items.ToArray() : null);
        }
Пример #3
0
        public StatResultItem[] CountByVector(IRasterDataProvider raster, string shpFullname, string shpPrimaryField, Func <T, int, int> weight)
        {
            if (weight == null)
            {
                return(null);
            }
            if (String.IsNullOrEmpty(shpFullname))
            {
                return(null);
            }
            //step2:读取矢量
            CodeCell.AgileMap.Core.Feature[] features = GetFeatures(shpFullname);
            if (features == null || features.Length == 0)
            {
                return(null);
            }
            double lon = raster.CoordEnvelope.Center.X;
            double lat = raster.CoordEnvelope.Center.Y;

            //step3:矢量栅格化
            Dictionary <string, Color> nameColors = new Dictionary <string, Color>();

            using (Bitmap bitmap = VectorsToBitmap(raster, features, shpPrimaryField, out nameColors))
            {
                int[]  aoi;
                Color  color;
                int    count;
                string name;
                List <StatResultItem> items = new List <StatResultItem>();
                IRasterOperator <T>   oper  = new RasterOperator <T>();
                foreach (Feature fea in features)
                {
                    name = fea.GetFieldValue(shpPrimaryField);
                    if (String.IsNullOrEmpty(name))
                    {
                        continue;
                    }
                    color = nameColors[name];
                    aoi   = GetAOIByFeature(bitmap, color);
                    if (aoi == null)
                    {
                        count = 0;
                    }
                    else
                    {
                        count = oper.Count(raster, aoi, weight);
                    }
                    StatResultItem it = new StatResultItem();
                    it.Name = name;
                    it.Code = fea.OID.ToString();
                    double d = AreaCountHelper.CalcArea(lon, lat, raster.ResolutionX, raster.ResolutionY) * count * Math.Pow(10, -6);
                    it.Value = Math.Round(d, 3);
                    items.Add(it);
                }
                return(items != null?items.ToArray() : null);
            }
        }
Пример #4
0
        private static StatResultItem[] AreaStatCurrentRegion <T>(IRasterDataProvider prd, string title, Func <T, bool> filter)
        {
            RasterOperator <T> oper = new RasterOperator <T>();
            int            count    = oper.Count(prd, null, filter);
            StatResultItem sri      = new StatResultItem();

            sri.Name = "当前区域";
            //精细面积计算
            //sri.Value = oper.Area(prd, null, filter);
            double lon = prd.CoordEnvelope.Center.X;
            double lat = prd.CoordEnvelope.Center.Y;

            sri.Value = Math.Round(count * AreaCountHelper.CalcArea(lon, lat, prd.ResolutionX, prd.ResolutionX) / Math.Pow(10, 6), 3);
            return(new StatResultItem[] { sri });
        }
Пример #5
0
        /// <summary>
        /// 基于栅格分类的统计
        /// 输出按分类的覆盖面积
        /// </summary>
        /// <typeparam name="T">待统计数据类型</typeparam>
        /// <param name="raster">待统计数据</param>
        /// <param name="templateName">栅格分类模版(统计分类),如县界\土地类型</param>
        /// <param name="func">统计条件</param>
        public SortedDictionary <string, double> Stat <T>(IRasterDataProvider raster, string templateName, Func <T, bool> func)
        {
            if (raster == null)
            {
                return(null);
            }
            _perAreas = AreaCountHelper.CalcArea(raster.CoordEnvelope.Center.X, raster.CoordEnvelope.Center.Y, raster.ResolutionX, raster.ResolutionY) * Math.Pow(10, -6);
            string tempFile = FindRasterFullname(templateName);

            if (templateName == "China_LandRaster")
            {
                return(StatRasterTemplate <T>(raster, tempFile, func));
            }
            else
            {
                SortedDictionary <string, double> statResult = StatRasterTemplate <T>(raster, tempFile, func);
                SortedDictionary <string, double> newDic     = ToSSXLevel(statResult);//县级统计,向上推至省市级。
                return(newDic);
            }
        }
Пример #6
0
        private StatResultItem[] CountByLandRaster(IRasterDataProvider raster, Func <T, int, int> weight)
        {
            IRasterDictionaryTemplate <byte> temp  = RasterDictionaryTemplateFactory.CreateLandRasterTemplate();
            Dictionary <byte, string>        paris = temp.CodeNameParis;

            if (paris == null)
            {
                return(null);
            }
            double lon = raster.CoordEnvelope.Center.X;
            double lat = raster.CoordEnvelope.Center.Y;
            IRasterOperator <T>   oper  = new RasterOperator <T>();
            List <StatResultItem> items = new List <StatResultItem>();

            int[] aoi;
            int   count;

            foreach (string value in paris.Values)
            {
                aoi = temp.GetAOI(value, raster.CoordEnvelope.MinX, raster.CoordEnvelope.MaxX, raster.CoordEnvelope.MinY, raster.CoordEnvelope.MaxY, new Size(raster.Width, raster.Height));
                if (aoi == null)
                {
                    continue;
                }
                count = oper.Count(raster, aoi, weight);
                if (count == 0)
                {
                    continue;
                }
                StatResultItem it = new StatResultItem();
                it.Name  = value;
                it.Value = count;
                double d = AreaCountHelper.CalcArea(lon, lat, raster.ResolutionX, raster.ResolutionY) * count * Math.Pow(10, -6);
                it.Value = Math.Round(d, 3);
                items.Add(it);
            }
            return(items != null?items.ToArray() : null);
        }
Пример #7
0
        private SortedDictionary <string, StatAreaItem> StatTemplate <T>(IRasterDataProvider[] rasters, IRasterDataProvider tempRaster, Func <T, bool> func)
        {
            try
            {
                //01、计算所有输入栅格的的范围并集
                CoordEnvelope maxEnv = UnionEnv(rasters);
                //计算待统计栅格与分类栅格的相交区域,以创建相同的虚拟栅格
                CoordEnvelope        virtureEnv = maxEnv.Intersect(tempRaster.CoordEnvelope);
                VirtualRasterHeader  vHeader    = VirtualRasterHeader.Create(virtureEnv, rasters[0].ResolutionX, rasters[0].ResolutionX);
                List <VirtualRaster> vRasters   = new List <VirtualRaster>();
                for (int i = 0; i < rasters.Length; i++)
                {
                    VirtualRaster vRaster = new VirtualRaster(rasters[i], vHeader);
                    vRasters.Add(vRaster);
                }
                VirtualRaster template = new VirtualRaster(tempRaster, vHeader);
                int[]         tdata    = template.ReadData <int>(1, 0, 0, vHeader.Width, vHeader.Height);
                //2、依据逻辑,计算初步统计结果,这一步对于大数据,可考虑分块处理
                int    calcLength  = vHeader.Width * vHeader.Height * vRasters.Count;
                int    dataLength  = vHeader.Width * vHeader.Height;
                byte[] valueCalced = new byte[dataLength];
                SortedDictionary <string, StatAreaItem> result = new SortedDictionary <string, StatAreaItem>();
                for (int i = 0; i < vRasters.Count; i++)
                {
                    T[] datas = vRasters[i].ReadData <T>(1, 0, 0, vHeader.Width, vHeader.Height);
                    for (int j = 0; j < dataLength; j++)
                    {
                        UpdateProgress((int)((j + (i * dataLength)) * 100f / calcLength), "正在计算统计数据");
                        if (func(datas[j]))
                        {
                            string key = tdata[j].ToString();
                            if (result.ContainsKey(key))//累计计数
                            {
                                result[key].GrandTotal += 1;
                            }
                            else
                            {
                                result.Add(key, new StatAreaItem()
                                {
                                    GrandTotal = 1
                                });
                            }
                            if (valueCalced[j] == 0)//覆盖计数
                            {
                                valueCalced[j]     = 1;
                                result[key].Cover += 1;
                            }
                        }
                    }
                }

                double perAreas = AreaCountHelper.CalcArea(vHeader.CoordEnvelope.Center.X, vHeader.CoordEnvelope.Center.Y, vHeader.ResolutionX, vHeader.ResolutionY) * Math.Pow(10, -6);
                foreach (string key in result.Keys)
                {
                    result[key].GrandTotal *= perAreas;
                    result[key].Cover      *= perAreas;
                }
                return(result);
            }
            finally
            {
                UpdateProgress(100, "统计数据计算完毕");
            }
        }
Пример #8
0
        /// <summary>
        /// 覆盖面积:指的是覆盖到的面积,
        /// 累计面积:即累计覆盖面积,相同区域不同时次的累计计算
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rasters"></param>
        /// <param name="rasterTemplate"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        private SortedDictionary <string, StatAreaItem> StatTemplate <T>(IRasterDataProvider[] rasters, IRasterDataProvider typeRaster, Func <T, bool> func, bool isCombinSameDay)
        {
            try
            {
                //01、计算所有输入栅格的的范围并集
                CoordEnvelope maxEnv = UnionEnv(rasters);
                //计算待统计栅格与分类栅格的相交区域,以创建相同的虚拟栅格。
                CoordEnvelope       virtureEnv = maxEnv.Intersect(typeRaster.CoordEnvelope);
                VirtualRasterHeader vHeader    = VirtualRasterHeader.Create(virtureEnv, rasters[0].ResolutionX, rasters[0].ResolutionX);

                VirtualRaster typelate = new VirtualRaster(typeRaster, vHeader);
                int[]         tdata    = typelate.ReadData <int>(1, 0, 0, vHeader.Width, vHeader.Height);


                //2、依据逻辑,计算初步统计结果,这一步对于大数据,可考虑分块处理
                int    calcLength  = vHeader.Width * vHeader.Height * rasters.Length;
                int    dataLength  = vHeader.Width * vHeader.Height;
                byte[] valueCalced = new byte[dataLength];

                SortedDictionary <string, StatAreaItem> result = new SortedDictionary <string, StatAreaItem>();
                if (isCombinSameDay)
                {
                    //预处理同天数据
                    Dictionary <DateTime, List <IRasterDataProvider> > dic = new Dictionary <DateTime, List <IRasterDataProvider> >();
                    foreach (IRasterDataProvider raster in rasters)
                    {
                        if (raster.DataIdentify.OrbitDateTime == DateTime.MinValue)
                        {
                            RasterIdentify identify = new RasterIdentify(raster.fileName);
                            raster.DataIdentify.OrbitDateTime = identify.OrbitDateTime;
                        }
                        List <IRasterDataProvider> lst;
                        if (dic.TryGetValue(raster.DataIdentify.OrbitDateTime, out lst))
                        {
                            dic[raster.DataIdentify.OrbitDateTime].Add(raster);
                        }
                        else
                        {
                            lst = new List <IRasterDataProvider>();
                            lst.Add(raster);
                            dic.Add(raster.DataIdentify.OrbitDateTime, lst);
                        }
                    }
                    foreach (DateTime dateKey in dic.Keys)
                    {
                        IRasterDataProvider[] rastersSameDay = dic[dateKey].ToArray();
                        if (rastersSameDay.Length == 1 || dateKey == DateTime.MinValue)
                        {
                            for (int i = 0; i < rastersSameDay.Length; i++)
                            {
                                VirtualRaster vRaster = new VirtualRaster(rastersSameDay[i], vHeader);
                                T[]           datas   = vRaster.ReadData <T>(1, 0, 0, vHeader.Width, vHeader.Height);
                                for (int j = 0; j < dataLength; j++)
                                {
                                    UpdateProgress((int)((j + (i * dataLength)) * 100f / calcLength), "正在计算统计数据");
                                    if (func(datas[j]))
                                    {
                                        string key = tdata[j].ToString();
                                        if (result.ContainsKey(key))//累计计数
                                        {
                                            result[key].GrandTotal += 1;
                                        }
                                        else
                                        {
                                            result.Add(key, new StatAreaItem()
                                            {
                                                GrandTotal = 1
                                            });
                                        }
                                        if (valueCalced[j] == 0)//覆盖计数
                                        {
                                            valueCalced[j]     = 1;
                                            result[key].Cover += 1;
                                        }
                                    }
                                }
                            }
                        }
                        else//同天数据处理,
                        {
                            byte[] sameDayCalced = new byte[vHeader.Width * vHeader.Height];
                            for (int i = 0; i < rasters.Length; i++)
                            {
                                VirtualRaster vRaster = new VirtualRaster(rasters[i], vHeader);
                                T[]           datas   = vRaster.ReadData <T>(1, 0, 0, vHeader.Width, vHeader.Height);
                                for (int j = 0; j < dataLength; j++)
                                {
                                    UpdateProgress((int)((j + (i * dataLength)) * 100f / calcLength), "正在计算统计数据");
                                    if (func(datas[j]))
                                    {
                                        if (sameDayCalced[j] == 0)//
                                        {
                                            sameDayCalced[j] = 1;
                                            string key = tdata[j].ToString();
                                            if (result.ContainsKey(key))//累计计数
                                            {
                                                result[key].GrandTotal += 1;
                                            }
                                            else
                                            {
                                                result.Add(key, new StatAreaItem()
                                                {
                                                    GrandTotal = 1
                                                });
                                            }
                                            if (valueCalced[j] == 0)//覆盖计数
                                            {
                                                valueCalced[j]     = 1;
                                                result[key].Cover += 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < rasters.Length; i++)
                    {
                        VirtualRaster vRaster = new VirtualRaster(rasters[i], vHeader);
                        T[]           datas   = vRaster.ReadData <T>(1, 0, 0, vHeader.Width, vHeader.Height);
                        for (int j = 0; j < dataLength; j++)
                        {
                            UpdateProgress((int)((j + (i * dataLength)) * 100f / calcLength), "正在计算统计数据");
                            if (func(datas[j]))
                            {
                                string key = tdata[j].ToString();
                                if (result.ContainsKey(key))//累计计数
                                {
                                    result[key].GrandTotal += 1;
                                }
                                else
                                {
                                    result.Add(key, new StatAreaItem()
                                    {
                                        GrandTotal = 1
                                    });
                                }
                                if (valueCalced[j] == 0)//覆盖计数
                                {
                                    valueCalced[j]     = 1;
                                    result[key].Cover += 1;
                                }
                            }
                        }
                    }
                }

                double perAreas = AreaCountHelper.CalcArea(vHeader.CoordEnvelope.Center.X, vHeader.CoordEnvelope.Center.Y, vHeader.ResolutionX, vHeader.ResolutionY) * Math.Pow(10, -6);
                foreach (string key in result.Keys)
                {
                    result[key].GrandTotal *= perAreas;
                    result[key].Cover      *= perAreas;
                }
                return(result);
            }
            finally
            {
                UpdateProgress(100, "统计数据计算完毕");
            }
        }
Пример #9
0
 public RasterStatByRaster(Action <int, string> progressTracker)
 {
     _progressTracker = progressTracker;
     _perAreas        = AreaCountHelper.CalcArea(110, 35, 0.01, 0.01) * Math.Pow(10, -6);
 }