0

I have a little problem with mercator projection, i'm getting nan value when i try projection on latitude...

My codederived from this question : 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;
    }

This example of returned value : 12-15 21:42:27.165: I/LATLONG2RAD(32629): Nouvelle conversion

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

Returned values for projection :

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

Community
  • 1
  • 1
WhatsUp
  • 453
  • 1
  • 5
  • 21
  • Math.log() with a value less than 0 will return NaN – JRowan Dec 15 '13 at 21:02
  • thx i think this algo will no help me, i use the mercator projection class from java map library, and this return nan value too, i'm little lost, you can look this in this above code in commentary. Any idea ? – WhatsUp Dec 15 '13 at 21:06
  • 1
    your not using it how he uses it in that post he has other things like :// 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); to check, you should go back and reread the code he uses. – JRowan Dec 15 '13 at 21:21

1 Answers1

1

Finally, JRowan it s ok i re read the code , and i retry with Java Map Library with projection mercator, and i find my problem the first to nan value with osm geopoint my values is in micro degrees not in double :

My new code :

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;
    }

The projection seems to be good :

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

But now the other pb is with the compute of intersection :

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

On the Open street map the lines segments intersect but the code say no, i dont understand why. The point A and B are my last and new location and C and D are 2 points at left and right of a point E(define by press of a button on the first crossing with bearing +90 and -90 of current location) that is between A and B.

I use emulator to have sames locations all the time but when i try to compute E on the 2nd crossing between A and B and betweeen C and D, E is not on segment C and D

WhatsUp
  • 453
  • 1
  • 5
  • 21