private void RenderMgrsGridCell( DrawingContext pDC, GridZone pCell) { var view = Viewport.Extend; Point[] clipPolygon = new Point[] { new Point(pCell.Extend.WestBoundLongitude, pCell.Extend.NorthBoundLatitude), new Point(pCell.Extend.EastBoundLongitude, pCell.Extend.NorthBoundLatitude), new Point(pCell.Extend.EastBoundLongitude, pCell.Extend.SouthBoundLatitude), new Point(pCell.Extend.WestBoundLongitude, pCell.Extend.SouthBoundLatitude) }; int interval = (int)MgrsGridPrecision; // Number of meters int precision = (int)MgrsGridPrecision; var sw = pCell.SouthWest(); var ne = pCell.NorthEast(); var swUtm = sw.ConvertToUtm(); var neUtm = ne.ConvertToUtm(); var swMgrs = sw.ConvertToMgrs(); Debug.Assert(swUtm.Zone == neUtm.Zone, "UTM cell over multiple zones"); int utmZone = swUtm.Zone; double sw_utm_e = (Math.Floor(swUtm.East / precision) * precision); double sw_utm_n = (Math.Floor(swUtm.North / precision) * precision); double ne_utm_e = (Math.Floor(neUtm.East / precision) * precision); double ne_utm_n = (Math.Floor(neUtm.North / precision) * precision); int numberOfCells = (((int)(Math.Abs(sw_utm_n - ne_utm_n) / interval)) * ((int)(Math.Abs(sw_utm_e - ne_utm_e) / interval))); if (numberOfCells > 210) { return; // Safety check; takes to long to render } int count = 0; for (double currentNorth = sw_utm_n; currentNorth <= ne_utm_n; currentNorth += interval) { for (double currentEast = sw_utm_e; currentEast <= ne_utm_e; currentEast += interval) { count++; var lowerLeftLatLon = UTMtoLL(currentNorth, currentEast, utmZone, sw.Latitude >= 0 ? Geocentric.Hemisphere.North : Geocentric.Hemisphere.South); ; var upperRightLatLon = UTMtoLL(currentNorth + interval, currentEast + interval, utmZone, sw.Latitude >= 0 ? Geocentric.Hemisphere.North : Geocentric.Hemisphere.South); ; var lowerRightLatLon = UTMtoLL(currentNorth, currentEast + interval, utmZone, sw.Latitude >= 0 ? Geocentric.Hemisphere.North : Geocentric.Hemisphere.South); ; var upperLeftLatLon = UTMtoLL(currentNorth + interval, currentEast, utmZone, sw.Latitude >= 0 ? Geocentric.Hemisphere.North : Geocentric.Hemisphere.South); ; Point[] utm = new Point[] { new Point(upperLeftLatLon.Longitude, upperLeftLatLon.Latitude), new Point(upperRightLatLon.Longitude, upperRightLatLon.Latitude), new Point(lowerRightLatLon.Longitude, lowerRightLatLon.Latitude), new Point(lowerLeftLatLon.Longitude, lowerLeftLatLon.Latitude), }; var intersect = SutherlandHodgman.GetIntersectedPolygon(utm, clipPolygon); if ((intersect != null) && (intersect.Length >= 4)) { for (int i = 0; i < intersect.Length - 1; i++) { var p1 = Viewport.EsriMap.MapToScreen(WGS84LatLongPoint.Create(intersect[i].Y, intersect[i].X).ToEsriGeometry(102100)); var p2 = Viewport.EsriMap.MapToScreen(WGS84LatLongPoint.Create(intersect[i + 1].Y, intersect[i + 1].X).ToEsriGeometry(102100)); pDC.DrawLine(mMgrsLinePen, new Point(p1.X, p1.Y), new Point(p2.X, p2.Y)); } var bb = new MgrsBoundingBox(intersect); if (currentNorth + interval > ne_utm_n) RenderMgrsCellLabels(pDC, bb, currentNorth, currentEast, NorthEast.North); if (currentEast + interval > ne_utm_e) RenderMgrsCellLabels(pDC, bb, currentNorth, currentEast, NorthEast.East); RenderMgrsCenterLabel(pDC, bb, currentNorth, currentEast); } } } }
private System.Windows.Size CellSizeInPixels(MgrsBoundingBox pBoundingBox) { var upperRight = Viewport.EsriMap.MapToScreen(pBoundingBox.UpperRight.ToEsriGeometry(102100)); var lowerLeft = Viewport.EsriMap.MapToScreen(pBoundingBox.LowerLeft.ToEsriGeometry(102100)); return new System.Windows.Size(Math.Abs(lowerLeft.X - upperRight.X), Math.Abs(lowerLeft.Y - upperRight.Y)); }
private void RenderMgrsCellLabels(DrawingContext pDC, MgrsBoundingBox pBoundingBox, double pNorth, double pEast, NorthEast pDirection) { int numberOfDigits; switch (MgrsGridPrecision) { case EGridPrecision.Grid10km: numberOfDigits = 1; break; case EGridPrecision.Grid1km: numberOfDigits = 2; break; case EGridPrecision.Grid100m: numberOfDigits = 3; break; default: return; // Dont render label } var cellSize = CellSizeInPixels(pBoundingBox); if ((cellSize.Width > 30) && (cellSize.Height > 30)) { var eastFont = new FormattedText(pNorth.ToString().Substring(2, numberOfDigits), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, mTypefaceMgrsCellLabel, mFontSizeMgrsCellLabel, mFontColorMgrsCellLabel); var northFont = new FormattedText(pEast.ToString().Substring(1, numberOfDigits), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, mTypefaceMgrsCellLabel, mFontSizeMgrsCellLabel, mFontColorMgrsCellLabel); var lowerLeft = Viewport.EsriMap.MapToScreen(pBoundingBox.LowerLeft.ToEsriGeometry(102100)); int marge = 3; if (pDirection == NorthEast.East) { // Horizontal labels var rectNorth = GetBoundingRect(northFont); rectNorth.Inflate(marge * 2, marge * 2); var northPoint = new Point(lowerLeft.X + (cellSize.Width / 2) - (rectNorth.Width / 2), lowerLeft.Y - (rectNorth.Height / 2)); pDC.DrawRoundedRectangle(mFontColorMgrsFill, mMgrsLabelLinePen, new Rect(northPoint, rectNorth.Size), 2, 2); pDC.DrawText(eastFont, new Point(northPoint.X + marge, northPoint.Y + marge)); } if (pDirection == NorthEast.North) { // Vertical var rectEast = GetBoundingRect(eastFont); rectEast.Inflate(marge * 2, marge * 2); var eastPoint = new Point(lowerLeft.X - (rectEast.Width / 2), lowerLeft.Y - (cellSize.Height / 2) - (rectEast.Height / 2)); pDC.DrawRoundedRectangle(mFontColorMgrsFill, mMgrsLabelLinePen, new Rect(eastPoint, rectEast.Size), 2, 2); pDC.DrawText(northFont, new Point(eastPoint.X + marge, eastPoint.Y + marge)); } } }
private void RenderMgrsCenterLabel(DrawingContext pDC, MgrsBoundingBox pBoundingBox, double pNorth, double pEast) { if (!Cfg.DrawCenterMgrsGridLabel) return; var cellSize = CellSizeInPixels(pBoundingBox); var center = pBoundingBox.CenterOfMass(); var c = center.ConvertToMgrs(); var p3 = Viewport.EsriMap.MapToScreen(center.ToEsriGeometry(102100)); int fontSize; string label; if (cellSize.Width < 100) { fontSize = 16; label = c.Grid; } else if (cellSize.Width < 150) { fontSize = 18; label = c.Grid; } else { fontSize = 20; label = c.Zoneband + " " + c.Grid; } var ft = new FormattedText(label, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, mTypefaceMgrsCellCenterLabel, fontSize, mFontColorMgrsCellCenterLabel); //pDC.DrawText(ft, new Point(p3.X - (ft.Width / 2), p3.Y - (ft.Height / 2))); System.Windows.Media.Geometry textGeometry = ft.BuildGeometry(new Point(p3.X - (ft.Width / 2), p3.Y - (ft.Height / 2))); pDC.DrawGeometry(Brushes.White, new Pen(Brushes.Black, 1), textGeometry); }