package net.sourceforge.gpstools.utils;
/* gpsdings
* Copyright (C) 2009 Moritz Ringler
* $Id: GPXUtils.java 358 2008-11-24 19:06:17Z 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 net.sourceforge.gpstools.gpx.*;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.util.Enumeration;
import com.jhlabs.map.proj.Projection;
import com.jhlabs.map.proj.OrthographicAzimuthalProjection;
public class GPXPath extends Path2D.Float{
/**
*
*/
private static final long serialVersionUID = -8139244117306999026L;
final private Projection proj;
final private GPXVisitor visitor = new GPXVisitor(){
@Override
public void visit(Trkseg seg){
final Enumeration<? extends Trkpt> eTrkpt = seg.enumerateTrkpt();
if (eTrkpt.hasMoreElements()){
moveTo(eTrkpt.nextElement());
}
while(eTrkpt.hasMoreElements()){
lineTo(eTrkpt.nextElement());
}
}
};
public GPXPath(Projection proj){
super();
this.proj = proj;
}
public GPXPath(Iterable<GpxType> igpx){
this(makeOrthogonalProjection(igpx));
for (GpxType gpx:igpx){
try {
add(gpx);
} catch (GPXVisitorException ex)
{
// should never happen.
throw new Error(ex);
}
}
}
private static Projection makeOrthogonalProjection(Iterable<GpxType> igpx){
final GPXBounds bounds = new GPXBounds(){
@Override
public void visit(Rte rte){
// do nothing.
}
@Override
public void visit(Wpt pt){
// do nothing.
}
};
for (GpxType gpx:igpx){
try
{
bounds.visit(gpx);
}
catch (GPXVisitorException ex)
{
// should never happen.
throw new Error(ex);
}
}
final Rectangle2D bnd = bounds.getBounds();
final Projection proj = new OrthographicAzimuthalProjection();
proj.setProjectionLongitudeDegrees(bnd.getX() + bnd.getWidth()/2);
proj.setProjectionLatitudeDegrees(bnd.getY() + bnd.getHeight()/2);
proj.initialize();
return proj;
}
public void add(GpxType gpx) throws GPXVisitorException{
visitor.visit(gpx);
}
public void add(Trk trk) throws GPXVisitorException {
visitor.visit(trk);
}
public void add(Trkseg seg) throws GPXVisitorException{
visitor.visit(seg);
}
public Point2D project(WptType pt){
Point2D.Double in = new Point2D.Double(pt.getLon().doubleValue(), pt.getLat().doubleValue());
return proj.transform(in, in);
}
public void moveTo(WptType pt){
Point2D xy = project(pt);
moveTo((float) xy.getX(), (float) xy.getY());
}
public void lineTo(WptType pt){
Point2D xy = project(pt);
lineTo((float) xy.getX(), (float) xy.getY());
}
}