Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern ICAO_GPS_PATTERN = Pattern.compile("(([^@]*)@)?"
            + "([0-8][0-9])([0-5][0-9])([0-5][0-9])([NSns])" + "([01][0-9][0-9])([0-5][0-9])([0-5][0-9])([EWew])");

    public static String decodeGpsAddress(String name, double coords[]) {
        /*
         * Match predictable GPS pattern of DDMMSS[N|S]DDDMMSS[E|W]
         */
        Matcher m = ICAO_GPS_PATTERN.matcher(name);
        if (m.matches()) {
            String label;
            try {
                label = m.group(1) == null ? "" : m.group(1);
                double lat_deg = Double.parseDouble(m.group(3)), lat_min = Double.parseDouble(m.group(4)),
                        lat_sec = Double.parseDouble(m.group(5)),
                        lat_south = m.group(6).equalsIgnoreCase("S") ? -1 : 1,
                        lon_deg = Double.parseDouble(m.group(7)), lon_min = Double.parseDouble(m.group(8)),
                        lon_sec = Double.parseDouble(m.group(9)),
                        lon_west = m.group(10).equalsIgnoreCase("W") ? -1 : 1;
                coords[0] = lon_west * truncGeo(lon_deg + lon_min / 60.0 + lon_sec / (60.0 * 60.0));
                coords[1] = lat_south * truncGeo(lat_deg + lat_min / 60.0 + lat_sec / (60.0 * 60.0));
            } catch (Exception e) {
                return null;
            }
            /*
             * Sane input
             */
            if ((!isLatitudeSane(coords[1])) || (!isLongitudeSane(coords[0]))) {
                return null;
            }
            return label;
        } else if (name.contains("&")) {
            String token[] = new String[2];
            token[1] = token[0] = name;
            if (name.contains("@")) {
                /*
                 * This could be the geo point from maps
                 */
                token = name.split("@");
            }
            /*
             * This is lon/lat destination
             */
            String tokens[] = token[1].split("&");

            try {
                coords[0] = Double.parseDouble(tokens[1]);
                coords[1] = Double.parseDouble(tokens[0]);
            } catch (Exception e) {
                return null;
            }

            /*
             * Sane input
             */
            if ((!isLatitudeSane(coords[1])) || (!isLongitudeSane(coords[0]))) {
                return null;
            }

            return token[0];

        }
        return null;
    }

    /**
     * 
     * @param lonlat
     */
    public static double truncGeo(double lonlat) {
        lonlat *= 10000;
        lonlat = Math.round(lonlat);
        lonlat /= 10000;
        return lonlat;
    }

    /**
     * 
     * @param lat
     * @return
     */
    public static boolean isLatitudeSane(double lat) {
        return (lat > -90) && (lat < 90);
    }

    /**
     * 
     * @param lon
     * @return
     */
    public static boolean isLongitudeSane(double lon) {
        return (lon < 180) && (lon > -180);
    }
}