【问题标题】:Segment intersection : Mercator projection getting nan value段相交:墨卡托投影得到 nan 值
【发布时间】:2013-12-16 07:08:16
【问题描述】:

我对墨卡托投影有一点问题,当我尝试在纬度上投影时,我得到了 nan 值...

我的代码来自这个问题: ConvertLATLONGTOXY

private static List<Point2D.Double> LatLongConvertionToXY(List<GeoPoint> coordinates) {
        List<Point2D.Double> xys = new ArrayList<Point2D.Double>();

        /*MercatorProjection projection = new MercatorProjection();*/
        Log.i("LATLONG2RAD", "Nouvelle conversion");
        for (GeoPoint coordinate : coordinates) {
            double latitude = Double.valueOf(coordinate.getLatitudeE6());
            double longitude = Double.valueOf(coordinate.getLongitudeE6());

            // convert to radian
            latitude = latitude * Math.PI / 180;
            longitude = longitude * Math.PI / 180;
            Log.i("LATLONG2RAD", String.valueOf(latitude)+" : "+String.valueOf(longitude));
            /*Point2D.Double d = projection.project(longitude,latitude,
                    new Point2D.Double());*/
            Point2D.Double d=new Point2D.Double();
            double QUARTERPI = Math.PI / 4.0;
            d.x = longitude;
            d.y = Math.log(Math.tan(QUARTERPI + 0.5 * latitude));
            Log.i("PointLATLONG YX", String.valueOf(d.y)+" : "+String.valueOf(d.x));
            xys.add(d);
        }

        return xys;
    }

这个返回值的例子: 12-15 21:42:27.165: I/LATLONG2RAD(32629): 新式转换

12-15 21:42:27.165: I/LATLONG2RAD(32629): LAT 782581.6732236275: LONG -10478.398323613315

12-15 21:42:27.165: I/LATLONG2RAD(32629): LAT 782581.6732236275: LONG -10478.398323613315

12-15 21:42:27.165: I/LATLONG2RAD(32629): LAT 782587.2931838189: LONG -10476.478461436123

12-15 21:42:27.165: I/LATLONG2RAD(32629): LAT 782571.9517396939: LONG -10476.478461436123

投影的返回值:

12-15 21:42:27.165:I/PointLATLONG YX(32629):NaN:-10478.398323613315

12-15 21:42:27.165:I/PointLATLONG YX(32629):NaN:-10478.398323613315

12-15 21:42:27.165:I/PointLATLONG YX(32629):NaN:-10476.478461436123

12-15 21:42:27.165:I/PointLATLONG YX(32629):1.7354151627839085:-10476.478461436123

【问题讨论】:

  • Math.log() 值小于 0 将返回 NaN
  • thx 我认为这个算法对我没有帮助,我使用 java 地图库中的墨卡托投影类,这也返回 nan 值,我有点迷茫,你可以在上面的代码中查看在评论中。有什么想法吗?
  • 你没有使用它他在那个帖子中如何使用它他还有其他东西,例如://我们需要确定最小 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);要检查,您应该返回并重新阅读他使用的代码。

标签: android google-maps map geometry openstreetmap


【解决方案1】:

最后,JRowan 没关系,我重新阅读了代码,然后我用带有投影墨卡托的 Java Map Library 重试,我发现我的问题是第一个使用 osm geopoint 到 nan 值的问题,我的值是微度而不是双倍:

我的新代码:

private static List<Point2D.Double> LatLongConvertionToXY(List<GeoPoint> coordinates) {
        List<Point2D.Double> xys = new ArrayList<Point2D.Double>();

        MercatorProjection projection = new MercatorProjection();
        Log.i("LATLONG2RAD", "New");
        for (GeoPoint coordinate : coordinates) {
            double latitude = Double.valueOf(coordinate.getLatitudeE6());
            double longitude = Double.valueOf(coordinate.getLongitudeE6());

            //to decimal
            latitude=latitude / 1E6;
            longitude=longitude / 1E6;
            Log.i("LATLONG2RAD", "BEFORE RADIAN "+String.valueOf(latitude)+" : "+String.valueOf(longitude));
            //convert to radian
            latitude = latitude * Math.PI / 180;
            longitude = longitude * Math.PI / 180;
            //Log.i("LATLONG2RAD", String.valueOf(latitude)+" : "+String.valueOf(longitude));
            Point2D.Double d = projection.project(longitude,latitude,
                    new Point2D.Double());

            Log.i("LATLONG2RAD"," Y X "+ String.valueOf(d.y)+" : "+String.valueOf(d.x));
            xys.add(d);
        }

        return xys;
    }

投影看起来不错:

12-15 22:41:40.257:I/LATLONG2RAD(1138):Y X 0.8505407464143129:0.11730010582132741

12-15 22:41:40.267:I/LATLONG2RAD(1138):Y X 0.8506068875926396:0.11715522604011937

12-15 22:41:40.267:I/LATLONG2RAD(1138):Y X 0.8505923208133851:0.1172139738227415

12-15 22:41:40.267:I/LATLONG2RAD(1138):Y X 0.8505727537288554:0.1172139738227415

但现在另一个 pb 正在计算交集:

public static GeoPoint intersectionV1bis(List<GeoPoint> mGeoPoints) {
        if (mGeoPoints.size() == 0 || mGeoPoints.size() > 4)
            return null;
        List<Point2D.Double> coordinates = LatLongConvertionToXY(mGeoPoints);
        double x1 = coordinates.get(0).x;
        double y1 = coordinates.get(0).y;
        double x2 = coordinates.get(1).x;
        double y2 = coordinates.get(1).y;
        double x3 = coordinates.get(2).x;
        double y3 = coordinates.get(2).y;
        double x4 = coordinates.get(3).x;
        double y4 = coordinates.get(3).y;
        double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (d == 0){
            Log.i("INTERSECT V1","Segments parallels");
            return null;
        }

        double xi = ((x3 - x4) * (x1 * y2 - y1 * x2) - (x1 - x2)
                * (x3 * y4 - y3 * x4))
                / d;
        double yi = ((y3 - y4) * (x1 * y2 - y1 * x2) - (y1 - y2)
                * (x3 * y4 - y3 * x4))
                / d;



        GeoPoint p = new GeoPoint(xi, yi);
        Log.i("INTERSECT","xi long"+String.valueOf(xi)+" : yi lat"+String.valueOf(yi));
        if (xi < Math.min(x1, x2) || xi > Math.max(x1, x2)){
            Log.i("INTERSECT V1","Not in segment 1");
            return null;
        }

        if (xi < Math.min(x3, x4) || xi > Math.max(x3, x4)){
            Log.i("INTERSECT V1","Not in segment 2");
            return null;
        }
        Log.i("INTERSECT","Intersection");
        return p;
    }

12-15 22:41:40.267: I/INTERSECT(1138): xi long0.11721397382272966 : yi lat0.8505800677870533

在开放街道地图上,线段相交,但代码说不,我不明白为什么。点 A 和 B 是我的最后一个新位置,C 和 D 是 E 点左右两侧的 2 个点(通过在第一个十字路口按下按钮来定义,当前位置的方位为 +90 和 -90),即在 A 和 B 之间。

我一直使用模拟器来拥有相同的位置,但是当我尝试在 A 和 B 之间以及 C 和 D 之间的第二次交叉处计算 E 时,E 不在段 C 和 D 上

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多