Home  >  Article  >  Java  >  How to Convert Latitude and Longitude to Pixel Coordinates Using the Mercator Projection?

How to Convert Latitude and Longitude to Pixel Coordinates Using the Mercator Projection?

Linda Hamilton
Linda HamiltonOriginal
2024-11-07 01:58:03693browse

How to Convert Latitude and Longitude to Pixel Coordinates Using the Mercator Projection?

Mercator Projection Pixel Conversion

The Mercator projection is widely used in mapping, particularly for navigation charts. It transforms latitude and longitude coordinates into a rectangular grid, making it ideal for displaying maps of the world on a flat surface.

Formula Derivation

The Mercator projection is derived from cylindrical projections. The formulas for converting latitude and longitude to rectangular coordinates are:

E = FE + R (λ – λ0)
N = FN + R ln[tan(π/4 + φ/2)]

where:

  • E is the Easting coordinate
  • N is the Northing coordinate
  • FE and FN are false easting and false northing values
  • R is the radius of the Earth
  • λ is the longitude
  • λ0 is the longitude of natural origin
  • φ is the latitude

In spherical Mercator projection, FE and FN are not used, so the formula simplifies to:

x = (λ + 180) * (mapWidth / 360)
y = (mapHeight / 2) - (mapWidth * ln(tan((PI / 4) + (latRad / 2))) / (2 * PI))

where:

  • x is the Easting coordinate in pixels
  • y is the Northing coordinate in pixels
  • latRad is the latitude in radians
  • mapWidth and mapHeight are the dimensions of the map image in pixels

Implementation

In Java, you can implement the Mercator projection conversion as follows:

public static void main(String[] args) {
    double latitude = 41.145556;
    double longitude = -73.995;

    double mapWidth = 200;
    double mapHeight = 100;
    
    // Convert latitude from degrees to radians
    double latRad = latitude * Math.PI / 180;

    // Calculate Easting and Northing coordinates
    double x = (longitude + 180) * (mapWidth / 360);
    double y = (mapHeight / 2) - (mapWidth * Math.log(Math.tan((Math.PI / 4) + (latRad / 2))) / (2 * Math.PI));
    
    System.out.println("Easting: " + x);
    System.out.println("Northing: " + y);
}

The above is the detailed content of How to Convert Latitude and Longitude to Pixel Coordinates Using the Mercator Projection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn