Package net.sourceforge.gpstools

Source Code of net.sourceforge.gpstools.Elevations

package net.sourceforge.gpstools;

/* gpsdings
* Copyright (C) 2011 Moritz Ringler
* $Id: GpxOVL.java 441 2010-12-13 20:04:20Z ringler $
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;

import net.sourceforge.gpstools.dem.DEMException;
import net.sourceforge.gpstools.dem.ElevationModel;
import net.sourceforge.gpstools.dem.HgtElevationModel;
import net.sourceforge.gpstools.gpx.Gpx;
import net.sourceforge.gpstools.gpx.GpxType;
import net.sourceforge.gpstools.gpx.Rte;
import net.sourceforge.gpstools.gpx.Trk;
import net.sourceforge.gpstools.gpx.Wpt;
import net.sourceforge.gpstools.gpx.WptType;
import net.sourceforge.gpstools.utils.GPXVisitor;
import net.sourceforge.gpstools.utils.GPXVisitorException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Options;

public class Elevations extends GPSDings implements Closeable {
    private final ElevationModel elevationModel;
    private File outputDir;

    private boolean processWpt = false;
    private boolean processTrkpt = false;
    private boolean processRtept = false;

    private boolean overwrite = false;

    public Elevations() throws IOException {
        this.elevationModel = new HgtElevationModel();
    }

    public Elevations(ElevationModel model) {
        if (model == null) {
            throw new IllegalArgumentException("Model may not be null.");
        }

        this.elevationModel = model;
    }

    public void addElevations(File[] input) throws IOException, DEMException {
        for (File f : input) {
            try {
                InputStream in = new FileInputStream(f);
                try {
                    Gpx gpx = GPSDings.readGPX(in);
                    this.addElevations(gpx);
                    File outFile = new File(this.getOutputDirectory(),
                            f.getName());
                    OutputStream out = new FileOutputStream(outFile);
                    try {
                        GPSDings.writeGPX(gpx, out);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            } catch (IOException ex) {
                System.err.println(ex.getLocalizedMessage());
            }
        }
    }

    public ElevationModel getElevationModel() {
        return this.elevationModel;
    }

    public void setOutputDirectory(File dir) {
        this.outputDir = dir;
    }

    public File getOutputDirectory() {
        return this.outputDir;
    }

    /**
     * @param overwrite
     *            the overwrite to set
     */
    public void setOverwrite(boolean overwrite) {
        this.overwrite = overwrite;
    }

    /**
     * @return the overwrite
     */
    public boolean isOverwrite() {
        return overwrite;
    }

    /**
     * @param processRtept
     *            the processRtept to set
     */
    public void setProcessRtept(boolean processRtept) {
        this.processRtept = processRtept;
    }

    /**
     * @return the processRtept
     */
    public boolean getProcessRtept() {
        return processRtept;
    }

    /**
     * @param processTrkpt
     *            the processTrkpt to set
     */
    public void setProcessTrkpt(boolean processTrkpt) {
        this.processTrkpt = processTrkpt;
    }

    /**
     * @return the processTrkpt
     */
    public boolean getProcessTrkpt() {
        return processTrkpt;
    }

    /**
     * @param processWpt
     *            the processWpt to set
     */
    public void setProcessWpt(boolean processWpt) {
        this.processWpt = processWpt;
    }

    /**
     * @return the processWpt
     */
    public boolean getProcessWpt() {
        return processWpt;
    }

    @Override
    public void close() throws IOException {
        if (this.elevationModel != null) {
            this.elevationModel.close();
        }
    }

    public void setCacheDirectory(File value) {
        if (this.elevationModel instanceof HgtElevationModel)
        {
            ((HgtElevationModel)this.elevationModel).setCacheDir(value);
        }
    }

    public File getCacheDirectory() {
        return (this.elevationModel instanceof HgtElevationModel)
            ? ((HgtElevationModel)this.elevationModel).getCacheDir()
            : null;
    }

    public void addElevations(GpxType xgpx) throws DEMException {
        try {
            (new Visitor()).visit(xgpx);
        } catch (GPXVisitorException e) {
            throw (DEMException) e.getCause();
        }
    }

    public void addElevation(WptType pt) throws DEMException {
        BigDecimal ele = pt.getEle();
        if (ele == null || this.overwrite) {
            ele = this.elevationModel.getElevation(pt.getLat(), pt.getLon());
            pt.setEle(ele);
        }
    }

    public static void main(String[] argv) throws IOException {
        (new ElevationsCommandLine(argv)).execute();
    }

    private class Visitor extends GPXVisitor {
        @Override
        public void visit(Wpt pt) throws GPXVisitorException {
            if (Elevations.this.processWpt) {
                this.visitPt(pt);
            }
        }

        @Override
        public void visit(Rte rte) throws GPXVisitorException {
            if (Elevations.this.processRtept) {
                super.visit(rte);
            }
        }

        @Override
        public void visit(Trk trk) throws GPXVisitorException {
            if (Elevations.this.processTrkpt) {
                super.visit(trk);
            }
        }

        @Override
        public void visitPt(WptType pt) throws GPXVisitorException {
            try {
                Elevations.this.addElevation(pt);
            } catch (DEMException fee) {
                throw new GPXVisitorException(pt, fee.getMessage(), fee);
            }
        }
    }

    private static class ElevationsCommandLine extends
            AbstractCommandLine<Elevations> {
        public static final String OPT_TO_DIR = "todir";
        public static final String OPT_WPT = "Wpt";
        public static final String OPT_TRKPT = "Trkpt";
        public static final String OPT_RTEPT = "Rtept";
        public static final String OPT_OVERWRITE = "Overwrite";
        public static final String OPT_CACHE_DIR = "cache-dir";

        @Override
        public void printHelp(PrintStream out) {
            try {
                super.printHelp(out);
                cat("elevations.txt");
                out.println();
                out.println("Data source: " + this.app.getElevationModel().getInfo());
                out.println("System information:");
                out.println("Java version: "
                        + System.getProperty("java.version"));
            } catch (Exception ex) {
                throw new Error(ex);
            }
        }

        public ElevationsCommandLine(String[] argv) throws IOException {
            super(new Elevations(), argv);
        }

        @Override
        public void execute() {
            try {
                // Locale.setDefault(Locale.US);
                processArguments();

                String[] inp = getInput();

                if (inp.length == 0 || inp.length == 1 && "-".equals(inp[0])) {
                    GpxType xgpx = GPSDings.readGPX(System.in);
                    try {
                        this.app.addElevations(xgpx);
                        GPSDings.writeGPX(xgpx, System.out);
                    } finally {
                        this.app.close();
                    }
                } else {
                    File[] input = getInputFiles();
                    if (input == null) {
                        System.exit(1);
                    }

                    if (this.app.getOutputDirectory() == null) {
                        throw new MissingOptionException(OPT_TO_DIR);
                    }

                    app.addElevations(input);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                System.err.println(ex.getClass().getName() + ": "
                        + ex.getMessage());
                System.exit(1);
            }
        }

        @Override
        protected Options createOptions() {
            Options options = super.createOptions();
            options.addOption(makeOption(OPT_TO_DIR, File.class, 1, "directory"));
            options.addOption(makeOption(OPT_CACHE_DIR, File.class, 1, "directory"));
            options.addOption(makeOption(OPT_WPT, boolean.class, 0, null));
            options.addOption(makeOption(OPT_TRKPT, boolean.class, 0, null));
            options.addOption(makeOption(OPT_RTEPT, boolean.class, 0, null));
            options.addOption(makeOption(OPT_OVERWRITE, boolean.class, 0, null));
            return options;
        }

        @Override
        protected CommandLine processOptions(Options options)
                throws org.apache.commons.cli.ParseException, IOException,
                java.text.ParseException {
            CommandLine cl = super.processOptions(options);
            char c;

            /* todir option */
            opt = OPT_TO_DIR;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                File outputdir = new File(cl.getOptionValue(c));
                if (!outputdir.isDirectory() || outputdir.mkdirs()) {
                    throw new FileNotFoundException(
                            "Cannot neither access nor create directory "
                                    + outputdir.getAbsolutePath());
                }
                app.setOutputDirectory(outputdir);
            }
           
            opt = OPT_CACHE_DIR;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                File cacheDir = new File(cl.getOptionValue(c));
                if (!cacheDir.isDirectory() || cacheDir.mkdirs()) {
                    throw new FileNotFoundException(
                            "Cannot neither access nor create directory "
                                    + cacheDir.getAbsolutePath());
                }
               
                app.setCacheDirectory(cacheDir);
            }
           
            boolean isNoOp = true;

            opt = OPT_WPT;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                app.setProcessWpt(true);
                isNoOp = false;
            }

            opt = OPT_TRKPT;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                app.setProcessTrkpt(true);
                isNoOp = false;
            }

            opt = OPT_RTEPT;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                app.setProcessRtept(true);
                isNoOp = false;
            }
           
            if (isNoOp)
            {
                throw new MissingOptionException("You must provide at least one of the -T, -R, -W options.");
            }           
           
            opt = OPT_OVERWRITE;
            c = opt.charAt(0);
            if (cl.hasOption(c)) {
                app.setOverwrite(true);
            }

            return cl;
        }
    }
}
TOP

Related Classes of net.sourceforge.gpstools.Elevations

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview'); i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');