示例#1
0
        void IClip.ClipByLayerFileInsideSde(object in_feature, string in_layer_file_path, object clip_feature, string clip_layer_file_path, string out_feature)
        {
            using (ComReleaser releaser = new ComReleaser())
            {
                try
                {
                    Geoprocessor gp = new Geoprocessor();
                    IDataManager _dataManager = new DataManager(this._environment);
                    ESRI.ArcGIS.AnalysisTools.Clip clipTool = new ESRI.ArcGIS.AnalysisTools.Clip();

                    //releaser.ManageLifetime(gp);
                    //releaser.ManageLifetime(clipTool);
                    //IVariantArray param = new VarArrayClass();

                    string inlayer = string.Format("{0}.lyr", in_layer_file_path);
                    string cliplayer = string.Format("{0}.lyr", clip_layer_file_path);
                    //MessageBox.Show(string.Format("line 61 GExtractTool in={0}, clip={1}", inlayer, cliplayer));
                    _dataManager.SaveToLayerFile((ILayer)in_feature, inlayer);
                    _dataManager.SaveToLayerFile((ILayer)clip_feature, cliplayer);

                    //MessageBox.Show(((IFeatureLayer)in_feature).FeatureClass.AliasName);
                    gp.SetEnvironmentValue("workspace", this._environment);
                    clipTool.in_features = inlayer;
                    clipTool.clip_features = cliplayer;
                    clipTool.out_feature_class = out_feature;//string.Format("{0}{1}", this._temFullPath, out_feature);//"C:\\tayninh\\temp\\tempmdb.mdb\\" + out_feature;

                    runTool(gp, clipTool, null);
                }
                catch (Exception err) { MessageBox.Show("loi clip: " + err.ToString()); }
            }
        }
        /// <summary>
        /// Handles the click event of the Button and starts asynchronous geoprocessing.
        /// </summary>
        private void btnRunGP_Click(object sender, EventArgs e)
        {
            try
            {
                #region tidy up any previous gp runs

                //Clear the ListView control
                listView1.Items.Clear();

                //Remove any result layers present in the map
                IMapLayers mapLayers = axMapControl1.Map as IMapLayers;
                foreach (IFeatureLayer resultLayer in _resultsList)
                {
                    mapLayers.DeleteLayer(resultLayer);
                }

                axTOCControl1.Update();

                //Empty the results layer list
                _resultsList.Clear();

                //make sure that my GP tool queue is empty
                _myGPToolsToExecute.Clear();

                #endregion

                //Buffer the selected cities by the specified distance
                ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer();
                bufferTool.in_features = _layersDict["Cities"];
                bufferTool.buffer_distance_or_field = txtBufferDistance.Text + " Miles";
                bufferTool.out_feature_class        = "city_buffer.shp";

                //Clip the zip codes layer with the result of the buffer tool
                ESRI.ArcGIS.AnalysisTools.Clip clipTool = new ESRI.ArcGIS.AnalysisTools.Clip();
                clipTool.in_features       = _layersDict["ZipCodes"];
                clipTool.clip_features     = bufferTool.out_feature_class;
                clipTool.out_feature_class = "city_buffer_clip.shp";

                //To run multiple GP tools asynchronously, all tool inputs must exist before ExecuteAsync is called.
                //The output from the first buffer operation is used as an input to the second clip
                //operation. To deal with this restriction a Queue is created and all tools added to it. The first tool
                //is executed from this method, whereas the second is executed from the ToolExecuted event. This approach
                //is scalable - any additional geoprocessing tools added to this queue will also be executed in turn.
                _myGPToolsToExecute.Enqueue(bufferTool);
                _myGPToolsToExecute.Enqueue(clipTool);
                _gp.ExecuteAsync(_myGPToolsToExecute.Dequeue());
            }
            catch (Exception ex)
            {
                listView1.Items.Add(new ListViewItem(new string[2] {
                    "N/A", ex.Message
                }, "error"));
            }
        }
示例#3
0
        void Clip(string[] inputFeatures, string outputFilePath)
        {
            Geoprocessor geoprocessor = new Geoprocessor();

            ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();

            clip.in_features       = inputFeatures[0];
            clip.clip_features     = inputFeatures[1];
            clip.out_feature_class = outputFilePath;
            geoprocessor.Execute(clip, null);
            AddShapeFileToView(outputFilePath);
        }
示例#4
0
        public static void GetTysonPolygon()
        {
            IFeatureLayer pStudentLayer = Func.GetFeatureLayerByName("Students");
            IFeatureLayer pChinaLayer   = Func.GetFeatureLayerByName("BOUA_PJ");
            string        strOutput     = Application.StartupPath + @"\Data\TysonAll.shp";

            Geoprocessor gp = new Geoprocessor()
            {
                OverwriteOutput = true
            };

            ESRI.ArcGIS.AnalysisTools.CreateThiessenPolygons pCreateThiessenPolygons = new ESRI.ArcGIS.AnalysisTools.CreateThiessenPolygons()
            {
                in_features       = pStudentLayer,
                out_feature_class = strOutput,
                fields_to_copy    = "ONLY_FID"
            };
            IGeoProcessorResult pResult = gp.ExecuteAsync(pCreateThiessenPolygons);

            new Timer()
            {
                Enabled  = true,
                Interval = 500
            }.Tick += (sender, e) => {
                if (pResult.Status == esriJobStatus.esriJobSucceeded)
                {
                    ESRI.ArcGIS.AnalysisTools.Clip pClip = new ESRI.ArcGIS.AnalysisTools.Clip()
                    {
                        in_features       = strOutput,
                        clip_features     = pChinaLayer,
                        out_feature_class = Application.StartupPath + @"\Data\tyson.shp"
                    };
                    pResult = gp.ExecuteAsync(pClip);
                    new Timer()
                    {
                        Enabled  = true,
                        Interval = 500
                    }.Tick += (sender2, e2) => {
                        if (pResult.Status == esriJobStatus.esriJobSucceeded)
                        {
                            m_pMapC2.AddShapeFile(Application.StartupPath + @"\Data", "tyson.shp");
                            SetTysonSymbol();
                            (sender2 as Timer).Enabled = false;
                            CloseWaitForm();
                        }
                    };
                    (sender as Timer).Enabled = false;
                }
            };
        }
示例#5
0
        /// <summary>
        /// 简单调用一个裁剪工具
        /// </summary>
        public void GPtest1()
        {
            Geoprocessor gp = new Geoprocessor();

            //输出文件是否能被覆盖
            gp.OverwriteOutput = true;
            gp.SetEnvironmentValue("scratchworkspace", @"F:\gpTest\data");
            ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();
            clip.in_features       = @"F:\gpTest\data\po1.shp";
            clip.clip_features     = @"F:\gpTest\data\po2.shp";
            clip.cluster_tolerance = "";
            clip.out_feature_class = @"\gpTest\data\result.shp";
            gp.Execute(clip, null);
        }
示例#6
0
文件: GPClip.cs 项目: Leopold-Z/Test
        private void ClipOneLayer(String obj)
        {
            ScrollToBottom(txtMessages);

            txtMessages.Text += "裁剪的层: " + obj + "\r\n";
            txtMessages.Text += "\r\n开始裁剪. 这可能要花几分钟时间...\r\n";
            txtMessages.Update();

            Geoprocessor gp = new Geoprocessor();

            gp.OverwriteOutput = true;
            txtMessages.Text  += "裁剪...\r\n";
            txtMessages.Update();
            try
            {
                ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();
                if (GetFeatureLayer(obj) != null)
                {
                    clip.in_features       = GetFeatureLayer(obj);
                    clip.out_feature_class = txtOutputPath.Text + "\\" + "clip_" + obj;
                }
                else
                {
                    return;
                }
                clip.clip_features     = clipFeatureClass;
                clip.cluster_tolerance = tolerance;
                IGeoProcessorResult results = (IGeoProcessorResult)gp.Execute(clip, null);
                if (results != null)
                {
                    if (results.Status != esriJobStatus.esriJobSucceeded)
                    {
                        txtMessages.Text += "裁剪要素/层失败: " + obj + "\r\n";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("裁剪要素问题: " + ex.Message);
            }
            txtMessages.Text += ReturnMessages(gp);
            //scroll the textbox to the bottom
            ScrollToBottom(txtMessages);

            txtMessages.Text += "\r\n完成.\r\n";
            txtMessages.Text += "-------------------------------------------------------\r\n";
            //scroll the textbox to the bottom
            ScrollToBottom(txtMessages);
        }
示例#7
0
        void IClip.Clip(object in_feature, object clip_feature, string out_feature)
        {
            try
            {
                Geoprocessor gp = new Geoprocessor();
                ESRI.ArcGIS.AnalysisTools.Clip clipTool = new ESRI.ArcGIS.AnalysisTools.Clip();
                //IVariantArray param = new VarArrayClass();
                gp.SetEnvironmentValue("workspace", this._environment);
                //MessageBox.Show(((IFeatureLayer)in_feature).FeatureClass.AliasName);
                clipTool.in_features = in_feature;
                clipTool.clip_features = clip_feature;
                clipTool.out_feature_class = string.Format("{0}",out_feature);//"C:\\tayninh\\temp\\tempmdb.mdb\\" + out_feature;

                runTool(gp, clipTool, null);
            }
            catch (Exception err) { MessageBox.Show("loi clip: " + err.ToString()); }
        }
示例#8
0
 private bool ClipLayers(ILayer selectedLayer)
 {
     try
     {
         IFeatureWorkspace fWorkspace               = AppSingleton.Instance().PersonalWorkspace as IFeatureWorkspace;
         ESRI.ArcGIS.Geoprocessor.Geoprocessor gp   = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
         ESRI.ArcGIS.AnalysisTools.Clip        clip = new ESRI.ArcGIS.AnalysisTools.Clip();
         clip.in_features       = selectedLayer;
         clip.clip_features     = AppSingleton.Instance().SinirLayer;
         clip.out_feature_class = AppSingleton.Instance().WorkspacePath + "\\Clip_" + selectedLayer.Name;
         gp.AddOutputsToMap     = AppSingleton.Instance().AralariEkle;
         gp.OverwriteOutput     = true;
         gp.Execute(clip, null);
         //return clip.out_feature_class.ToString();
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
示例#9
0
        public bool ClipVector(string inFeaClass, string clipFeaClass, string outFeaClass, out string sMsg)
        {
            sMsg = string.Empty;
            bool result;
            IGeoProcessorResult geoProcessorResult = null;

            try
            {
                ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();
                clip.in_features       = inFeaClass;
                clip.clip_features     = clipFeaClass;
                clip.out_feature_class = outFeaClass;
                //geoProcessorResult = this.m_gp.Execute(clip, null) as IGeoProcessorResult;
                //sMsg += GetGPMessages(this.m_gp);
                //Geoprocessor m_gp = new Geoprocessor() { OverwriteOutput = true };
                geoProcessorResult = m_gp.Execute(clip, null) as IGeoProcessorResult;
                sMsg += GetGPMessages(m_gp);
                if (geoProcessorResult.Status == esriJobStatus.esriJobSucceeded)
                {
                    sMsg  += "裁切成功!";
                    result = true;
                }
                else
                {
                    sMsg  += "裁切失败!";
                    result = false;
                }
            }
            catch (Exception ex)
            {
                sMsg   = sMsg + "裁切失败:" + ex.Message;
                result = false;
            }
            finally
            {
                //Marshal.ReleaseComObject(geoProcessorResult);
            }
            return(result);
        }
示例#10
0
 public string ClipLayers(ILayer sinirLayer, ILayer selectedLayer)
 {
     try
     {
         IFeatureWorkspace fWorkspace               = PersonalWorkspace as IFeatureWorkspace;
         ESRI.ArcGIS.Geoprocessor.Geoprocessor gp   = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
         ESRI.ArcGIS.AnalysisTools.Clip        clip = new ESRI.ArcGIS.AnalysisTools.Clip();
         clip.in_features   = selectedLayer;
         clip.clip_features = sinirLayer;
         var layerName = System.IO.Path.GetFileNameWithoutExtension(selectedLayer.Name);
         clip.out_feature_class = WorkspacePath + "\\Clip_" + layerName;
         gp.AddOutputsToMap     = AppSingleton.Instance().AralariEkle;
         gp.OverwriteOutput     = true;
         gp.Execute(clip, null);
         //return clip.out_feature_class.ToString();
         return(clip.out_feature_class.ToString());
     }
     catch (Exception ex)
     {
         return(string.Empty);
     }
 }
示例#11
0
        /// <summary>
        /// 裁剪矢量图层
        /// </summary>
        /// <param name="clipFilename">裁剪图层文件</param>
        /// <param name="inputFilename">被裁剪的图层文件</param>
        /// <param name="outputFilename">输出文件路径</param>
        public void clipLayers(string clipFilename, string inputFilename, string outputFilename)
        {
            ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            gp.OverwriteOutput = true;
            gp.AddOutputsToMap = false;

            ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();
            clip.in_features       = inputFilename;
            clip.clip_features     = clipFilename;
            clip.out_feature_class = outputFilename;

            try
            {
                IGeoProcessorResult results = (IGeoProcessorResult)gp.Execute(clip, null);
                if (results.Status != esriJobStatus.esriJobSucceeded)
                {
                    throw new Exception("裁剪图层(clipLayers)遇到错误");
                }
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Debug.WriteLine(this.GetType().Name + "出错,错误信息:" + e.Message);
            }
        }
    /// <summary>
    /// Handles the click event of the Button and starts asynchronous geoprocessing.
    /// </summary>
    private void btnRunGP_Click(object sender, EventArgs e)
    {
      try
      {
        #region tidy up any previous gp runs

        //Clear the ListView control
        listView1.Items.Clear();

        //Remove any result layers present in the map
        IMapLayers mapLayers = axMapControl1.Map as IMapLayers;
        foreach (IFeatureLayer resultLayer in _resultsList)
        {
          mapLayers.DeleteLayer(resultLayer);
        }

        axTOCControl1.Update();
        
        //Empty the results layer list
        _resultsList.Clear();

        //make sure that my GP tool queue is empty
        _myGPToolsToExecute.Clear();

        #endregion

        //Buffer the selected cities by the specified distance
        ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer();
        bufferTool.in_features = _layersDict["Cities"];
        bufferTool.buffer_distance_or_field = txtBufferDistance.Text + " Miles";
        bufferTool.out_feature_class = "city_buffer.shp";
       
        //Clip the zip codes layer with the result of the buffer tool
        ESRI.ArcGIS.AnalysisTools.Clip clipTool = new ESRI.ArcGIS.AnalysisTools.Clip();
        clipTool.in_features = _layersDict["ZipCodes"];
        clipTool.clip_features = bufferTool.out_feature_class;
        clipTool.out_feature_class = "city_buffer_clip.shp";

        //To run multiple GP tools asynchronously, all tool inputs must exist before ExecuteAsync is called.
        //The output from the first buffer operation is used as an input to the second clip
        //operation. To deal with this restriction a Queue is created and all tools added to it. The first tool
        //is executed from this method, whereas the second is executed from the ToolExecuted event. This approach
        //is scalable - any additional geoprocessing tools added to this queue will also be executed in turn.
        _myGPToolsToExecute.Enqueue(bufferTool);
        _myGPToolsToExecute.Enqueue(clipTool);
        _gp.ExecuteAsync(_myGPToolsToExecute.Dequeue());
      }
      catch (Exception ex)
      {
        listView1.Items.Add(new ListViewItem(new string[2] { "N/A", ex.Message }, "error")); 
      }
    }
        protected void DoClip(IActiveView activeView, IFeatureLayer ifl_active, IGeometry geometry)
        {
            try
            {
                ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
                ESRI.ArcGIS.Carto.ILayerFile layerFile = new ESRI.ArcGIS.Carto.LayerFileClass();

                ISpatialFilter isf = new SpatialFilterClass();
                isf.Geometry = geometry;
                isf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                gd.Title = "Save clipped feature class";

                gd.ObjectFilter = new GxFilterFeatureClassesClass(); //new GxFilterFeatureClassesClass();
                if (gd.DoModalSave(ArcMap.Application.hWnd) == false)
                {
                    return;
                }

                while (System.IO.File.Exists(gd.FinalLocation.FullName + "\\" + gd.Name) || gd.ReplacingObject)
                {
                    if (System.Windows.Forms.MessageBox.Show("You've selected a feature class that already exists. Select a different feature class name.", "Overwrite Feature Class", System.Windows.Forms.MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Cancel)
                    {
                        return;
                    }

                    if (gd.DoModalSave(ArcMap.Application.hWnd) == false)
                    {
                        return;
                    }
                }

                // Create a new in-memory workspace. This returns a name object.
                InMemoryWorkspaceFactory wsf = new InMemoryWorkspaceFactoryClass();
                IWorkspaceName workspaceName = wsf.Create(null, "MyWorkspace", null, 0);

                IName name = (IName)workspaceName;

                IFeatureWorkspace workspace;
                IWorkspaceEdit iwe;
                IFields flds;

                setFeatureSpatialReference(ifl_active, name, out workspace, out iwe, out flds);

                IFeatureClass ifc_new = workspace.CreateFeatureClass("AAA", flds, null, null, esriFeatureType.esriFTSimple, ifl_active.FeatureClass.ShapeFieldName, "");
                IFeatureLayer fl = new FeatureLayerClass();
                IGeoFeatureLayer gfl = (IGeoFeatureLayer)fl;

                IRgbColor rgbColor = new RgbColorClass();
                rgbColor.Red = 255;
                rgbColor.Green = 0;
                rgbColor.Blue = 0;

                IColor color = rgbColor; // Implicit Cast

                fl.FeatureClass = ifc_new;
                fl.Name = "IntersectingShape";

                ISimpleFillSymbol sfs = new SimpleFillSymbolClass();

                ISimpleLineSymbol sls = new SimpleLineSymbolClass();
                sls.Color = color;
                sls.Width = 4.0;
                sls.Style = esriSimpleLineStyle.esriSLSSolid;

                color.NullColor = true;

                sfs.Color = color;
                sfs.Outline = sls;

                ISimpleRenderer isr = new SimpleRendererClass();
                isr.Symbol = (ISymbol)sfs;

                gfl.Renderer = (IFeatureRenderer)isr;

                IObjectCopy cpy = new ObjectCopyClass();

                iwe.StartEditing(true);
                iwe.StartEditOperation();

                IFeatureBuffer fb = ifc_new.CreateFeatureBuffer();
                IFeatureCursor csri = ifc_new.Insert(false);

                fb.Shape = geometry;

                csri.InsertFeature(fb);
                csri.Flush();

                iwe.StopEditOperation();
                iwe.StopEditing(true);

                map.AddLayer(fl);

                ESRI.ArcGIS.AnalysisTools.Clip tool = new ESRI.ArcGIS.AnalysisTools.Clip();
                tool.clip_features = fl;
                tool.in_features = ifl_active;

                tool.out_feature_class = gd.FinalLocation.FullName + "\\" + gd.Name; /*ws.PathName*/ //"In_memory" + "\\NWI_Clip_Result";

                gp.AddOutputsToMap = true;
                gp.OverwriteOutput = true;

                gp.ToolExecuted += new EventHandler<ToolExecutedEventArgs>(gp_ToolExecuted);
                gp.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(gp_ProgressChanged);

                gp.ExecuteAsync(tool);
            }
            catch (Exception err)
            {
            }
            finally
            {
                SelectArrowToolOnToolbar();
            }
        }
示例#14
0
        private void gpProcess()
        {
            try
            {
                #region tidy up any previous gp runs

                //Clear the ListView control
                listView1.Items.Clear();

                //Remove any result layers present in the map
                IMapLayers mapLayers = axMapControl1.Map as IMapLayers;
                foreach (IFeatureLayer resultLayer in _resultsList)
                {
                    mapLayers.DeleteLayer(resultLayer);
                }

                axTOCControl1.Update();

                //Empty the results layer list
                _resultsList.Clear();

                //make sure that my GP tool queue is empty
                _myGPToolsToExecute.Clear();

                #endregion

                //Buffer the selected cities by the specified distance
                ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer();
                bufferTool.in_features = _layersDict["Cities"];
                bufferTool.buffer_distance_or_field = txtBufferDistance.Text + " Miles";
                bufferTool.out_feature_class = "city_buffer.shp";

                //Clip the zip codes layer with the result of the buffer tool
                ESRI.ArcGIS.AnalysisTools.Clip clipTool = new ESRI.ArcGIS.AnalysisTools.Clip();
                clipTool.in_features = _layersDict["ZipCodes"];
                clipTool.clip_features = bufferTool.out_feature_class;
                clipTool.out_feature_class = "city_buffer_clip.shp";

                _myGPToolsToExecute.Enqueue(bufferTool);
                _myGPToolsToExecute.Enqueue(clipTool);

                _gp.ExecuteAsync(_myGPToolsToExecute.Dequeue());
            }
            catch (Exception ex)
            {
                listView1.Items.Add(new ListViewItem(new string[2] { "N/A", ex.Message }, "error"));
            }
        }