关闭

关闭

封号提示

内容

首页 C源码ArcEngine最短路径分析.doc

C源码ArcEngine最短路径分析.doc

C源码ArcEngine最短路径分析.doc

上传者: Daniel华伟 2017-10-07 评分 4.5 0 60 8 272 暂无简介 简介 举报

简介:本文档为《C源码ArcEngine最短路径分析doc》,可适用于IT/计算机领域,主题内容包含C源码ArcEngine最短路径分析ArcEngine最短路径分析(源码)几何网络usingSystemusingESRIArcGISCartous符等。

C源码ArcEngine最短路径分析ArcEngine最短路径分析(源码)几何网络usingSystemusingESRIArcGISCartousingESRIArcGISGeometryusingESRIArcGISGeodatabaseusingESRIArcGISNetworkAnalysisnamespaceGisEditor{<summary>最短路径分析<summary>publicclassClsPathFinder{privateIGeometricNetworkmipGeometricNetworkprivateIMapmipMapprivateIPointCollectionmipPointsprivateIPointToEIDmipPointToEIDprivatedoublemdblPathCost=privateIEnumNetEIDmipEnumNetEIDJunctionsprivateIEnumNetEIDmipEnumNetEIDEdgesprivateIPolylinemipPolyline#regionPublicFunction返回和设置当前地图publicIMapSetOrGetMap{set{mipMap=value}get{returnmipMap}}打开几何数据集的网络工作空间publicvoidOpenFeatureDatasetNetwork(IFeatureDatasetFeatureDataset){CloseWorkspace()if(!InitializeNetworkAndMap(FeatureDataset))ConsoleWriteLine("打开network出错")}输入点的集合publicIPointCollectionStopPoints{set{mipPoints=value}get{returnmipPoints}}路径成本publicdoublePathCost{get{returnmdblPathCost}}返回路径的几何体publicIPolylinePathPolyLine(){IEIDInfoipEIDInfoIGeometryipGeometryif(mipPolyline!=)returnmipPolylinemipPolyline=newPolylineClass()IGeometryCollectionipNewGeometryColl=mipPolylineasIGeometryCollectionISpatialReferenceipSpatialReference=mipMapSpatialReferenceIEIDHelperipEIDHelper=newEIDHelperClass()ipEIDHelperGeometricNetwork=mipGeometricNetworkipEIDHelperOutputSpatialReference=ipSpatialReferenceipEIDHelperReturnGeometries=trueIEnumEIDInfoipEnumEIDInfo=ipEIDHelperCreateEnumEIDInfo(mipEnumNetEIDEdges)intcount=ipEnumEIDInfoCountipEnumEIDInfoReset()for(inti=i<counti){ipEIDInfo=ipEnumEIDInfoNext()ipGeometry=ipEIDInfoGeometryipNewGeometryCollAddGeometryCollection(ipGeometryasIGeometryCollection)}returnmipPolyline}解决路径publicvoidSolvePath(stringWeightName){try{intintEdgeUserClassIDintintEdgeUserIDintintEdgeUserSubIDintintEdgeIDIPointipFoundEdgePointdoubledblEdgePercent*PutEdgeOrigins方法的第二个参数要求是IEdgeFlag类型的数组,*在VB等其他语言的代码中只需传人该类型数组的第一个元素即*可但C#中的机制有所不同需要作出如下修改:使用*ITraceFlowSolverGEN替代ITraceFlowSolver*ITraceFlowSolverGENipTraceFlowSolver=newTraceFlowSolverClass()asITraceFlowSolverGENINetSolveripNetSolver=ipTraceFlowSolverasINetSolverINetworkipNetwork=mipGeometricNetworkNetworkipNetSolverSourceNetwork=ipNetworkINetElementsipNetElements=ipNetworkasINetElementsintintCount=mipPointsPointCount定义一个边线旗数组IEdgeFlagpEdgeFlagList=newEdgeFlagClassintCountfor(inti=i<intCounti){INetFlagipNetFlag=newEdgeFlagClass()asINetFlagIPointipEdgePoint=mipPointsgetPoint(i)查找输入点的最近的边线mipPointToEIDGetNearestEdge(ipEdgePoint,outintEdgeID,outipFoundEdgePoint,outdblEdgePercent)ipNetElementsQueryIDs(intEdgeID,esriElementTypeesriETEdge,outintEdgeUserClassID,outintEdgeUserID,outintEdgeUserSubID)ipNetFlagUserClassID=intEdgeUserClassIDipNetFlagUserID=intEdgeUserIDipNetFlagUserSubID=intEdgeUserSubIDIEdgeFlagpTemp=(IEdgeFlag)(ipNetFlagasIEdgeFlag)pEdgeFlagListi=pTemp}ipTraceFlowSolverPutEdgeOrigins(refpEdgeFlagList)INetSchemaipNetSchema=ipNetworkasINetSchemaINetWeightipNetWeight=ipNetSchemagetWeightByName(WeightName)INetSolverWeightsipNetSolverWeights=ipTraceFlowSolverasINetSolverWeightsipNetSolverWeightsFromToEdgeWeight=ipNetWeight开始边线的权重ipNetSolverWeightsToFromEdgeWeight=ipNetWeight终止边线的权重objectvaRes=newobjectintCount通过findpath得到边线和交汇点的集合ipTraceFlowSolverFindPath(esriFlowMethodesriFMConnected,esriShortestPathObjFnesriSPObjFnMinSum,outmipEnumNetEIDJunctions,outmipEnumNetEIDEdges,intCount,refvaRes)计算元素成本mdblPathCost=for(inti=i<vaResLengthi){doublemVa=(double)vaResimdblPathCost=mdblPathCostmVa}mipPolyline=}catch(Exceptionex){ConsoleWriteLine(exMessage)}}#endregion#regionPrivateFunction初始化几何网络和地图privateboolInitializeNetworkAndMap(IFeatureDatasetFeatureDataset){IFeatureClassContaineripFeatureClassContainerIFeatureClassipFeatureClassIGeoDatasetipGeoDatasetILayeripLayerIFeatureLayeripFeatureLayerIEnvelopeipEnvelope,ipMaxEnvelopedoubledblSearchTolINetworkCollectionipNetworkCollection=FeatureDatasetasINetworkCollectionintcount=ipNetworkCollectionGeometricNetworkCount获取第一个几何网络工作空间mipGeometricNetwork=ipNetworkCollectiongetGeometricNetwork()INetworkipNetwork=mipGeometricNetworkNetworkif(mipMap!=){mipMap=newMapClass()ipFeatureClassContainer=mipGeometricNetworkasIFeatureClassContainercount=ipFeatureClassContainerClassCountfor(inti=i<counti){ipFeatureClass=ipFeatureClassContainergetClass(i)ipFeatureLayer=newFeatureLayerClass()ipFeatureLayerFeatureClass=ipFeatureClassmipMapAddLayer(ipFeatureLayer)}}count=mipMapLayerCountipMaxEnvelope=newEnvelopeClass()for(inti=i<counti){ipLayer=mipMapgetLayer(i)ipFeatureLayer=ipLayerasIFeatureLayeripGeoDataset=ipFeatureLayerasIGeoDatasetipEnvelope=ipGeoDatasetExtentipMaxEnvelopeUnion(ipEnvelope)}mipPointToEID=newPointToEIDClass()mipPointToEIDSourceMap=mipMapmipPointToEIDGeometricNetwork=mipGeometricNetworkdoubledblWidth=ipMaxEnvelopeWidthdoubledblHeight=ipMaxEnvelopeHeightif(dblWidth>dblHeight)dblSearchTol=dblWidthelsedblSearchTol=dblHeightmipPointToEIDSnapTolerance=dblSearchTolreturntrue}关闭工作空间privatevoidCloseWorkspace(){mipGeometricNetwork=mipPoints=mipPointToEID=mipEnumNetEIDJunctions=mipEnumNetEIDEdges=mipPolyline=}#endregion}}备注:在调用该类时的次序:ClsPathFindermipPathFinderif(mipPathFinder==)打开几何网络工作空间{mipPathFinder=newClsPathFinder()ipMap=thismActiveViewFocusMapipLayer=ipMapgetLayer()ipFeatureLayer=ipLayerasIFeatureLayeripFDB=ipFeatureLayerFeatureClassFeatureDatasetmipPathFinderSetOrGetMap=ipMapmipPathFinderOpenFeatureDatasetNetwork(ipFDB)}privatevoidViewMapOnMouseDown(objectsender,ESRIArcGISMapControlIMapControlEventsOnMouseDownEvente)获取地图上鼠标输入的点{IPointipNewif(mipPoints==){mipPoints=newMultipointClass()mipPathFinderStopPoints=mipPoints}ipNew=ViewMapActiveViewScreenDisplayDisplayTransformationToMapPoint(ex,ey)objecto=TypeMissingmipPointsAddPoint(ipNew,refo,refo)}mipPathFinderSolvePath("Weight")先解析路径IPolylineipPolyResult=mipPathFinderPathPolyLine()最后返回最短路径网络数据集的最短路径这里的添加出发点或者目的点是往“Facilities”或“Incidents”图层上添加元素。获取结果也是从“Routes”中获取Polyline。往“Facilities”或“Incidents”图层上添加元素用到的主要方法是INALocator的QueryLocationByPoint函数生成路径主要接口是INASolver和它的Solve方法。获取结果是按属性查找因为“Routes”类其实就是一个图层类只不过只是存在于内存。CMapControlDefaultmmapIPointCollectionPtrmipPointCollectionILayerPtripLayer=mmapGetLayer()网络数据集INALayerPtripNaLayer=ipLayerif(==ipNaLayer){return}INAContextPtripNaContextHRESULThr=ipNaLayer>getContext(ipNaContext)INAClassLoaderPtripNAClassLoader(CLSIDNAClassLoader)INALocatorPtripNALocator=hr=ipNaContext>getLocator(ipNALocator)ipNALocator>putSnapToleranceUnits(esriMeters)ipNALocator>putSnapTolerance()ipNaContexthr=ipNAClassLoader>putrefLocator(ipNALocator)INamedSetPtripNamedSet=ipNaContext>getNAClasses(ipNamedSet)CStringszName="Facilities"BSTRbstrName=szNameAllocSysString()INAClassPtripNAFacilitiesClass=hr=ipNamedSet>getItemByName(bstrName,(IUnknown**)ipNAFacilitiesClass)szName="Incidents"bstrName=szNameAllocSysString()INAClassPtripNAIncidentsClass=hr=ipNamedSet>getItemByName(bstrName,(IUnknown**)ipNAIncidentsClass)szName="CFRoutes"bstrName=szNameAllocSysString()INAClassPtripNARoutesClass=hr=ipNamedSet>getItemByName(bstrName,(IUnknown**)ipNARoutesClass)INALocationPtripNALocation(CLSIDNALocation)INALocationPtripNALocation(CLSIDNALocation)ipNAClassLoader>getLocator(ipNALocator)IPointPtripBeginPoint(CLSIDPoint)mipPointCollection>getPoint(,ipBeginPoint)IPointPtripEndPoint(CLSIDPoint)mipPointCollection>getPoint(,ipEndPoint)IPointPtripPoint(CLSIDPoint)IPointPtripPoint(CLSIDPoint)doubledbLVal=ipNALocator>QueryLocationByPoint(ipBeginPoint,ipNALocation,ipPoint,dbLVal)ipNALocator>QueryLocationByPoint(ipEndPoint,ipNALocation,ipPoint,dbLVal)INALocationObjectPtripNALocationObject=IFeatureClassPtripFeatureClass=ipNAIncidentsClassIFeaturePtripFeature=ipFeatureClass>CreateFeature(ipFeature)IRowSubtypesPtripRowSubtypes=ipFeatureipRowSubtypes>InitDefaultValues()ipFeature>putrefShape(ipBeginPoint)ITablePtripTable=ipFeature>getTable(ipTable)longnIndex=szName="Sequence"bstrName=szNameAllocSysString()ipTable>FindField(bstrName,nIndex)VARIANTvarintvarintintVal=varintvt=VTINTipFeature>putValue(nIndex,varint)szName="Name"bstrName=szNameAllocSysString()ipTable>FindField(bstrName,nIndex)ipFeature>putValue(nIndex,COleVariant("StartPoint"))ipNALocationObject=ipFeatureipNALocationObject>putNALocation(ipNALocation)ipFeature>Store()IFieldsPtripFields(CLSIDFields)hr=ipTable>getFields(ipFields)longnFieldCount=hr=ipFields>getFieldCount(nFieldCount)for(intk=k<nFieldCountk){IFieldPtripField(CLSIDField)ipFields>getField(k,ipField)BSTRbstrFieldNameipField>getName(bstrFieldName)CStringszFieldName=bstrFieldName}ipFeatureClass=ipNAFacilitiesClassipFeatureClass>CreateFeature(ipFeature)ipRowSubtypes=ipFeatureipRowSubtypes>InitDefaultValues()ipFeature>putrefShape(ipEndPoint)ipTable=ipFeature>getTable(ipTable)nIndex=szName="Sequence"bstrName=szNameAllocSysString()ipTable>FindField(bstrName,nIndex)varintintVal=ipFeature>putValue(nIndex,varint)szName="Name"bstrName=szNameAllocSysString()ipTable>FindField(bstrName,nIndex)ipFeature>putValue(nIndex,COleVariant("EndPoint"))ipNALocationObject=ipFeatureipNALocationObject>putNALocation(ipNALocation)ipFeature>Store()INAClosestFacilitySolverPtripNACFSolver=INASolverPtripNASolver=ipNaContext>getSolver(ipNASolver)IGPMessagesPtripGPM(CLSIDGPMessages)ITrackCancelPtripTrackCancel(CLSIDTrackCancel)VARIANTBOOLbIsPartialSolutionipNASolver>Solve(ipNaContext,ipGPM,ipTrackCancel,bIsPartialSolution)szName="CFRoutes"bstrName=szNameAllocSysString()ipNARoutesClass=hr=ipNamedSet>getItemByName(bstrName,(IUnknown**)ipNARoutesClass)IFeatureClassPtripFeatureClassRoutes=ipNARoutesClassIFeatureCursorPtripFCursor=IQueryFilterPtripQueryFilter(CLSIDQueryFilter)CStringszQueryFilter("ObjectID>")BSTRbstrQueryFilter=szQueryFilterAllocSysString()ipQueryFilter>putWhereClause(bstrQueryFilter)VARIANTBOOLbCycle=VARIANTFALSEtry{ipFeatureClassRoutes>Search(ipQueryFilter,bCycle,ipFCursor)}catch(CException*e){CStringszErrorMsge>GetErrorMessage(szErrorMsgGetBuffer(MAXPATH),MAXPATH)szErrorMsgReleaseBuffer()e>Delete()szErrorMsg="n"OutputDebugStr(szErrorMsg)}catch(){OutputDebugStr("AnUnknownedExceptionOccurred!n")}IFeaturePtripLineFeature=hr=ipFCursor>NextFeature(ipLineFeature)while(ipLineFeature!=){IGeometryPtripGeometry=IPolylinePtripPolyLine=ipLineFeature>getShape(ipGeometry)esriGeometryTypetypeipGeometry>getGeometryType(type)if(type==esriGeometryPolyline){ipPolyLine=ipGeometryAddPolyline(ipPolyLine,)}hr=ipFCursor>NextFeature(ipLineFeature)}IActiveViewPtripActiveView=ipActiveView=mmapGetActiveView()ipActiveView>Refresh()

精彩专题

职业精品

上传我的资料

热门资料

资料评价:

/ 18
所需积分:0 立即下载

意见
反馈

返回
顶部

Q