Example #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()); }
            }
        }
Example #2
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()); }
        }
    /// <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();
            }
        }
Example #5
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"));
            }
        }