/// <summary> /// Updates hidden polygon segment lines to aid when knowing the mouse is over a polygon line. /// </summary> /// <param name="polygon">Polygon to update the line segments for</param> private void UpdatePolygonLines(PolygonViewModel polygon) { // Clear the previous line segments polygon.LineSegments.Clear(); // Loop over the points int previousPoint = 0; for (int index = 1; index < polygon.PointCollection.Count; index++, previousPoint++) { // Create a polygon line segment from the previous point to the next point CreatePolygonLineSegment( polygon, polygon.PointCollection[previousPoint], polygon.PointCollection[index]); // If this is the last point then... if (index == polygon.PointCollection.Count - 1) { // Create a segment back to the first point CreatePolygonLineSegment( polygon, polygon.PointCollection[index], polygon.PointCollection[0]); } } }
private void DrawFill(Graphics graphics, PolygonViewModel viewModel) { if (viewModel?.Items == null || viewModel.Items.Count == 0) { return; } var points = TranslateAndScale(viewModel.Items); var path = new GraphicsPath(FillMode.Winding); for (var i = 0; i < points.Count; i++) { var p1 = points[i % points.Count]; var p2 = points[(i + 1) % points.Count]; path.AddLine( new PointF((float)p1.X, (float)p1.Y), new PointF((float)p2.X, (float)p2.Y)); } using (var brush = new SolidBrush(viewModel.EdgeColor)) { graphics.FillPath(brush, path); } }
public MainPage() { InitializeComponent(); polygonViewModel = new PolygonViewModel(); BindingContext = polygonViewModel; map.MoveToRegion(MapSpan.FromCenterAndRadius( new Position(51, 09), Distance.FromMiles(50))); }
private void DrawVertices(Graphics graphics, PolygonViewModel viewModel) { if (viewModel?.Items == null || viewModel.Items.Count == 0) { return; } var points = TranslateAndScale(viewModel.Items); // Draw edges var count = viewModel.IsOpen ? points.Count - 1 : points.Count; using (var pen = new Pen(viewModel.EdgeColor)) { for (var i = 0; i < count; i++) { var point = points[i]; var next = points[(i + 1) % points.Count]; graphics.DrawLine(pen, (float)point.X, (float)point.Y, (float)next.X, (float)next.Y); } } // Draw vertex markers using (var brush = new SolidBrush(viewModel.VertexColor)) { for (var i = 0; i < points.Count; i++) { var point = points[i]; var vertex = viewModel.Items[i]; var rect = new RectangleF((float)point.X - 3, (float)point.Y - 3, 6, 6); graphics.FillRectangle(brush, rect.X, rect.Y, rect.Width, rect.Height); // Draw vertex label var label = $"({vertex.X:####0.0}, {vertex.Y:####0.0})"; var labelSize = graphics.MeasureString(label, Font); graphics.DrawString(label, Font, Brushes.White, (float)point.X - labelSize.Width / 2, (float)point.Y + labelSize.Height / 2); } } }
/// <summary> /// Creates a polygon line segment. /// </summary> /// <param name="polygon">Polygon to create the line segment for</param> /// <param name="previousPoint">Previous polygon point</param> /// <param name="nextPoint">Next polygon point</param> private void CreatePolygonLineSegment(PolygonViewModel polygon, PolygonPointViewModel previousPoint, PolygonPointViewModel nextPoint) { PolygonLineSegment segment = new PolygonLineSegment(); segment.Line.Visibility = Visibility.Visible; segment.Line.Opacity = 0.0; segment.Line.StrokeThickness = 4; segment.Line.Stroke = Brushes.DodgerBlue; segment.Line.X1 = previousPoint.X; segment.Line.Y1 = previousPoint.Y; segment.Line.X2 = nextPoint.X; segment.Line.Y2 = nextPoint.Y; segment.StartPoint = previousPoint; segment.EndPoint = nextPoint; canvas.Children.Add(segment.Line); polygon.LineSegments.Add(segment); }
/// <summary> /// Returns true if the mouse is over a polygon line. /// </summary> /// <param name="mousePosition">Mouse position</param> /// <param name="polygonLineSegment">Polygon line segment the mouse is over</param> /// <param name="polygonParent">Polygon the line segment belongs to</param> /// <returns></returns> private bool IsMouseOverLine(Point mousePosition, // ReSharper disable once RedundantAssignment ref PolygonLineSegment polygonLineSegment, // ReSharper disable once RedundantAssignment ref PolygonViewModel polygonParent) { // Default to NOT being over a polygon line bool mouseOverLine = false; polygonLineSegment = null; polygonParent = null; // No need to do this processing if the editor is not in // add polygon point mode if (VM.AddPoint) { // Update all the polygon lines to make sure they are accurate UpdatePolygonLines(); // Loop over all the polygons foreach (PolygonViewModel polygon in VM.Polygons) { // Loop over all the line segments foreach (PolygonLineSegment segment in polygon.LineSegments) { // If the mouse is over a line then... if (segment.Line.IsMouseOver) { // Return the line segment polygonLineSegment = segment; // Return the polygon which contains the line segment polygonParent = polygon; // Indicate that the mouse is over a polygon line segment mouseOverLine = true; } } } } // Return whether the mouse is over a polygon line segment return(mouseOverLine); }
// GET: Polygon public ActionResult Index() { var latStr = Request.QueryString["lat"]; var lngStr = Request.QueryString["lng"]; if (string.IsNullOrEmpty(latStr) || string.IsNullOrEmpty(lngStr) || !double.TryParse(latStr, out double lat) || !double.TryParse(lngStr, out double lng)) { return(null); } var index = ContentSearchManager.GetIndex("sc9xp0_spatial_master"); using (var context = index.CreateSearchContext()) { var queryable = context.GetQueryable <PolygonResultItem>().InsidePolygon(s => s.Polygon, new Coordinate(lat, lng)); var list = queryable.ToList(); if (list.Any()) { var res = list.First(); var multiPoly = JsonConvert.DeserializeObject <MultiPolygon>(res.PolygonResult); var featureCol = new FeatureCollection(); var feature = new Feature(multiPoly); featureCol.Features.Add(feature); var viewModel = new PolygonViewModel { PolygonsFeature = featureCol }; return(this.View("~/Views/Polygon.cshtml", viewModel)); } } return(null); }
public MainWindow() { InitializeComponent(); _polygonViewModel = new PolygonViewModel((PolygonsSource)this.Resources["Items"], CollectionViewSource.GetDefaultView(MainDataGrid.ItemsSource)); this.DataContext = _polygonViewModel; }
public static bool ReadPolygonFromFile(string filePath, out PolygonViewModel polygon) { #region Line reading methods (double longitute, double latitude)? ReadPointFromLine(string line) { // Split line into parts and trim both string[] point = line.Split(',').Select(x => x.Trim(' ')).ToArray(); if (point.Length == 2 && double.TryParse(point[0], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out double longitude) && double.TryParse(point[1], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out double latitude)) { return(longitude, latitude); } return(null); } (string attributeKey, string attributeValue)? ReadAttributeFromLine(string line) { string[] pair = line.Split(':').Select(x => x.Trim(' ')).ToArray(); if (pair.Length == 2) { return(pair[0], pair[1]); } return(null); } #endregion string[] textPolygon = _fSHelper.ReadLines(filePath); PolygonViewModel polygonVM = new PolygonViewModel() { Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue), Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green), StrokeThickness = 2, Opacity = 0.6, }; try { foreach (string line in textPolygon) { if (!string.IsNullOrEmpty(line)) { if (char.IsDigit(line[0])) { var point = ReadPointFromLine(line); if (point.HasValue) { polygonVM.Locations.Add(new Location(point.Value.latitude, point.Value.longitute)); } } else { var keyValue = ReadAttributeFromLine(line); if (keyValue.HasValue) { if (keyValue.Value.attributeKey.ToLower() == "name") { polygonVM.Title = keyValue.Value.attributeValue; } else if (keyValue.Value.attributeKey.ToLower() == "alias") { polygonVM.Alias = keyValue.Value.attributeValue; } } } } } polygon = polygonVM; return(true); } catch (Exception e) { Debug.WriteLine(e); polygon = null; return(false); } }