Geometry类型的转换

        最近需要把ESRI.ArcGIS.IGeometry转为ADF的Geometry,在ESRI论坛上面看了一些帖子,获得以下这种方法:

ESRI.ArcGIS.ADF.ArcGISServer.PolygonN polyn = (ESRI.ArcGIS.ADF.ArcGISServer.PolygonN)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ComObjectToValueObject(polygon, sc, typeof(ESRI.ArcGIS.ADF.ArcGISServer.PolygonN));

       可惜不知为何,运行到ComObjectToValueObject(...)时总是提示:“值不在预期范围内”,这问题纠缠了很久,最后还是无法解决。

       后来,又在刘光的书上看到了另外的一种转换方法:

ESRI.ArcGIS.Geometry.IPointCollection pointcollection = (ESRI.ArcGIS.Geometry.IPointCollection)pFeature.Shape; ESRI.ArcGIS.ADF.Web.Geometry.Point[] adf_points = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection(pointcollection); ESRI.ArcGIS.ADF.Web.Geometry.PointCollection adf_pointcollection = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection(); for (int i = 0; i < adf_points.Length - 1; i++) { adf_pointcollection.Add(adf_points[i]); } ESRI.ArcGIS.ADF.Web.Geometry.Ring adf_ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring(); adf_ring.Points = adf_pointcollection; ESRI.ArcGIS.ADF.Web.Geometry.RingCollection adf_ringcollection = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection(); adf_ringcollection.Add(adf_ring); ESRI.ArcGIS.ADF.Web.Geometry.Polygon adf_polygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon(); adf_polygon.Rings = adf_ringcollection;

        这种方法是将IGeometry先转为IPointCollection,由ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection()方法,将IPointCollection转为ADF的点组合,再由这些点组成ADF的PointCollection,将PointCollection转为ADF的Ring,再由这些Rings转为RingCollection,由RingCollection再转为ADF的Polygon。

        这种方法后来成功了,能够成功的转为ADF的Polygon,但是在Web上高亮却失败了。同样不明白原因,跟踪了好久还是没有结果。下面是高亮的代码:

 

ElementGraphicsLayer projectLayer = null; IFeatureCursor pFeatureCursor; IFeature pFeature; IGeometry geometry; ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom; ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource; IGISFunctionality gisfunc = Map1.GetFunctionality(funName); if (gisfunc == null) return ""; gResource = gisfunc.Resource as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource; if (gResource == null) return ""; pFeatureCursor = GISCode.AttributesQuery("分析区", BSMcondition); if (pFeatureCursor != null) { foreach (System.Data.DataTable dt in gResource.Graphics.Tables) { if (dt is ElementGraphicsLayer) { projectLayer = (ElementGraphicsLayer)dt; } } // 第一次调用时ElementGraphicsLayer不存在,需要创建一个 if (projectLayer == null) { projectLayer = new ElementGraphicsLayer(); gResource.Graphics.Tables.Add(projectLayer); } // 清除已有数据 projectLayer.Clear(); try { pFeature = pFeatureCursor.NextFeature(); while (pFeature != null) { geometry = pFeature.Shape; geom = ADFCode.IGeometryToADF(geometry); // 创建一个GraphicElement ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = null; ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Yellow); // 将GraphicElement添加到ElementGraphicsLayer中 projectLayer.Add(ge); pFeature = pFeatureCursor.NextFeature(); } } catch (InvalidCastException) { //throw new Exception("No geometry available in datatable"); throw new Exception("数据表中没有可用的几何体"); }

       如果有哪位前辈或同学知道我错误所在的,麻烦帮我解答一下。不甚感激了!

 

 

你可能感兴趣的:(ArcGIS,Server学习笔记)