// Calculates the linear distance between two user-defined geometries private async void DistanceButton_Click(object sender, RoutedEventArgs e) { try { txtResults.Visibility = Visibility.Collapsed; _graphicsOverlay.Graphics.Clear(); // wait for user to draw a polyline var line = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polyline, _lineSymbol); _graphicsOverlay.Graphics.Add(new Graphic(line, _lineSymbol)); // wait for user to draw a point var point = await MyMapView.Editor.RequestPointAsync(); _graphicsOverlay.Graphics.Add(new Graphic(point, _pointSymbol)); // Calculate distance between line and point double distance = GeometryEngine.Distance(line, point) * METERS_TO_MILES; txtResults.Text = string.Format("Distance between geometries: {0:0.000} miles", distance); txtResults.Visibility = Visibility.Visible; } catch (Exception ex) { var _x = new MessageDialog("Distance Calculation Error: " + ex.Message, "Sample Error").ShowAsync(); txtResults.Visibility = Visibility.Collapsed; _graphicsOverlay.Graphics.Clear(); } }
// calculates how far down a line a certain point on the line is located as a value from 0..1 private double GetFractionAlongLine(Polyline segment, ProximityResult proximity, MapPoint location) { double distance1 = 0; double distance2 = 0; int pointIndex = proximity.PointIndex; int vertexCount = segment.Parts.GetPartsAsPoints().First().Count(); var vertexPoint = segment.Parts.GetPartsAsPoints().ElementAt(proximity.PartIndex).ElementAt(pointIndex); MapPoint previousPoint; int onSegmentIndex = 0; //Detect which line segment we currently are on if (pointIndex == 0) //Snapped to first vertex { onSegmentIndex = 0; } else if (pointIndex == vertexCount - 1) //Snapped to last vertex { onSegmentIndex = segment.Parts.GetPartsAsPoints().First().Count() - 2; } else { MapPoint nextPoint = segment.Parts.GetPartsAsPoints().First().ElementAt(pointIndex + 1); var d1 = GeometryEngine.Distance(vertexPoint, nextPoint); var d2 = GeometryEngine.Distance(location, nextPoint); if (d1 < d2) { onSegmentIndex = pointIndex - 1; } else { onSegmentIndex = pointIndex; } } previousPoint = segment.Parts.GetPartsAsPoints().First().First(); for (int j = 1; j < onSegmentIndex + 1; j++) { MapPoint point = segment.Parts.GetPartsAsPoints().First().ElementAt(j); distance1 += GeometryEngine.Distance(previousPoint, point); previousPoint = point; } distance1 += GeometryEngine.Distance(previousPoint, location); previousPoint = segment.Parts.GetPartsAsPoints().First().ElementAt(onSegmentIndex + 1); distance2 = GeometryEngine.Distance(location, previousPoint); previousPoint = vertexPoint; for (int j = onSegmentIndex + 2; j < segment.Parts[0].Count; j++) { MapPoint point = segment.Parts.GetPartsAsPoints().First().ElementAt(j); distance2 += GeometryEngine.Distance(previousPoint, point); previousPoint = point; } //var previousPoint = proximity.PointIndex ? segment.GetPoint(proximity.PartIndex + 1, 0) : segment.GetPoint(proximity.PartIndex, proximity.PointIndex + 1); if (distance1 + distance2 == 0) { return(1); } return(distance1 / (distance1 + distance2)); }
public void UpdateMapPointToScale() { Point secondP = new Point(0, 1); _mapPointToScale = _MyMapView.ScreenToLocation(secondP); Scale = GeometryEngine.Distance(_BBox_Left_Top, _mapPointToScale) / 1.735; //return a; }
/// <summary> /// Create a circular polygon around a mappoint for with a radius in pixels. /// </summary> /// <param name="mapPoint">Center of the circle as a mappoint.</param> /// <param name="pixels">Circle radius in screen pixels.</param> /// <returns>A polygon geometry.</returns> private Polygon CreateSearchPolygon(MapPoint mapPoint, int pixels) { //get search radius var screenPoint = MapView.Active.MapToScreen(mapPoint); var radiusScreenPoint = new System.Windows.Point((screenPoint.X + pixels), screenPoint.Y); var radiusMapPoint = MapView.Active.ScreenToMap(radiusScreenPoint); var searchRadius = GeometryEngine.Distance(mapPoint, radiusMapPoint); //build a search circle geometry var cent = new Coordinate2D(mapPoint); var searchGeom = EllipticArcBuilder.CreateEllipticArcSegment(cent, searchRadius, esriArcOrientation.esriArcClockwise, MapView.Active.Map.SpatialReference); var searchPB = new PolygonBuilder(new[] { searchGeom }); return(searchPB.ToGeometry()); }
private async void QueryFeatures(Geometry buffer, MapPoint location) { //create query var query = new QueryParameters(); query.Geometry = buffer; query.MaxAllowableOffset = 0; System.Diagnostics.Debug.WriteLine(buffer.ToJson()); var layer = MyMapView.Map.OperationalLayers[0] as FeatureLayer; var table = layer.FeatureTable; var results = await table.QueryFeaturesAsync(query); layer.ClearSelection(); /* // =================================================================== * // Single Closest Bar * // ------------------------------------------------------------------- * * double shortDist = -1; * Feature feature = null; * * //check for closest bars within buffer * foreach (var item in results) * { * var calcDistance = GeometryEngine.Distance(location, item.Geometry); * * if (shortDist == -1) * { * shortDist = calcDistance; * feature = item; * continue; * } * * if (calcDistance < shortDist) * { * shortDist = calcDistance; * feature = item; * } * } * * //exit if no closest bar * if (feature == null) * return; * * if (!(GeometryEngine.Intersects(buffer, feature.Geometry))) * return; * * //highlight closest bar * layer.SelectFeature(feature); * layer.SelectionColor = Colors.DarkRed; * * //draw line to closest bar * var linePoints = new PolylineBuilder(SpatialReferences.WebMercator); * linePoints.AddPoint(location); * linePoints.AddPoint(feature.Geometry as MapPoint); * var line = linePoints.ToGeometry(); * * var lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.DashDotDot, Colors.Maroon, 2); * var lineGraphic = new Graphic(line, lineSymbol); * MyOverlay.Graphics.Add(lineGraphic); * * * //create text symbol for distance at midpoint of line * var x = (line.Extent.XMin + line.Extent.XMax) / 2; * var y = (line.Extent.YMin + line.Extent.YMax) / 2; * var textPoint = new MapPoint(x, y); * var text = String.Format("{0:0.00}", shortDist / 1609.34) + " miles"; * var textSymbol = new TextSymbol(text, Colors.Black, 15, Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Center, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Baseline); * textSymbol.FontWeight = Esri.ArcGISRuntime.Symbology.FontWeight.Bold; * //textSymbol.BackgroundColor = Colors.Maroon; * //textSymbol.Angle = Math.Atan2(line.Extent.YMax - line.Extent.YMin, line.Extent.XMax - line.Extent.XMin) * 180.0 / Math.PI; * var textGraphic = new Graphic(textPoint, textSymbol); * MyOverlay.Graphics.Add(textGraphic); * * //===================================================================*/ //================================================================== // Two Closest Bars //------------------------------------------------------------------ double[] shortDist = { -1, -1 }; Feature[] feature = new Feature[2]; System.Diagnostics.Debug.WriteLine("#1: shortdist is at: " + shortDist[0].ToString() + " " + shortDist[1].ToString()); //check for closest bars within buffer foreach (var item in results) { var calcDistance = GeometryEngine.Distance(location, item.Geometry); System.Diagnostics.Debug.WriteLine("feature distance: " + (calcDistance / 1609.34).ToString() + " meters; shordist = " + shortDist[0].ToString() + " " + shortDist[1].ToString()); if (shortDist[0] == -1) { shortDist[0] = calcDistance; System.Diagnostics.Debug.WriteLine("setting first"); feature[0] = item; continue; } if (shortDist[1] == -1) { System.Diagnostics.Debug.WriteLine("setting second"); shortDist[1] = calcDistance; feature[1] = item; continue; } if (calcDistance < shortDist[0]) { System.Diagnostics.Debug.WriteLine("less than first"); shortDist[1] = shortDist[0]; feature[1] = feature[0]; shortDist[0] = calcDistance; feature[0] = item; } else if (calcDistance < shortDist[1]) { System.Diagnostics.Debug.WriteLine("less than second"); shortDist[1] = calcDistance; feature[1] = item; } } //exit if no closest bar if (feature[0] == null || feature[1] == null) { return; } if (!(GeometryEngine.Intersects(buffer, feature[0].Geometry))) { return; } //highlight closest bar for (int i = 0; i < 2; i++) { layer.SelectFeature(feature[i]); layer.SelectionColor = Colors.DarkRed; //draw line to closest bar var linePoints = new PolylineBuilder(SpatialReferences.WebMercator); linePoints.AddPoint(location); linePoints.AddPoint(feature[i].Geometry as MapPoint); var line = linePoints.ToGeometry(); var lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.DashDotDot, Colors.Maroon, 2); var lineGraphic = new Graphic(line, lineSymbol); MyOverlay.Graphics.Add(lineGraphic); //create text symbol for distance at midpoint of line var x = (line.Extent.XMin + line.Extent.XMax) / 2; var y = (line.Extent.YMin + line.Extent.YMax) / 2; var textPoint = new MapPoint(x, y); var text = String.Format("{0:0.00}", shortDist[i] / 1609.34) + " miles"; var textSymbol = new TextSymbol(text, Colors.Black, 15, Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Center, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Baseline); textSymbol.FontWeight = Esri.ArcGISRuntime.Symbology.FontWeight.Bold; //textSymbol.BackgroundColor = Colors.Maroon; //textSymbol.Angle = Math.Atan2(line.Extent.YMax - line.Extent.YMin, line.Extent.XMax - line.Extent.XMin) * 180.0 / Math.PI; var textGraphic = new Graphic(textPoint, textSymbol); MyOverlay.Graphics.Add(textGraphic); } }