Beispiel #1
0
 /// <summary>
 /// 裁剪
 /// </summary>
 /// <param name="inVectorFile"></param>
 /// <param name="borderFile"></param>
 /// <param name="outFile"></param>
 public static void Clip(string inVectorFile, string borderFile, string outFile)
 {
     ESRI.ArcGIS.AnalysisTools.Clip pClip = new ESRI.ArcGIS.AnalysisTools.Clip
     {
         in_features       = inVectorFile,
         clip_features     = borderFile,
         out_feature_class = outFile
     };
     GeoprocessorHelper.GpExecute(pClip);
 }
Beispiel #2
0
        public static bool Clip(string in_features, string clip_feature, string out_feature, string tolerance = "0.0001 METERS")
        {
            var tool = new ESRI.ArcGIS.AnalysisTools.Clip();

            tool.in_features       = in_features;
            tool.clip_features     = clip_feature;
            tool.out_feature_class = out_feature;
            tool.cluster_tolerance = tolerance;
            return(Excute(tool));
        }
        private void ClipFeatures(string clipFromLayer, string clipFeatureLayer, string outFeatureClassName)
        {
            int numTrys = 0;

            ESRI.ArcGIS.AnalysisTools.Clip c = new ESRI.ArcGIS.AnalysisTools.Clip();
            c.in_features       = clipFromLayer;
            c.clip_features     = clipFeatureLayer;
            c.cluster_tolerance = "0.001 meters";
            c.out_feature_class = outFeatureClassName;
            while (numTrys <= 10)
            {
                if (numTrys == 10)
                {
                    //System.Windows.Forms.MessageBox.Show("Tried clip 10 times and failed");
                }
                else
                {
                    if (RunProcess(c, null))
                    {
                        mLog.Debug("Passed clip features after " + (numTrys + 1).ToString() + " trys");
                        numTrys = 999;
                    }
                    else
                    {
                        numTrys++;
                        mLog.Debug("****************************Failed dissolve " + numTrys + " times");
                        mLog.Debug("Attempting repair geometry of clipFromLayer");
                        RepairGeometry r = new RepairGeometry();
                        r.in_features = clipFromLayer;
                        RunProcess(r, null);
                        mLog.Debug("Attempting repair geometry of clipFeatureLayer");
                        RepairGeometry r2 = new RepairGeometry();
                        r2.in_features = clipFeatureLayer;
                        RunProcess(r2, null);
                        mLog.Debug("Reseting geoprocessor");
                        myProcessor = null;
                        myProcessor = new Geoprocessor();
                    }
                }
            }
        }
Beispiel #4
0
        private void Clip(string cityName) 
        {
            IWorkspaceFactory pWorkspaceFactory;
            //使用Geoprocessor进行Clip
            Geoprocessor gp = new Geoprocessor();
            ESRI.ArcGIS.AnalysisTools.Clip myclip = new ESRI.ArcGIS.AnalysisTools.Clip();
            IFeatureLayer pFlyr1 = new FeatureLayer();
            IFeatureLayer pFlyr2 = new FeatureLayer();
            IFeatureClass pFclass1;
            IFeatureClass pFclass2;
            //OverwriteOutput为真时,输出图层会覆盖当前文件夹下的同名图层
            gp.OverwriteOutput = true;
            string stroutputpath = "schdata\\分析结果.shp";
            //FileInfo fFile = new FileInfo(stroutputpath);

            pFlyr1 = GetLayerByName(cityName);
            pFlyr2 = GetLayerByName("中学_buffer");

            pFclass1 = pFlyr1.FeatureClass;
            pFclass2 = pFlyr2.FeatureClass;

            myclip.clip_features = pFclass2;
            myclip.in_features = pFclass1;

            myclip.out_feature_class = stroutputpath;

            gp.Execute(myclip, null);

            //if (MessageBox.Show("添加到图层?") == System.Windows.Forms.DialogResult.Yes)
            //{
            //    axMapControl1.AddShapeFile("d:\\Temp","分析结果.shp");
            //    axMapControl1.ActiveView.Refresh();
            //}

            axMap.AddShapeFile("schdata", "分析结果.shp");
            axMap.ActiveView.Refresh();
        }
Beispiel #5
0
        public bool cilpOperator(IMap iMap, string inLayerName, string clipLayerName, string outpath)
        {
            IFeatureLayer inLayer   = (IFeatureLayer)GetLayerByName(inLayerName);
            IFeatureLayer clipLayer = (IFeatureLayer)GetLayerByName(clipLayerName);

            //调用GeoProcessing工具
            Geoprocessor geoprocessor = new Geoprocessor();

            geoprocessor.OverwriteOutput = true;

            IFeatureClass inLayerFeaCls   = inLayer.FeatureClass;
            IFeatureClass clipLayerFeaCls = clipLayer.FeatureClass;

            //调用clip工具
            ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip(
                inLayerFeaCls, clipLayerFeaCls, outpath + "\\" + inLayerName + "_Clip");

            geoprocessor.Execute(clip, null);

            //输出切割后图层
            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(outpath, 0);
            IFeatureClass     outFClass        = featureWorkspace.OpenFeatureClass(inLayerName + "_Clip");
            IFeatureLayer     outLayer         = new FeatureLayerClass();

            outLayer.FeatureClass = outFClass;
            outLayer.Name         = inLayerName + "_Clip";
            iMap.AddLayer(outLayer);

            IActiveView activeView = iMap as IActiveView;

            activeView.Refresh();

            if (inLayerName == "DLTB")
            {
                int            raIndex       = outFClass.FindField("SHAPE_Area");
                IFeatureCursor featureCursor = outLayer.FeatureClass.Search(null, false);
                IFeature       feature       = featureCursor.NextFeature();

                while (feature != null)
                {
                    if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        //使用IArea接口计算面积
                        IArea pArea = feature.Shape as IArea;
                        feature.set_Value(raIndex, pArea.Area);
                    }
                    feature.Store();
                    feature = featureCursor.NextFeature();
                }
            }
            else if (inLayerName == "XZDW")
            {
                //计算切割后图层中线状地物的长度
                IFieldsEdit pFieldsEdit = outFClass.Fields as IFieldsEdit;
                IField      pField      = new FieldClass();
                IFieldEdit  pFieldEdit  = pField as IFieldEdit;
                pFieldEdit.Name_2 = "Real_Leng";
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                outFClass.AddField(pField);

                int            rlIndex       = outFClass.FindField("Real_Leng");
                int            slIndex       = outFClass.FindField("SHAPE_Leng");
                int            xzdwmjIndex   = outFClass.FindField("XZDWMJ");
                IFeatureCursor featureCursor = outLayer.FeatureClass.Search(null, false);
                IFeature       feature       = featureCursor.NextFeature();

                while (feature != null)
                {
                    if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                    {
                        //使用ICure接口计算长度
                        ICurve pCurve = feature.Shape as ICurve;
                        feature.set_Value(rlIndex, pCurve.Length);
                        //计算切割后线状地物面积
                        Double real_mj = Double.Parse(feature.get_Value(rlIndex).ToString()) / Double.Parse(feature.get_Value(slIndex).ToString())
                                         * Double.Parse(feature.get_Value(xzdwmjIndex).ToString());
                        feature.set_Value(xzdwmjIndex, real_mj.ToString());
                    }
                    feature.Store();
                    feature = featureCursor.NextFeature();
                }
            }

            return(true);
        }//切割
Beispiel #6
0
        //ȷ��  ��ť
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {

                IFeatureClass featureclassInput = null;
                IFeatureClass featureclassClip = null;

                IFields outfields = null;

                // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace
                IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();

                // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\"
                // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities"
                //IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));

                IFeatureWorkspace featureWorkspaceInput = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(TxtInputFile.Text), 0); // Explicit Cast
                IFeatureWorkspace featureWorkspaceOut= (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(txtOutputPath.Text), 0);
                IFeatureWorkspace featureWorkspaceClip = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(txtClipsFile.Text), 0);

                //timer �ؼ�����
                timerPro.Enabled = true;
                //scroll the textbox to the bottom

                txtMessages.Text = "\r\n������ʼ,�������Ҫ������ʱ��,���Ժ�..\r\n";
                txtMessages.Update();

                //inputfeatureclass = featureWorkspace.OpenFeatureClass("land00_ws");
                //clipfeatureclass = featureWorkspace.OpenFeatureClass("drain_ws_buffer");

                featureclassInput = featureWorkspaceInput.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(TxtInputFile.Text));
                featureclassClip = featureWorkspaceClip.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtClipsFile.Text));
                outfields = featureclassInput.Fields;

                Geoprocessor gp = new Geoprocessor();
                gp.OverwriteOutput = true;
                //IFeatureClass outfeatureclass = featureWorkspace.CreateFeatureClass("Clip_result", outfields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
                IFeatureClass featureclassOut = null;

                //�ļ����ڵĴ���
                if (File.Exists(txtOutputPath.Text.Trim()) == true)
                {
                    featureclassOut = featureWorkspaceOut.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtOutputPath.Text));
                    DelFeatureFile(featureclassOut, txtOutputPath.Text.Trim());
                }

                featureclassOut = featureWorkspaceOut.CreateFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtOutputPath.Text), outfields,null,null,esriFeatureType.esriFTSimple, "Shape", "");

                ESRI.ArcGIS.AnalysisTools.Clip clipTool =
                    new ESRI.ArcGIS.AnalysisTools.Clip(featureclassInput, featureclassClip, featureclassOut);

                gp.Execute(clipTool,null);
                workspaceFactory = null;

                //����feature��
                //IDataset pDataset = outfeatureclass as IDataset;
                //pDataset.Copy("Clip_result1", featureWorkspace as IWorkspace);

                //���ͼ�㵽��ǰ��ͼ
                IFeatureLayer outlayer = new FeatureLayerClass();
                outlayer.FeatureClass = featureclassOut;
                outlayer.Name = featureclassOut.AliasName;
                pMap.AddLayer((ILayer)outlayer);
                //
                txtMessages.Text += "\r\n�������.\r\n";
                timerPro.Enabled = false;
            }
            catch(Exception ex)
            {
                //
                txtMessages.Text += "\r\n����ʧ��.\r\n";
                timerPro.Enabled = false;
                MessageBox.Show(ex.Message.ToString());
            }
        }
Beispiel #7
0
        //确定  按钮
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                IFeatureClass featureclassInput = null;
                IFeatureClass featureclassClip  = null;

                IFields outfields = null;

                // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace
                IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();

                // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\"
                // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities"
                //IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));

                IFeatureWorkspace featureWorkspaceInput = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(TxtInputFile.Text), 0); // Explicit Cast
                IFeatureWorkspace featureWorkspaceOut   = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(txtOutputPath.Text), 0);
                IFeatureWorkspace featureWorkspaceClip  = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(txtClipsFile.Text), 0);

                //timer 控件可用
                timerPro.Enabled = true;
                //scroll the textbox to the bottom

                txtMessages.Text = "\r\n分析开始,这可能需要几分钟时间,请稍候..\r\n";
                txtMessages.Update();

                //inputfeatureclass = featureWorkspace.OpenFeatureClass("land00_ws");
                //clipfeatureclass = featureWorkspace.OpenFeatureClass("drain_ws_buffer");

                featureclassInput = featureWorkspaceInput.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(TxtInputFile.Text));
                featureclassClip  = featureWorkspaceClip.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtClipsFile.Text));
                outfields         = featureclassInput.Fields;

                Geoprocessor gp = new Geoprocessor();
                gp.OverwriteOutput = true;
                //IFeatureClass outfeatureclass = featureWorkspace.CreateFeatureClass("Clip_result", outfields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
                IFeatureClass featureclassOut = null;

                //文件存在的处理
                if (File.Exists(txtOutputPath.Text.Trim()) == true)
                {
                    featureclassOut = featureWorkspaceOut.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtOutputPath.Text));
                    DelFeatureFile(featureclassOut, txtOutputPath.Text.Trim());
                }

                featureclassOut = featureWorkspaceOut.CreateFeatureClass(System.IO.Path.GetFileNameWithoutExtension(txtOutputPath.Text), outfields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

                ESRI.ArcGIS.AnalysisTools.Clip clipTool =
                    new ESRI.ArcGIS.AnalysisTools.Clip(featureclassInput, featureclassClip, featureclassOut);

                gp.Execute(clipTool, null);
                workspaceFactory = null;

                //复制feature层
                //IDataset pDataset = outfeatureclass as IDataset;
                //pDataset.Copy("Clip_result1", featureWorkspace as IWorkspace);

                //添加图层到当前地图
                IFeatureLayer outlayer = new FeatureLayerClass();
                outlayer.FeatureClass = featureclassOut;
                outlayer.Name         = featureclassOut.AliasName;
                pMap.AddLayer((ILayer)outlayer);
                //
                txtMessages.Text += "\r\n分析完成.\r\n";
                timerPro.Enabled  = false;
            }
            catch (Exception ex)
            {
                //
                txtMessages.Text += "\r\n分析失败.\r\n";
                timerPro.Enabled  = false;
                MessageBox.Show(ex.Message.ToString());
            }
        }