Exemple #1
0
        private void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location.
            MapPoint tappedLocation = geoViewInputEventArgs.Location;

            // Show the tapped location.
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon.
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon.
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon.
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon.
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue.
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red.
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI.
            _distanceLabel.Text = $"Vertex dist: {distanceVertex} km, Point dist: {distanceCoordinate} km";
        }
        private void MapViewTapped(object sender, Esri.ArcGISRuntime.Xamarin.Forms.GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location
            MapPoint tappedLocation = geoViewInputEventArgs.Location;

            // Show the tapped location
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI
            ResultsLabel.Text = $"Vertex dist: {distanceVertex} km, Point dist: {distanceCoordinate} km";
        }
        // Accept user click point and find nearest target geometry point
        private async Task GetNearestCoordAsync(bool vertexOnly)
        {
            var target = _targetOverlay.Graphics.Select(g => g.Geometry).FirstOrDefault();

            if (target == null)
            {
                return;
            }

            txtInstruct.Text = "Click the map to find the nearest coordinate";
            var point = await MyMapView.Editor.RequestPointAsync();

            ProximityResult result = null;

            if (vertexOnly)
            {
                result = GeometryEngine.NearestVertex(target, point);
            }
            else
            {
                result = GeometryEngine.NearestCoordinate(target, point);
            }

            _coordinateOverlay.Graphics.Clear();
            _coordinateOverlay.Graphics.Add(new Graphic(point, _userPointSymbol));
            _coordinateOverlay.Graphics.Add(new Graphic(result.Point));

            txtResult.Visibility = Visibility.Visible;
            txtResult.Text       = string.Format("Nearest Point: Index: {0}, Distance: {1:0.000}", result.PointIndex, result.Distance);
        }
Exemple #4
0
        private async Task UpdateGeometry(MapPoint point)
        {
            if (_selectedFeature.Geometry is Polyline line)
            {
                // Get the nearest point on the selected line.
                ProximityResult nearestVertex = GeometryEngine.NearestVertex(line, point);

                // Create a new polyline.
                PolylineBuilder polylineBuilder = new PolylineBuilder(line);
                Part            part            = polylineBuilder.Parts[nearestVertex.PartIndex];

                // Replace the nearest point with the new point.
                part.SetPoint(nearestVertex.PointIndex, point);

                // Update the geometry of the feature.
                _selectedFeature.Geometry = GeometryEngine.Project(polylineBuilder.ToGeometry(), _selectedFeature.Geometry.SpatialReference);
                await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
            }
            else if (_selectedFeature.Geometry is MapPoint)
            {
                // Update the geometry of the feature.
                _selectedFeature.Geometry = point;
                await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
            }

            // Clear the selection.
            (_selectedFeature.FeatureTable.Layer as FeatureLayer).ClearSelection();
            _selectedFeature = null;
        }
Exemple #5
0
        private void MapViewTapped(object sender, GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location
            MapPoint tappedLocation = (MapPoint)GeometryEngine.NormalizeCentralMeridian(geoViewInputEventArgs.Location);

            // Show the tapped location
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI
            ResultsLabel.Content =
                string.Format("Vertex dist: {0} km, Point dist: {1} km", distanceVertex, distanceCoordinate);
        }
        // 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.Paths[0].Count;
            var        vertexPoint = segment.Paths[proximity.PartIndex][pointIndex];
            Coordinate 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.Paths[0].Count - 2;
            }
            else
            {
                Coordinate nextPoint = segment.Paths[0][pointIndex + 1];
                var        d1        = GeometryEngine.DistanceFromGeometry(new MapPoint(vertexPoint, segment.SpatialReference), new MapPoint(nextPoint, segment.SpatialReference));
                var        d2        = GeometryEngine.DistanceFromGeometry(location, new MapPoint(nextPoint, segment.SpatialReference));
                if (d1 < d2)
                {
                    onSegmentIndex = pointIndex - 1;
                }
                else
                {
                    onSegmentIndex = pointIndex;
                }
            }
            previousPoint = segment.Paths[0][0];
            for (int j = 1; j < onSegmentIndex + 1; j++)
            {
                Coordinate point = segment.Paths[0][j];
                distance1    += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference));
                previousPoint = point;
            }
            distance1    += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), location);
            previousPoint = segment.Paths[0][onSegmentIndex + 1];
            distance2     = GeometryEngine.DistanceFromGeometry(location, new MapPoint(previousPoint, segment.SpatialReference));
            previousPoint = vertexPoint;
            for (int j = onSegmentIndex + 2; j < segment.Paths[0].Count; j++)
            {
                Coordinate point = segment.Paths[0][j];
                distance2    += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference));
                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));
        }
        // 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));
        }
Exemple #8
0
        public static MapPoint GetNearestCoordinateInGraphicsCollection(MapPoint point, IList <Graphic> graphics)
        {
            ProximityResult nearest = null;
            MapPoint        loc     = point.ToWgs84(); // :)

            foreach (var graphic in graphics)
            {
                ProximityResult result = GeometryEngine.NearestCoordinate(graphic.Geometry, loc);
                if (nearest == null || result.Distance < nearest.Distance)
                {
                    nearest = result;
                }
            }

            return(nearest?.Coordinate);
        }
        // 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.Paths[0].Count;
            var vertexPoint = segment.Paths[proximity.PartIndex][pointIndex];
            Coordinate 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.Paths[0].Count - 2;
            else
            {
                Coordinate nextPoint = segment.Paths[0][pointIndex + 1];
                var d1 = GeometryEngine.DistanceFromGeometry(new MapPoint(vertexPoint, segment.SpatialReference), new MapPoint(nextPoint, segment.SpatialReference));
                var d2 = GeometryEngine.DistanceFromGeometry(location, new MapPoint( nextPoint, segment.SpatialReference));
                if (d1 < d2)
                    onSegmentIndex = pointIndex - 1;
                else
                    onSegmentIndex = pointIndex;
            }
            previousPoint = segment.Paths[0][0];
            for (int j = 1; j < onSegmentIndex + 1; j++)
            {
                Coordinate point = segment.Paths[0][j];
                distance1 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference));
                previousPoint = point;
            }
            distance1 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), location);
            previousPoint = segment.Paths[0][onSegmentIndex + 1];
            distance2 = GeometryEngine.DistanceFromGeometry(location, new MapPoint(previousPoint, segment.SpatialReference));
            previousPoint = vertexPoint;
            for (int j = onSegmentIndex + 2; j < segment.Paths[0].Count; j++)
            {
                Coordinate point = segment.Paths[0][j];
                distance2 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference));
                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);
        }
		// 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);
		}