Hi,
I'm creating a MapTool to help with creating a fully digitized sewer connection. It works by sketching a line on the map; the first click creates the connection to the sewer, the last click the 'end' of the line, and everything in between is the actual sewer connection.
Currently I check for the first click to be intersecting the gravityMainLayer in the OnSketchCompleteAsync. It works, and I've created some IntersectsLayer code to determine an intersection of the first point.
<SPAN class="keyword token">protected</SPAN> <SPAN class="keyword token">override</SPAN> Task<SPAN class="operator token"><</SPAN><SPAN class="keyword token">bool</SPAN><SPAN class="operator token">></SPAN> <SPAN class="token function">OnSketchCompleteAsync</SPAN><SPAN class="punctuation token">(</SPAN>Geometry geometry<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> pointCollection <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>Multipart<SPAN class="punctuation token">)</SPAN>geometry<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>Points<SPAN class="punctuation token">;</SPAN>
MapPoint connectionPoint <SPAN class="operator token">=</SPAN> pointCollection<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
MapPoint plugPoint <SPAN class="operator token">=</SPAN> pointCollection<SPAN class="punctuation token">[</SPAN>pointCollection<SPAN class="punctuation token">.</SPAN>Count <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="token function">IntersectsLayer</SPAN><SPAN class="punctuation token">(</SPAN>connectionPoint<SPAN class="punctuation token">,</SPAN> gravityMainLayer<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Dialogs<SPAN class="punctuation token">.</SPAN>MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The starting point must intersect the GravitySewer layer."</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Error"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">return</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
blah<SPAN class="punctuation token">,</SPAN> blah<SPAN class="punctuation token">,</SPAN> blah<SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">base</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">OnSketchCompleteAsync</SPAN><SPAN class="punctuation token">(</SPAN>geometry<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">public</SPAN> <SPAN class="keyword token">bool</SPAN> <SPAN class="token function">IntersectsLayer</SPAN><SPAN class="punctuation token">(</SPAN>MapPoint point<SPAN class="punctuation token">,</SPAN> FeatureLayer layer<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">try</SPAN>
<SPAN class="punctuation token">{</SPAN>
SpatialQueryFilter spatialFilter <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">SpatialQueryFilter</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
spatialFilter<SPAN class="punctuation token">.</SPAN>FilterGeometry <SPAN class="operator token">=</SPAN> point<SPAN class="punctuation token">;</SPAN>
spatialFilter<SPAN class="punctuation token">.</SPAN>SpatialRelationship <SPAN class="operator token">=</SPAN> SpatialRelationship<SPAN class="punctuation token">.</SPAN>Intersects<SPAN class="punctuation token">;</SPAN>
RowCursor theCursor <SPAN class="operator token">=</SPAN> layer<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Search</SPAN><SPAN class="punctuation token">(</SPAN>spatialFilter<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
Feature feature<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN>theCursor<SPAN class="punctuation token">.</SPAN><SPAN class="token function">MoveNext</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">using</SPAN> <SPAN class="punctuation token">(</SPAN>feature <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>Feature<SPAN class="punctuation token">)</SPAN>theCursor<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">catch</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="token class-name">Exception</SPAN> ex<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Dialogs<SPAN class="punctuation token">.</SPAN>MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN>ex<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ToString</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>To make things work better for the user, I am trying to have it check for the intersection after the first mouse click, to avoid the user making more clicks when not necessary. So I am using the OnToolMouseDown to capture the first click, and the look for the intersection. The problem is that each time the IntersectLayer always returns false, even though I am for sure clicking a point that is intersecting the gravityMainLayer. As soon as I comment out the OnToolMouseDown code, everything works fine again.
So my question is should the OnToolMouseDown event return the same MapPoint as the OnSketchCompleteAsync event?? From stepping through the code, it looks like a valid point, but I don't understand why it is never intersecting.
<SPAN class="keyword token">protected</SPAN> <SPAN class="keyword token">override</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">OnToolMouseDown</SPAN><SPAN class="punctuation token">(</SPAN>MapViewMouseButtonEventArgs e<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>ChangedButton <SPAN class="operator token">==</SPAN> System<SPAN class="punctuation token">.</SPAN>Windows<SPAN class="punctuation token">.</SPAN>Input<SPAN class="punctuation token">.</SPAN>MouseButton<SPAN class="punctuation token">.</SPAN>Left<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
clickCount<SPAN class="operator token">++</SPAN><SPAN class="punctuation token">;</SPAN>
e<SPAN class="punctuation token">.</SPAN>Handled <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Handle the event args to get the call to the corresponding async method</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token">//base.OnToolMouseDown(e);</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">protected</SPAN> <SPAN class="keyword token">override</SPAN> Task <SPAN class="token function">HandleMouseDownAsync</SPAN><SPAN class="punctuation token">(</SPAN>MapViewMouseButtonEventArgs e<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>clickCount <SPAN class="operator token">==</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> mapPoint <SPAN class="operator token">=</SPAN> MapView<SPAN class="punctuation token">.</SPAN>Active<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ClientToMap</SPAN><SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">.</SPAN>ClientPoint<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="token function">IntersectsLayer</SPAN><SPAN class="punctuation token">(</SPAN>mapPoint<SPAN class="punctuation token">,</SPAN> gravityMainLayer<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Dialogs<SPAN class="punctuation token">.</SPAN>MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"The starting point must intersect the GravitySewer layer."</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Error"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
clickCount <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">base</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">HandleMouseDownAsync</SPAN><SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>