예제 #1
0
        /// <summary>
        /// return local coordinates from lat/lng
        /// </summary>
        /// <param name="latlng"></param>
        /// <returns></returns>
        public GPoint FromLatLngToLocal(PointLatLng latlng)
        {
            GPoint pLocal = Provider.Projection.FromLatLngToPixel(latlng, Zoom);

            pLocal.Offset(renderOffset);
            pLocal.OffsetNegative(compensationOffset);
            return(pLocal);
        }
예제 #2
0
        /// <summary>
        /// gets lat/lng from local control coordinates
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public PointLatLng FromLocalToLatLng(long x, long y)
        {
            GPoint p = new GPoint(x, y);

            p.OffsetNegative(renderOffset);
            p.Offset(compensationOffset);

            return(Provider.Projection.FromPixelToLatLng(p, Zoom));
        }
        public override void OnRender(IGraphics g)
        {
            if (colorbrushs == null)
            {
                colorbrushs = new SolidBrush[color.Length];
                int a = 0;
                foreach (var color1 in color)
                {
                    colorbrushs[a] = new SolidBrush(Color.FromArgb(140, color1.R, color1.G, color1.B));
                    a++;
                }
            }

            DateTime start = DateTime.Now;
            var      pos   = Overlay.Control.FromLatLngToLocal(Position);

            pos.Offset(-LocalPosition.X, -LocalPosition.Y);

            double width =
                (Overlay.Control.MapProvider.Projection.GetDistance(Overlay.Control.FromLocalToLatLng(0, 0),
                                                                    Overlay.Control.FromLocalToLatLng(Overlay.Control.Width, 0)) * 1000.0);
            double m2pixelwidth = Overlay.Control.Width / width;

            Rect screenRect = new Rect(Overlay.Control.Width / -2, Overlay.Control.Height / -2, Overlay.Control.Width,
                                       Overlay.Control.Height);

            int skipped = 0;

            foreach (var pg in overlapCount)
            {
                GPoint p = Overlay.Control.FromLatLngToLocal(pg.Key);

                p.Offset(-pos.X, -pos.Y);

                if (p.X < screenRect.Left || p.X > screenRect.Right ||
                    p.Y < screenRect.Top || p.Y > screenRect.Bottom)
                {
                    skipped++;
                    continue;
                }

                var col = Math.Min(pg.Value - 1, 7);

                var coloruse = colorbrushs[col] as SolidBrush;

                var widthc = 5 * m2pixelwidth;

                var halfwidthc = widthc / 2.0f;

                g.FillPie(coloruse, (float)(p.X - halfwidthc), (float)(p.Y - halfwidthc), (float)(widthc),
                          (float)(widthc), 0, 360);
            }

            drawLegend(g);
            Console.WriteLine("OnRender " + (DateTime.Now - start));
        }
예제 #4
0
 /// <summary>
 /// updates marker position, internal access usualy
 /// </summary>
 void UpdateLocalPosition()
 {
     if (Map != null)
     {
         GPoint p = Map.FromLatLngToLocal(Position);
         p.Offset(-(long)Map.MapTranslateTransform.X, -(long)Map.MapTranslateTransform.Y);
         LocalPositionX = (int)(p.X + (long)(Offset.X));
         LocalPositionY = (int)(p.Y + (long)(Offset.Y));
     }
 }
예제 #5
0
        /// <summary>
        /// moves current position into map center
        /// </summary>
        internal void GoToCurrentPositionOnZoom()
        {
            compensationOffset = positionPixel; // TODO: fix

            // reset stuff
            renderOffset = GPoint.Empty;
            dragPoint    = GPoint.Empty;

            // goto location and centering
            if (MouseWheelZooming)
            {
                if (MouseWheelZoomType != MouseWheelZoomType.MousePositionWithoutCenter)
                {
                    GPoint pt = new GPoint(-(positionPixel.X - Width / 2), -(positionPixel.Y - Height / 2));
                    pt.Offset(compensationOffset);
                    renderOffset.X = pt.X - dragPoint.X;
                    renderOffset.Y = pt.Y - dragPoint.Y;
                }
                else // without centering
                {
                    renderOffset.X = -positionPixel.X - dragPoint.X;
                    renderOffset.Y = -positionPixel.Y - dragPoint.Y;
                    renderOffset.Offset(mouseLastZoom);
                    renderOffset.Offset(compensationOffset);
                }
            }
            else // use current map center
            {
                mouseLastZoom = GPoint.Empty;

                GPoint pt = new GPoint(-(positionPixel.X - Width / 2), -(positionPixel.Y - Height / 2));
                pt.Offset(compensationOffset);
                renderOffset.X = pt.X - dragPoint.X;
                renderOffset.Y = pt.Y - dragPoint.Y;
            }

            UpdateCenterTileXYLocation();
        }
        /// <summary>
        ///     moves current position into map center
        /// </summary>
        internal void GoToCurrentPositionOnZoom()
        {
            CompensationOffset = _positionPixel; // TODO: fix

            // reset stuff
            RenderOffset = GPoint.Empty;
            _dragPoint   = GPoint.Empty;

            // goto location and centering
            if (MouseWheelZooming)
            {
                if (MouseWheelZoomType != MouseWheelZoomType.MousePositionWithoutCenter)
                {
                    var pt = new GPoint(-(_positionPixel.X - Width / 2), -(_positionPixel.Y - Height / 2));
                    pt.Offset(CompensationOffset);
                    RenderOffset.X = pt.X - _dragPoint.X;
                    RenderOffset.Y = pt.Y - _dragPoint.Y;
                }
                else // without centering
                {
                    RenderOffset.X = -_positionPixel.X - _dragPoint.X;
                    RenderOffset.Y = -_positionPixel.Y - _dragPoint.Y;
                    RenderOffset.Offset(MouseLastZoom);
                    RenderOffset.Offset(CompensationOffset);
                }
            }
            else // use current map center
            {
                MouseLastZoom = GPoint.Empty;

                var pt = new GPoint(-(_positionPixel.X - Width / 2), -(_positionPixel.Y - Height / 2));
                pt.Offset(CompensationOffset);
                RenderOffset.X = pt.X - _dragPoint.X;
                RenderOffset.Y = pt.Y - _dragPoint.Y;
            }

            UpdateCenterTileXYLocation();
        }
        public GPoint FromLatLngToLocal(PointLatLng point)
        {
            GPoint ret = Core.FromLatLngToLocal(point);

            if (MapRenderTransform != null)
            {
                var tp = MapRenderTransform.Transform(new System.Windows.Point(ret.X, ret.Y));
                ret.X = (int)tp.X;
                ret.Y = (int)tp.Y;
            }

            if (MapTranslateTransform != null)
            {
                ret.Offset(-(int)MapTranslateTransform.X, -(int)MapTranslateTransform.Y);
            }

            return(ret);
        }
예제 #8
0
        public override void OnRender(Graphics g)
        {
            DateTime start = DateTime.Now;
            var      pos   = Overlay.Control.FromLatLngToLocal(Position);

            pos.Offset(-LocalPosition.X, -LocalPosition.Y);

            double width =
                (Overlay.Control.MapProvider.Projection.GetDistance(Overlay.Control.FromLocalToLatLng(0, 0),
                                                                    Overlay.Control.FromLocalToLatLng(Overlay.Control.Width, 0)) * 1000.0);
            double m2pixelwidth = Overlay.Control.Width / width;

            Rect screenRect = new Rect(Overlay.Control.Width / -2, Overlay.Control.Height / -2, Overlay.Control.Width,
                                       Overlay.Control.Height);

            foreach (var pg in overlapCount)
            {
                GPoint p = Overlay.Control.FromLatLngToLocal(pg.Key);

                p.Offset(-pos.X, -pos.Y);

                if (p.X < screenRect.Left || p.X > screenRect.Right ||
                    p.Y < screenRect.Top || p.Y > screenRect.Bottom)
                {
                    continue;
                }

                var col = Math.Min(pg.Value - 1, 7);

                var coloruse = colorbrushs[col];

                var widthc = 5 * m2pixelwidth;

                var halfwidthc = widthc / 2.0f;

                g.FillPie(coloruse, (float)(p.X - halfwidthc), (float)(p.Y - halfwidthc), (float)(widthc),
                          (float)(widthc), 0, 360);
            }

            drawLegend(g);
            Console.WriteLine(DateTime.Now - start);
        }
예제 #9
0
        public void drawLegend(Graphics g)
        {
            // top left corner
            var tl = Overlay.Control.FromLocalToLatLng(0, 0);
            // top right corner
            var tr = Overlay.Control.FromLocalToLatLng(Overlay.Control.Width, 0);
            // width in m
            double width = Overlay.Control.MapProvider.Projection.GetDistance(tl, tr) * 1000.0;
            // meters per pixel
            double m2pixelwidth = Overlay.Control.Width / width;

            var widthc = 20;

            var halfwidthc = widthc / 2.0f;

            var pos = Overlay.Control.FromLatLngToLocal(Position);

            pos.Offset(-LocalPosition.X, -LocalPosition.Y);

            using (StringFormat stringFormat = new StringFormat())
            {
                stringFormat.Alignment     = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;

                int a = 0;
                foreach (var brush in colorbrushs)
                {
                    GPoint p = new GPoint(20, (long)(100 + a * (widthc + 5)));

                    p.Offset(-pos.X, -pos.Y);

                    g.FillPie(brush, (float)(p.X - halfwidthc), (float)(p.Y - halfwidthc), (float)(widthc),
                              (float)(widthc), 0, 360);

                    g.DrawString((a + 1).ToString(), SystemFonts.DefaultFont, Brushes.White,
                                 new RectangleF((float)(p.X - halfwidthc), (float)(p.Y - halfwidthc), (float)widthc,
                                                (float)widthc), stringFormat);
                    a++;
                }
            }
        }
예제 #10
0
        string MakeTileImageUrl(GPoint pos, int zoom, string language)
        {
            if (this.initialized == false)
            {
                return(string.Empty);
            }

            GPoint px1 = Projection.FromTileXYToPixel(pos);
            GPoint px2 = px1;

            px1.Offset(0, Projection.TileSize.Height);
            PointLatLng p1 = Projection.FromPixelToLatLng(px1, zoom);

            px2.Offset(Projection.TileSize.Width, 0);
            PointLatLng p2 = Projection.FromPixelToLatLng(px2, zoom);

            //if (this.coordTrans != null)
            //{
            //    double[] convp1 = this.coordTrans.MathTransform.Transform(new double[] { p1.Lat, p1.Lng });
            //    p1.Lat = Math.Round(convp1[0], 6);
            //    p1.Lng = Math.Round(convp1[1], 6);
            //    double[] convp2 = this.coordTrans.MathTransform.Transform(new double[] { p2.Lat, p2.Lng });
            //    p2.Lat = Math.Round(convp2[0], 6);
            //    p2.Lng = Math.Round(convp2[1], 6);
            //}

            //string ret = string.Format(CultureInfo.InvariantCulture, UrlFormat, this.url, this.layer,
            //    p1.Lng.ToString(NumberFormatInfo.InvariantInfo), p1.Lat.ToString(NumberFormatInfo.InvariantInfo),
            //    p2.Lng.ToString(NumberFormatInfo.InvariantInfo), p2.Lat.ToString(NumberFormatInfo.InvariantInfo),
            //    Projection.TileSize.Width.ToString(NumberFormatInfo.InvariantInfo), Projection.TileSize.Height.ToString(NumberFormatInfo.InvariantInfo),
            //    this.srs, "image/" + this.tileFormat);


            //  var ret = string.Format(CultureInfo.InvariantCulture, UrlFormat, p1.Lng, p1.Lat, p2.Lng, p2.Lat, Projection.TileSize.Width, Projection.TileSize.Height);

            var ret = string.Format(CultureInfo.InvariantCulture, UrlFormat, this.url, this.layer, p1.Lng, p1.Lat, p2.Lng, p2.Lat, Projection.TileSize.Width, Projection.TileSize.Height, "CRS:84", "image/png");

            // ret = string.Format(CultureInfo.InvariantCulture, UrlFormat, this.url, this.layer, p1.Lng, p1.Lat, p2.Lng, p2.Lat, Projection.TileSize.Width, Projection.TileSize.Height, "EPSG:3857", "image/png");

            return(ret);
        }
예제 #11
0
        /// <summary>
        /// darg map by offset in pixels
        /// </summary>
        /// <param name="offset"></param>
        public void DragOffset(GPoint offset)
        {
            renderOffset.Offset(offset);

            UpdateCenterTileXYLocation();

            if (centerTileXYLocation != centerTileXYLocationLast)
            {
                centerTileXYLocationLast = centerTileXYLocation;
                UpdateBounds();
            }

            {
                LastLocationInBounds = Position;

                IsDragging = true;
                Position   = FromLocalToLatLng((int)Width / 2, (int)Height / 2);
                IsDragging = false;
            }

            OnMapDrag?.Invoke();
        }
예제 #12
0
        /// <summary>
        /// darg map by offset in pixels
        /// </summary>
        /// <param name="offset"></param>
        public void DragOffset(GPoint offset)
        {
            renderOffset.Offset(offset);

            UpdateCenterTileXYLocation();

            if (centerTileXYLocation != centerTileXYLocationLast)
            {
                centerTileXYLocationLast = centerTileXYLocation;
                UpdateBounds();
            }

            {
                LastLocationInBounds = CurrentPosition;
                CurrentPosition      = FromLocalToLatLng((int)Width / 2, (int)Height / 2);
            }

            if (OnMapDrag != null)
            {
                OnMapDrag();
            }
        }
예제 #13
0
        private void inputProject(GMapMarker item)
        {
            // check authentication
            GPLC.user.Authenticate(GPLCAuthority.Administrator);

            // set context menu
            if (ttc != null)
            {
                ttc.Dispose();
                ttc = null;
            }

            /* create a input project tooltip container for context menu
             *  : pass a marker
             * */
            ttc = new ToolTipContentContainer(item);

            // compute show position
            GPoint p = gMap.FromLatLngToLocal(item.Position);

            p.Offset(item.Size.Width * 2 / 3, -1 * (item.Size.Height));
            // show contextMenu
            contextMenu(ttc, new Point(Convert.ToInt32(p.X), Convert.ToInt32(p.Y)));
        }
예제 #14
0
        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            MapInfo info = (MapInfo)e.Argument;

            if (!info.Area.IsEmpty)
            {
                string bigImage = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + Path.DirectorySeparatorChar + "GMap at zoom " + info.Zoom + " - " + info.Type + "-" + DateTime.Now.Ticks + ".png";
                if (!string.IsNullOrEmpty(textBoxSaveAs.Text))
                {
                    bigImage = Path.Combine(Path.GetDirectoryName(textBoxSaveAs.Text), Path.GetFileName(bigImage));
                }

                var smallImage = Path.Combine(Path.GetDirectoryName(bigImage), "layer.png");
                if (!string.IsNullOrEmpty(textBoxSaveAs.Text))
                {
                    smallImage = textBoxSaveAs.Text;
                }

                e.Result = bigImage;

                // current area
                GPoint         topLeftPx     = info.Type.Projection.FromLatLngToPixel(info.Area.LocationTopLeft, info.Zoom);
                GPoint         rightButtomPx = info.Type.Projection.FromLatLngToPixel(info.Area.Bottom, info.Area.Right, info.Zoom);
                GPoint         pxDelta       = new GPoint(rightButtomPx.X - topLeftPx.X, rightButtomPx.Y - topLeftPx.Y);
                GMap.NET.GSize maxOfTiles    = info.Type.Projection.GetTileMatrixMaxXY(info.Zoom);

                int shrinkLoop = 0;

                int padding = info.MakeWorldFile || info.MakeKmz ? 0 : 22;
                {
                    using (Bitmap bmpDestination = new Bitmap((int)(pxDelta.X + padding * 2), (int)(pxDelta.Y + padding * 2)))
                    {
                        using (Graphics gfx = Graphics.FromImage(bmpDestination))
                        {
                            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            gfx.SmoothingMode     = SmoothingMode.HighQuality;

                            int i = 0;

                            // get tiles & combine into one
                            lock (tileArea)
                            {
                                foreach (var p in tileArea)
                                {
                                    if (bg.CancellationPending)
                                    {
                                        e.Cancel = true;
                                        return;
                                    }

                                    int pc = (int)(((double)++i / tileArea.Count) * 100);
                                    bg.ReportProgress(pc, p);

                                    foreach (var tp in info.Type.Overlays)
                                    {
                                        Exception ex;
                                        GMapImage tile;

                                        // tile number inversion(BottomLeft -> TopLeft) for pergo maps
                                        if (tp.InvertedAxisY)
                                        {
                                            tile = GMaps.Instance.GetImageFrom(tp, new GPoint(p.X, maxOfTiles.Height - p.Y), info.Zoom, out ex) as GMapImage;
                                        }
                                        else // ok
                                        {
                                            tile = GMaps.Instance.GetImageFrom(tp, p, info.Zoom, out ex) as GMapImage;
                                        }

                                        if (tile != null)
                                        {
                                            using (tile)
                                            {
                                                long x = p.X * info.Type.Projection.TileSize.Width - topLeftPx.X + padding;
                                                long y = p.Y * info.Type.Projection.TileSize.Width - topLeftPx.Y + padding;
                                                {
                                                    gfx.DrawImage(tile.Img, x, y, info.Type.Projection.TileSize.Width, info.Type.Projection.TileSize.Height);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // draw routes
                            {
                                foreach (GMapRoute r in Main.routes.Routes)
                                {
                                    if (r.IsVisible)
                                    {
                                        using (GraphicsPath rp = new GraphicsPath())
                                        {
                                            for (int j = 0; j < r.Points.Count; j++)
                                            {
                                                var    pr = r.Points[j];
                                                GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                                px.Offset(padding, padding);
                                                px.Offset(-topLeftPx.X, -topLeftPx.Y);

                                                GPoint p2 = px;

                                                if (j == 0)
                                                {
                                                    rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
                                                }
                                                else
                                                {
                                                    System.Drawing.PointF p = rp.GetLastPoint();
                                                    rp.AddLine(p.X, p.Y, p2.X, p2.Y);
                                                }
                                            }

                                            if (rp.PointCount > 0)
                                            {
                                                gfx.DrawPath(r.Stroke, rp);
                                            }
                                        }
                                    }
                                }
                            }

                            // draw polygons
                            {
                                foreach (GMapPolygon r in Main.polygons.Polygons)
                                {
                                    if (r.IsVisible)
                                    {
                                        using (GraphicsPath rp = new GraphicsPath())
                                        {
                                            for (int j = 0; j < r.Points.Count; j++)
                                            {
                                                var    pr = r.Points[j];
                                                GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                                px.Offset(padding, padding);
                                                px.Offset(-topLeftPx.X, -topLeftPx.Y);

                                                GPoint p2 = px;

                                                if (j == 0)
                                                {
                                                    rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
                                                }
                                                else
                                                {
                                                    System.Drawing.PointF p = rp.GetLastPoint();
                                                    rp.AddLine(p.X, p.Y, p2.X, p2.Y);
                                                }
                                            }

                                            if (rp.PointCount > 0)
                                            {
                                                rp.CloseFigure();

                                                gfx.FillPath(r.Fill, rp);

                                                gfx.DrawPath(r.Stroke, rp);
                                            }
                                        }
                                    }
                                }
                            }

                            // draw markers
                            {
                                foreach (GMapMarker r in Main.objects.Markers)
                                {
                                    if (r.IsVisible)
                                    {
                                        var    pr = r.Position;
                                        GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                        px.Offset(padding, padding);
                                        px.Offset(-topLeftPx.X, -topLeftPx.Y);
                                        px.Offset(r.Offset.X, r.Offset.Y);

                                        gfx.ResetTransform();
                                        gfx.TranslateTransform(-r.LocalPosition.X, -r.LocalPosition.Y);
                                        gfx.TranslateTransform((int)px.X, (int)px.Y);

                                        r.OnRender(gfx);
                                    }
                                }

                                // tooltips above
                                foreach (GMapMarker m in Main.objects.Markers)
                                {
                                    if (m.IsVisible && m.ToolTip != null && m.IsVisible)
                                    {
                                        if (!string.IsNullOrEmpty(m.ToolTipText))
                                        {
                                            var    pr = m.Position;
                                            GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                            px.Offset(padding, padding);
                                            px.Offset(-topLeftPx.X, -topLeftPx.Y);
                                            px.Offset(m.Offset.X, m.Offset.Y);

                                            gfx.ResetTransform();
                                            gfx.TranslateTransform(-m.LocalPosition.X, -m.LocalPosition.Y);
                                            gfx.TranslateTransform((int)px.X, (int)px.Y);

                                            m.ToolTip.OnRender(gfx);
                                        }
                                    }
                                }
                                gfx.ResetTransform();
                            }

                            // draw info
                            if (!info.MakeWorldFile)
                            {
                                System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
                                {
                                    rect.Location = new System.Drawing.Point(padding, padding);
                                    rect.Size     = new System.Drawing.Size((int)pxDelta.X, (int)pxDelta.Y);
                                }

                                using (Font f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold))
                                {
                                    // draw bounds & coordinates
                                    using (Pen p = new Pen(Brushes.DimGray, 3))
                                    {
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

                                        gfx.DrawRectangle(p, rect);

                                        string topleft = info.Area.LocationTopLeft.ToString();
                                        SizeF  s       = gfx.MeasureString(topleft, f);

                                        gfx.DrawString(topleft, f, p.Brush, rect.X + s.Height / 2, rect.Y + s.Height / 2);

                                        string rightBottom = new PointLatLng(info.Area.Bottom, info.Area.Right).ToString();
                                        SizeF  s2          = gfx.MeasureString(rightBottom, f);

                                        gfx.DrawString(rightBottom, f, p.Brush, rect.Right - s2.Width - s2.Height / 2, rect.Bottom - s2.Height - s2.Height / 2);
                                    }

                                    // draw scale
                                    using (Pen p = new Pen(Brushes.Blue, 1))
                                    {
                                        double rez    = info.Type.Projection.GetGroundResolution(info.Zoom, info.Area.Bottom);
                                        int    px100  = (int)(100.0 / rez);  // 100 meters
                                        int    px1000 = (int)(1000.0 / rez); // 1km

                                        gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px1000, 10);
                                        gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px100, 10);

                                        string leftBottom = "scale: 100m | 1Km";
                                        SizeF  s          = gfx.MeasureString(leftBottom, f);
                                        gfx.DrawString(leftBottom, f, p.Brush, rect.X + 10, rect.Bottom - s.Height - 20);
                                    }
                                }
                            }
                        }

                        ImageUtilities.SavePng(bigImage, bmpDestination);

                        Image shrinkImage = null;
                        try
                        {
                            shrinkImage = bmpDestination;

                            long size;
                            if (long.TryParse(textBoxSizeFile.Text, out size))
                            {
                                var fileLength = new FileInfo(bigImage).Length;
                                if (fileLength > 0 && (fileLength / 1024 / 1024) > size)
                                {
                                    while (shrinkLoop <= 100)
                                    {
                                        shrinkLoop++;

                                        int nextWidth  = shrinkImage.Width * 80 / 100;
                                        int nextHeight = shrinkImage.Height * 80 / 100;

                                        // Shrink image
                                        shrinkImage = Stuff.FixedSize(shrinkImage, nextWidth, nextHeight);
                                        ImageUtilities.SavePng(smallImage, shrinkImage);

                                        fileLength = new FileInfo(smallImage).Length;
                                        if (fileLength > 0 && (fileLength / 1024 / 1024) < size)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    ImageUtilities.SavePng(smallImage, shrinkImage);
                                }
                            }
                            else
                            {
                                MessageBox.Show("Invalide size - no shrinking.", "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "GMap.NET - Demo.WindowsForms", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        finally
                        {
                            shrinkImage.Dispose();
                        }
                    }
                }

                //The worldfile for the original image is:

                //0.000067897543      // the horizontal size of a pixel in coordinate units (longitude degrees in this case);
                //0.0000000
                //0.0000000
                //-0.0000554613012    // the comparable vertical pixel size in latitude degrees, negative because latitude decreases as you go from top to bottom in the image.
                //-111.743323868834   // longitude of the pixel in the upper-left-hand corner.
                //35.1254392635083    // latitude of the pixel in the upper-left-hand corner.

                // generate world file
                if (info.MakeWorldFile)
                {
                    string wf = Path.ChangeExtension(bigImage, "pgw");
                    using (StreamWriter world = File.CreateText(wf))
                    {
                        world.WriteLine("{0:0.000000000000}", (info.Area.WidthLng / pxDelta.X));
                        world.WriteLine("0.0000000");
                        world.WriteLine("0.0000000");
                        world.WriteLine("{0:0.000000000000}", (-info.Area.HeightLat / pxDelta.Y));
                        world.WriteLine("{0:0.000000000000}", info.Area.Left);
                        world.WriteLine("{0:0.000000000000}", info.Area.Top);
                        world.Close();
                    }

                    wf = Path.ChangeExtension(smallImage, "pgw");
                    using (StreamWriter world = File.CreateText(wf))
                    {
                        world.WriteLine("{0:0.000000000000}", (info.Area.WidthLng / pxDelta.X) * (shrinkLoop > 0 ? Math.Pow(1.25, shrinkLoop) : 1));
                        world.WriteLine("0.0000000");
                        world.WriteLine("0.0000000");
                        world.WriteLine("{0:0.000000000000}", (-info.Area.HeightLat / pxDelta.Y) * (shrinkLoop > 0 ? Math.Pow(1.25, shrinkLoop) : 1));
                        world.WriteLine("{0:0.000000000000}", info.Area.Left);
                        world.WriteLine("{0:0.000000000000}", info.Area.Top);
                        world.Close();
                    }
                }

                if (info.MakeKmz)
                {
                    var kmzFile = Path.GetDirectoryName(bigImage) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(bigImage) + ".kmz";
                    e.Result = kmzFile;

                    using (ZipStorer zip = ZipStorer.Create(kmzFile, "GMap.NET"))
                    {
                        zip.AddFile(ZipStorer.Compression.Store, bigImage, "files/map.jpg", "map");

                        using (var readme = new MemoryStream(
                                   Encoding.UTF8.GetBytes(
                                       string.Format(CultureInfo.InvariantCulture, @"<?xml version=""1.0"" encoding=""UTF-8""?> 
<kml xmlns=""http://www.opengis.net/kml/2.2"" xmlns:gx=""http://www.google.com/kml/ext/2.2"" xmlns:kml=""http://www.opengis.net/kml/2.2"" xmlns:atom=""http://www.w3.org/2005/Atom"">
<GroundOverlay>
	<name>{8}</name>
	<LookAt>
		<longitude>{6}</longitude>
		<latitude>{7}</latitude>
		<altitude>0</altitude>
		<heading>0</heading>
		<tilt>0</tilt>
		<range>69327.55500845652</range>
	</LookAt>
	<color>91ffffff</color>
	<Icon>
		<href>files/map.jpg</href>
	</Icon>
	<gx:LatLonQuad>
		<coordinates>
			{0},{1},0 {2},{3},0 {4},{5},0 {6},{7},0 
		</coordinates>
	</gx:LatLonQuad>
</GroundOverlay>
</kml>", info.Area.Left, info.Area.Bottom,
                                                     info.Area.Right, info.Area.Bottom,
                                                     info.Area.Right, info.Area.Top,
                                                     info.Area.Left, info.Area.Top,
                                                     kmzFile))))
                        {
                            zip.AddStream(ZipStorer.Compression.Store, "doc.kml", readme, DateTime.Now, "kml");
                            zip.Close();
                        }
                    }
                }
            }
        }
      /// <summary>
      /// gets lat/lng from local control coordinates
      /// </summary>
      /// <param name="x"></param>
      /// <param name="y"></param>
      /// <returns></returns>
      public PointLatLng FromLocalToLatLng(long x, long y)
      {
         GPoint p = new GPoint(x, y);
         p.OffsetNegative(renderOffset);
         p.Offset(compensationOffset);

         return Provider.Projection.FromPixelToLatLng(p, Zoom);
      }
      /// <summary>
      /// moves current position into map center
      /// </summary>
      internal void GoToCurrentPositionOnZoom()
      {
         compensationOffset = positionPixel; // TODO: fix

         // reset stuff
         renderOffset = GPoint.Empty;
         dragPoint = GPoint.Empty;

         // goto location and centering
         if(MouseWheelZooming)
         {
            if(MouseWheelZoomType != MouseWheelZoomType.MousePositionWithoutCenter)
            {
               GPoint pt = new GPoint(-(positionPixel.X - Width / 2), -(positionPixel.Y - Height / 2));
               pt.Offset(compensationOffset);
               renderOffset.X = pt.X - dragPoint.X;
               renderOffset.Y = pt.Y - dragPoint.Y;
            }
            else // without centering
            {
               renderOffset.X = -positionPixel.X - dragPoint.X;
               renderOffset.Y = -positionPixel.Y - dragPoint.Y;
               renderOffset.Offset(mouseLastZoom);
               renderOffset.Offset(compensationOffset);
            }
         }
         else // use current map center
         {
            mouseLastZoom = GPoint.Empty;

            GPoint pt = new GPoint(-(positionPixel.X - Width / 2), -(positionPixel.Y - Height / 2));
            pt.Offset(compensationOffset);
            renderOffset.X = pt.X - dragPoint.X;
            renderOffset.Y = pt.Y - dragPoint.Y;
         }

         UpdateCenterTileXYLocation();
      }
예제 #17
0
        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            MapInfo info = (MapInfo)e.Argument;

            if (!info.Area.IsEmpty)
            {
                string bigImage = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + Path.DirectorySeparatorChar + "GMap at zoom " + info.Zoom + " - " + info.Type + "-" + DateTime.Now.Ticks + ".png";
                e.Result = bigImage;

                // current area
                GPoint         topLeftPx     = info.Type.Projection.FromLatLngToPixel(info.Area.LocationTopLeft, info.Zoom);
                GPoint         rightButtomPx = info.Type.Projection.FromLatLngToPixel(info.Area.Bottom, info.Area.Right, info.Zoom);
                GPoint         pxDelta       = new GPoint(rightButtomPx.X - topLeftPx.X, rightButtomPx.Y - topLeftPx.Y);
                GMap.NET.GSize maxOfTiles    = info.Type.Projection.GetTileMatrixMaxXY(info.Zoom);

                int padding = info.MakeWorldFile ? 0 : 22;
                {
                    using (Bitmap bmpDestination = new Bitmap((int)(pxDelta.X + padding * 2), (int)(pxDelta.Y + padding * 2)))
                    {
                        using (Graphics gfx = Graphics.FromImage(bmpDestination))
                        {
                            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            gfx.SmoothingMode     = SmoothingMode.HighQuality;

                            int i = 0;

                            // get tiles & combine into one
                            lock (tileArea)
                            {
                                foreach (var p in tileArea)
                                {
                                    if (bg.CancellationPending)
                                    {
                                        e.Cancel = true;
                                        return;
                                    }

                                    int pc = (int)(((double)++i / tileArea.Count) * 100);
                                    bg.ReportProgress(pc, p);

                                    foreach (var tp in info.Type.Overlays)
                                    {
                                        Exception ex;
                                        GMapImage tile;

                                        // tile number inversion(BottomLeft -> TopLeft) for pergo maps
                                        if (tp.InvertedAxisY)
                                        {
                                            tile = GMaps.Instance.GetImageFrom(tp, new GPoint(p.X, maxOfTiles.Height - p.Y), info.Zoom, out ex) as GMapImage;
                                        }
                                        else // ok
                                        {
                                            tile = GMaps.Instance.GetImageFrom(tp, p, info.Zoom, out ex) as GMapImage;
                                        }

                                        if (tile != null)
                                        {
                                            using (tile)
                                            {
                                                long x = p.X * info.Type.Projection.TileSize.Width - topLeftPx.X + padding;
                                                long y = p.Y * info.Type.Projection.TileSize.Width - topLeftPx.Y + padding;
                                                {
                                                    gfx.DrawImage(tile.Img, x, y, info.Type.Projection.TileSize.Width, info.Type.Projection.TileSize.Height);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // draw polygons
                            {
                                foreach (GMapPolygon r in GmapsObjects.Polygons)
                                {
                                    if (r.IsVisible)
                                    {
                                        using (GraphicsPath rp = new GraphicsPath())
                                        {
                                            for (int j = 0; j < r.Points.Count; j++)
                                            {
                                                var    pr = r.Points[j];
                                                GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                                px.Offset(padding, padding);
                                                px.Offset(-topLeftPx.X, -topLeftPx.Y);

                                                GPoint p2 = px;

                                                if (j == 0)
                                                {
                                                    rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
                                                }
                                                else
                                                {
                                                    System.Drawing.PointF p = rp.GetLastPoint();
                                                    rp.AddLine(p.X, p.Y, p2.X, p2.Y);
                                                }
                                            }

                                            if (rp.PointCount > 0)
                                            {
                                                rp.CloseFigure();

                                                gfx.FillPath(r.Fill, rp);

                                                gfx.DrawPath(r.Stroke, rp);
                                            }
                                        }
                                    }
                                }
                            }

                            // draw markers
                            {
                                foreach (GMapMarker r in GmapsObjects.Markers)
                                {
                                    if (r.IsVisible)
                                    {
                                        var    pr = r.Position;
                                        GPoint px = info.Type.Projection.FromLatLngToPixel(pr.Lat, pr.Lng, info.Zoom);

                                        px.Offset(padding, padding);
                                        px.Offset(-topLeftPx.X, -topLeftPx.Y);
                                        px.Offset(r.Offset.X, r.Offset.Y);

                                        r.LocalPosition = new System.Drawing.Point((int)px.X, (int)px.Y);

                                        r.OnRender(gfx);
                                    }
                                }

                                // tooltips above
                                foreach (GMapMarker m in GmapsObjects.Markers)
                                {
                                    if (m.IsVisible && m.ToolTip != null && m.IsVisible)
                                    {
                                        if (!string.IsNullOrEmpty(m.ToolTipText))
                                        {
                                            m.ToolTip.OnRender(gfx);
                                        }
                                    }
                                }
                            }

                            // draw info
                            {
                                System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
                                {
                                    rect.Location = new System.Drawing.Point(padding, padding);
                                    rect.Size     = new System.Drawing.Size((int)pxDelta.X, (int)pxDelta.Y);
                                }

                                using (Font f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold))
                                {
                                    // draw bounds & coordinates
                                    using (Pen p = new Pen(Brushes.DimGray, 3))
                                    {
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

                                        gfx.DrawRectangle(p, rect);

                                        string topleft = info.Area.LocationTopLeft.ToString();
                                        SizeF  s       = gfx.MeasureString(topleft, f);

                                        gfx.DrawString(topleft, f, p.Brush, rect.X + s.Height / 2, rect.Y + s.Height / 2);

                                        string rightBottom = new PointLatLng(info.Area.Bottom, info.Area.Right).ToString();
                                        SizeF  s2          = gfx.MeasureString(rightBottom, f);

                                        gfx.DrawString(rightBottom, f, p.Brush, rect.Right - s2.Width - s2.Height / 2, rect.Bottom - s2.Height - s2.Height / 2);
                                    }

                                    // draw scale
                                    using (Pen p = new Pen(Brushes.Blue, 1))
                                    {
                                        double rez    = info.Type.Projection.GetGroundResolution(info.Zoom, info.Area.Bottom);
                                        int    px100  = (int)(100.0 / rez);  // 100 meters
                                        int    px1000 = (int)(1000.0 / rez); // 1km

                                        gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px1000, 10);
                                        gfx.DrawRectangle(p, rect.X + 10, rect.Bottom - 20, px100, 10);

                                        string leftBottom = "scale: 100m | 1Km";
                                        SizeF  s          = gfx.MeasureString(leftBottom, f);
                                        gfx.DrawString(leftBottom, f, p.Brush, rect.X + 10, rect.Bottom - s.Height - 20);
                                    }
                                }
                            }
                        }

                        bmpDestination.Save(ImageStream, ImageFormat.Jpeg);
                    }
                }

                //The worldfile for the original image is:

                //0.000067897543      // the horizontal size of a pixel in coordinate units (longitude degrees in this case);
                //0.0000000
                //0.0000000
                //-0.0000554613012    // the comparable vertical pixel size in latitude degrees, negative because latitude decreases as you go from top to bottom in the image.
                //-111.743323868834   // longitude of the pixel in the upper-left-hand corner.
                //35.1254392635083    // latitude of the pixel in the upper-left-hand corner.

                // generate world file
                if (info.MakeWorldFile)
                {
                    string wf = bigImage + "w";
                    using (StreamWriter world = File.CreateText(wf))
                    {
                        world.WriteLine("{0:0.000000000000}", (info.Area.WidthLng / pxDelta.X));
                        world.WriteLine("0.0000000");
                        world.WriteLine("0.0000000");
                        world.WriteLine("{0:0.000000000000}", (-info.Area.HeightLat / pxDelta.Y));
                        world.WriteLine("{0:0.000000000000}", info.Area.Left);
                        world.WriteLine("{0:0.000000000000}", info.Area.Top);
                        world.Close();
                    }
                }
            }
        }