2009年4月12日星期日

【原】如何给Facilities图层添加元素

这里所说的Facilities图层是指Closest Facility图层的子图层,就是设施点图层。我上次在做最短路径分析时,得到了Facilities图层的接口,但是不知道如何给它添加元素。因为ILayer实现了IFeatureClass的接口,所以很容易就获取到IFeatureClass接口,但是是不是直接就Create一个Feature然后添加、保存上去呢?打开ArcMap,先看看它的属性,有ObjectID、PosAlong、SourceID、SourceOID、SideOfEdge等等,在帮助文档里找到了注释:SourceID是网络数据集上所定位到的图元的源ID,可以从INetworkSource::ID获得此ID;SourceOID是定位到的图元的对象ID;SideOfEdge是指在边的左侧还是右侧;PosAlong是指定位到的图元沿边的距离。要是一个字段一个字段的去计算,然后再添加,累死你,所幸ArcEngine提供了一个方法QueryLocationByPoint,可以帮我们完成这件事。
它的接口如下:HRESULT QueryLocationByPoint(IPoint* point,INALocation** Location,IPoint** outPoint,double* distanceFromPoint);AE帮助上是这种说的,Using the input Point object, the method calls QueryLocationByPoint on each of its associated NALocatorAgent classes. The corresponding NALocation, the point where the location was found, and the distance from the input point to the source feature found are all returned.
下面是实现代码
ILayerPtr ipLayer = m_map.GetLayer(0);
INALayerPtr ipNaLayer = ipLayer;
INAContextPtr ipNaContext;
HRESULT hr = ipNaLayer->get_Context(&ipNaContext);
INAClassLoaderPtr ipNAClassLoader(CLSID_NAClassLoader);
INALocatorPtr ipNALocator = NULL;hr = ipNaContext->get_Locator(&ipNALocator);
hr = ipNAClassLoader->putref_Locator(ipNALocator);
IPointPtr ipPoint;INALocationPtr ipNALocation(CLSID_NALocation);
ipNAClassLoader->get_Locator(&ipNALocator);
IPointPtr ipOutPoint(CLSID_Point);
double dbLVal = 0.0;
ipNALocator->QueryLocationByPoint(ipPoint, &ipNALocation, &ipOutPoint, &dbLVal);

没有评论:

发表评论