public void Remove(GeomCoordinate coordinate) { if (_values != null) { _values.Remove(coordinate); } }
public HashItem[] FindNearestVertex(GeomCoordinate pt) { var list = new NearestSet(); try { _lockVr.EnterReadLock(); if (_rVertexTree.NodeCount == 0) { return(list.ToArray()); } var res = _rVertexTree.Distance(pt, CoordinateTolerance); foreach (var node in res) { var row = (MapDb.VertexesRow)node.Row; var coordinate = new GeomCoordinate(row.Longitude, row.Latitude); var distance = coordinate.Distance(pt); list.Add(new HashItem(distance, row.ID)); } } finally { _lockVr.ExitReadLock(); } return(list.OrderBy(item => item.Key).ToArray()); }
/// <summary> /// Translate latitude to Y coordinate of the Tile level bitmap /// </summary> public static long GetY(GeomCoordinate coordinate, int level) { const double d2R = Math.PI / 180; var z = (1 + Math.Sin(coordinate.Latitude * d2R)) / (1 - Math.Sin(coordinate.Latitude * d2R)); return((long)(Math.Floor(BitmapOrigo(level) - 0.5 * Math.Log(z) * PixelsPerRadian(level)))); }
private bool FindMapObjects(Point location, out HashItem[] vertexRows, out HashItem[] cableRows) { HashItem[] vertexR = null; HashItem[] cableR = null; try { var coordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, location); var tasks = new List <Task> { Task.Factory.StartNew(() => vertexR = _netLayer.FindNearestVertex(coordinate)), Task.Factory.StartNew(() => cableR = _netLayer.FindNearestCable(coordinate)) }; Task.WaitAll(tasks.ToArray()); } catch (Exception) { vertexR = null; cableR = null; } vertexRows = vertexR; cableRows = cableR; return((vertexRows != null && vertexRows.Length > 0) || (cableRows != null && cableRows.Length > 0)); }
public HashItem[] FindNearestCable(GeomCoordinate pt) { var list = new NearestSet(); try { _lockCr.EnterReadLock(); if (_rCableTree.NodeCount == 0) { return(list.ToArray()); } var res = _rCableTree.Distance(pt, CoordinateTolerance); foreach (var node in res) { var row = (MapDb.CablesRow)node.Row; var cableRect = new CoordinateRectangle(row.Longitude1, row.Latitude1, row.Longitude2, row.Latitude2); var distance = cableRect.LineDistance(pt); list.Add(new HashItem(distance, row.ID)); } } finally { _lockCr.ExitReadLock(); } return(list.OrderBy(item => item.Key).ToArray()); }
public void Add(GeomCoordinate coordinate) { if (_values == null) { _values = new List <GeomCoordinate>(); } _values.Add(coordinate); }
public ShapeLayer(int width, int height, GeomCoordinate centerCoordinate, int level) : base(width, height, centerCoordinate, level) { CoordinateTolerance = 0; _rVertexTree = new VertexTree(); _rCableTree = new CableTree(); }
public void MoveCenterMapObject(decimal longitude, decimal latitude) { if (_mapLayer.Terminating) { return; } CenterCoordinate = new GeomCoordinate(longitude, latitude); }
private void DownloadThread(CancellationToken ct) { var leftBound = new GeomCoordinate(Properties.Settings.Default.LeftMapBound, Properties.Settings.Default.TopMapBound); var rightBound = new GeomCoordinate(Properties.Settings.Default.RightMapBound, Properties.Settings.Default.BottomMapBound); var rectBound = new CoordinateRectangle(leftBound, rightBound); var mapBlockCount = 0; for (var mapLevel = Properties.Settings.Default.MinZoomLevel; mapLevel <= Properties.Settings.Default.MaxZoomLevel; mapLevel++) { var mapWidth = Convert.ToInt32((new ScreenCoordinate(rectBound.RightTop, mapLevel)).X - (new ScreenCoordinate(rectBound.LeftTop, mapLevel)).X) + 2 * TileBlock.BlockSize; var mapHeight = Convert.ToInt32((new ScreenCoordinate(rectBound.LeftBottom, mapLevel)).Y - (new ScreenCoordinate(rectBound.LeftTop, mapLevel)).Y) + 2 * TileBlock.BlockSize; var viewBound = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, mapLevel); var blockView = viewBound.BlockView; mapBlockCount += (blockView.Right - blockView.Left + 1) * (blockView.Bottom - blockView.Top + 1); } var mapBlockNumber = 0; BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount }); for (var mapLevel = Properties.Settings.Default.MinZoomLevel; mapLevel <= Properties.Settings.Default.MaxZoomLevel; mapLevel++) { var mapWidth = Convert.ToInt32((new ScreenCoordinate(rectBound.RightTop, mapLevel)).X - (new ScreenCoordinate(rectBound.LeftTop, mapLevel)).X) + 2 * TileBlock.BlockSize; var mapHeight = Convert.ToInt32((new ScreenCoordinate(rectBound.LeftBottom, mapLevel)).Y - (new ScreenCoordinate(rectBound.LeftTop, mapLevel)).Y) + 2 * TileBlock.BlockSize; var viewBound = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, mapLevel); var blockView = viewBound.BlockView; for (var x = blockView.Left; x <= blockView.Right; x++) { for (var y = blockView.Top; y <= blockView.Bottom; y++) { var block = new TileBlock(x, y, mapLevel); var fileName = Properties.Settings.GetMapFileName(block); if (!File.Exists(fileName)) { TileLayer.DownloadImageFromTile(block, false); } mapBlockNumber++; BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount }); if (ct.IsCancellationRequested) { return; } } } } BeginInvoke(ProgressEvent, new Object[] { 101, mapBlockNumber, mapBlockCount }); }
public void Distance(HashSet <ISpatialTreeNode> hashSet, GeomCoordinate coordinate, double variance, SpatialQueryIterator i) { //Query elements on the map close to coordinate (indexed search) lock (this) { if (!IsBottomSheet) { if (Sheets.HasChilds) { foreach (var sheet in Sheets.Values) { i.Next(); if (sheet.Rectangle.RectangeDistance(coordinate) <= variance) { sheet.Distance(hashSet, coordinate, variance, i); } } } } else if (Content.HasChilds) { foreach (var node in Content.Values) { i.Next(); double distance = -1; switch (node.NodeType) { case SpatialTreeNodeTypes.Point: distance = node.Coordinate.Distance(coordinate); break; case SpatialTreeNodeTypes.Line: distance = node.Rectangle.LineDistance(coordinate); break; case SpatialTreeNodeTypes.Rectangle: distance = node.Rectangle.RectangeDistance(coordinate); break; case SpatialTreeNodeTypes.Poligon: distance = node.Poligon.PoligonDistance(coordinate); break; } if (distance >= 0 && distance <= variance) { hashSet.Add(node); } } } } }
protected override bool SetCenterCoordinate(GeomCoordinate center, int level) { var res = base.SetCenterCoordinate(center, level); if (res) { DropWorkerThreadEvents(WorkerEventType.DownloadImage); } return(res); }
private void GetFullMapThread(CancellationToken ct) { var leftBound = new GeomCoordinate(Properties.Settings.Default.LeftMapBound, Properties.Settings.Default.TopMapBound); var rightBound = new GeomCoordinate(Properties.Settings.Default.RightMapBound, Properties.Settings.Default.BottomMapBound); var rectBound = new CoordinateRectangle(leftBound, rightBound); try { var mapWidth = Convert.ToInt32((new ScreenCoordinate(rectBound.RightTop, _mapLevel)).X - (new ScreenCoordinate(rectBound.LeftTop, _mapLevel)).X) + 2 * TileBlock.BlockSize; var mapHeight = Convert.ToInt32((new ScreenCoordinate(rectBound.LeftBottom, _mapLevel)).Y - (new ScreenCoordinate(rectBound.LeftTop, _mapLevel)).Y) + 2 * TileBlock.BlockSize; var image = GraphicLayer.CreateCompatibleBitmap(null, mapWidth, mapHeight, _mapPiFormat); var graphics = Graphics.FromImage(image); var viewBound = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, _mapLevel); var blockView = viewBound.BlockView; var mapBlockCount = (blockView.Right - blockView.Left + 1) * (blockView.Bottom - blockView.Top + 1); var mapBlockNumber = 0; BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount }); for (var x = blockView.Left; x <= blockView.Right; x++) { for (var y = blockView.Top; y <= blockView.Bottom; y++) { var block = new TileBlock(x, y, _mapLevel); var bmp = GraphicLayer.CreateCompatibleBitmap( TileLayer.DownloadImageFromFile(block) ?? TileLayer.DownloadImageFromTile(block, true), TileBlock.BlockSize, TileBlock.BlockSize, _mapPiFormat); var rect = ((ScreenRectangle)block).GetScreenRect(viewBound); graphics.DrawImageUnscaled(bmp, rect.Location.X, rect.Location.Y); mapBlockNumber++; BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount }); if (ct.IsCancellationRequested) { return; } } } BeginInvoke(SaveMapEvent, new Object[] { image }); } catch (Exception e) { BeginInvoke(ProgressEvent, new Object[] { 101, 0, 0 }); MessageBox.Show(e.Message, @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public GraphicLayer(int pWidth, int pHeight, GeomCoordinate centerCoordinate, int pLevel) { Width = pWidth; Height = pHeight; _centerCoordinate = centerCoordinate; _level = pLevel; PiFormat = ObtainCompatiblePixelFormat(); Resize(false); _drawLayerEvent += OnIvalidateLayer; }
protected virtual bool SetCenterCoordinate(GeomCoordinate center, int level) { if (_level != level || new ScreenCoordinate(_centerCoordinate, level).CompareTo( new ScreenCoordinate(center, level)) != 0) { _centerCoordinate = center; _level = level; TranslateCoords(); Update(new Rectangle(0, 0, Width, Height)); return(true); } return(false); }
public HashSet <ISpatialTreeNode> Distance(GeomCoordinate coordinate, double variance) { //for debug to see how many iterations used for a search var i = SpatialQueryIterator.Start(); var res = new HashSet <ISpatialTreeNode>(); _root.Distance(res, coordinate, variance, i); //index turning System.Diagnostics.Trace.WriteLine(string.Format("{5} Level nodes {1} {2} {3} {4}, Distance iterations - {0:d}", i.Value, NodeDimension[0], NodeDimension[1], NodeDimension[2], NodeDimension[3], typeof(TNode).Name)); return(res); }
private void MapCtl_DoubleClick(object sender, EventArgs e) { if (_mapLayer.Terminating) { return; } HashItem[] vertexRows; HashItem[] cableRows; if (!ShiftKey && !FindMapObjects(_mousePreviousLocation, out vertexRows, out cableRows)) { CenterCoordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, _mousePreviousLocation); } }
/* * * public static GeomCoordinate GetCentralGeoCoordinate(GeomCoordinate posA, GeomCoordinate posB) //IList<GeomCoordinate> geoCoordinates) * { * IList<GeomCoordinate> geoCoordinates = new List<GeomCoordinate> * { * posA, * posB * }; * * //if (geoCoordinates.Count == 1) * //{ * // return geoCoordinates.Single(); * //} * * double x = 0; * double y = 0; * double z = 0; * foreach (var geoCoordinate in geoCoordinates) * { * var latitude = geoCoordinate.Latitude * Math.PI / 180; * var longitude = geoCoordinate.Longitude * Math.PI / 180; * x += Math.Cos(latitude) * Math.Cos(longitude); * y += Math.Cos(latitude) * Math.Sin(longitude); * z += Math.Sin(latitude); * } * var total = geoCoordinates.Count; * x = x / total; * y = y / total; * z = z / total; * var centralLongitude = Math.Atan2(y, x); * var centralSquareRoot = Math.Sqrt(x * x + y * y); * var centralLatitude = Math.Atan2(z, centralSquareRoot); * return new GeomCoordinate(centralLatitude * 180 / Math.PI, centralLongitude * 180 / Math.PI); * } * public static GeomCoordinate MidPoint(GeomCoordinate posA, GeomCoordinate posB) * { * GeomCoordinate midPoint = new GeomCoordinate(0d,0d); * * double dLon = DegreesToRadians(posB.Longitude - posA.Longitude); * double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon); * double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon); * * midPoint.Latitude = RadiansToDegrees(Math.Atan2( * Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)), * Math.Sqrt( * (Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) * * (Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) + By * By))); * * midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx)); * * return midPoint; * } * * private static double DegreesToRadians(double angle) * { * return Math.PI * angle / 180.0; * } * * public static double RadiansToDegrees(double radians) * { * double degrees = (180 / Math.PI) * radians; * return (degrees); * } */ /// <summary> /// Line middle point /// </summary> public static GeomCoordinate GetMiddlePoint(GeomCoordinate c1, GeomCoordinate c2) { const double d2R = Math.PI / 180; const double r2D = 180 / Math.PI; var dLon = d2R * (c2.Longitude - c1.Longitude); var c1Rlat = d2R * (c1.Latitude); var c2Rlat = d2R * (c2.Latitude); var bX = Math.Cos(c2Rlat) * Math.Cos(dLon); var bY = Math.Cos(c2Rlat) * Math.Sin(dLon); var longitude = Math.Round(c1.Longitude + r2D * (Math.Atan2(bY, Math.Cos(c1Rlat) + bX)), 5); var latitude = Math.Round(r2D * (Math.Atan2(Math.Sin(c1Rlat) + Math.Sin(c2Rlat), Math.Sqrt((Math.Cos(c1Rlat) + bX) * (Math.Cos(c1Rlat) + bX) + bY * bY))), 5); return(new GeomCoordinate(longitude, latitude)); }
private void MapCtl_MouseMove(object sender, MouseEventArgs e) { if (_mapLayer.Terminating) { return; } CursorCoordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, new Point(e.X, e.Y)); if (_mouseMoveMap) { var deltaX = _mousePreviousLocation.X - e.X; var deltaY = _mousePreviousLocation.Y - e.Y; CenterCoordinate = _coordinatePreviosLocation + new ScreenCoordinate(deltaX, deltaY, Level); } else { if (String.IsNullOrEmpty(toolTip1.GetToolTip(this))) { HashItem[] vertexRows; HashItem[] cableRows; FindMapObjects(e.Location, out vertexRows, out cableRows); if (vertexRows.Length > 0) { var vertex = _netLayer.GetVertex(vertexRows[0].Value); if (vertex != null) { toolTip1.RemoveAll(); toolTip1.Show(vertex.Caption, this, e.Location, 3000); } } else if (cableRows.Length > 0) { var cable = _netLayer.GetCable(cableRows[0].Value); if (cable != null) { toolTip1.RemoveAll(); toolTip1.Show(cable.Caption, this, e.Location, 3000); } } } } }
private void GenerateSampleData() { _netLayer.ClearData(); //--sample for show smothness var rnd = new Random(); var rangeX = Convert.ToInt32((Settings.Default.RightMapBound - Settings.Default.LeftMapBound) * 100000); var rangeY = Convert.ToInt32((Settings.Default.TopMapBound - Settings.Default.BottomMapBound) * 100000); var longitude1 = Convert.ToDecimal(Settings.Default.LeftMapBound + (double)rnd.Next(0, rangeX) / 100000); var latitude1 = Convert.ToDecimal(Settings.Default.BottomMapBound + (double)rnd.Next(0, rangeY) / 100000); var cableDbRows = new MapDb.CablesDataTable(); var vertexDbRows = new MapDb.VertexesDataTable(); while (cableDbRows.Count < 200) { var cableRow = cableDbRows.NewCablesRow(); cableRow.Longitude1 = longitude1; cableRow.Latitude1 = latitude1; cableRow.Longitude2 = Convert.ToDecimal(Settings.Default.LeftMapBound + (double)rnd.Next(0, rangeX) / 100000); cableRow.Latitude2 = Convert.ToDecimal(Settings.Default.BottomMapBound + (double)rnd.Next(0, rangeY) / 100000); var rect = new CoordinateRectangle(cableRow.Longitude1, cableRow.Latitude1, cableRow.Longitude2, cableRow.Latitude2); cableRow.Length = Convert.ToDecimal(rect.LineLength); if (cableRow.Length <= 5000 && cableRow.Length > 200) { longitude1 = cableRow.Longitude2; latitude1 = cableRow.Latitude2; cableRow.Caption = rect.ToString(); cableDbRows.AddCablesRow(cableRow); var vertexRow = vertexDbRows.NewVertexesRow(); vertexRow.Longitude = longitude1; vertexRow.Latitude = latitude1; var pt = new GeomCoordinate(vertexRow.Longitude, vertexRow.Latitude); vertexRow.Caption = pt.ToString(); vertexDbRows.AddVertexesRow(vertexRow); } } _netLayer.MergeData(vertexDbRows); _netLayer.MergeData(cableDbRows); //--end sample }
private void RedrawVertexes(ScreenRectangle localScreenView) { try { _lockVr.EnterReadLock(); if (_rVertexTree.NodeCount == 0) { return; } var res = _rVertexTree.Query(localScreenView); foreach (var node in res) { var row = (MapDb.VertexesRow)node.Row; var coordinate = new GeomCoordinate(row.Longitude, row.Latitude); var pt = coordinate.GetScreenPoint(localScreenView); DrawBitmap(Vertex, Point.Subtract(pt, HalfVertexSize)); if (Level >= 14) { var caption = row.Caption; if (!String.IsNullOrEmpty(caption)) { DrawString(caption, HalfVertexSize.Height, Point.Add(pt, HalfVertexSize)); } } } } catch (Exception ex) { //do nothing System.Diagnostics.Trace.WriteLine(ex.Message); } finally { _lockVr.ExitReadLock(); } }
public TileLayer(int width, int height, GeomCoordinate centerCoordinate, int level) : base(width, height, centerCoordinate, level) { _emptyBlock = CreateCompatibleBitmap(null, TileBlock.BlockSize, TileBlock.BlockSize, PiFormat); }
/// <summary> /// Get Tile bitmap block number Y by latitude /// </summary> public static long GetNumBlockY(GeomCoordinate coordinate, int level) { return((long)Math.Floor((double)GetY(coordinate, level) / TileBlock.BlockSize)); }
private void MapCtl_MouseDown(object sender, MouseEventArgs e) { if (_mapLayer.Terminating) { return; } if (_mouseMoveMap) { return; } if (e.Button == MouseButtons.Left) { CursorCoordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, new Point(e.X, e.Y)); HashItem[] vertexRows; HashItem[] cableRows; if (!FindMapObjects(e.Location, out vertexRows, out cableRows)) { _mousePreviousLocation = new Point(e.X, e.Y); if (ShiftKey) { MapCtl_MouseWheel(this, new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, 1)); CenterCoordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, _mousePreviousLocation); } else { _mouseMoveMap = true; _coordinatePreviosLocation = CenterCoordinate; } } else { if (vertexRows.Length > 0) { var vertex = _netLayer.GetVertex(vertexRows[0].Value); if (vertex != null) { MessageBox.Show(vertex.Caption, @"Vertex found!"); } } else if (cableRows.Length > 0) { var cable = _netLayer.GetCable(cableRows[0].Value); if (cable != null) { MessageBox.Show(cable.Caption, @"Cable found!"); } } } } else if (e.Button == MouseButtons.Right) { HashItem[] vertexRows; HashItem[] cableRows; if (!FindMapObjects(e.Location, out vertexRows, out cableRows)) { _mousePreviousLocation = new Point(e.X, e.Y); if (ShiftKey) { MapCtl_MouseWheel(this, new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, -1)); CenterCoordinate = GeomCoordinate.GetCoordinateFromScreen(_netLayer.ScreenView, _mousePreviousLocation); } } else { var menu = new ContextMenu(); foreach (var row in vertexRows) { var vertex = _netLayer.GetVertex(row.Value); var item = menu.MenuItems.Add(String.Format(@"Delete Vertex: {0}", vertex.Caption), MapCtl_DeleteVertexClick); item.Tag = row.Value; } foreach (var row in cableRows) { var cable = _netLayer.GetCable(row.Value); var item = menu.MenuItems.Add(String.Format(@"Delete Cable: {0}", cable.Caption), MapCtl_DeleteCableClick); item.Tag = row.Value; } menu.Show(this, e.Location); } } }
/// <summary> /// Translate longitude to X coordinate of the Tile level bitmap /// </summary> public static long GetX(GeomCoordinate coordinate, int level) { return((long)(Math.Floor(BitmapOrigo(level) + coordinate.Longitude * PixelsPerDegree(level)))); }
public ScreenCoordinate(GeomCoordinate coordinate, int level) { X = MapUtilities.GetX(coordinate, level); Y = MapUtilities.GetY(coordinate, level); Level = level; }
public IntersectResult PointContains(GeomCoordinate point) { return(((CoordinateRectangle)this).PointContains(point)); }