Skip to content
Snippets Groups Projects
Commit 50e2bfd1 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Support for importing GPX files. Both core and UI code.

parent 7f191d31
Branches issue-13-gpx
No related tags found
No related merge requests found
Showing
with 615 additions and 58 deletions
...@@ -25,6 +25,18 @@ ...@@ -25,6 +25,18 @@
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.9.3</version> <version>2.9.3</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jenetics/jpx -->
<dependency>
<groupId>io.jenetics</groupId>
<artifactId>jpx</artifactId>
<version>1.2.2</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
......
...@@ -4,21 +4,19 @@ import java.io.File; ...@@ -4,21 +4,19 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Collectors;
import tdt4140.gr1800.app.gpx.GpxDocumentConverter;
import tdt4140.gr1800.app.json.GeoLocationsJsonPersistence; import tdt4140.gr1800.app.json.GeoLocationsJsonPersistence;
public class App { public class App {
private GeoLocationsPersistence geoLocationsLoader = new GeoLocationsJsonPersistence(); private GeoLocationsPersistence geoLocationsLoader = new GeoLocationsJsonPersistence();
public void loadGeoLocations(URI uri) throws Exception { private Collection<GeoLocations> geoLocations = null;
geoLocations = geoLocationsLoader.loadLocations(uri.toURL().openStream());
}
private Collection<GeoLocations> geoLocations;
public Iterable<String> getGeoLocationNames() { public Iterable<String> getGeoLocationNames() {
Collection<String> names = new ArrayList<String>(geoLocations != null ? geoLocations.size() : 0); Collection<String> names = new ArrayList<String>(geoLocations != null ? geoLocations.size() : 0);
...@@ -69,8 +67,55 @@ public class App { ...@@ -69,8 +67,55 @@ public class App {
return result; return result;
} }
public void setGeoLocations(Collection<GeoLocations> geoLocations) {
this.geoLocations = new ArrayList<>(geoLocations);
fireGeoLocationsUpdated(null);
}
public void addGeoLocations(GeoLocations geoLocations) {
if (hasGeoLocations(geoLocations.getName())) {
throw new IllegalArgumentException("Duplicate geo-locations name: " + geoLocations.getName());
}
this.geoLocations.add(geoLocations);
fireGeoLocationsUpdated(geoLocations);
}
public void removeGeoLocations(GeoLocations geoLocations) {
this.geoLocations.remove(geoLocations);
fireGeoLocationsUpdated(geoLocations);
}
private Collection<IGeoLocationsListener> geoLocationsListeners = new ArrayList<>();
public void addGeoLocationsListener(IGeoLocationsListener listener) {
geoLocationsListeners.add(listener);
}
public void removeGeoLocationsListener(IGeoLocationsListener listener) {
geoLocationsListeners.remove(listener);
}
protected void fireGeoLocationsUpdated(GeoLocations geoLocations) {
for (IGeoLocationsListener listener : geoLocationsListeners) {
listener.geoLocationsUpdated(geoLocations);
}
}
// //
private IDocumentPersistence<Collection<GeoLocations>, File> documentPersistence = new IDocumentPersistence<Collection<GeoLocations>, File>() {
@Override
public Collection<GeoLocations> loadDocument(File documentLocation) throws Exception {
return geoLocationsLoader.loadLocations(new FileInputStream(documentLocation));
}
@Override
public void saveDocument(Collection<GeoLocations> document, File documentLocation) throws Exception {
geoLocationsLoader.saveLocations(document, new FileOutputStream(documentLocation));
}
};
private DocumentStorageImpl<Collection<GeoLocations>, File> documentStorage = new DocumentStorageImpl<Collection<GeoLocations>, File>() { private DocumentStorageImpl<Collection<GeoLocations>, File> documentStorage = new DocumentStorageImpl<Collection<GeoLocations>, File>() {
@Override @Override
...@@ -80,7 +125,7 @@ public class App { ...@@ -80,7 +125,7 @@ public class App {
@Override @Override
protected void setDocument(Collection<GeoLocations> document) { protected void setDocument(Collection<GeoLocations> document) {
geoLocations = document; setGeoLocations(document);
} }
@Override @Override
...@@ -88,26 +133,43 @@ public class App { ...@@ -88,26 +133,43 @@ public class App {
return new ArrayList<GeoLocations>(); return new ArrayList<GeoLocations>();
} }
@Override public Collection<GeoLocations> loadDocument(File documentLocation) throws Exception {
protected Collection<GeoLocations> loadDocument(File file) throws IOException { return documentPersistence.loadDocument(documentLocation);
try {
return geoLocationsLoader.loadLocations(new FileInputStream(file));
} catch (Exception e) {
throw new IOException(e);
}
} }
@Override public void saveDocument(Collection<GeoLocations> document, File documentLocation) throws Exception {
protected void storeDocument(Collection<GeoLocations> document, File file) throws IOException { documentPersistence.saveDocument(document, documentLocation);
try { }
geoLocationsLoader.saveLocations(document, new FileOutputStream(file));
} catch (Exception e) { public Collection<IDocumentImporter<File>> getDocumentImporters() {
throw new IOException(e); return documentLoaders.stream().map(loader -> new IDocumentImporter<File>() {
} @Override
public void importDocument(File file) throws IOException {
try {
setDocumentAndLocation(loader.loadDocument(file), null);
} catch (Exception e) {
throw new IOException(e);
}
}
}).collect(Collectors.toList());
} }
}; };
public IDocumentStorage<File> getDocumentStorage() { public IDocumentStorage<File> getDocumentStorage() {
return documentStorage; return documentStorage;
} }
private Collection<IDocumentLoader<Collection<GeoLocations>, File>> documentLoaders = Arrays.asList(
new IDocumentLoader<Collection<GeoLocations>, File>() {
private GpxDocumentConverter gpxConverter = new GpxDocumentConverter();
@Override
public Collection<GeoLocations> loadDocument(File documentLocation) throws Exception {
return gpxConverter.loadDocument(documentLocation);
}
}
);
public Iterable<IDocumentLoader<Collection<GeoLocations>, File>> getDocumentLoaders() {
return documentLoaders;
}
} }
...@@ -2,7 +2,7 @@ package tdt4140.gr1800.app.core; ...@@ -2,7 +2,7 @@ package tdt4140.gr1800.app.core;
import java.io.IOException; import java.io.IOException;
public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> { public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L>, IDocumentPersistence<D, L> {
private L documentLocation; private L documentLocation;
...@@ -25,8 +25,6 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> { ...@@ -25,8 +25,6 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> {
protected abstract void setDocument(D document); protected abstract void setDocument(D document);
protected abstract D createDocument(); protected abstract D createDocument();
protected abstract D loadDocument(L storage) throws IOException;
protected abstract void storeDocument(D document, L storage) throws IOException;
@Override @Override
public void newDocument() { public void newDocument() {
...@@ -35,12 +33,20 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> { ...@@ -35,12 +33,20 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> {
@Override @Override
public void openDocument(L storage) throws IOException { public void openDocument(L storage) throws IOException {
setDocumentAndLocation(loadDocument(storage), storage); try {
setDocumentAndLocation(loadDocument(storage), storage);
} catch (Exception e) {
throw new IOException(e);
}
} }
@Override @Override
public void saveDocument() throws IOException { public void saveDocument() throws IOException {
storeDocument(getDocument(), getDocumentLocation()); try {
saveDocument(getDocument(), getDocumentLocation());
} catch (Exception e) {
throw new IOException(e);
}
} }
public void saveDocumentAs(L documentLocation) throws IOException { public void saveDocumentAs(L documentLocation) throws IOException {
...@@ -54,7 +60,7 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> { ...@@ -54,7 +60,7 @@ public abstract class DocumentStorageImpl<D, L> implements IDocumentStorage<L> {
} }
} }
public void saveCopyAs(L documentLocation) throws IOException { public void saveCopyAs(L documentLocation) throws Exception {
storeDocument(getDocument(), documentLocation); saveDocument(getDocument(), documentLocation);
} }
} }
package tdt4140.gr1800.app.core;
public interface GeoLocated {
public LatLong getLatLong();
default double getLatitude() {
return getLatLong().latitude;
}
default double getLongitude() {
return getLatLong().longitude;
}
default boolean equalsLatLong(GeoLocated geoLoc) {
return getLatitude() == geoLoc.getLatitude() && getLongitude() == geoLoc.getLongitude();
}
default double distance(GeoLocated geoLoc) {
return getLatLong().distance(geoLoc.getLatLong());
}
}
...@@ -7,7 +7,7 @@ import java.util.Iterator; ...@@ -7,7 +7,7 @@ import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class GeoLocations implements Iterable<LatLong> { public class GeoLocations implements Iterable<GeoLocated> {
private String name; private String name;
...@@ -19,7 +19,7 @@ public class GeoLocations implements Iterable<LatLong> { ...@@ -19,7 +19,7 @@ public class GeoLocations implements Iterable<LatLong> {
this.name = name; this.name = name;
} }
private Collection<LatLong> locations = new ArrayList<LatLong>(); private Collection<GeoLocated> locations = new ArrayList<GeoLocated>();
private boolean path = false; private boolean path = false;
public GeoLocations(LatLong...latLongs) { public GeoLocations(LatLong...latLongs) {
...@@ -27,7 +27,7 @@ public class GeoLocations implements Iterable<LatLong> { ...@@ -27,7 +27,7 @@ public class GeoLocations implements Iterable<LatLong> {
addLocation(latLongs[i]); addLocation(latLongs[i]);
} }
} }
public GeoLocations(String name, LatLong...latLongs) { public GeoLocations(String name, LatLong...latLongs) {
this(latLongs); this(latLongs);
setName(name); setName(name);
...@@ -41,34 +41,34 @@ public class GeoLocations implements Iterable<LatLong> { ...@@ -41,34 +41,34 @@ public class GeoLocations implements Iterable<LatLong> {
this.path = path; this.path = path;
} }
public Comparator<? super LatLong> closestComparator(LatLong latLong) { public Comparator<? super GeoLocated> closestComparator(GeoLocated latLong) {
return (latLong1, latLong2) -> (int) Math.signum(latLong.distance(latLong1) - latLong.distance(latLong2)); return (geoLoc1, geoLoc2) -> (int) Math.signum(latLong.distance(geoLoc1) - latLong.distance(geoLoc2));
} }
public Collection<LatLong> findLocationsNearby(LatLong latLong, double distance) { public Collection<GeoLocated> findLocationsNearby(GeoLocated geoLoc, double distance) {
return locations.stream() return locations.stream()
.filter(latLong2 -> distance == 0.0 ? latLong2.equals(latLong) : latLong.distance(latLong2) <= distance) .filter(geoLoc2 -> distance == 0.0 ? geoLoc2.equalsLatLong(geoLoc) : geoLoc.distance(geoLoc2) <= distance)
.sorted(closestComparator(latLong)) .sorted(closestComparator(geoLoc))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public LatLong findNearestLocation(LatLong latLong) { public GeoLocated findNearestLocation(GeoLocated geoLoc) {
Optional<LatLong> min = locations.stream() Optional<GeoLocated> min = locations.stream()
.min(closestComparator(latLong)); .min(closestComparator(geoLoc));
return min.isPresent() ? min.get() : null; return min.isPresent() ? min.get() : null;
} }
// //
public void addLocation(LatLong latLong) { public void addLocation(LatLong geoLoc) {
locations.add(latLong); locations.add(geoLoc);
} }
public void removeLocations(LatLong latLong, double distance) { public void removeLocations(GeoLocated geoLoc, double distance) {
Iterator<LatLong> it = locations.iterator(); Iterator<GeoLocated> it = locations.iterator();
while (it.hasNext()) { while (it.hasNext()) {
LatLong latLong2 = it.next(); GeoLocated geoLoc2 = it.next();
if (distance == 0.0 ? latLong2.equals(latLong) : latLong.distance(latLong2) <= distance) { if (distance == 0.0 ? geoLoc2.equalsLatLong(geoLoc) : geoLoc.getLatLong().distance(geoLoc2.getLatLong()) <= distance) {
it.remove(); it.remove();
} }
} }
...@@ -79,7 +79,7 @@ public class GeoLocations implements Iterable<LatLong> { ...@@ -79,7 +79,7 @@ public class GeoLocations implements Iterable<LatLong> {
} }
@Override @Override
public Iterator<LatLong> iterator() { public Iterator<GeoLocated> iterator() {
return locations.iterator(); return locations.iterator();
} }
} }
package tdt4140.gr1800.app.core;
import java.io.IOException;
public interface IDocumentImporter<L> {
public void importDocument(L documentLocation) throws IOException;
}
package tdt4140.gr1800.app.core;
public interface IDocumentLoader<D, L> {
public D loadDocument(L documentLocation) throws Exception;
}
package tdt4140.gr1800.app.core;
public interface IDocumentPersistence<D, L> extends IDocumentLoader<D, L>, IDocumentSaver<D, L> {
}
package tdt4140.gr1800.app.core;
public interface IDocumentSaver<D, L> {
public void saveDocument(D document, L documentLocation) throws Exception;
}
package tdt4140.gr1800.app.core; package tdt4140.gr1800.app.core;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
public interface IDocumentStorage<L> { public interface IDocumentStorage<L> {
public L getDocumentLocation(); public L getDocumentLocation();
...@@ -9,4 +10,6 @@ public interface IDocumentStorage<L> { ...@@ -9,4 +10,6 @@ public interface IDocumentStorage<L> {
public void newDocument(); public void newDocument();
public void openDocument(L documentLocation) throws IOException; public void openDocument(L documentLocation) throws IOException;
public void saveDocument() throws IOException; public void saveDocument() throws IOException;
public Collection<IDocumentImporter<L>> getDocumentImporters();
} }
package tdt4140.gr1800.app.core;
public interface IGeoLocationsListener {
public void geoLocationsUpdated(GeoLocations geoLocations);
}
package tdt4140.gr1800.app.core; package tdt4140.gr1800.app.core;
public class LatLong { public class LatLong implements GeoLocated {
public final double latitude, longitude; public final double latitude, longitude;
...@@ -57,6 +57,13 @@ public class LatLong { ...@@ -57,6 +57,13 @@ public class LatLong {
return new LatLong(lat, lon); return new LatLong(lat, lon);
} }
// GeoLocated
@Override
public LatLong getLatLong() {
return this;
}
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: :*/ /*:: :*/
/*:: This routine calculates the distance between two points (given the :*/ /*:: This routine calculates the distance between two points (given the :*/
......
package tdt4140.gr1800.app.gpx;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import io.jenetics.jpx.GPX;
import io.jenetics.jpx.Route;
import io.jenetics.jpx.Track;
import io.jenetics.jpx.TrackSegment;
import io.jenetics.jpx.WayPoint;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.IDocumentLoader;
import tdt4140.gr1800.app.core.LatLong;
public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocations>, File> {
private GpxDocumentLoader gpxLoader = new GpxDocumentLoader();
@Override
public Collection<GeoLocations> loadDocument(File documentLocation) throws Exception {
GPX gpx = gpxLoader.loadDocument(documentLocation.toURI().toURL());
return convert(gpx);
}
private String trackNameFormat = "%s";
private String trackCountFormat = "Track %s";
private String trackSegmentFormat = "%s.%s";
public void setTrackNameFormat(String trackNameFormat) {
this.trackNameFormat = trackNameFormat;
}
public void setTrackCountFormat(String trackCountFormat) {
this.trackCountFormat = trackCountFormat;
}
public void setTrackSegmentFormat(String trackSegmentFormat) {
this.trackSegmentFormat = trackSegmentFormat;
}
private String routeNameFormat = "%s";
private String routeCountFormat = "Route %s";
public void setRouteNameFormat(String routeNameFormat) {
this.routeNameFormat = routeNameFormat;
}
public void setRouteCountFormat(String routeCountFormat) {
this.routeCountFormat = routeCountFormat;
}
public Collection<GeoLocations> convert(GPX gpx) throws Exception {
Collection<GeoLocations> geoLocations = new ArrayList<GeoLocations>();
int trackCount = 1;
for (Track track : gpx.getTracks()) {
String trackName = (track.getName().isPresent() ? String.format(trackNameFormat, track.getName().get()) : String.format(trackCountFormat, trackCount));
int segmentCount = 1;
for (TrackSegment segment : track) {
boolean singleTrack = track.getSegments().size() <= 1;
String name = (singleTrack ? trackName : String.format(trackSegmentFormat, trackName, segmentCount));
GeoLocations gl = new GeoLocations(name, convert(segment.getPoints()));
gl.setPath(true);
geoLocations.add(gl);
}
}
int routeCount = 1;
for (Route route : gpx.getRoutes()) {
String routeName = (route.getName().isPresent() ? String.format(routeNameFormat, route.getName().get()) : String.format(routeCountFormat, routeCount));
GeoLocations gl = new GeoLocations(routeName, convert(route.getPoints()));
gl.setPath(true);
geoLocations.add(gl);
}
if (! gpx.getWayPoints().isEmpty()) {
geoLocations.add(new GeoLocations("Waypoints", convert(gpx.getWayPoints())));
}
return geoLocations;
}
private LatLong[] convert(Collection<WayPoint> points) {
Collection<LatLong> latLongs = points.stream()
.map(point -> convert(point))
.collect(Collectors.toList());
return latLongs.toArray(new LatLong[latLongs.size()]);
}
private LatLong convert(WayPoint point) {
return new LatLong(point.getLatitude().doubleValue(), point.getLongitude().doubleValue());
}
}
package tdt4140.gr1800.app.gpx;
import java.io.InputStream;
import java.net.URL;
import io.jenetics.jpx.GPX;
import tdt4140.gr1800.app.core.IDocumentLoader;
public class GpxDocumentLoader implements IDocumentLoader<GPX, URL> {
public GPX loadDocument(InputStream inputStream) throws Exception {
return GPX.read(inputStream, true);
}
@Override
public GPX loadDocument(URL documentLocation) throws Exception {
return loadDocument(documentLocation.openStream());
}
}
...@@ -6,8 +6,8 @@ import com.fasterxml.jackson.core.JsonGenerator; ...@@ -6,8 +6,8 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import tdt4140.gr1800.app.core.GeoLocated;
import tdt4140.gr1800.app.core.GeoLocations; import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
public class GeoLocationsJsonSerializer extends StdSerializer<GeoLocations> { public class GeoLocationsJsonSerializer extends StdSerializer<GeoLocations> {
...@@ -30,10 +30,10 @@ public class GeoLocationsJsonSerializer extends StdSerializer<GeoLocations> { ...@@ -30,10 +30,10 @@ public class GeoLocationsJsonSerializer extends StdSerializer<GeoLocations> {
jsonGen.writeBoolean(geoLocations.isPath()); jsonGen.writeBoolean(geoLocations.isPath());
jsonGen.writeFieldName(LOCATIONS_FIELD_NAME); jsonGen.writeFieldName(LOCATIONS_FIELD_NAME);
jsonGen.writeStartArray(); jsonGen.writeStartArray();
for (LatLong latLon : geoLocations) { for (GeoLocated geoLoc : geoLocations) {
jsonGen.writeStartArray(); jsonGen.writeStartArray();
jsonGen.writeNumber(latLon.latitude); jsonGen.writeNumber(geoLoc.getLatitude());
jsonGen.writeNumber(latLon.longitude); jsonGen.writeNumber(geoLoc.getLongitude());
jsonGen.writeEndArray(); jsonGen.writeEndArray();
} }
jsonGen.writeEndArray(); jsonGen.writeEndArray();
......
...@@ -29,11 +29,11 @@ public class GeoLocationsTest { ...@@ -29,11 +29,11 @@ public class GeoLocationsTest {
} }
public static void assertGeoLocations(GeoLocations geoLocations, LatLong...latLongs) { public static void assertGeoLocations(GeoLocations geoLocations, LatLong...latLongs) {
Iterator<LatLong> it = geoLocations.iterator(); Iterator<GeoLocated> it = geoLocations.iterator();
Assert.assertEquals(latLongs.length, geoLocations.size()); Assert.assertEquals(latLongs.length, geoLocations.size());
int pos = 0; int pos = 0;
while (it.hasNext()) { while (it.hasNext()) {
Assert.assertEquals(latLongs[pos], it.next()); checkGeoLocated(latLongs[pos], it.next());
pos++; pos++;
} }
} }
...@@ -53,14 +53,18 @@ public class GeoLocationsTest { ...@@ -53,14 +53,18 @@ public class GeoLocationsTest {
assertGeoLocations(latLong1, latLong1, latLong2); assertGeoLocations(latLong1, latLong1, latLong2);
} }
static void checkGeoLocated(GeoLocated geoLoc1, GeoLocated geoLoc2) {
Assert.assertTrue(geoLoc1.equalsLatLong(geoLoc2));
}
@Test @Test
public void testFindLocationsNearby() { public void testFindLocationsNearby() {
LatLong latLong = new LatLong(0, 0); LatLong latLong = new LatLong(0, 0);
Assert.assertTrue(geoLocations.findLocationsNearby(latLong, 0).isEmpty()); Assert.assertTrue(geoLocations.findLocationsNearby(latLong, 0).isEmpty());
geoLocations.addLocation(latLong); geoLocations.addLocation(latLong);
Collection<LatLong> locationsNearby = geoLocations.findLocationsNearby(latLong, 0); Collection<GeoLocated> locationsNearby = geoLocations.findLocationsNearby(latLong, 0);
Assert.assertEquals(1, locationsNearby.size()); Assert.assertEquals(1, locationsNearby.size());
Assert.assertEquals(latLong, geoLocations.iterator().next()); checkGeoLocated(latLong, geoLocations.iterator().next());
} }
@Test @Test
...@@ -68,8 +72,8 @@ public class GeoLocationsTest { ...@@ -68,8 +72,8 @@ public class GeoLocationsTest {
LatLong latLong = new LatLong(0, 0); LatLong latLong = new LatLong(0, 0);
Assert.assertNull(geoLocations.findNearestLocation(latLong)); Assert.assertNull(geoLocations.findNearestLocation(latLong));
geoLocations.addLocation(latLong); geoLocations.addLocation(latLong);
LatLong nearestlocations = geoLocations.findNearestLocation(latLong); GeoLocated nearestlocations = geoLocations.findNearestLocation(latLong);
Assert.assertEquals(latLong, nearestlocations); checkGeoLocated(latLong, nearestlocations);
} }
@Test @Test
......
package tdt4140.gr1800.app.core;
import java.util.Collection;
import java.util.Iterator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import io.jenetics.jpx.GPX;
import tdt4140.gr1800.app.gpx.GpxDocumentConverter;
import tdt4140.gr1800.app.gpx.GpxDocumentLoader;
public class GpxPersistenceTest {
private GpxDocumentLoader loader;
private GpxDocumentConverter converter;
@Before
public void setUp() {
loader = new GpxDocumentLoader();
converter = new GpxDocumentConverter();
}
@Test
public void testLoadDocument() {
try {
GPX gpx = loader.loadDocument(getClass().getResource("sample1.gpx"));
Assert.assertEquals(1, gpx.getTracks().size());
Assert.assertEquals(1, gpx.getTracks().get(0).getSegments().size());
Assert.assertEquals(3, gpx.getTracks().get(0).getSegments().get(0).getPoints().size());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
@Test
public void testConvertSample1() {
try {
GPX gpx = loader.loadDocument(getClass().getResource("sample1.gpx"));
Collection<GeoLocations> geoLocations = converter.convert(gpx);
Assert.assertEquals(1, geoLocations.size());
GeoLocations track = geoLocations.iterator().next();
Assert.assertEquals("Example GPX Document", track.getName());
Assert.assertEquals(3, track.size());
Assert.assertTrue(track.isPath());
} catch (Exception e) {
System.err.println(e);
Assert.fail(e.getMessage());
}
}
@Test
public void testConvertAchterbroekRoute() {
try {
GPX gpx = loader.loadDocument(getClass().getResource("Achterbroek-route.gpx"));
Collection<GeoLocations> geoLocations = converter.convert(gpx);
Assert.assertEquals(2, geoLocations.size());
Iterator<GeoLocations> iterator = geoLocations.iterator();
GeoLocations route = iterator.next();
Assert.assertEquals("Achterbroek naar De Maatjes 13 km RT", route.getName());
Assert.assertEquals(19, route.size());
Assert.assertTrue(route.isPath());
GeoLocations waypoints = iterator.next();
Assert.assertEquals("Achterbroek naar De Maatjes 13 km RT", route.getName());
Assert.assertEquals(1, waypoints.size());
Assert.assertFalse(waypoints.isPath());
} catch (Exception e) {
System.err.println(e);
Assert.fail(e.getMessage());
}
}
@Test
public void testConvertAchterbroekTrack() {
try {
GPX gpx = loader.loadDocument(getClass().getResource("Achterbroek-track.gpx"));
Collection<GeoLocations> geoLocations = converter.convert(gpx);
Assert.assertEquals(2, geoLocations.size());
Iterator<GeoLocations> iterator = geoLocations.iterator();
GeoLocations track = iterator.next();
Assert.assertEquals("Achterbroek naar De Maatjes 13 km TR", track.getName());
Assert.assertEquals(26, track.size());
Assert.assertTrue(track.isPath());
GeoLocations waypoints = iterator.next();
Assert.assertEquals(1, waypoints.size());
Assert.assertFalse(waypoints.isPath());
} catch (Exception e) {
System.err.println(e);
Assert.fail(e.getMessage());
}
}
}
<?xml version="1.0"?>
<gpx
version="1.0"
creator="VB Net GPS: vermeiren-willy@pandora.be"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<wpt lat="51.39709" lon="4.501519">
<name>START</name>
</wpt>
<rte>
<name>Achterbroek naar De Maatjes 13 km RT</name>
<rtept lat="51.39709" lon="4.501519">
<name>1</name>
<cmt>1</cmt>
</rtept>
<rtept lat="51.398635" lon="4.502678">
<name>2</name>
<cmt>2</cmt>
</rtept>
<rtept lat="51.405072" lon="4.498043">
<name>3</name>
<cmt>3</cmt>
</rtept>
<rtept lat="51.417346" lon="4.515038">
<name>4</name>
<cmt>4</cmt>
</rtept>
<rtept lat="51.41932" lon="4.513879">
<name>5</name>
<cmt>5</cmt>
</rtept>
<rtept lat="51.421552" lon="4.523535">
<name>6</name>
<cmt>6</cmt>
</rtept>
<rtept lat="51.423011" lon="4.522848">
<name>7</name>
<cmt>7</cmt>
</rtept>
<rtept lat="51.423826" lon="4.5295">
<name>8</name>
<cmt>8</cmt>
</rtept>
<rtept lat="51.427002" lon="4.528513">
<name>9</name>
<cmt>9</cmt>
</rtept>
<rtept lat="51.430402" lon="4.552363">
<name>10</name>
<cmt>10</cmt>
</rtept>
<rtept lat="51.426229" lon="4.555399">
<name>11</name>
<cmt>11</cmt>
</rtept>
<rtept lat="51.426015" lon="4.554734">
<name>12</name>
<cmt>12</cmt>
</rtept>
<rtept lat="51.423569" lon="4.556108">
<name>13</name>
<cmt>13</cmt>
</rtept>
<rtept lat="51.41932" lon="4.546237">
<name>14</name>
<cmt>14</cmt>
</rtept>
<rtept lat="51.410952" lon="4.534092">
<name>15</name>
<cmt>15</cmt>
</rtept>
<rtept lat="51.406231" lon="4.543018">
<name>16</name>
<cmt>16</cmt>
</rtept>
<rtept lat="51.396695" lon="4.515966">
<name>17</name>
<cmt>17</cmt>
</rtept>
<rtept lat="51.396017" lon="4.515853">
<name>18</name>
<cmt>18</cmt>
</rtept>
<rtept lat="51.397133" lon="4.502635">
<name>19</name>
<cmt>19</cmt>
</rtept>
</rte>
</gpx>
<?xml version="1.0"?>
<gpx
version="1.0"
creator="VB Net GPS: vermeiren-willy@pandora.be"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
<wpt lat="51.39709" lon="4.501519">
<name>START</name>
</wpt>
<trk>
<name>Achterbroek naar De Maatjes 13 km TR</name>
<trkseg>
<trkpt lat="51.39709" lon="4.501519">
<name>1</name>
</trkpt>
<trkpt lat="51.397705" lon="4.501452">
<name>2</name>
</trkpt>
<trkpt lat="51.398635" lon="4.502678">
<name>3</name>
</trkpt>
<trkpt lat="51.404042" lon="4.498472">
<name>4</name>
</trkpt>
<trkpt lat="51.405072" lon="4.498043">
<name>5</name>
</trkpt>
<trkpt lat="51.405456" lon="4.499259">
<name>6</name>
</trkpt>
<trkpt lat="51.417346" lon="4.515038">
<name>7</name>
</trkpt>
<trkpt lat="51.41932" lon="4.513879">
<name>8</name>
</trkpt>
<trkpt lat="51.421552" lon="4.523535">
<name>9</name>
</trkpt>
<trkpt lat="51.423011" lon="4.522848">
<name>10</name>
</trkpt>
<trkpt lat="51.423826" lon="4.5295">
<name>11</name>
</trkpt>
<trkpt lat="51.427002" lon="4.528513">
<name>12</name>
</trkpt>
<trkpt lat="51.427131" lon="4.534478">
<name>13</name>
</trkpt>
<trkpt lat="51.426819" lon="4.53744">
<name>14</name>
</trkpt>
<trkpt lat="51.430402" lon="4.552363">
<name>15</name>
</trkpt>
<trkpt lat="51.426229" lon="4.555399">
<name>16</name>
</trkpt>
<trkpt lat="51.426015" lon="4.554734">
<name>17</name>
</trkpt>
<trkpt lat="51.423569" lon="4.556108">
<name>18</name>
</trkpt>
<trkpt lat="51.423097" lon="4.553981">
<name>19</name>
</trkpt>
<trkpt lat="51.41932" lon="4.546237">
<name>20</name>
</trkpt>
<trkpt lat="51.410952" lon="4.534092">
<name>21</name>
</trkpt>
<trkpt lat="51.406231" lon="4.543018">
<name>22</name>
</trkpt>
<trkpt lat="51.396695" lon="4.515966">
<name>23</name>
</trkpt>
<trkpt lat="51.396017" lon="4.515853">
<name>24</name>
</trkpt>
<trkpt lat="51.396858" lon="4.506495">
<name>25</name>
</trkpt>
<trkpt lat="51.397133" lon="4.502635">
<name>26</name>
</trkpt>
</trkseg>
</trk>
</gpx>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="Oregon 400t" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2009-10-17T22:58:43Z</time>
</metadata>
<trk>
<name>Example GPX Document</name>
<trkseg>
<trkpt lat="47.644548" lon="-122.326897">
<ele>4.46</ele>
<time>2009-10-17T18:37:26Z</time>
</trkpt>
<trkpt lat="47.644548" lon="-122.33">
<ele>4.94</ele>
<time>2009-10-17T18:37:31Z</time>
</trkpt>
<trkpt lat="47.65" lon="-122.33">
<ele>6.87</ele>
<time>2009-10-17T18:37:34Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment