Java 将经度/纬度转换为X/Y坐标

Java 将经度/纬度转换为X/Y坐标,java,google-maps,latitude-longitude,cartesian,coordinate-transformation,Java,Google Maps,Latitude Longitude,Cartesian,Coordinate Transformation,我使用谷歌地图API创建了一张地图,突出显示了明尼苏达州的所有县。基本上,我使用一组经度/纬度坐标创建了县多边形。以下是生成的地图的屏幕截图:- 用户要求之一是能够将类似的地图作为图像,以便将其嵌入到PowerPoint/keynote幻灯片中。我找不到任何有用的Google Maps API,它允许我按原样保存自定义地图(如果你知道方法,请告诉我),所以我想我应该用Java中的Graphics2D绘制它 在阅读了将经度/纬度转换为X/Y坐标的公式后,我得到了以下代码:- private st

我使用谷歌地图API创建了一张地图,突出显示了明尼苏达州的所有县。基本上,我使用一组经度/纬度坐标创建了县多边形。以下是生成的地图的屏幕截图:-

用户要求之一是能够将类似的地图作为图像,以便将其嵌入到PowerPoint/keynote幻灯片中。我找不到任何有用的Google Maps API,它允许我按原样保存自定义地图(如果你知道方法,请告诉我),所以我想我应该用Java中的Graphics2D绘制它

在阅读了将经度/纬度转换为X/Y坐标的公式后,我得到了以下代码:-

private static final int    EARTH_RADIUS    = 6371;
private static final double FOCAL_LENGTH    = 500;

...

BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();

for (Coordinate coordinate : coordinates) {
    double latitude = Double.valueOf(coordinate.getLatitude());
    double longitude = Double.valueOf(coordinate.getLongitude());

    latitude = latitude * Math.PI / 180;
    longitude = longitude * Math.PI / 180;

    double x = EARTH_RADIUS * Math.sin(latitude) * Math.cos(longitude);
    double y = EARTH_RADIUS * Math.sin(latitude) * Math.sin(longitude);
    double z = EARTH_RADIUS * Math.cos(latitude);

    double projectedX = x * FOCAL_LENGTH / (FOCAL_LENGTH + z);
    double projectedY = y * FOCAL_LENGTH / (FOCAL_LENGTH + z);

    // scale the map bigger
    int magnifiedX = (int) Math.round(projectedX * 5);
    int magnifiedY = (int) Math.round(projectedY * 5);

    ...
    g.drawPolygon(...);
    ...
}
生成的地图与Google Maps API使用相同的经度/纬度集生成的地图类似。然而,它似乎有点倾斜,看起来有点歪,我不知道如何修复这个问题

我如何使县的形状看起来就像上面谷歌地图API生成的一样

多谢

最终解决方案

多亏了@quantummechanical和@Anon,我终于找到了解决方案

墨卡托投影确实起到了作用。我正在使用来执行墨卡托投影的计算

private static final int    IMAGE_WIDTH     = 1000;
private static final int    IMAGE_HEIGHT    = 1000;
private static final int    IMAGE_PADDING   = 50;

...

private List<Point2D.Double> convertToXY(List<Coordinate> coordinates) {
    List<Point2D.Double> xys = new ArrayList<Point2D.Double>();

    MercatorProjection projection = new MercatorProjection();

    for (Coordinate coordinate : coordinates) {
        double latitude = Double.valueOf(coordinate.getLatitude());
        double longitude = Double.valueOf(coordinate.getLongitude());

        // convert to radian
        latitude = latitude * Math.PI / 180;
        longitude = longitude * Math.PI / 180;

        Point2D.Double d = projection.project(longitude, latitude, new Point2D.Double());

        // shift by 10 to remove negative Xs and Ys
        // scaling by 6000 to make the map bigger
        int magnifiedX = (int) Math.round((10 + d.x) * 6000);
        int magnifiedY = (int) Math.round((10 + d.y) * 6000);

        minX = (minX == -1) ? magnifiedX : Math.min(minX, magnifiedX);
        minY = (minY == -1) ? magnifiedY : Math.min(minY, magnifiedY);

        xys.add(new Point2D.Double(magnifiedX, magnifiedY));
    }

    return xys;
}

...
以下是生成的地图:-

太完美了

更新01-25-2013

下面是基于宽度和高度(以像素为单位)创建图像贴图的代码。在本例中,我不依赖Java Map项目库,而是提取了相关公式并将其嵌入到代码中。与上面依赖任意缩放值的代码示例(上面的示例使用6000)相比,这使您可以更好地控制贴图生成

公共类映射服务{
//更改:要创建的图像的输出路径
私有静态最终字符串IMAGE_FILE_PATH=“/some/user/PATH/map.png”;
//更改此选项:以像素为单位的图像宽度
私有静态最终整型图像\u宽度\u IN_PX=300;
//更改此选项:以像素为单位的图像高度
专用静态最终整型图像高度(单位:像素x=500);
//更改此设置:以像素为单位的最小填充
私有静态最终整数最小值\u图像\u填充\u IN\u PX=50;
//四分之一圆周率公式
私人决赛静态双四分之一PI=Math.PI/4.0;
//提供县边界经纬度数据的服务
私人CountyService CountyService;
public void run()引发异常{
//配置缓冲图像和图形以绘制地图
BufferedImage BufferedImage=新的BufferedImage(图像宽度),
图像高度单位为像素,
BuffereImage.TYPE_INT_RGB);
Graphics2D g=buffereImage.createGraphics();
Map Map=newhashmap();
map.put(RenderingHints.KEY\u插值,RenderingHints.VALUE\u插值双三次);
map.put(RenderingHints.KEY\u RENDERING,RenderingHints.VALUE\u RENDER\u QUALITY);
map.put(RenderingHints.KEY\u抗锯齿,RenderingHints.VALUE\u抗锯齿开);
RenderingHints renderHints=新的RenderingHints(地图);
g、 设置渲染提示(渲染提示);
//最小和最大坐标,用于下面的计算
Point2D.Double minXY=新的Point2D.Double(-1,-1);
Point2D.Double maxXY=新的Point2D.Double(-1,-1);
//县列表,其中每个县包含构成县边界的坐标列表
集合countyBoundaries=newArrayList();
//对于每个县,使用墨卡托投影公式将经度/纬度转换为X/Y
对于(County-County:countyService.GetAllCountries()){
Collection lonLat=new ArrayList();
for(CountyBoundary CountyBoundary:county.getCountyBoundaries()){
//换算成弧度
double longitude=countyBoundary.getLongitude()*Math.PI/180;
双纬度=countyBoundary.getLatitude()*Math.PI/180;
Point2D.Double xy=新的Point2D.Double();
xy.x=经度;
xy.y=Math.log(Math.tan(四分之一π+0.5*纬度));
//我们需要确定最小X和Y值的原因是为了绘制地图,
//我们需要偏移位置,这样就不会有负的X和Y值
minXY.x=(minXY.x==-1)?xy.x:Math.min(minXY.x,xy.x);
minXY.y=(minXY.y==-1)?xy.y:Math.min(minXY.y,xy.y);
lonLat.add(xy);
}
countyBoundaries.add(lonLat);
}
//重新调整坐标以确保没有负值
用于(收集点:countyBoundaries){
用于(点2D。双点:点){
point.x=point.x-minXY.x;
point.y=point.y-minXY.y;
//现在,我们需要跟踪最大X和Y值
maxXY.x=(maxXY.x==-1)?point.x:Math.max(maxXY.x,point.x);
maxXY.y=(maxXY.y==-1)?point.y:Math.max(maxXY.y,point.y);
}
}
int paddingBothSides=最小图像填充像素*2;
//图像上地图的实际绘图空间
int mapWidth=图像宽度,单位为像素-填充两个像素;
int MAPHEIGH=图像高度(单位:像素)-填充两个像素;
//确定宽高比,因为我们需要放大地图以适应给定的图像尺寸
double mapWidthRatio=mapWidth/maxXY.x;
双贴图高度比=贴图高度/maxXY.y;
//使用不同的宽度和高度比率将导致地图被拉伸。因此,我们必须确定
//完全适合给定图像维度的全局比率
双全局比率=毫安
...

Polygon polygon = new Polygon();

for (Point2D.Double point : xys) {
    int adjustedX = (int) (IMAGE_PADDING + (point.getX() - minX));

    // need to invert the Y since 0,0 starts at top left
    int adjustedY = (int) (IMAGE_HEIGHT - IMAGE_PADDING - (point.getY() - minY));

    polygon.addPoint(adjustedX, adjustedY);
}

...
public class MapService {
    // CHANGE THIS: the output path of the image to be created
    private static final String IMAGE_FILE_PATH = "/some/user/path/map.png";

    // CHANGE THIS: image width in pixel
    private static final int IMAGE_WIDTH_IN_PX = 300;

    // CHANGE THIS: image height in pixel
    private static final int IMAGE_HEIGHT_IN_PX = 500;

    // CHANGE THIS: minimum padding in pixel
    private static final int MINIMUM_IMAGE_PADDING_IN_PX = 50;

    // formula for quarter PI
    private final static double QUARTERPI = Math.PI / 4.0;

    // some service that provides the county boundaries data in longitude and latitude
    private CountyService countyService;

    public void run() throws Exception {
        // configuring the buffered image and graphics to draw the map
        BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH_IN_PX,
                                                        IMAGE_HEIGHT_IN_PX,
                                                        BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bufferedImage.createGraphics();
        Map<RenderingHints.Key, Object> map = new HashMap<RenderingHints.Key, Object>();
        map.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        map.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RenderingHints renderHints = new RenderingHints(map);
        g.setRenderingHints(renderHints);

        // min and max coordinates, used in the computation below
        Point2D.Double minXY = new Point2D.Double(-1, -1);
        Point2D.Double maxXY = new Point2D.Double(-1, -1);

        // a list of counties where each county contains a list of coordinates that form the county boundary
        Collection<Collection<Point2D.Double>> countyBoundaries = new ArrayList<Collection<Point2D.Double>>();

        // for every county, convert the longitude/latitude to X/Y using Mercator projection formula
        for (County county : countyService.getAllCounties()) {
            Collection<Point2D.Double> lonLat = new ArrayList<Point2D.Double>();

            for (CountyBoundary countyBoundary : county.getCountyBoundaries()) {
                // convert to radian
                double longitude = countyBoundary.getLongitude() * Math.PI / 180;
                double latitude = countyBoundary.getLatitude() * Math.PI / 180;

                Point2D.Double xy = new Point2D.Double();
                xy.x = longitude;
                xy.y = Math.log(Math.tan(QUARTERPI + 0.5 * latitude));

                // The reason we need to determine the min X and Y values is because in order to draw the map,
                // we need to offset the position so that there will be no negative X and Y values
                minXY.x = (minXY.x == -1) ? xy.x : Math.min(minXY.x, xy.x);
                minXY.y = (minXY.y == -1) ? xy.y : Math.min(minXY.y, xy.y);

                lonLat.add(xy);
            }

            countyBoundaries.add(lonLat);
        }

        // readjust coordinate to ensure there are no negative values
        for (Collection<Point2D.Double> points : countyBoundaries) {
            for (Point2D.Double point : points) {
                point.x = point.x - minXY.x;
                point.y = point.y - minXY.y;

                // now, we need to keep track the max X and Y values
                maxXY.x = (maxXY.x == -1) ? point.x : Math.max(maxXY.x, point.x);
                maxXY.y = (maxXY.y == -1) ? point.y : Math.max(maxXY.y, point.y);
            }
        }

        int paddingBothSides = MINIMUM_IMAGE_PADDING_IN_PX * 2;

        // the actual drawing space for the map on the image
        int mapWidth = IMAGE_WIDTH_IN_PX - paddingBothSides;
        int mapHeight = IMAGE_HEIGHT_IN_PX - paddingBothSides;

        // determine the width and height ratio because we need to magnify the map to fit into the given image dimension
        double mapWidthRatio = mapWidth / maxXY.x;
        double mapHeightRatio = mapHeight / maxXY.y;

        // using different ratios for width and height will cause the map to be stretched. So, we have to determine
        // the global ratio that will perfectly fit into the given image dimension
        double globalRatio = Math.min(mapWidthRatio, mapHeightRatio);

        // now we need to readjust the padding to ensure the map is always drawn on the center of the given image dimension
        double heightPadding = (IMAGE_HEIGHT_IN_PX - (globalRatio * maxXY.y)) / 2;
        double widthPadding = (IMAGE_WIDTH_IN_PX - (globalRatio * maxXY.x)) / 2;

        // for each country, draw the boundary using polygon
        for (Collection<Point2D.Double> points : countyBoundaries) {
            Polygon polygon = new Polygon();

            for (Point2D.Double point : points) {
                int adjustedX = (int) (widthPadding + (point.getX() * globalRatio));

                // need to invert the Y since 0,0 starts at top left
                int adjustedY = (int) (IMAGE_HEIGHT_IN_PX - heightPadding - (point.getY() * globalRatio));

                polygon.addPoint(adjustedX, adjustedY);
            }

            g.drawPolygon(polygon);
        }

        // create the image file
        ImageIO.write(bufferedImage, "PNG", new File(IMAGE_FILE_PATH));
    }
}
double Re = 6378137;
double Rp = 6356752.31424518;

double latrad = lat/180.0*Math.PI;
double lonrad = lon/180.0*Math.PI;

double coslat = Math.cos(latrad);
double sinlat = Math.sin(latrad);
double coslon = Math.cos(lonrad);
double sinlon = Math.sin(lonrad);

double term1 = (Re*Re*coslat)/
  Math.sqrt(Re*Re*coslat*coslat + Rp*Rp*sinlat*sinlat);

double term2 = alt*coslat + term1;

double x=coslon*term2;
double y=sinlon*term2;
double z = alt*sinlat + (Rp*Rp*sinlat)/
  Math.sqrt(Re*Re*coslat*coslat + Rp*Rp*sinlat*sinlat);