The Java code calculates the number of slices in WGS84 and Google Global Mercator and the pixel size of the stitching image based on the latitude and longitude range

Source: Internet
Author: User
Tags pow

Code written according to the requirements of the project.

Applicable scenario: On the network map, such as the day map and Google Maps, the user with the mouse on the map to pull a rectangle, want to download the rectangle in a certain level of tile data, and all tiles into a complete, including geographical coordinates of the TIF image.

Before downloading tiles and tiled tiles, the user would like to see the number of tiles to be downloaded and the pixel size of the image to be tiled before deciding whether to splice.

The Java code calculates the number of tiles to download and the pixel dimensions of the resulting image to be stitched, based on the latitude and longitude range of the rectangle and the user-specified tile level.

Support EPSG4326 latitude and longitude with EPSG3857 Google Global Mercator projection. Latitude and Longitude tile drawing rules are the same as the world map, starting from the first layer, the first layer consists of two tiles. Google Global Mercator starts off on the No. 0 floor and a tile on the No. 0 floor.

public class Main {

private void Lonlattotile (double lon,double lat,int zoom,int[] txy)
{
Double resfact = 180.0/256.0;
double[] Pxy = new double[]{0.0,0.0};
Double res = RESFACT/MATH.POW (2, (double) zoom);
Pxy[0] = (180.0 + lon)/Res;
PXY[1] = (90.0-lat)/Res;

TXY[0] = (int) (Math.ceil (pxy[0]/256.0)-1);
TXY[1] = (int) (Math.ceil (pxy[1]/256.0)-1);
}

private void Latlontometers (double lon, double lat,double[] mxy)
{
Double M_originshift = 2 * 3.141592653589793 * 6378137/2.0;

MXY[0] = lon * m_originshift/180.0;
MXY[1] = Math.log (Math.tan ((+ lat) * 3.141592653589793/360.0)/(3.141592653589793/180.0);

MXY[1] = mxy[1] * m_originshift/180.0;
}

private void Meterstotile (double mx, double my, int zoom, int[] txy)
{
Double m_initialresolution = 2 * 3.141592653589793 * 6378137/256;
Double M_originshift = 2 * 3.141592653589793 * 6378137/2.0;

Double res = M_INITIALRESOLUTION/MATH.POW (2, (double) zoom);
Double px = (mx + m_originshift)/Res;
Double py = (m_originshift-my)/Res;

TXY[0] = (int) (Math.ceil (px/(float) (256))-1);
TXY[1] = (int) (Math.ceil (py/(float) (256))-1);
}

Calculate latitude and longitude output tile number and image pixel size to be tiled
public int getgeodeticsize (double Minlon, double Maxlon, double Minlat, double maxlat, int zoom, int[] pixsize) {

int[] Tminxy = new int[]{0,0};
int[] Tmaxxy = new int[]{0,0};

Lonlattotile (MINLON,MINLAT,ZOOM-1,TMINXY);
Lonlattotile (MAXLON,MAXLAT,ZOOM-1,TMAXXY);

Pixsize[0] = (1+math.abs (tmaxxy[0]-tminxy[0])) * 256;
PIXSIZE[1] = (1+math.abs (tmaxxy[1]-tminxy[1])) * 256;

int tnum = (1+math.abs (tmaxxy[0]-tminxy[0])) * (1+math.abs (tmaxxy[1]-tminxy[1]));

return tnum;
}
Calculate Google projection output tile number and image pixel size to be tiled
public int getmercatorsize (double Minlon, double Maxlon, double Minlat, double maxlat, int zoom, int[] pixsize) {

double[] Oulxy = new double[]{0,0};
double[] odrxy = new double[]{0,0};
Latlontometers (MINLON,MAXLAT,OULXY);
Latlontometers (MAXLON,MINLAT,ODRXY);
Double Ominx = oulxy[0];
Double Omaxx = odrxy[0];
Double ominy = odrxy[1];
Double omaxy = oulxy[1];

Int[] tminxy= new int[]{0,0};
int[] Tmaxxy = new int[]{0,0};
Meterstotile (ominx,ominy, Zoom, TMINXY);
Meterstotile (omaxx,omaxy, Zoom, TMAXXY);

Pixsize[0] = (1+math.abs (tmaxxy[0]-tminxy[0])) * 256;
PIXSIZE[1] = (1+math.abs (tmaxxy[1]-tminxy[1])) * 256;

int tnum = (1+math.abs (tmaxxy[0]-tminxy[0])) * (1+math.abs (tmaxxy[1]-tminxy[1]));

return tnum;
}

public static void Main (string[] args) {
System.out.println ("Hello world!");
Double Minlon = 119.54384371341310;
Double Maxlon = 119.93413672204591;
Double Minlat = 33.068895415323247;
Double Maxlat = 33.433168890047206;
int zoom = 13;

Main E=new main ();

Int[] pixsize= new int[]{0,0};
int tnum;
Tnum = E.getgeodeticsize (Minlon, Maxlon, Minlat, Maxlat, Zoom, pixsize);
System.out.println ("Latitude Data Tile Number:" + tnum + "image size:" + pixsize[0] + "*" + pixsize[1]);
Tnum = E.getmercatorsize (Minlon, Maxlon, Minlat, Maxlat, Zoom, pixsize);
SYSTEM.OUT.PRINTLN ("Google Data Tile Number:" + tnum + "image size:" + pixsize[0] + "*" + pixsize[1]);

}


}
Operation Result:

Java code calculates the number of slices and stitching image pixel dimensions of WGS84 and Google Global Mercator based on the latitude and longitude range

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.