mapguide开源版提供了mapguide的详细API,冲这些API里大家可以找到一个叫做MgFeatureQueryOptions的类,遍历的重点就在这个类上。话不多说看代码,

首先就是老生常谈咱们要实例化一下mapguide的配置

   InitializeWebTier();
        MgUserInformation userinfo = new MgUserInformation(SessionId);
        MgSiteConnection siteConnection = new MgSiteConnection();
        siteConnection.Open(userinfo);

完成这些以后咱们就要拿出mapguide的两大利器,feature和resource顾名思义规则和数据源,一个是元素规则,一个是数据源

        MgResourceService resourceService = siteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;
        MgFeatureService featureService = siteConnection.CreateService(MgServiceType.FeatureService) as MgFeatureService;
获取图层并遍历图层上的元素

    MgMap map = new MgMap();
        map.Open(resourceService, mapName);

        MgLayer layer = map.GetLayers().GetItem(layName) as MgLayer;

        MgResourceIdentifier fier = new MgResourceIdentifier(layer.FeatureSourceId);
        string featureclass = layer.FeatureClassName;
        string featuregeometry = layer.GetFeatureGeometryName();
        string[] schemaClass = featureclass.Split(\’:\’);

        MgClassDefinition classDefine = featureService.GetClassDefinition(fier, schemaClass[0], schemaClass[1]);
        MgGeometricPropertyDefinition geometric = classDefine.GetProperties().GetItem(featuregeometry) as MgGeometricPropertyDefinition;
        string spatialContext = geometric.GetSpatialContextAssociation();

        MgCoordinateSystemTransform csTransform = null;
        MgCoordinateSystemTransform csInverseTransform = null;
        MgCoordinateSystemFactory coorSysFactory = new MgCoordinateSystemFactory();

        MgSpatialContextReader contextReader = featureService.GetSpatialContexts(fier, false);
        while (contextReader.ReadNext() && csTransform == null)
        {
            if (spatialContext == contextReader.GetName())
            {
                MgCoordinateSystem source = coorSysFactory.Create(contextReader.GetCoordinateSystemWkt());
                MgCoordinateSystem target = coorSysFactory.Create(map.GetMapSRS());
                csTransform = coorSysFactory.GetTransform(source, target);
                csInverseTransform = coorSysFactory.GetTransform(target, source);
            }
        }

        contextReader.Close();

做完以上这一个GIS图层上的元素就被读到了我们的元素属性集合上变成了一只楚楚动人的小羔羊,剩下的事情就是添油加醋,干掉他。       

        MgFeatureQueryOptions queryOption = new MgFeatureQueryOptions();

        MgAgfReaderWriter geometryReaderWriter = new MgAgfReaderWriter();
        MgFeatureReader featureReader = featureService.SelectFeatures(fier, featureclass, queryOption);
        List<PointXY> pointXY = new List<PointXY>();
        while (featureReader.ReadNext())
        {
            MgByteReader byteReader = featureReader.GetGeometry(featuregeometry);
            MgGeometry geometry = geometryReaderWriter.Read(byteReader);
            MgPoint centerPoint = geometry.GetCentroid();
            centerPoint = (MgPoint)centerPoint.Transform(csTransform);
            string context = featureReader.GetString(“内容”);
            int id = featureReader.GetInt32(“FeatId”);
            PointXY xy = new PointXY();
            xy.Context = context;
            xy.FeatId = id;
            xy.X = centerPoint.Coordinate.X;
            xy.Y = centerPoint.Coordinate.Y;
            pointXY.Add(xy);
        }

 这里我定义了一个实体来存放遍历出来的元素,这样图层的元素就被我们顺利的拿了出来,当然我们在使用MgFeatureQueryOptions 的时候也可以借鉴一下他的filter方法来对内容做个筛选。

好了mapguide的图层元素遍历就算是完成了。有兴趣的民工筒子可以给我留言大家一起探讨。    

版权声明:本文为wenleilu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wenleilu/archive/2011/01/14/1935726.html