How to convert geographical coordinates to pixels?

↘锁芯ラ 提交于 2021-02-19 08:33:51

问题


I am developing wpf application. I have one static world map of 500 width and 500 height. I have one form in my application. In this from user enters the latitude and longitude and submit the details. I want to show the exact location of these latitude and longitude on my static map. So I am trying to convert these latitude and longitude into pixels. I am using ellipse to show the circle on my static map. How should I convert the geographical coordinates into pixels in C# ? Can you please provide me any code or link through which I can solve the above issue ? My question is similar to the link

Convert long/lat to pixel x/y on a given picture

I found this link useful.

Edit : I have used the link

http://code.google.com/p/geographical-dot-net/source/browse/trunk/GeographicalDotNet/GeographicalDotNet/Projection/GoogleMapsAPIProjection.cs

and written the following code

GoogleMapsAPIProjection googleApiProjObj = new GoogleMapsAPIProjection(0);
            float x = 18.29F;
            float y = 73.57F;
            System.Drawing.PointF p1 = new System.Drawing.PointF(x,y);
            System.Drawing.PointF p2 =googleApiProjObj.FromCoordinatesToPixel(p1);

            //CircleEllipse.Margin = new Thickness(longitudePixels,latitudePixels, 0, 0);
            CircleEllipse.Margin = new Thickness(p2.X, p2.Y, 0, 0);
            CircleEllipse.Visibility = Visibility.Visible;

18.29 and 73.57 are lat and log of Pune city in India. In the above code p2.x giving me 141 and p2.y giving me 49. So the above code not showing me the Pune location on map. My xaml code is as follows

<ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="4" Width="500" Height="500" Background="Gray">
            <Grid>
                <Image Margin="0,0,0,0" x:Name="MapImage" Source="Images\World-Blank-Map.png" Stretch="Fill" Width="500" Height="500" ></Image>
                <Ellipse Canvas.Top="50" 
      Canvas.Left="50" 
      Fill="Red" 
      Height="5"
      Width="5"
      Visibility="Collapsed"
      StrokeThickness="4"                     

      x:Name="CircleEllipse"
      HorizontalAlignment="Left"
      VerticalAlignment="Top"
      Margin="0,0,0,0" />
            </Grid>
        </ScrollViewer>

回答1:


You provided your own answer, take a look at the code at the following

http://code.google.com/p/geographical-dot-net/source/browse/trunk/GeographicalDotNet/GeographicalDotNet/Projection/GoogleMapsAPIProjection.cs

public class GoogleMapsAPIProjection
{
    private readonly double PixelTileSize = 256d;
    private readonly double DegreesToRadiansRatio = 180d / Math.PI;
    private readonly double RadiansToDegreesRatio = Math.PI / 180d;
    private readonly PointF PixelGlobeCenter;
    private readonly double XPixelsToDegreesRatio;
    private readonly double YPixelsToRadiansRatio;

    public GoogleMapsAPIProjection(double zoomLevel)
    {
        var pixelGlobeSize = this.PixelTileSize * Math.Pow(2d, zoomLevel);
        this.XPixelsToDegreesRatio = pixelGlobeSize / 360d;
        this.YPixelsToRadiansRatio = pixelGlobeSize / (2d * Math.PI);
        var halfPixelGlobeSize = Convert.ToSingle(pixelGlobeSize / 2d);
        this.PixelGlobeCenter = new PointF(
            halfPixelGlobeSize, halfPixelGlobeSize);
    }

    public PointF FromCoordinatesToPixel(PointF coordinates)
    {
        var x = Math.Round(this.PixelGlobeCenter.X
            + (coordinates.X * this.XPixelsToDegreesRatio));
        var f = Math.Min(
            Math.Max(
                 Math.Sin(coordinates.Y * RadiansToDegreesRatio),
                -0.9999d),
            0.9999d);
        var y = Math.Round(this.PixelGlobeCenter.Y + .5d * 
            Math.Log((1d + f) / (1d - f)) * -this.YPixelsToRadiansRatio);
        return new PointF(Convert.ToSingle(x), Convert.ToSingle(y));
    }

    public PointF FromPixelToCoordinates(PointF pixel)
    {
        var longitude = (pixel.X - this.PixelGlobeCenter.X) /
            this.XPixelsToDegreesRatio;
        var latitude = (2 * Math.Atan(Math.Exp(
            (pixel.Y - this.PixelGlobeCenter.Y) / -this.YPixelsToRadiansRatio))
            - Math.PI / 2) * DegreesToRadiansRatio;
        return new PointF(
            Convert.ToSingle(latitude),
            Convert.ToSingle(longitude));
    }
}



回答2:


I post my api Mercator Projection for Google Maps. ;-) You have three classes:

  • Constants
  • PointCoordinates: latitude and longitude.
  • PointPixel: x,y for level 0 of google Maps.

    You can convert PointPixel from/to PointCoordinates

{

public static class PointUtils
{
    public const double MercatorGoogleHeight = 256;
    public const double MercatorGoogleWidth = 256;
    public const double PixelLongintudeOrigin = MercatorGoogleWidth / 2;
    public const double PixelLatitudeOrigin = MercatorGoogleHeight / 2;
    public const double PixelsPerLongintudeDegre = MercatorGoogleWidth / 360;
    public const double RadsPerLatitudeDegre = MercatorGoogleHeight / (2 * Math.PI);
}

/// <summary>
/// Point Pixel on Mercator Google Zoom 0
/// </summary>
public class PointPixel
{
    public double X { get; set; }
    public double Y { get; set; }

    public PointCoordinates ToPointCoordinates()
    {
        var lng = (X - PointUtils.PixelLongintudeOrigin) / PointUtils.PixelsPerLongintudeDegre;
        var latRad = (Y - PointUtils.PixelLatitudeOrigin)/-PointUtils.RadsPerLatitudeDegre;
        var lat = (2*Math.Atan(Math.Exp(latRad)) - Math.PI/2).RadToDeg();
        return new PointCoordinates()
            {
                Latitude = lat,
                Longitude = lng
            };
    }

}

/// <summary>
/// Point on Map World
/// </summary>
public class PointCoordinates
{

    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public PointPixel ToPointPixel()
    {
        var x = PointUtils.PixelLongintudeOrigin + PointUtils.PixelsPerLongintudeDegre * Longitude;
        var siny = Math.Sin(Latitude.DegToRad());
        var y = PointUtils.PixelLatitudeOrigin - (Math.Log((1 + siny) / (1 - siny)) / 2) * PointUtils.RadsPerLatitudeDegre;
        return new PointPixel() { X = x, Y = y };

    }


}


来源:https://stackoverflow.com/questions/12741128/how-to-convert-geographical-coordinates-to-pixels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!