Пример #1
0
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="pFeatureClass">要素类</param>
        /// <param name="fieldName">栅格采用的字段</param>
        /// <param name="rasterWorkSpace">输出栅格的工作空间</param>
        /// <param name="newRasterName">新的栅格名称</param>
        public void Convert(IFeatureClass pFeatureClass,
                            string fieldName,
                            string rasterWorkSpace,
                            string newRasterName)
        {
            FileHelper.DeleteFile(rasterWorkSpace, newRasterName, ".tif", ".tfw", ".tif.aux");
            IFeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptorClass();

            featureClassDescriptor.Create(pFeatureClass, null, fieldName);
            IGeoDataset                geoDataset                = (IGeoDataset)featureClassDescriptor;
            IWorkspaceFactory          workspaceFactory          = new RasterWorkspaceFactoryClass();
            IWorkspace                 workspace                 = workspaceFactory.OpenFromFile(rasterWorkSpace, 0);
            IConversionOp              conversionOp              = new RasterConversionOpClass();
            IRasterAnalysisEnvironment rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)conversionOp;

            rasterAnalysisEnvironment.OutWorkspace = workspace;


            //set cell size
            rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _cellSize);
            //set output extent
            object objectMissing = Type.Missing;

            rasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _extentEnvelope, ref objectMissing);
            //set output spatial reference
            rasterAnalysisEnvironment.OutSpatialReference = ((IGeoDataset)pFeatureClass).SpatialReference;
            //convertion
            conversionOp.ToRasterDataset(geoDataset, _rasterType, workspace, newRasterName);
        }
Пример #2
0
        private void sure_Click(object sender, EventArgs e)
        {
            string layer_name = layers1.Text;
            ILayer choose     = null;
            double @base      = double.Parse(basenumber.Text);
            double interval   = double.Parse(dis.Text);


            ILayer temp_lay;

            for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
            {
                temp_lay = m_hookHelper.FocusMap.get_Layer(i);
                if (temp_lay.Name == layer_name)
                {
                    choose = temp_lay;
                }
            }

            IRasterLayer chooseras = choose as IRasterLayer;


            ISurfaceOp2 pSurfaceOp = default(ISurfaceOp2);

            pSurfaceOp = new RasterSurfaceOp() as ISurfaceOp2;
            IGeoDataset       pRasterDataset       = chooseras as IGeoDataset;
            IWorkspace        pShpWS               = default(IWorkspace);
            IWorkspaceFactory pShpWorkspaceFactory =
                new  ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory();

            pShpWS     = pShpWorkspaceFactory.OpenFromFile(FilePath, 0);
            pSurfaceOp = new RasterSurfaceOp() as ISurfaceOp2;
            IRasterAnalysisEnvironment pRasterAEnv =
                (IRasterAnalysisEnvironment)pSurfaceOp;

            pRasterAEnv.OutWorkspace = pShpWS;
            IGeoDataset   pOutput       = default(IGeoDataset);
            IFeatureClass pFeatureClass = default(IFeatureClass);
            IFeatureLayer pFLayer       = default(IFeatureLayer);



            object tmpbase;

            tmpbase = (object)@base;
            object tmpmy = 1;

            pOutput = pSurfaceOp.Contour(pRasterDataset, interval,
                                         ref tmpbase, ref tmpmy);
            pFeatureClass        = (IFeatureClass)pOutput;
            pFLayer              = new FeatureLayer();
            pFLayer.FeatureClass = pFeatureClass;
            IGeoFeatureLayer pGeoFL = default(IGeoFeatureLayer);

            pGeoFL = (IGeoFeatureLayer)pFLayer;
            pGeoFL.DisplayAnnotation = false;
            pGeoFL.DisplayField      = "CONTOUR";
            pGeoFL.Name = "CONTOUR";
            m_hookHelper.FocusMap.AddLayer(pGeoFL);
        }
Пример #3
0
        /// <summary>
        /// 矢量点转栅格
        /// </summary>
        /// <param name="xjFeatureLayer">矢量图层</param>
        /// <param name="RasterPath">栅格绝对路径</param>
        /// <param name="CellSize">栅格边长</param>
        /// <param name="SecletctedField">所选字段(高程等)</param>
        /// <returns>返回栅格图层</returns>
        private ILayer xjShpPointToRaster(IFeatureLayer xjFeatureLayer, string RasterPath, double CellSize, string SecletctedField)
        {
            IFeatureClass           xjFeatureClass           = xjFeatureLayer.FeatureClass;
            IFeatureClassDescriptor xjFeatureClassDescriptor = new FeatureClassDescriptorClass();//using ESRI.ArcGIS.GeoAnalyst;

            xjFeatureClassDescriptor.Create(xjFeatureClass, null, SecletctedField);
            IGeoDataset xjGeoDataset = xjFeatureClassDescriptor as IGeoDataset;

            IWorkspaceFactory          xjwsf          = new RasterWorkspaceFactoryClass(); //using ESRI.ArcGIS.DataSourcesRaster;
            string                     xjRasterFolder = System.IO.Path.GetDirectoryName(RasterPath);
            IWorkspace                 xjws           = xjwsf.OpenFromFile(xjRasterFolder, 0);
            IConversionOp              xjConversionOp = new RasterConversionOpClass();
            IRasterAnalysisEnvironment xjRasteren     = xjConversionOp as IRasterAnalysisEnvironment;

            object xjCellSize = CellSize as object;

            xjRasteren.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref xjCellSize);

            string         xjFileName = System.IO.Path.GetFileName(RasterPath);
            IRasterDataset xjdaset2   = xjConversionOp.ToRasterDataset(xjGeoDataset, "TIFF", xjws, xjFileName);

            IRasterLayer xjRasterLayer = new RasterLayerClass();

            xjRasterLayer.CreateFromDataset(xjdaset2);
            ILayer xjLayer = xjRasterLayer;

            xjRasterLayer.Name = xjFileName;

            return(xjLayer);
        }
Пример #4
0
        public IGeoDataset GetIDW(IFeatureClass _pFeatureClass, string _pFieldName, double _pDistance, double _pCell, int _pPower)
        {
            IGeoDataset             Geo              = _pFeatureClass as IGeoDataset;
            object                  pExtent          = Geo.Extent;
            object                  o                = Type.Missing;
            IFeatureClassDescriptor pFeatureClassDes = new FeatureClassDescriptorClass();

            pFeatureClassDes.Create(_pFeatureClass, null, _pFieldName);
            IInterpolationOp           pInterOp    = new RasterInterpolationOpClass();
            IRasterAnalysisEnvironment pRasterAEnv = pInterOp as IRasterAnalysisEnvironment;

            // pRasterAEnv.Mask = Geo;
            pRasterAEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref pExtent, ref o);

            object pCellSize = _pCell;//可以根据不同的点图层进行设置

            pRasterAEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref pCellSize);

            IRasterRadius pRasterrad = new RasterRadiusClass();
            object        obj        = Type.Missing;

            // pRasterrad.SetFixed(_pDistance, ref obj);
            pRasterrad.SetVariable(12);

            object pBar = Type.Missing;

            IGeoDataset pGeoIDW = pInterOp.IDW(pFeatureClassDes as IGeoDataset, _pPower, pRasterrad, ref pBar);

            return(pGeoIDW);
        }
Пример #5
0
        private void IDWInterpolation(IFeatureClass pointFeature, IRaster refRaster, string name)
        {
            IFeatureClassDescriptor pointDescript = new FeatureClassDescriptorClass();

            pointDescript.Create(pointFeature, null, name);
            object                     extend   = (refRaster as IGeoDataset).Extent;
            IRasterProps               refProps = refRaster as IRasterProps;
            object                     cell     = refProps.MeanCellSize().X;
            IInterpolationOp           interpla = new RasterInterpolationOpClass();
            IRasterAnalysisEnvironment IDWEnv   = interpla as IRasterAnalysisEnvironment;

            IDWEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cell);
            IDWEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extend);
            IGeoDataset   output = interpla.IDW((IGeoDataset)pointDescript, 2, null, null);
            IGeoDataset   input  = refRaster as IGeoDataset;
            IExtractionOp op     = new RasterExtractionOpClass();

            output = op.Raster(output, input);
            var               clipRaster   = (IRaster)output;
            ISaveAs           pSaveAs      = clipRaster as ISaveAs;
            IWorkspaceFactory workspaceFac = new RasterWorkspaceFactoryClass();
            var               groups       = Regex.Match(name, @"^(\d+)_(\d+)_(\d+)$").Groups;

            name = string.Format("{0:D2}{1:D2}", int.Parse(groups[2].Value), int.Parse(groups[3].Value));
            IDataset outDataset = pSaveAs.SaveAs(fileName + name + ".img", workspaceFac.OpenFromFile(filePath, 0), "IMAGINE Image");

            System.Runtime.InteropServices.Marshal.ReleaseComObject(outDataset);
        }
Пример #6
0
        private IFunctionRasterDataset createBarrierRaster()
        {
            IFeatureClassDescriptor ftrDesc = new FeatureClassDescriptorClass();
            IQueryFilter            qf      = new QueryFilterClass();

            ftrDesc.Create(BarriersFeatureClass, qf, BarriersFeatureClass.OIDFieldName);
            IConversionOp convOp = new RasterConversionOpClass();
            IRasterAnalysisEnvironment       rasterAnalysisEnvironment       = (IRasterAnalysisEnvironment)convOp;
            IRasterAnalysisGlobalEnvironment rasterAnalysisGlobalEnvironment = (IRasterAnalysisGlobalEnvironment)convOp;

            rasterAnalysisGlobalEnvironment.AvoidDataConversion = true;
            rasterAnalysisEnvironment.OutSpatialReference       = sp;
            rasterAnalysisEnvironment.OutWorkspace = OutWorkspace;
            //object cells = Dem.RasterInfo.CellSize;
            object ext  = ((IGeoDataset)Dem).Extent;
            object snap = ((IGeoDataset)Dem);

            rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, Dem);
            rasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ext, Dem);
            string                 outRdName = rsUtil.getSafeOutputName(OutWorkspace, "sBarrier");
            IRasterDataset         geoDset   = convOp.ToRasterDataset((IGeoDataset)ftrDesc, "GRID", OutWorkspace, outRdName);
            IGeoDatasetSchemaEdit2 geoSch    = (IGeoDatasetSchemaEdit2)geoDset;

            if (geoSch.CanAlterSpatialReference)
            {
                geoSch.AlterSpatialReference(rasterAnalysisEnvironment.OutSpatialReference);
            }
            return(rsUtil.createIdentityRaster(geoDset));
        }
Пример #7
0
        /// <summary>
        /// 执行矢量转栅格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FtoR_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            //设置CancellationPending可以退出任务线程
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
            }
            else
            {
                try
                {
                    FileInfo inputinfo  = new FileInfo(featuretoraster_inputcombox.Text);
                    FileInfo outputinfo = new FileInfo(featuretoraster_output.Text);


                    IWorkspaceFactory outputworkspaceFactory = new RasterWorkspaceFactoryClass();
                    IWorkspace        outputworkspaceop      = outputworkspaceFactory.OpenFromFile(outputinfo.DirectoryName, 0);//创建输出工作空间

                    IWorkspaceFactory       workspacefactory       = new ShapefileWorkspaceFactoryClass();
                    IFeatureWorkspace       featureworkspace       = workspacefactory.OpenFromFile(inputinfo.DirectoryName, 0) as IFeatureWorkspace;
                    IFeatureClass           featureclass           = featureworkspace.OpenFeatureClass(inputinfo.Name);
                    IFeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptorClass();
                    featureClassDescriptor.Create(featureclass, null, featuretoraster_fieldcombox.Text);
                    IGeoDataset geodatasetop = featureClassDescriptor as IGeoDataset;

                    IConversionOp conversionOp = new RasterConversionOpClass();
                    //转换设置
                    IRasterAnalysisEnvironment rasterAnalysisEnvironment = conversionOp as IRasterAnalysisEnvironment;
                    //栅格的大小
                    double dCellSize = Convert.ToDouble(featuretoraster_cellsize.Text);
                    object oCellSize = dCellSize as object;
                    //设置栅格大小
                    rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref oCellSize);
                    string format = "";
                    if (outputinfo.Extension == ".tif")
                    {
                        format = "TIFF";
                    }
                    else if (outputinfo.Extension == ".img")
                    {
                        format = "IMAGINE Image";
                    }
                    else
                    {
                        format = "GRID";
                    }
                    //启动预处理计时
                    watch = new Stopwatch();
                    watch.Start();
                    //执行转换
                    conversionOp.ToRasterDataset(geodatasetop, format, outputworkspaceop, outputinfo.Name);
                }
                catch (Exception erro)
                {
                    MessageBox.Show(erro.Message, "错误");
                }
            }
        }
Пример #8
0
        private IGeoDataset GetBeforeGeo(IFeatureClass fc)
        {
            IInterpolationOp3 pInterpolationOp = new RasterInterpolationOpClass();

            IFields fields = fc.Fields;
            int     index  = fields.FindFieldByAliasName("高程");
            IField  field  = fields.get_Field(index);

            ISpatialFilter filter = new SpatialFilter();

            filter.Geometry   = geo;
            filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            IFeatureClassDescriptor pFcd = new FeatureClassDescriptorClass();

            pFcd.Create(fc, filter, field.Name);

            //定义搜索半径
            IRasterRadius pRadius = new RasterRadiusClass();
            object        Missing = Type.Missing;

            pRadius.SetVariable(12, ref Missing);

            //设置栅格图像的单位大小
            object cellSizeProvider         = 5;
            IRasterAnalysisEnvironment pEnv = pInterpolationOp as IRasterAnalysisEnvironment;

            pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            IGeoDataset outGeoDataset = pInterpolationOp.IDW(pFcd as IGeoDataset, 2, pRadius, ref Missing);

            return(outGeoDataset);
        }
Пример #9
0
        private IInterpolationOp SetRasterInterpolationAnalysisEnv(String rasterpath, double cellsize, IFeatureLayer pFeatLayer)
        {
            object            Missing = Type.Missing;
            IWorkspaceFactory pWSF;

            pWSF = new RasterWorkspaceFactoryClass();
            IWorkspace                 pWorkspace       = pWSF.OpenFromFile(rasterpath, 0);
            IInterpolationOp           pInterpolationOp = new RasterInterpolationOpClass();
            IRasterAnalysisEnvironment pRsEnv           = (IRasterAnalysisEnvironment)pInterpolationOp;

            pRsEnv.OutWorkspace = pWorkspace;
            //装箱操作
            object objCellSize = cellsize;

            pRsEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objCellSize);

            IEnvelope pEnv = new EnvelopeClass();

            pEnv.XMin = pFeatLayer.AreaOfInterest.XMin;
            pEnv.XMax = pFeatLayer.AreaOfInterest.XMax;
            pEnv.YMin = pFeatLayer.AreaOfInterest.YMin;
            pEnv.YMax = pFeatLayer.AreaOfInterest.YMax;
            object objExtent = pEnv;

            pRsEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objExtent, ref Missing);
            return(pInterpolationOp);
        }
Пример #10
0
        public static void Mlayer_IDW_Click()
        {
            // 用反距离IDW插值生成的栅格图像。如下:
            IInterpolationOp pInterpolationOp = new RasterInterpolationOpClass();

            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            string            pPath             = Application.StartupPath + @"\MakeContours\Cont.shp";
            string            pFolder           = System.IO.Path.GetDirectoryName(pPath);
            string            pFileName         = System.IO.Path.GetFileName(pPath);

            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0);

            IFeatureWorkspace       pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass           oFeatureClass     = pFeatureWorkspace.OpenFeatureClass(pFileName);
            IFeatureClassDescriptor pFCDescriptor     = new FeatureClassDescriptorClass();

            pFCDescriptor.Create(oFeatureClass, null, "shape.z");
            IRasterRadius pRadius = new RasterRadiusClass();

            object objectMaxDistance = null;
            object objectbarrier     = null;
            object missing           = Type.Missing;

            pRadius.SetVariable(12, ref objectMaxDistance);

            object    dCellSize      = 1;
            object    snapRasterData = Type.Missing;
            IEnvelope pExtent;

            pExtent = new EnvelopeClass();
            Double xmin = 27202;
            Double xmax = 31550;

            Double ymin = 19104;
            Double ymax = 22947;

            pExtent.PutCoords(xmin, ymin, xmax, ymax);
            object extentProvider           = pExtent;
            IRasterAnalysisEnvironment pEnv = pInterpolationOp as IRasterAnalysisEnvironment;

            pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dCellSize);
            pEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
            IGeoDataset poutGeoDataset = pInterpolationOp.IDW((IGeoDataset)pFCDescriptor, 2, pRadius, ref objectbarrier);
            ISurfaceOp  surOp          = new RasterSurfaceOpClass();


            IRaster pOutRaster = poutGeoDataset as IRaster;

            IRasterLayer pOutRasLayer = new RasterLayer();

            pOutRasLayer.CreateFromRaster(pOutRaster);

            IMap pMap = Common.DataEditCommon.g_pMap;

            pMap.AddLayer(pOutRasLayer);
            Common.DataEditCommon.g_axTocControl.Refresh();
            Common.DataEditCommon.g_pAxMapControl.ActiveView.Refresh();
        }
Пример #11
0
        public static void Mlayer_Krige_Click()
        {
            // 用克里金Krige插值生成的栅格图像。如下:
            IWorkspaceFactory       pWorkspaceFactory = new ShapefileWorkspaceFactory();
            string                  pPath             = Application.StartupPath + @"\MakeContours\Cont.shp";
            string                  pFolder           = System.IO.Path.GetDirectoryName(pPath);
            string                  pFileName         = System.IO.Path.GetFileName(pPath);
            IWorkspace              pWorkspace        = pWorkspaceFactory.OpenFromFile(pFolder, 0);
            IFeatureWorkspace       pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass           oFeatureClass     = pFeatureWorkspace.OpenFeatureClass(pFileName);
            IFeatureClassDescriptor pFCDescriptor     = new FeatureClassDescriptorClass();

            pFCDescriptor.Create(oFeatureClass, null, "shape.z");

            IInterpolationOp           pInterpolationOp = new RasterInterpolationOpClass();
            IRasterAnalysisEnvironment pEnv             = pInterpolationOp as IRasterAnalysisEnvironment;

            object Cellsize = 0.004;//Cell size for output raster;0.004

            pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref Cellsize);
            //设置输出范围
            //27202 19104;27202 22947;31550 22947;31550 19104
            object    snapRasterData = Type.Missing;
            IEnvelope pExtent;

            pExtent = new EnvelopeClass();
            Double xmin = 27202;
            Double xmax = 31550;

            Double ymin = 19104;
            Double ymax = 22947;

            pExtent.PutCoords(xmin, ymin, xmax, ymax);
            object extentProvider = pExtent;

            pEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
            Double        dSearchD     = 10;
            object        pSearchCount = 3;
            object        missing      = Type.Missing;
            IRasterRadius pRadius      = new RasterRadius();

            pRadius.SetFixed(dSearchD, ref pSearchCount);
            //pRadius.SetVariable((int)pSearchCount, ref dSearchD);

            IGeoDataset poutGeoDataset = pInterpolationOp.Krige((IGeoDataset)pFCDescriptor, esriGeoAnalysisSemiVariogramEnum.esriGeoAnalysisGaussianSemiVariogram, pRadius, false, ref missing);

            IRaster      pOutRaster   = poutGeoDataset as IRaster;
            IRasterLayer pOutRasLayer = new RasterLayer();

            pOutRasLayer.CreateFromRaster(pOutRaster);

            IMap pMap = Common.DataEditCommon.g_pMap;

            pMap.AddLayer(pOutRasLayer);
            Common.DataEditCommon.g_axTocControl.Refresh();
            Common.DataEditCommon.g_pAxMapControl.ActiveView.Refresh();
        }
Пример #12
0
        /// <summary>
        /// Sets the output environment for this raster tool, using the specified extent for output.
        /// </summary>
        /// <param name="pRasterTool">The tool to set the ouput environment for.</param>
        /// <param name="sSubDir">The subdirectory to use for output.</param>
        /// <param name="extentProvider">An IEnvelope or RasterDataset that defines the output extent.</param>
        /// <remarks>The extentProvider determines the extent, but the default raster (the input DEM) determines cell size
        /// and snap registration of the cells.</remarks>
        internal void SetAnalysisEnvironment(IRasterAnalysisEnvironment rasterTool, object extentProvider)
        {
            object inputGrid = _GetDefaultGrid();

            rasterTool.DefaultOutputRasterPrefix = "UrbDelTmp";
            rasterTool.OutWorkspace        = GetTempRasterWorkspace();
            rasterTool.OutSpatialReference = ((IGeoDataset)inputGrid).SpatialReference;
            rasterTool.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref inputGrid);
            rasterTool.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref inputGrid);
        }
        private IGeoDataset GetAfterGeo(IFeatureClass fc)
        {
            try
            {
                ISpatialFilter pSpatialFilter = new SpatialFilter();
                pSpatialFilter.Geometry   = geo;
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                int count = fc.FeatureCount(pSpatialFilter);
                if (count == 0)
                {
                    return(null);
                }
                IFeatureCursor cursor = fc.Search(pSpatialFilter, true);
                IFeature       feature;
                while ((feature = cursor.NextFeature()) != null)
                {
                    int index = feature.Fields.FindField("HEIGHT");
                    if (index == -1)
                    {
                        continue;
                    }
                    feature.set_Value(index, h);
                    feature.Store();
                }

                IInterpolationOp3 pInterpolationOp = new RasterInterpolationOpClass();

                IFields fields = fc.Fields;
                int     index1 = fields.FindField("HEIGHT");
                if (index1 == -1)
                {
                    return(null);
                }
                IField field = fields.get_Field(index1);

                IFeatureClassDescriptor pFcd = new FeatureClassDescriptorClass();
                pFcd.Create(fc, pSpatialFilter, field.Name);

                //定义搜索半径
                IRasterRadius pRadius = new RasterRadiusClass();
                object        Missing = Type.Missing;
                pRadius.SetVariable(12, ref Missing);

                //设置栅格图像的单位大小
                object cellSizeProvider         = 5;
                IRasterAnalysisEnvironment pEnv = pInterpolationOp as IRasterAnalysisEnvironment;
                pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
                IGeoDataset outGeoDataset = pInterpolationOp.IDW(pFcd as IGeoDataset, 2, pRadius, ref Missing);
                return(outGeoDataset);
            }
            catch (System.Exception ex)
            {
                return(null);
            }
        }
Пример #14
0
        /// <summary>
        /// 设置输出数据空间参考
        /// </summary>
        /// <param name="raster"></param>
        /// <param name="pRasAnalysisEnvironment"></param>
        private static void SetOutSpatialReference(IRaster raster, IRasterAnalysisEnvironment pRasAnalysisEnvironment)
        {
            //设置输出数据空间参考
            IGeoDataset inGeoDataset = raster as IGeoDataset;

            if (inGeoDataset == null)
            {
                return;
            }

            pRasAnalysisEnvironment.OutSpatialReference = inGeoDataset.SpatialReference;
        }
Пример #15
0
        private IGeoDataset GetBeforeGeo(IFeatureClass fc, IGeometry geo)
        {
            try
            {
                WaitForm.SetCaption("正在生成栅格表面...");
                IInterpolationOp3 pInterpolationOp = new RasterInterpolationOpClass();

                IFields       fields            = fc.Fields;
                FacilityClass fac               = FacilityClassManager.Instance.GetFacilityClassByName("GCD");
                DFDataConfig.Class.FieldInfo fi = fac.GetFieldInfoBySystemName("Altitude");
                if (fi == null)
                {
                    return(null);
                }

                int index = fields.FindField(fi.Name);
                if (index == -1)
                {
                    return(null);
                }
                IField field = fields.get_Field(index);

                ISpatialFilter filter = new SpatialFilter();
                filter.Geometry   = geo;
                filter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
                int count = fc.FeatureCount(filter);
                if (count == 0)
                {
                    return(null);
                }
                IFeatureClassDescriptor pFcd = new FeatureClassDescriptorClass();
                pFcd.Create(fc, filter, field.Name);

                //定义搜索半径
                IRasterRadius pRadius = new RasterRadiusClass();
                object        Missing = Type.Missing;
                pRadius.SetVariable(12, ref Missing);

                //设置栅格图像的单位大小
                object cellSizeProvider         = 5;
                IRasterAnalysisEnvironment pEnv = pInterpolationOp as IRasterAnalysisEnvironment;
                pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
                IGeoDataset outGeoDataset = pInterpolationOp.IDW(pFcd as IGeoDataset, 2, pRadius, ref Missing);
                return(outGeoDataset);
            }
            catch (System.Exception ex)
            {
                return(null);

                WaitForm.Stop();
            }
        }
Пример #16
0
        /// <summary>
        /// 设置栅格分析环境的输出空间为临时文件夹
        /// </summary>
        /// <param name="rasterAnalysisEnvironment">栅格分析环境</param>
        public static void SetRasterAnalysisOutWorkspace(IRasterAnalysisEnvironment rasterAnalysisEnvironment)
        {
            if (rasterAnalysisEnvironment == null)
            {
                return;
            }
            //获取临时目录
            string     finalTemPath = TempFile.CreateNewTempDirectory();
            IWorkspace workspace    = RasterDataInfoClass.GetRasterWorkspace(finalTemPath);

            //设置输出工作空间
            rasterAnalysisEnvironment.OutWorkspace = workspace;
        }
Пример #17
0
        private void button3_Click(object sender, EventArgs e)
        {
            string inputFullFilePath  = textBox1.Text;
            string outputFullFilePath = textBox2.Text;
            // deal with the outputFullFilePath
            int    _index                = outputFullFilePath.LastIndexOf("\\");
            string outputRasterName      = (outputFullFilePath.Substring(_index + 1)); //name
            string outputRasterDirectory = outputFullFilePath.Remove(_index);          //directory

            //deal with input
            IRasterLayer pRasterLayer = new RasterLayerClass();

            pRasterLayer.CreateFromFilePath(inputFullFilePath);
            IRaster raster = pRasterLayer.Raster;

            IMapAlgebraOp mapAlgebraOp;

            mapAlgebraOp = new RasterMapAlgebraOpClass();
            //控制raster分析的环境
            IRasterAnalysisEnvironment rasterAnalysisEnvironment = default(IRasterAnalysisEnvironment);

            rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)mapAlgebraOp;

            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IWorkspace        workspace        = workspaceFactory.OpenFromFile(outputRasterDirectory, 0);//这里应该是输出raster的路径

            rasterAnalysisEnvironment.OutWorkspace = workspace;

            try
            {
                mapAlgebraOp.BindRaster((IGeoDataset)raster, "this");
                //定义表达式(elevationMode为要减去的数值)不要忘了"[ ]"
                //"CON(ISNULL([this]),0,[this])"
                //"~(([this]>=92)&([this]<=272))&([this]>=2)"
                string   strOut    = textBox3.Text;
                IRaster  outRaster = (IRaster)mapAlgebraOp.Execute(strOut);
                ISaveAs2 saveAs;
                saveAs = (ISaveAs2)outRaster;
                saveAs.SaveAs(outputRasterName, workspace, "TIFF");//输出名称(注意:名称中加后缀名,例:test.tif),工作空间,格式
                MessageBox.Show("栅格计算器成功!");

                IRasterLayer pRlayer = new RasterLayer();
                pRlayer.CreateFromRaster(outRaster);
                pRlayer.Name = System.IO.Path.GetFileName(outputFullFilePath);
                mainForm.axMapControl1.AddLayer(pRlayer, 0);
            }
            catch (Exception ex)
            {
            }
        }
Пример #18
0
 public bool RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo, string FileName)
 {
     try
     {
         IRaster       pRaster                      = pRasterLayer.Raster;
         IRasterProps  pProps                       = pRaster as IRasterProps;
         object        cellSizeProvider             = pProps.MeanCellSize().X;
         IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
         IExtractionOp pExtractionOp                = new RasterExtractionOpClass();
         IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;
         pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
         object extentProvider = clipGeo.Envelope;
         object snapRasterData = Type.Missing;
         pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
         IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true); //裁切操作
         IRaster     clipRaster;                                                                       //裁切后得到的IRaster
         if (pOutputDataset is IRasterLayer)
         {
             IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
             clipRaster = rasterLayer.Raster;
         }
         else if (pOutputDataset is IRasterDataset)
         {
             IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
             clipRaster = rasterDataset.CreateDefaultRaster();
         }
         else if (pOutputDataset is IRaster)
         {
             clipRaster = pOutputDataset as IRaster;
         }
         else
         {
             return(false);
         }
         //保存裁切后得到的clipRaster
         //如果直接保存为img影像文件
         IWorkspaceFactory pWKSF      = new RasterWorkspaceFactoryClass();
         IWorkspace        pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0);
         ISaveAs           pSaveAs    = clipRaster as ISaveAs;
         IDataset          pDataset   = pSaveAs.SaveAs(System.IO.Path.GetFileName(FileName) + ".tif", pWorkspace, "TIFF");//以TIF格式保存
         System.Runtime.InteropServices.Marshal.ReleaseComObject(pDataset);
         return(true);
         //MessageBox.Show("成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     catch (Exception exp)
     {
         MessageBox.Show(exp.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return(false);
     }
 }
        /// <summary>
        /// 矢量转栅格
        /// </summary>
        /// <param name="IFeatureClass">要转换的矢量数据</param>
        /// <param name="eleField">转换的字段名</param>
        /// <param name="cellsize">栅格大小,默认为null</param>
        ///<param name="rasterSavePath">保存路径</param>
        ///<param name="demName">dem名称</param>
        /// <returns>返回ILayer类型的图层</returns>
        public ILayer FeatureToRaster(IFeatureClass pFeatureClass, string eleField, object cellSize, string rasterSavePath, string demName)
        {
            IFeatureClassDescriptor pFeatureClassDescriptor = new FeatureClassDescriptorClass(); //获取转化的字段

            pFeatureClassDescriptor.Create(pFeatureClass, null, eleField);                       //转换字段
            IGeoDataset pGeoDataset = (IGeoDataset)pFeatureClassDescriptor;                      //获取第一个参数

            //默认栅格大小
            if (Convert.ToDouble(cellSize) <= 0)
            {
                IEnvelope envelope = pGeoDataset.Extent;
                if (envelope.Width > envelope.Height)
                {
                    cellSize = envelope.Height / 250;
                }
                else
                {
                    cellSize = envelope.Width / 250;
                }
            }
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IRasterWorkspace2 rasterWorkspace2 = workspaceFactory.OpenFromFile(rasterSavePath, 0) as IRasterWorkspace2;

            //in case that there is already an existing raster with the raster name, try to delete it
            if (System.IO.Directory.Exists(System.IO.Path.Combine(rasterSavePath, demName)))
            {
                IDataset dataset = rasterWorkspace2.OpenRasterDataset(demName) as IDataset;
                dataset.Delete();
            }
            IConversionOp conversionOp = new RasterConversionOpClass();
            IRasterAnalysisEnvironment rasterAnalysisEnvironment = conversionOp as IRasterAnalysisEnvironment;

            rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSize);
            IRasterDataset rasterDataset = null;

            try
            { rasterDataset = conversionOp.ToRasterDataset(pGeoDataset, "TIFF", (IWorkspace)rasterWorkspace2, demName); }
            catch (Exception ee)
            { MessageBox.Show(ee.Message); }
            ILayer layer = null;

            if (rasterDataset != null)
            {
                IRasterLayer pRlyr = new RasterLayerClass();
                pRlyr.CreateFromDataset(rasterDataset);
                layer = pRlyr as ILayer;
            }
            return(layer);
        }
Пример #20
0
        // RasterModel ger möjlighet för klassificering, ändring av värden m.m.
        public void rasterModel(List <IRasterLayer> rList, List <string> sList1, List <string> sList2, string script, string rName)
        {
            IRasterModel rasterModel = new RasterModelClass();

            IRasterAnalysisEnvironment env = (IRasterAnalysisEnvironment)rasterModel;
            IWorkspaceFactory          workspaceFactory = new RasterWorkspaceFactoryClass();
            IWorkspace workspace = workspaceFactory.OpenFromFile(@"H:\VT2019\GIS_App\Projekt\Program\Data\UtData\Raster", 0);

            env.OutWorkspace = workspace;

            rasterModel.Script = script;

            for (int i = 0; i < rList.Count; i++)
            {
                rasterModel.BindRaster(rList[i].Raster, sList1[i]);
            }

            rasterModel.Execute();
            IRaster rResult1 = rasterModel.get_BoundRaster(sList2[sList2.Count - 1]);

            for (int i = 0; i < sList1.Count; i++)
            {
                rasterModel.UnbindSymbol(sList1[i]);
            }

            IRasterLayer newRaster = new RasterLayer();

            newRaster.CreateFromRaster(rResult1);
            newRaster.Name = rName;
            map.AddLayer(newRaster);

            switch (rName)
            {
            case "Kostnadsraster":
                cost = newRaster;
                //map.DeleteLayer(newRaster);
                break;

            case "Osäkra platser":
                source = newRaster;
                break;

            case "Säkra platser":
                dest = newRaster;
                break;
            }
        }
Пример #21
0
        public static IRaster buildSlope(string string_0, string string_1, string string_2)
        {
            IWorkspaceFactory          factory     = new RasterWorkspaceFactory();
            IRasterDataset             dataset     = ((IRasterWorkspace)factory.OpenFromFile(string_0, 0)).OpenRasterDataset(string_1);
            ISurfaceOp                 op          = new RasterSurfaceOp() as ISurfaceOp;
            IRasterAnalysisEnvironment environment = (IRasterAnalysisEnvironment)op;
            IWorkspaceFactory          factory2    = new RasterWorkspaceFactory();
            IWorkspace                 workspace2  = factory2.OpenFromFile(string_2, 0);

            environment.OutWorkspace = workspace2;
            object zFactor = new object();

            zFactor = Missing.Value;
            return
                ((IRaster)
                 op.Slope((IGeoDataset)dataset, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref zFactor));
        }
Пример #22
0
        }//Open Feature Class

        private void IDWOperate(IFeatureClass inFC)
        {
            IFeatureClassDescriptor iFCDesc = new FeatureClassDescriptorClass();
            //获取选中的字符串
            string sFieldName = comboBox1.SelectedText;

            iFCDesc.Create(inFC, null, sFieldName);
            //创建RasterInterpolationOp对象
            IInterpolationOp iIo = new RasterInterpolationOpClass();

            //设置输出Raster网格大小,默认的Raster输出空间范围和输入数据相同
            IRasterAnalysisEnvironment iRae = (IRasterAnalysisEnvironment)iIo;
            object oCellSize = (object)Convert.ToDouble(textBox2.Text);

            iRae.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref oCellSize);

            //搜索范围设置
            IRasterRadius iRadius = new RasterRadiusClass();
            //switch (comboBox2.SelectedIndex)
            //{
            //    case 0:
            object dis = (object)Convert.ToDouble(textBox3.Text);

            iRadius.SetVariable(Convert.ToInt32(textBox1.Text), ref dis);
            //        break;
            //    case 1:
            //        object cou = (object)Convert.ToDouble(textBox3.Text);
            //        iRadius.SetFixed(Convert.ToDouble(textBox1.Text), ref cou);
            //        break;
            //    default:
            //        break;
            //}

            IGeoDataset iInputGeo  = (IGeoDataset)iFCDesc;
            object      barrier    = Type.Missing;
            IGeoDataset iOutputGeo = iIo.IDW(iInputGeo, Convert.ToDouble(textBox2.Text), iRadius, ref barrier);

            IRaster iOutRaster = (IRaster)iOutputGeo;
            IRasterBandCollection iRasBnadC = (IRasterBandCollection)iOutRaster;
            IWorkspaceFactory     iWSF      = new RasterWorkspaceFactoryClass();
            IWorkspace            iWS       = iWSF.OpenFromFile(textBox5.Text, 0);

            IDataset iDs = iRasBnadC.SaveAs("Raster_test", iWS, "GRID");
        }
Пример #23
0
        /// <summary>
        /// 设置栅格分析环境
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="analysisOperation"></param>
        /// <param name="referencedRaster"></param>
        /// <param name="cellSize"></param>
        public static void SetAnalysisEnvironment <T>(T analysisOperation, IRaster referencedRaster, object cellSize)
            where T : IMapAlgebraOp
        {
            IRasterAnalysisEnvironment pRasAnalysisEnvironment = (IRasterAnalysisEnvironment)analysisOperation;

            //设置输出空间
            SetRasterAnalysisOutWorkspace(pRasAnalysisEnvironment);
            //设置输出参考栅格
            IRasterProps rasterProps = (IRasterProps)referencedRaster;

            //装箱操作,必须设置,因为输入栅格可能空间参考等属性不同
            //设置输出数据像元大小
            pRasAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSize);
            //设置栅格分析处理范围
            object objExtent = rasterProps.Extent;

            pRasAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objExtent);
            //设置输出数据空间参考
            SetOutSpatialReference(referencedRaster, pRasAnalysisEnvironment);
        }
Пример #24
0
        public static void RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo)
        {
            IRaster       pRaster                      = pRasterLayer.Raster;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp pExtractionOp                = new RasterExtractionOpClass();
            IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;

            pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            object extentProvider = clipGeo.Envelope;
            object snapRasterData = Type.Missing;

            pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvMinOf, ref extentProvider, ref snapRasterData);
            //IWorkspaceFactory pWrokspaceFactory = new RasterWorkspaceFactoryClass();
            //if (flag == 0)
            //{
            //    flag++;
            //}
            //if (flag == 1)
            //{
            //    pRasterAnaEnvir.OutWorkspace = pWrokspaceFactory.OpenFromFile("D://temp", 0);
            //}
            IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo, true); //裁切操作
            IRaster     clipRaster     = pOutputDataset as IRaster;                           //裁切后得到的IRaster

            //保存裁切后得到的clipRaster
            //如果直接保存为img影像文件
            IWorkspaceFactory pWKSF    = new RasterWorkspaceFactory();
            SaveFileDialog    savefile = new SaveFileDialog();

            savefile.Title  = "选择输出影像";
            savefile.Filter = "栅格文件(*.tif)|*.tif|栅格文件(*.img)|*.img";
            if (savefile.ShowDialog() == DialogResult.OK)
            {
                IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(savefile.FileName), 0);
                ISaveAs    pSaveAs    = clipRaster as ISaveAs;
                pSaveAs.SaveAs(System.IO.Path.GetFileName(savefile.FileName), pWorkspace, "TIFF");//以img格式保存;
                MessageBox.Show("裁切成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Пример #25
0
        private IRaster RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo)
        {
            if (clipGeo == null)
            {
                return(null);
            }

            IRaster clipRaster = null;

            IRaster       pRaster                      = pRasterLayer.Raster;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp pExtractionOp                = new RasterExtractionOpClass();
            IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;

            pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            object extentProvider = clipGeo.Envelope;
            object snapRasterData = Type.Missing;

            pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
            IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true);

            if (pOutputDataset is IRasterLayer)
            {
                IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
                clipRaster = rasterLayer.Raster;
            }
            else if (pOutputDataset is IRasterDataset)
            {
                IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
                clipRaster = rasterDataset.CreateDefaultRaster();
            }
            else if (pOutputDataset is IRaster)
            {
                clipRaster = pOutputDataset as IRaster;
            }

            return(clipRaster);
        }
Пример #26
0
        public void RasterMeaAtt(IRaster pRaster, IGeometry pGeoMetry, ref double pixelmax, ref double pixelmin)
        {
            IPolygon      clipGeo                      = pGeoMetry as IPolygon;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp pExtractionOp                = new RasterExtractionOpClass();
            IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;

            pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            object extentProvider = clipGeo.Envelope;
            object snapRasterData = Type.Missing;

            pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
            IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true); //裁切操作
            IRaster     clipRaster     = null;                                                            //裁切后得到的IRaster

            if (pOutputDataset is IRasterLayer)
            {
                IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
                clipRaster = rasterLayer.Raster;
            }
            else if (pOutputDataset is IRasterDataset)
            {
                IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
                clipRaster = rasterDataset.CreateDefaultRaster();
            }
            else if (pOutputDataset is IRaster)
            {
                clipRaster = pOutputDataset as IRaster;
            }
            else
            {
                //return false;
            }

            CalRasterAtt(clipRaster as IRaster2, ref pixelmax, ref pixelmin);
        }
Пример #27
0
        // Konverterar raster till vektor.
        public IFeatureLayer convertRasterToVector(string outPath, string fileName)
        {
            IGeoDataset pLayerDataset = (IGeoDataset)dest;

            IConversionOp conversionOp     = new RasterConversionOpClass();
            IRasterAnalysisEnvironment env = (IRasterAnalysisEnvironment)conversionOp;

            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IWorkspace        workspace        = workspaceFactory.OpenFromFile(outPath, 0);

            env.OutWorkspace = workspace;

            IWorkspaceFactory wsFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace        shapeWS   = wsFactory.OpenFromFile(outPath, 0);

            IFeatureWorkspace fWorkspace = (IFeatureWorkspace)wsFactory.OpenFromFile(outPath, 0);
            IWorkspace2       workTemp   = (IWorkspace2)fWorkspace;

            if (workTemp.NameExists[esriDatasetType.esriDTFeatureClass, fileName])
            {
                IDataset dataset = (IDataset)fWorkspace.OpenFeatureClass(fileName);
                dataset.Delete();
            }

            IGeoDataset   featPolClassOut = conversionOp.RasterDataToPolygonFeatureData(pLayerDataset, shapeWS, fileName + ".shp", true);
            IFeatureClass fClass          = fWorkspace.OpenFeatureClass(fileName);
            IFeatureLayer fLayer          = new FeatureLayerClass();

            fLayer.FeatureClass = fClass;
            fLayer.Name         = fClass.AliasName;
            fLayer.Visible      = true;

            map.AddLayer(fLayer);

            return(fLayer);
        }
Пример #28
0
        /// <summary>
        /// Sets the output environment for this raster tool, using the default extent (of the DEM)
        /// </summary>
        /// <param name="pRasterTool">The tool to set the ouput environment for.</param>
        /// <param name="sSubDir">The subdirectory to use for output.</param>
        internal void SetAnalysisEnvironment(IRasterAnalysisEnvironment rasterTool)
        {
            object inputGrid = _GetDefaultGrid();

            SetAnalysisEnvironment(rasterTool, inputGrid);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //通过IDW插值生成栅格图层
            #region
            label2.Text = "正在进行IDW插值";
            IFeatureLayer pFeatureLayer_point = axMapControl1.Map.Layer[0] as IFeatureLayer;//获取点图层
            IRasterRadius pRadius             = new RasterRadiusClass();
            object        missing             = Type.Missing;
            pRadius.SetVariable(12, ref missing);
            IFeatureClassDescriptor pFCDescriptor = new FeatureClassDescriptorClass();
            pFCDescriptor.Create(pFeatureLayer_point.FeatureClass, null, "高程");

            object                     cellSizeProvider = 185.244192;
            IInterpolationOp           pInterpolationOp = new RasterInterpolationOpClass();
            IRasterAnalysisEnvironment pEnv             = pInterpolationOp as IRasterAnalysisEnvironment;
            pEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            IRaster pOutRaster;

            try
            {
                pOutRaster = pInterpolationOp.IDW(pFCDescriptor as IGeoDataset, 2, pRadius, ref missing) as IRaster;
            }
            catch
            {
                pOutRaster = pInterpolationOp.IDW(pFCDescriptor as IGeoDataset, 2, pRadius, ref missing) as IRaster;
            }

            //Add output into ArcMap as a raster layer
            RasterLayer pOutRasLayer = new RasterLayerClass();
            pOutRasLayer.CreateFromRaster(pOutRaster);
            pOutRasLayer.Name = "栅格";
            axMapControl1.Map.AddLayer(pOutRasLayer);
            #endregion
            //提取等值线
            #region
            label2.Text = "正在生成等值线...";
            IGeoDataset                pGeoDataSet                = pOutRaster as IGeoDataset;
            IWorkspaceFactory          pWorkspaceFactory1         = new ShapefileWorkspaceFactory();
            string                     file_path                  = System.IO.Path.GetDirectoryName(database_path);
            IWorkspace                 pShpWorkspace              = pWorkspaceFactory1.OpenFromFile(file_path, 0);
            ISurfaceOp2                pSurfaceOp2                = new RasterSurfaceOpClass();
            IRasterAnalysisEnvironment pRasterAnalysisEnvironment = pSurfaceOp2 as IRasterAnalysisEnvironment;

            pRasterAnalysisEnvironment.Reset();
            pRasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
            pRasterAnalysisEnvironment.OutWorkspace = pShpWorkspace;
            double      dInterval      = 0.8; //间距
            IGeoDataset pOutputDataSet = pSurfaceOp2.Contour(pGeoDataSet, dInterval, ref missing, ref missing);

            IFeatureClass pFeatureClass1 = pOutputDataSet as IFeatureClass;
            IFeatureLayer pFeatureLayer  = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass1;

            IGeoFeatureLayer pGeoFeatureLayer = pFeatureLayer as IGeoFeatureLayer;
            pGeoFeatureLayer.DisplayAnnotation = true;
            pGeoFeatureLayer.DisplayField      = "Contour";
            pGeoFeatureLayer.Name = "高程等值线";
            axMapControl1.Map.AddLayer(pGeoFeatureLayer);
            label2.Text = "完毕";
            #endregion
        }
Пример #30
0
        /// <summary>
        /// 执行地图代数运算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks></remarks>

        private void Button_OK_Click(System.Object sender, System.EventArgs e)
        {
            IRasterLayer pRasLayer = default(IRasterLayer);
            IRaster      pRaster   = default(IRaster);

            IEnvelope layExtend            = default(IEnvelope);
            double    AnalysisExtentLeft   = 0;
            double    AnalysisExtentRight  = 0;
            double    AnalysisExtentTop    = 0;
            double    AnalysisExtentBottom = 0;

            string layerNameFir = null;

            try
            {
                if (LayerList.Count != 0)
                {
                    if (textBox_Result.Text.ToString().Length != 0)
                    {
                        layerNameFir = LayerList[0].ToString();

                        layExtend            = GetLayerExtend(layerNameFir);
                        AnalysisExtentLeft   = layExtend.XMin;
                        AnalysisExtentRight  = layExtend.XMax;
                        AnalysisExtentTop    = layExtend.YMax;
                        AnalysisExtentBottom = layExtend.YMin;

                        pMapAlgebraOp = new RasterMapAlgebraOp() as IMapAlgebraOp;
                        //设置栅格计算分析环境
                        IRasterAnalysisEnvironment pRasAnaEnv = default(IRasterAnalysisEnvironment);
                        pRasAnaEnv            = (IRasterAnalysisEnvironment)pMapAlgebraOp;
                        pRasAnaEnv.VerifyType = esriRasterVerifyEnum.esriRasterVerifyOn;

                        object dddd;
                        dddd = GetRasterCellSize(layerNameFir);
                        pRasAnaEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dddd);
                        //设置分析范围pAnaExtent
                        IEnvelope pAnaExtent = default(IEnvelope);
                        pAnaExtent = new Envelope() as IEnvelope;

                        pAnaExtent.XMin = Convert.ToDouble(AnalysisExtentLeft);
                        pAnaExtent.XMax = Convert.ToDouble(AnalysisExtentRight);
                        pAnaExtent.YMax = Convert.ToDouble(AnalysisExtentTop);
                        pAnaExtent.YMin = Convert.ToDouble(AnalysisExtentBottom);

                        object dd1 = pAnaExtent;
                        object dd2 = null;


                        pRasAnaEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref dd1, ref dd2);

                        foreach (string LayerName in LayerList)
                        {
                            pRasLayer = (IRasterLayer)FindLayerByName(pCurMap, LayerName);
                            //MsgBox(LayerName)
                            pRaster = pRasLayer.Raster;
                            RasterList.Add(pRaster);
                        }
                        //将容量设置为 ArrayList 中元素的实际数目
                        LayerList.TrimToSize();
                        RasterList.TrimToSize();

                        //绑定
                        int i = 0;
                        if (LayerList.Count == RasterList.Count)
                        {
                            for (i = 0; i <= LayerList.Count - 1; i++)
                            {
                                pMapAlgebraOp.BindRaster((IGeoDataset)RasterList[i], LayerList[i].ToString());
                            }
                        }


                        //获取文本框中的运算表达式()
                        string sCalExpression = null;
                        sCalExpression = textBox_Calculate.Text;
                        //执行地图代数运算
                        IRaster pOutRasterDS = default(IRaster);
                        pOutRasterDS = (IRaster)pMapAlgebraOp.Execute(sCalExpression);

                        //解除绑定
                        if (LayerList.Count == RasterList.Count)
                        {
                            for (i = 0; i <= LayerList.Count - 1; i++)
                            {
                                pMapAlgebraOp.UnbindRaster(LayerList[i].ToString());
                            }
                        }


                        //保存到工作空间
                        IWorkspaceFactory pWsFact = default(IWorkspaceFactory);
                        IWorkspace        pWS     = default(IWorkspace);
                        int hwnd = 0;
                        pWsFact = new RasterWorkspaceFactory();
                        pWS     = pWsFact.OpenFromFile(sOutRasPath, hwnd);
                        IRasterBandCollection pRasterbandCollection = default(IRasterBandCollection);

                        pRasterbandCollection = (IRasterBandCollection)pOutRasterDS;
                        IDataset pDataset = default(IDataset);

                        pDataset = pRasterbandCollection.SaveAs(sOutRasName, pWS, "IMAGINE Image");

                        //输出到mapcontrol中
                        IRasterDataset pOutResultDS = default(IRasterDataset);
                        pOutResultDS = (IRasterDataset)pDataset;
                        IRasterLayer pOutRasterLayer = default(IRasterLayer);
                        pOutRasterLayer = new RasterLayer();
                        pOutRasterLayer.CreateFromDataset(pOutResultDS);
                        //MapControlMain.AddLayer(pOutRasterLayer)
                        pCurMap.AddLayer(pOutRasterLayer);
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("保存计算结果为空,请输入结果文件名!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //Interaction.MsgBox(ex.ToString);
            }
        }