问题描述
算法的思路是按照下面的帖子中来的:http://www.cnblogs.com/gisoracle/archive/2013/04/09/3009285.html线数据是从"主要铁路.shp"文件中读入的,从另外一个pointshapefile.shp文件读入一些目标点数据,然后想找出铁路线上离目标点最近的点。如图,读入和显示都没问题。程序如下usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.SystemUI;usingESRI.ArcGIS.DataSourcesFile;usingESRI.ArcGIS.Geodatabase;namespaceArcObject{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton_MovePoint_Click(objectsender,EventArgse){//查询缓冲区图层IFeatureLayerptLayer=queryFeatureLayer("pointshapefile");//得到指定图层上距point最近的feature上的最近点IFeatureClassptFeatClass=ptLayer.FeatureClass;IFeatureCursorfeatureCursor=ptFeatClass.Search(null,false);IFeaturefeature=featureCursor.NextFeature();//IPointpoint=newPointClass();IPointpoint=newPointClass();while(feature!=null){point=feature.ShapeasIPoint;//查找离点最近的线IFeaturenearFea=GetNearestFeature(point,1.2);IPointneaPoint=GetNearestPoint(point,nearFea);neaPoint.Z=point.Z;//移动点MoveToNeaPoint(feature,point,neaPoint);if(featureCursor==null||feature==null){return;}feature=featureCursor.NextFeature();}MessageBox.Show("ok--");}////得到指定图层上距point最近的feature上的最近点publicIPointGetNearestPoint(IPointpoint,IFeaturenearFea){IProximityOperatorProximity=(IProximityOperator)point;IFeatureLayerFeaLyr=queryFeatureLayer("主要铁路");IFeatureClassFeaCls=FeaLyr.FeatureClass;IQueryFilterqueryFilter=null;ITopologicalOperatortopoOper=(ITopologicalOperator)point;IGeometrygeo=topoOper.Buffer(1.2);ISpatialFiltersf=newSpatialFilter();sf.Geometry=geo;sf.GeometryField=FeaCls.ShapeFieldName;sf.SpatialRel=esriSpatialRelEnum.esriSpatialRelCrosses;IFeatureCursorFeaCur=FeaCls.Search(queryFilter,false);IFeatureFea=nearFea=FeaCur.NextFeature();doubleminDistince,Distance;if(Fea==null)returnnull;minDistince=Distance=Proximity.ReturnDistance((IGeometry)Fea.Shape);//最近的距离值//保存距离最近的featureFea=FeaCur.NextFeature();while(Fea!=null){Distance=Proximity.ReturnDistance((IGeometry)Fea.Shape);if(Distance<minDistince){minDistince=Distance;nearFea=Fea;}Fea=FeaCur.NextFeature();}//endwhileProximity=(IProximityOperator)nearFea.Shape;returnProximity.ReturnNearestPoint(point,esriSegmentExtension.esriNoExtension);}publicIFeatureGetNearestFeature(IPointp,doublerongcha){IFeaturenearFea;IProximityOperatorProximity=(IProximityOperator)p;IFeatureLayerFeaLyr=queryFeatureLayer("主要铁路");IFeatureClassFeaCls=FeaLyr.FeatureClass;IQueryFilterqueryFilter=null;IFeatureCursorFeaCur=FeaCls.Search(queryFilter,false);IFeatureFea=nearFea=FeaCur.NextFeature();doubleminDistince,Distance;if(Fea==null)returnnull;Distance=Proximity.ReturnDistance((IGeometry)pline);//最近的距离值minDistince=Distance;//保存距离最近的featureFea=FeaCur.NextFeature();while(Fea!=null){Distance=Proximity.ReturnDistance((IGeometry)Fea.Shape);if(Distance<minDistince){minDistince=Distance;nearFea=Fea;}Fea=FeaCur.NextFeature();}//endwhilereturnnearFea;}privatevoidMoveToNeaPoint(IFeaturefeature,IPointpoint,IPointneaPoint){//IGeometrytoGeometry=null;//IPointgeomType=newPointClass();IPointgeomType=newPointClass();feature.Shape=neaPointasIGeometry;feature.Store();}//查询给定名称的图层privateIFeatureLayerqueryFeatureLayer(stringname){//axMapControl1.AddShapeFile();for(inti=0;i<axMapControl1.LayerCount;i++){ILayerlayer=axMapControl1.get_Layer(i);stringnname=layer.Name;if(layer.Name.Equals(name)){return(IFeatureLayer)layer;}}returnnull;}privatevoidInput_Click(objectsender,EventArgse){AddShapefileUsingOpenFileDialog(this.axMapControl1.ActiveView);}publicvoidAddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveViewactiveView){//parametercheckif(activeView==null){return;}//UsetheOpenFileDialogClasstochoosewhichshapefiletoload.System.Windows.Forms.OpenFileDialogopenFileDialog=newSystem.Windows.Forms.OpenFileDialog();//openFileDialog.InitialDirectory="c:\";openFileDialog.InitialDirectory="D:\data";//openFileDialog.InitialDirectory="D:\data";openFileDialog.Filter="Shapefiles(*.shp)|*.shp";openFileDialog.FilterIndex=2;openFileDialog.RestoreDirectory=true;openFileDialog.Multiselect=false;if(openFileDialog.ShowDialog()==System.Windows.Forms.DialogResult.OK){//Theuserchoseaparticularshapefile.//Thereturnedstringwillbethefullpath,//filenameandfile-extensionforthechosenshapefile.//Example:"C:testcities.shp"stringshapefileLocation=openFileDialog.FileName;if(shapefileLocation!=""){//Ensuretheuserchoosesashapefile//CreateanewShapefileWorkspaceFactoryCoClasstocreateanewworkspaceIWorkspaceFactoryworkspaceFactory=newShapefileWorkspaceFactoryClass();//System.IO.Path.GetDirectoryName(shapefileLocation)//returnsthedirectorypartofthestring.Example:"C:test"stringdir=System.IO.Path.GetDirectoryName(shapefileLocation);workspaceFactory.OpenFromFile(dir,0);IFeatureWorkspacefeatureWorkspace=(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(dir,0);ESRI.ArcGIS.Geodatabase.IFeatureClassfeatureClass=featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));ESRI.ArcGIS.Carto.IFeatureLayerfeatureLayer=newESRI.ArcGIS.Carto.FeatureLayerClass();featureLayer.FeatureClass=featureClass;featureLayer.Name=featureClass.AliasName;featureLayer.Visible=true;activeView.FocusMap.AddLayer(featureLayer);//ZoomthedisplaytothefullextentofalllayersinthemapactiveView.Extent=activeView.FullExtent;activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography,null,null);}else{//Theuserdidnotchooseashapefile.//DowhateverremedialactionsasnecessarySystem.Windows.Forms.MessageBox.Show("Noshapefilechosen");//System.Windows.Forms.MessageBoxButtons.OK,//System.Windows.Forms.MessageBoxIcon.Exclamation);}}}}}
函数publicIPointGetNearestPoint(IPointpoint,IFeaturenearFea)中下面这一句抛出异常,目的是计算距离Distance=Proximity.ReturnDistance((IGeometry)Fea.Shape);我试着把Fea.Shape转换为IGeometryCollection也抛出异常IPolylinepltmp=newPolylineClass();IGeometryCollectiontmpColl=(IGeometryCollection)pltmp;tmpColl.AddGeometry((IGeometry)Fea.Shape);IPolylinepline=(IPolyline)tmpColl;刚接触这方面编程,查了些文档也没弄明白,请各位指教!
解决方案
本帖最后由 jiangyangdadao 于 2015-10-07 19:49:39 编辑
解决方案二:
给文件名就可以了