public void FindClosestElement() { using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\TestData\GrandTeton.gdb")))) using (Topology topology = geodatabase.OpenDataset <Topology>("Backcountry_Topology")) { // Build a topology graph using the extent of the topology dataset. topology.BuildGraph(topology.GetExtent(), (topologyGraph) => { MapPoint queryPointViaCampsites12 = null; using (Feature campsites12 = GetFeature(geodatabase, "Campsites", 12)) { queryPointViaCampsites12 = campsites12.GetShape() as MapPoint; } double searchRadius = 1.0; TopologyElement topologyElementViaCampsites12 = topologyGraph.FindClosestElement <TopologyElement>(queryPointViaCampsites12, searchRadius); System.Diagnostics.Debug.Assert(topologyElementViaCampsites12 != null, "There should be a topology element corresponding to 'queryPointViaCampsites12' within the 'searchRadius' units."); IReadOnlyList <FeatureInfo> parentFeatures = topologyElementViaCampsites12.GetParentFeatures(); Console.WriteLine("The parent features that spawn 'topologyElementViaCampsites12' are:"); foreach (FeatureInfo parentFeature in parentFeatures) { Console.WriteLine($"\t{parentFeature.FeatureClassName}; OID: {parentFeature.ObjectID}"); } TopologyNode topologyNodeViaCampsites12 = topologyGraph.FindClosestElement <TopologyNode>(queryPointViaCampsites12, searchRadius); if (topologyNodeViaCampsites12 != null) { // There exists a TopologyNode nearest to the query point within searchRadius units. } TopologyEdge topologyEdgeViaCampsites12 = topologyGraph.FindClosestElement <TopologyEdge>(queryPointViaCampsites12, searchRadius); if (topologyEdgeViaCampsites12 != null) { // There exists a TopologyEdge nearest to the query point within searchRadius units. } }); } }
private static void ExploreTopologyGraph(Geodatabase geodatabase, Topology topology) { // Build a topology graph using the extent of the topology dataset. topology.BuildGraph(topology.GetExtent(), (topologyGraph) => { // To visualize where the query point is, see "QueryPoint.PNG" in the "..\Data" folder. Feature featureViaTrailPoints66 = GetFeature(geodatabase, "Trail_Points", 66); try { MapPoint queryPointViaTrailPoints66 = featureViaTrailPoints66.GetShape() as MapPoint; double searchRadius = 1.0; TopologyNode topologyNodeViaTrailPoints66 = topologyGraph.FindClosestElement <TopologyNode>(queryPointViaTrailPoints66, searchRadius); System.Diagnostics.Debug.Assert(topologyNodeViaTrailPoints66 != null, "There should be a topology node corresponding to 'queryPointViaTrailPoints66' within the 'searchRadius' units."); IReadOnlyList <FeatureInfo> parentFeatures = topologyNodeViaTrailPoints66.GetParentFeatures(); Console.WriteLine("***************************************************************************"); Console.WriteLine("The parent features that spawn 'topologyNodeViaTrailPoints66' are:"); foreach (FeatureInfo parentFeature in parentFeatures) { Console.WriteLine($"\t{parentFeature.FeatureClassName}; OID: {parentFeature.ObjectID}"); } IReadOnlyList <TopologyEdge> allEdgesConnectedToNodeViaTrailPoints66 = topologyNodeViaTrailPoints66.GetEdges(); foreach (TopologyEdge edge in allEdgesConnectedToNodeViaTrailPoints66) { IReadOnlyList <FeatureInfo> parents = edge.GetParentFeatures(); System.Diagnostics.Debug.Assert(parents.Count == 1, "The edge should have only 1 corresponding parent feature."); FeatureInfo parentFeature = parents[0]; // To determine whether the edge's "from node" is coincident with "topologyNodeViaTrailPoints66". bool fromNodeIsCoincidentWithNodeViaTrailPoints66 = edge.GetFromNode() == topologyNodeViaTrailPoints66; // To determine whether the edge's "to node" is coincident with "topologyNodeViaTrailPoints66". bool toNodeIsCoincidentWithNodeViaTrailPoints66 = edge.GetToNode() == topologyNodeViaTrailPoints66; if (fromNodeIsCoincidentWithNodeViaTrailPoints66) { Console.WriteLine($"The [{parentFeature.FeatureClassName},{parentFeature.ObjectID}] edge's FromNode is coincident with 'topologyNodeViaTrailPoints66'."); } if (toNodeIsCoincidentWithNodeViaTrailPoints66) { Console.WriteLine($"The [{parentFeature.FeatureClassName},{parentFeature.ObjectID}] edge's ToNode is coincident with 'topologyNodeViaTrailPoints66'."); } // Get only polygon features lying to the left of this edge if available (specified by the "boundedByEdge" optional argument). IReadOnlyList <FeatureInfo> leftParentFeatures = edge.GetLeftParentFeatures(); if (leftParentFeatures.Count == 0) { // There are no polygon features lying to the left of this edge; get the set of polygon features that cover this edge. Console.WriteLine("The set of polygon features that cover this edge:"); leftParentFeatures = edge.GetLeftParentFeatures(false); } else { Console.WriteLine("The set of polygon features lying to the left of this edge (i.e., left parent polygon features bounded by this edge):"); } foreach (FeatureInfo leftParentFeature in leftParentFeatures) { Console.WriteLine($"\t{leftParentFeature.FeatureClassName}; OID: {leftParentFeature.ObjectID}"); // To get additional information from the parent feature (e.g., shape, globalID), use leftParentFeature.GetFeature(). } // Get only polygon features lying to the right of this edge if available (specified by the "boundedByEdge" optional argument). IReadOnlyList <FeatureInfo> rightParentFeatures = edge.GetRightParentFeatures(); if (rightParentFeatures.Count != 0) { Console.WriteLine("The set of polygon features lying to the right of this edge (i.e., right parent polygon features bounded by this edge):"); foreach (FeatureInfo rightParentFeature in rightParentFeatures) { Console.WriteLine($"\t{rightParentFeature.FeatureClassName}; OID: {rightParentFeature.ObjectID}"); } } } } finally { if (featureViaTrailPoints66 != null) { featureViaTrailPoints66.Dispose(); } } }); }
public void ExploreTopologyGraph() { using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\TestData\GrandTeton.gdb")))) using (Topology topology = geodatabase.OpenDataset <Topology>("Backcountry_Topology")) { // Build a topology graph using the extent of the topology dataset. topology.BuildGraph(topology.GetExtent(), (topologyGraph) => { using (Feature campsites12 = GetFeature(geodatabase, "Campsites", 12)) { IReadOnlyList <TopologyNode> topologyNodesViaCampsites12 = topologyGraph.GetNodes(campsites12); TopologyNode topologyNodeViaCampsites12 = topologyNodesViaCampsites12[0]; IReadOnlyList <TopologyEdge> allEdgesConnectedToNodeViaCampsites12 = topologyNodeViaCampsites12.GetEdges(); IReadOnlyList <TopologyEdge> allEdgesConnectedToNodeViaCampsites12CounterClockwise = topologyNodeViaCampsites12.GetEdges(false); System.Diagnostics.Debug.Assert(allEdgesConnectedToNodeViaCampsites12.Count == allEdgesConnectedToNodeViaCampsites12CounterClockwise.Count); foreach (TopologyEdge edgeConnectedToNodeViaCampsites12 in allEdgesConnectedToNodeViaCampsites12) { TopologyNode fromNode = edgeConnectedToNodeViaCampsites12.GetFromNode(); TopologyNode toNode = edgeConnectedToNodeViaCampsites12.GetToNode(); bool fromNodeIsTheSameAsTopologyNodeViaCampsites12 = (fromNode == topologyNodeViaCampsites12); bool toNodeIsTheSameAsTopologyNodeViaCampsites12 = (toNode == topologyNodeViaCampsites12); System.Diagnostics.Debug.Assert(fromNodeIsTheSameAsTopologyNodeViaCampsites12 || toNodeIsTheSameAsTopologyNodeViaCampsites12, "The FromNode *or* ToNode of each edge connected to 'topologyNodeViaCampsites12' should be the same as 'topologyNodeViaCampsites12' itself."); IReadOnlyList <FeatureInfo> leftParentFeaturesBoundedByEdge = edgeConnectedToNodeViaCampsites12.GetLeftParentFeatures(); foreach (FeatureInfo featureInfo in leftParentFeaturesBoundedByEdge) { System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(featureInfo.FeatureClassName)); System.Diagnostics.Debug.Assert(featureInfo.ObjectID > 0); EnsureShapeIsNotEmpty(featureInfo); } IReadOnlyList <FeatureInfo> leftParentFeaturesNotBoundedByEdge = edgeConnectedToNodeViaCampsites12.GetLeftParentFeatures(false); foreach (FeatureInfo featureInfo in leftParentFeaturesNotBoundedByEdge) { System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(featureInfo.FeatureClassName)); System.Diagnostics.Debug.Assert(featureInfo.ObjectID > 0); EnsureShapeIsNotEmpty(featureInfo); } IReadOnlyList <FeatureInfo> rightParentFeaturesBoundedByEdge = edgeConnectedToNodeViaCampsites12.GetRightParentFeatures(); foreach (FeatureInfo featureInfo in rightParentFeaturesBoundedByEdge) { System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(featureInfo.FeatureClassName)); System.Diagnostics.Debug.Assert(featureInfo.ObjectID > 0); EnsureShapeIsNotEmpty(featureInfo); } IReadOnlyList <FeatureInfo> rightParentFeaturesNotBoundedByEdge = edgeConnectedToNodeViaCampsites12.GetRightParentFeatures(false); foreach (FeatureInfo featureInfo in rightParentFeaturesNotBoundedByEdge) { System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(featureInfo.FeatureClassName)); System.Diagnostics.Debug.Assert(featureInfo.ObjectID > 0); EnsureShapeIsNotEmpty(featureInfo); } } } }); } }