示例#1
0
        /// <summary>
        /// 写入当前切片级别地理坐标系切片
        /// </summary>
        /// <param name="i"></param>
        /// <param name="tileFileInfo"></param>
        /// <param name="outPutDir"></param>
        public static void WriteGeoLevel(int i, TileFileInfo tileFileInfo, string outPutDir)
        {
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace        pWs           = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(tileFileInfo.filePath), 0);
            IFeatureClass     pFeatureClass = (pWs as IFeatureWorkspace).OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(tileFileInfo.filePath));
            IFeatureLayer     featureLayer  = new FeatureLayerClass
            {
                FeatureClass = pFeatureClass
            };
            IDataset dataSet = (IDataset)pFeatureClass;

            featureLayer.Name = dataSet.Name;
            IMap map = new MapClass();

            map.AddLayer(featureLayer);

            IMapControlDefault pMapControl = new MapControlClass();

            pMapControl.Map = map;
            IActiveView pActiveView = pMapControl.ActiveView;

            tagRECT rect = new tagRECT();

            rect.left   = rect.top = 0;
            rect.right  = 256;
            rect.bottom = 256;
            IExport pExport = null;

            IEnvelope pEnvelope = new EnvelopeClass();
            string    temp      = i.ToString();

            if (temp.Length < 2)
            {
                temp = "0" + temp;
            }
            System.IO.DirectoryInfo LevelDir = new System.IO.DirectoryInfo(outPutDir).CreateSubdirectory($"L{temp}");
            pMapControl.MapScale = TileParam.TileParamProjCoordinateSystem.TileScales[i];
            pMapControl.Refresh();
            Console.WriteLine($"Level:L{temp}:分辨率{TileParam.TileParamGeoCoordinateSystem.TileResolutions[i]}");
            double imageHeighy = TileParam.TileParamGeoCoordinateSystem.TileResolutions[i] * TileParam.TileParamGeoCoordinateSystem.TileSize;

            for (int dy = 1, iRnum = 0; TileParam.TileOriginGeo.OriginY - imageHeighy * dy > tileFileInfo.MinY - imageHeighy; dy++, iRnum++)
            {
                if (TileParam.TileOriginGeo.OriginY - imageHeighy * dy > tileFileInfo.MaxY)
                {
                    continue;
                }
                string tmpy = iRnum.ToString("X");
                while (tmpy.Length < 8)
                {
                    tmpy = "0" + tmpy;
                }
                Console.WriteLine($"--行号:R{tmpy}");
                System.IO.DirectoryInfo RowDir = LevelDir.CreateSubdirectory($"R{tmpy}");

                for (int dx = 1, iCnum = 0; TileParam.TileOriginGeo.OriginX + imageHeighy * dx < tileFileInfo.MaxX + imageHeighy; dx++, iCnum++)
                {
                    if (TileParam.TileOriginGeo.OriginX + imageHeighy * dx < tileFileInfo.MinX)
                    {
                        continue;
                    }
                    string tmpx = iCnum.ToString("X");
                    while (tmpx.Length < 8)
                    {
                        tmpx = "0" + tmpx;
                    }
                    try
                    {
                        pEnvelope.PutCoords(TileParam.TileOriginGeo.OriginX + imageHeighy * (dx - 1), TileParam.TileOriginGeo.OriginY - imageHeighy * dy, TileParam.TileOriginGeo.OriginX + imageHeighy * dx, TileParam.TileOriginGeo.OriginY - imageHeighy * (dy - 1));
                        pExport = ToExporter("PNG");
                        pExport.ExportFileName = RowDir.FullName + "\\C" + tmpx + ".png";
                        Console.WriteLine($"----列号:C{tmpx}    {pExport.ExportFileName}");
                        pExport.Resolution = 96;
                        IEnvelope pPixelBoundsEnv = new EnvelopeClass();
                        pPixelBoundsEnv.PutCoords(rect.left, rect.top, rect.right, rect.bottom);
                        pExport.PixelBounds = pPixelBoundsEnv;
                        //开始导出,获取DC
                        int hDC = pExport.StartExporting();
                        //导出
                        pActiveView.Output(hDC, 96, ref rect, pEnvelope, null);
                        //结束导出
                        pExport.FinishExporting();
                        //清理导出类
                        pExport.Cleanup();
                        RealeaseAO(pExport);
                        RealeaseAO(pPixelBoundsEnv);
                    }
                    catch (Exception ex) { Helpers.ErrorHelper.PrintException(ex); }
                }
            }
            RealeaseAO(workspaceFactory);
            RealeaseAO(pWs);
            RealeaseAO(pFeatureClass);
            RealeaseAO(featureLayer);
            RealeaseAO(map);
            RealeaseAO(pMapControl);
        }