Search in sources :

Example 6 with LatLonPoint

use of com.jwetherell.openmap.common.LatLonPoint in project Osmand by osmandapp.

the class PointDescription method getLocationName.

public static String getLocationName(Context ctx, double lat, double lon, boolean sh) {
    OsmandSettings st = ((OsmandApplication) ctx.getApplicationContext()).getSettings();
    int f = st.COORDINATES_FORMAT.get();
    if (f == PointDescription.UTM_FORMAT) {
        UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon));
        return pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " " + ((long) pnt.northing);
    } else if (f == PointDescription.OLC_FORMAT) {
        try {
            return getLocationOlcName(lat, lon);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "0, 0";
        }
    } else {
        try {
            return ctx.getString(sh ? R.string.short_location_on_map : R.string.location_on_map, LocationConvert.convert(lat, f), LocationConvert.convert(lon, f));
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ctx.getString(sh ? R.string.short_location_on_map : R.string.location_on_map, 0, 0);
        }
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint) OsmandSettings(net.osmand.plus.OsmandSettings) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint)

Example 7 with LatLonPoint

use of com.jwetherell.openmap.common.LatLonPoint in project Osmand by osmandapp.

the class PointDescription method getLocationName.

public static String getLocationName(SampleApplication ctx, double lat, double lon, boolean sh) {
    SampleApplication app = SampleApplication.getApp(ctx);
    int f = app.getCoordinatesFormat();
    if (f == PointDescription.UTM_FORMAT) {
        UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon));
        return pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " " + ((long) pnt.northing);
    } else {
        try {
            return ctx.getString(sh ? "short_location_on_map" : "location_on_map", LocationConvert.convert(lat, f), LocationConvert.convert(lon, f));
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ctx.getString(sh ? "short_location_on_map" : "location_on_map", 0, 0);
        }
    }
}
Also used : SampleApplication(net.osmand.core.samples.android.sample1.SampleApplication) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint)

Example 8 with LatLonPoint

use of com.jwetherell.openmap.common.LatLonPoint in project Osmand by osmandapp.

the class LocationParser method parseLocation.

public static LatLon parseLocation(String locPhrase) {
    locPhrase = locPhrase.trim();
    // avoid throwing exceptions by carefully checking exceptions
    if (locPhrase.length() > 0 && OpenLocationCode.isValidCode(locPhrase)) {
        OpenLocationCode olc = new OpenLocationCode(locPhrase);
        if (olc.isFull()) {
            OpenLocationCode.CodeArea codeArea = olc.decode();
            return new LatLon(codeArea.getCenterLatitude(), codeArea.getCenterLongitude());
        }
    }
    if (locPhrase.length() == 0 || !(locPhrase.charAt(0) == '-' || Character.isDigit(locPhrase.charAt(0)) || locPhrase.charAt(0) == 'S' || locPhrase.charAt(0) == 's' || locPhrase.charAt(0) == 'N' || locPhrase.charAt(0) == 'n' || locPhrase.contains("://"))) {
        return null;
    }
    List<Double> d = new ArrayList<>();
    List<Object> all = new ArrayList<>();
    List<String> strings = new ArrayList<>();
    splitObjects(locPhrase, d, all, strings);
    if (d.size() == 0) {
        return null;
    }
    // detect UTM
    if (all.size() == 4 && d.size() == 3 && all.get(1) instanceof String) {
        char ch = all.get(1).toString().charAt(0);
        if (Character.isLetter(ch)) {
            UTMPoint upoint = new UTMPoint(d.get(2), d.get(1), d.get(0).intValue(), ch);
            LatLonPoint ll = upoint.toLatLonPoint();
            return new LatLon(ll.getLatitude(), ll.getLongitude());
        }
    }
    if (all.size() == 3 && d.size() == 2 && all.get(1) instanceof String) {
        char ch = all.get(1).toString().charAt(0);
        String combined = strings.get(2);
        if (Character.isLetter(ch)) {
            try {
                String east = combined.substring(0, combined.length() / 2);
                String north = combined.substring(combined.length() / 2, combined.length());
                UTMPoint upoint = new UTMPoint(Double.parseDouble(north), Double.parseDouble(east), d.get(0).intValue(), ch);
                LatLonPoint ll = upoint.toLatLonPoint();
                return new LatLon(ll.getLatitude(), ll.getLongitude());
            } catch (NumberFormatException e) {
            }
        }
    }
    // try to find split lat/lon position
    int jointNumbers = 0;
    int lastJoin = 0;
    int degSplit = -1;
    // 0 - degree, 1 - minutes, 2 - seconds
    int degType = -1;
    boolean finishDegSplit = false;
    int northSplit = -1;
    int eastSplit = -1;
    for (int i = 1; i < all.size(); i++) {
        if (all.get(i - 1) instanceof Double && all.get(i) instanceof Double) {
            jointNumbers++;
            lastJoin = i;
        }
        if (all.get(i).equals("n") || all.get(i).equals("s") || all.get(i).equals("N") || all.get(i).equals("S")) {
            northSplit = i + 1;
        }
        if (all.get(i).equals("e") || all.get(i).equals("w") || all.get(i).equals("E") || all.get(i).equals("W")) {
            eastSplit = i;
        }
        int dg = -1;
        if (all.get(i).equals("°")) {
            dg = 0;
        } else if (all.get(i).equals("\'") || all.get(i).equals("′")) {
            dg = 1;
        } else if (all.get(i).equals("″") || all.get(i).equals("\"")) {
            dg = 2;
        }
        if (dg != -1) {
            if (!finishDegSplit) {
                if (degType < dg) {
                    degSplit = i + 1;
                    degType = dg;
                } else {
                    finishDegSplit = true;
                    degType = dg;
                }
            } else {
                if (degType < dg) {
                    degType = dg;
                } else {
                    // reject delimiter
                    degSplit = -1;
                }
            }
        }
    }
    int split = -1;
    if (jointNumbers == 1) {
        split = lastJoin;
    }
    if (northSplit != -1 && northSplit < all.size() - 1) {
        split = northSplit;
    } else if (eastSplit != -1 && eastSplit < all.size() - 1) {
        split = eastSplit;
    } else if (degSplit != -1 && degSplit < all.size() - 1) {
        split = degSplit;
    }
    if (split != -1) {
        double lat = parse1Coordinate(all, 0, split);
        double lon = parse1Coordinate(all, split, all.size());
        return new LatLon(lat, lon);
    }
    if (d.size() == 2) {
        return new LatLon(d.get(0), d.get(1));
    }
    // simple url case
    if (locPhrase.contains("://")) {
        double lat = 0;
        double lon = 0;
        boolean only2decimals = true;
        for (int i = 0; i < d.size(); i++) {
            if (d.get(i).doubleValue() != d.get(i).intValue()) {
                if (lat == 0) {
                    lat = d.get(i);
                } else if (lon == 0) {
                    lon = d.get(i);
                } else {
                    only2decimals = false;
                }
            }
        }
        if (lat != 0 && lon != 0 && only2decimals) {
            return new LatLon(lat, lon);
        }
    }
    // split by equal number of digits
    if (d.size() > 2 && d.size() % 2 == 0) {
        int ind = d.size() / 2 + 1;
        int splitEq = -1;
        for (int i = 0; i < all.size(); i++) {
            if (all.get(i) instanceof Double) {
                ind--;
            }
            if (ind == 0) {
                splitEq = i;
                break;
            }
        }
        if (splitEq != -1) {
            double lat = parse1Coordinate(all, 0, splitEq);
            double lon = parse1Coordinate(all, splitEq, all.size());
            return new LatLon(lat, lon);
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) UTMPoint(com.jwetherell.openmap.common.UTMPoint) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint) LatLon(net.osmand.data.LatLon) OpenLocationCode(com.google.openlocationcode.OpenLocationCode) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint)

Example 9 with LatLonPoint

use of com.jwetherell.openmap.common.LatLonPoint in project Osmand by osmandapp.

the class PointDescription method getLocationNamePlain.

public static String getLocationNamePlain(Context ctx, double lat, double lon) {
    SampleApplication app = SampleApplication.getApp(ctx);
    int f = app.getCoordinatesFormat();
    if (f == PointDescription.UTM_FORMAT) {
        UTMPoint pnt = new UTMPoint(new LatLonPoint(lat, lon));
        return pnt.zone_number + "" + pnt.zone_letter + " " + ((long) pnt.easting) + " " + ((long) pnt.northing);
    } else {
        try {
            return LocationConvert.convert(lat, f) + ", " + LocationConvert.convert(lon, f);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "0, 0";
        }
    }
}
Also used : SampleApplication(net.osmand.core.samples.android.sample1.SampleApplication) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) UTMPoint(com.jwetherell.openmap.common.UTMPoint)

Aggregations

LatLonPoint (com.jwetherell.openmap.common.LatLonPoint)9 UTMPoint (com.jwetherell.openmap.common.UTMPoint)9 EditText (android.widget.EditText)3 LatLon (net.osmand.data.LatLon)3 SuppressLint (android.annotation.SuppressLint)2 SampleApplication (net.osmand.core.samples.android.sample1.SampleApplication)2 OsmandApplication (net.osmand.plus.OsmandApplication)2 OsmandSettings (net.osmand.plus.OsmandSettings)2 OpenLocationCode (com.google.openlocationcode.OpenLocationCode)1 ArrayList (java.util.ArrayList)1