首页 > 代码库 > AE代码 积累

AE代码 积累

  1         /// <summary>  2         /// 计算两点角度  3         /// </summary>  4         /// <param name="p1">original point</param>  5         /// <param name="p2">end point</param>  6         /// <returns></returns>  7         private float Angle(IPoint p1, IPoint p2)  8         {  9             double tan = Math.Atan(Math.Abs((p2.Y - p1.Y) / (p2.X - p1.X))) * 180 / Math.PI; 10             if (p2.X > p1.X && p2.Y > p1.Y) //第一象限 11                 return -(float.Parse(tan.ToString())); 12             else if (p2.X > p1.X && p2.Y > p1.Y) //第二象限 13                 return float.Parse(tan.ToString()); 14             else if (p2.X < p1.X && p2.Y > p1.Y) //第三象限 15                 return float.Parse(tan.ToString()) - 180; 16             else //第四象限 17                 return 180-float.Parse(tan.ToString());                 18         } 19          20         /// <summary> 21         /// 获取面图层的质心<IPoint> 22         /// </summary> 23         /// <param name="pFeatureLayer">面图层</param> 24         /// <returns>List<></returns> 25         private List<pointofpolygon> Get_PointFromPolygon(IFeatureLayer pFeatureLayer) 26         { 27             ESRI.ArcGIS.Geodatabase.IQueryFilter pQueryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass(); 28             pQueryFilter.WhereClause = "FID IS NOT null"; 29             IArea pArea; 30             List<pointofpolygon> pts=new List<pointofpolygon>(); 31             //MessageBox.Show(pFeatureLayer.FeatureClass.FeatureCount(pQueryFilter).ToString()); 32             for (int i = 0; i < pFeatureLayer.FeatureClass.FeatureCount(pQueryFilter); i++) 33             { 34                 pArea = pFeatureLayer.FeatureClass.GetFeature(i).Shape as IArea; 35                 pts.Insert(i, new pointofpolygon(pFeatureLayer.FeatureClass.GetFeature(i).OID, pArea.Centroid)); 36             } 37             return pts; 38         } 39  40         /// <summary> 41         /// 计算p1点相对p2点的角度,整栋方向为0度 42         /// </summary> 43         /// <param name="p1">polyline上的点</param> 44         /// <param name="p2">original point,面的重心</param> 45         /// <returns></returns> 46         private float Get_Angle(IPoint p1, IPoint p2) 47         { 48             if (p1.X == p2.X && p1.Y == p2.Y) 49                 return -1; 50             IPoint p3=new PointClass(); 51             p3.Y=p2.Y; 52             p3.X=p2.X+10; 53             float c = (float)Math.Sqrt(Math.Pow((p1.X - p2.X), 2) + Math.Pow(p1.Y - p2.Y, 2)) *  54                 (float)Math.Sqrt(Math.Pow(p3.X - p2.X, 2) + Math.Pow(p3.Y - p2.Y, 2)); 55             if (c == 0) return -1; 56             float angle = (float)Math.Acos(((p1.X - p2.X) * (p3.X - p2.X) + (p1.Y - p2.Y) * (p3.Y - p2.Y)) / c); 57             return angle; 58         } 59  60         /// <summary> 61         /// 清空属性表中需要计算的字段的值 62         /// </summary> 63         /// <param name="pFLyr">输入图层</param> 64         private void ClearAttributeTable(IFeatureLayer pFLyr) 65         { 66             IDataset dataset = (IDataset)pFLyr.FeatureClass; 67             IWorkspace workspace = dataset.Workspace; 68             IWorkspaceEdit workspaceedit = (IWorkspaceEdit)workspace; 69             workspaceedit.StartEditing(true); 70             workspaceedit.StartEditOperation(); 71             for (int i = 0; i < pFLyr.FeatureClass.FeatureCount(new QueryFilter()); i++) 72             { 73                 IFeature pFeature = FlyrtoF(pFLyr, i); 74                 pFeature.set_Value(pFeature.Fields.FindField("inner"),null); 75                 pFeature.set_Value(pFeature.Fields.FindField("near"), null); 76                 pFeature.set_Value(pFeature.Fields.FindField("far"), null); 77                 pFeature.set_Value(pFeature.Fields.FindField("direction"), null); 78                 pFeature.set_Value(pFeature.Fields.FindField("sum"), null); 79                 pFeature.Store(); 80             } 81             workspaceedit.StopEditing(true); 82             workspaceedit.StopEditOperation(); 83         } 84  85         /// <summary> 86         /// 创建内存中的FeatureClass 87         /// </summary> 88         /// <param name="point"></param> 89         public void CreateFeatureClassInMemory(IPoint point) 90         { 91             IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); 92             IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "pWorkspace", null, 0); 93             IName pName = (IName)pWorkspaceName; 94             IWorkspace pWorkspace = (IWorkspace)pName.Open(); 95             IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; 96             IFields pFields = new FieldsClass(); 97             IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; 98             IField pField = new FieldClass(); 99             IFieldEdit pFieldEdit = pField as IFieldEdit;100             pFieldEdit.Name_2 = "SHAPE";101             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;102             IGeometryDef pGeometryDef = new GeometryDefClass();103             IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;104             pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;105             //为FeatureClass赋参考系,不写会出错***************************************106             ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();107             ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);108             pGeometryDefEdit.SpatialReference_2 = pSpatialReference;109             //************************************************************************110             pFieldEdit.GeometryDef_2 = pGeometryDef;111             pFieldsEdit.AddField(pField);112             pField = new FieldClass();//不要省略写!容易出问题113             //pFieldEdit = pField as IFieldEdit;114             //pFieldEdit.AliasName_2 = "高程";115             //pFieldEdit.Name_2 = "elevation";116             //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;117             pFieldsEdit.AddField(pField);118             IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("1", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");119 120             //插入到新建的FeatureClass中121             IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;122             pWorkspaceEdit.StartEditing(true);123             pWorkspaceEdit.StartEditOperation();124             IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();125             IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);126             pFeatureBuffer.Shape = point;127             pFeatureCursor.InsertFeature(pFeatureBuffer);128             129             pFeatureCursor.Flush();130             pWorkspaceEdit.StopEditOperation();131             pWorkspaceEdit.StopEditing(true);132 133             pFCInMemory = pFeatureClass;134         }135 136         /// <summary>137         /// 计算pt是否在point的可视范围中138         /// </summary>139         /// <param name="pRasterLayer">dem</param>140         /// <param name="point">polyline转的点</param>141         /// <param name="pt">polygon转的点</param>142         /// <returns>返回值为1或者0,1为可视,0为不可视</returns>143         private bool CalViewshed(IRasterLayer pRasterLayer, IPoint point, IPoint pt)144         {145             //获取两个点的高程146             IRasterSurface pRasterSurface = new RasterSurfaceClass();147             pRasterSurface.PutRaster(pRasterLayer.Raster, 0);148             ISurface pSurface = pRasterSurface as ISurface;149             double elv_point = pSurface.GetElevation(point);150             point.Z = elv_point;151             double elv_pt = pSurface.GetElevation(pt);152             pt.Z = elv_pt;153             //IPolyline pPolyline = AxMapC.TrackLine() as IPolyline;154             IPoint pPoint = null;155             Boolean pBool = true;156             IPolyline pVPolyline = null;157             IPolyline pInPolyline = null;158             object pRef = 0.13;159             pSurface.GetLineOfSight(point, pt, out pPoint, out pVPolyline, out pInPolyline, out pBool, false, false, ref pRef);160             //如果pt在pVPolyline上,则可视;否则不可视161             return pBool;162         }163 164         /// <summary>165         /// 计算点所在栅格的值166         /// </summary>167         /// <param name="point"></param>168         /// <param name="rasterlayer">栅格</param>169         /// <returns>返回栅格值</returns>170         private int GetPointInRasterValue(IPoint point, IRasterLayer rasterlayer)171         {172             //第一种方法173             object obj=null;174             IRasterProps rasterprops = (IRasterProps)rasterlayer.Raster;175             long dHeight = rasterprops.Height;176             long dWidth = rasterprops.Width;177             double dx = rasterprops.MeanCellSize().X;178             double dy = rasterprops.MeanCellSize().Y;179             rstPixelType pixelType = rasterprops.PixelType;180             IPnt pnt = new PntClass();181             pnt.SetCoords(dx,dy);182             IPixelBlock pixelBlock = rasterlayer.Raster.CreatePixelBlock(pnt);183             IPnt pnt1 = new PntClass();184             double xx = rasterprops.Extent.XMin;185             double yy = rasterprops.Extent.YMax;186             int row = (int)Math.Abs((yy - point.Y) / dx)+1;187             int col = (int)Math.Abs((xx - point.X) / dy)+1;188             pnt1.SetCoords(row,col);189             rasterlayer.Raster.Read(pnt1, pixelBlock); ;190             if (pixelBlock != null)191             {192                 obj = pixelBlock.GetVal(0, 0, 0);193             }194             if (obj != null)195                 return (int)obj;196             else return -1;197             //第二种方法198             IRasterSurface pRasterSurface=new RasterSurfaceClass();199             pRasterSurface.PutRaster(rasterlayer.Raster,0);200             ISurface pSurface=pRasterSurface as ISurface;201             double elv = pSurface.GetElevation(point);202         }203 204         /// <summary>205         /// IRasterLayer类型转换成IGeoDataset206         /// </summary>207         /// <param name="pRasterLyr">输入DEM图层</param>208         /// <returns>pSurfaceOP.Visibility的第一个参数</returns>209         private IGeoDataset GetGeoDatasetFromLayer(IRasterLayer pRasterLyr)210         {211             IRaster pRaster = pRasterLyr.Raster;212             IRasterBandCollection pRasterBandCollection = pRaster as IRasterBandCollection;213             IRasterBand pRasterBand = pRasterBandCollection.Item(0);214             IRasterDataset pRasterDataset = pRasterBand as IRasterDataset;215             return pRasterDataset as IGeoDataset;216         }217 218         /// <summary>219         /// IgeoDataset转换成IRasterLayer220         /// </summary>221         /// <param name="geodataset"></param>222         /// <returns></returns>223         private IRasterLayer GetRasterLayerFromGeoDataset(IGeoDataset geodataset)224         {225             IRasterLayer pRasterLayer = new RasterLayerClass();226             pRasterLayer.CreateFromDataset(geodataset as IRasterDataset);227             return pRasterLayer;228         }229 230         /// <summary>231         /// FeatureLayer To FeatureClass232         /// </summary>233         /// <param name="pFeatureLyr"></param>234         /// <returns></returns>235         private IFeatureClass FLyrToFC(IFeatureLayer pFeatureLyr)236         {237             return pFeatureLyr.FeatureClass;238         }239         /// <summary>240         /// FeatureLayer To Feature241         /// </summary>242         /// <param name="pFeatureLyr"></param>243         /// <param name="index"></param>244         /// <returns></returns>245         private IFeature FlyrtoF(IFeatureLayer pFeatureLyr,int index)246         {247             IFeatureClass pFeatureClass=FLyrToFC(pFeatureLyr);248             return pFeatureClass.GetFeature(index);249         }250 251         /// <summary>252         /// 编辑属性表253         /// </summary>254         /// <param name="pt_polyline">多条polyline上的节点</param>255         /// <param name="pt_polygon">polygon的质心</param>256         /// <param name="pFeatureLyr">polygo Layer</param>257         private void Calulatation(List<List<IPoint>> pt_polyline,List<pointofpolygon> pt_polygon, IFeatureLayer pFeatureLyr)258         {259             //使图层处于编辑状态260             IDataset dataset = (IDataset)pFeatureLyr.FeatureClass;261             IWorkspace workspace = dataset.Workspace;262             IWorkspaceEdit workspaceedit = (IWorkspaceEdit)workspace;263             workspaceedit.StartEditing(true);264             workspaceedit.StartEditOperation();265             IFeature feature;266             float dir = 0, buf = 0;267             int direction=0;268 269             //修改某一记录某一字段的270             feature.set_Value(feature.Fields.FindField("direction"), (float)feature.get_Value(feature.Fields.FindField("far")) + 0); 271             feature.Store();272             //关闭要素的编辑状态273             workspaceedit.StopEditing(true);274             workspaceedit.StopEditOperation();275         }

 

AE代码 积累