// Import the data from a GPX file. Boolean indicates whether data has been imported before
public void addData(File file, boolean firsttime) throws IOException, FunctionEvaluationException {
// Start by reading the file and analyzing it contents
Gpx gpx = GPSDings.readGPX(new FileInputStream(file));
TrackAnalyzer analyzer = new TrackAnalyzer();
analyzer.addAllTracks(gpx);
// The garmin GPX running data contains only one track containing one segment
Trkseg track = gpx.getTrk(0).getTrkseg(0);
// Start a new transaction
Transaction tx = graphDb.beginTx();
// Contains the record that was added previously (in order to create a relation between the new and the previous node)
SpatialDatabaseRecord fromrecord = null;
// Iterate all points
for (int i = 0; i < track.getTrkptCount(); i++) {
// Create a new coordinate for this point
Coordinate to = new Coordinate(track.getTrkpt(i).getLon().doubleValue(),track.getTrkpt(i).getLat().doubleValue());
// Check whether we can find a node from which is located within a distance of 20 meters
List<GeoPipeFlow> closests = GeoPipeline.startNearestNeighborLatLonSearch(runningLayer, to, 0.02).sort("OrthodromicDistance").getMin("OrthodromicDistance").toList();
SpatialDatabaseRecord torecord = null;
// If first time, we add all nodes. Otherwise, we check whether we find a node that is close enough to the current location
if (!firsttime && (closests.size() == 1)) {
// Retrieve the node
System.out.println("Using existing: " + closests.get(0).getProperty("OrthodromicDistance"));
torecord = closests.get(0).getRecord();
// Recalculate average speed
double previousspeed = (Double)torecord.getProperty("speed");
int previousoccurences = (Integer)torecord.getProperty("occurences");
double currentspeed = analyzer.getHorizontalSpeed(track.getTrkpt(i).getTime());
double denormalizespeed = previousspeed * previousoccurences;
double newspeed = ((denormalizespeed + currentspeed) / (previousoccurences + 1));
// Update the data accordingly
torecord.setProperty("speed",newspeed);
torecord.setProperty("occurences",previousoccurences+1);
}
else {
// New node, add it
torecord = runningLayer.add(runningLayer.getGeometryFactory().createPoint(to));
// Set the data accordingly
torecord.setProperty("speed", analyzer.getHorizontalSpeed(track.getTrkpt(i).getTime()));
torecord.setProperty("occurences", 1);
}
// If a previous node is available (and they are not identical), add a directed relationship between both
if (fromrecord != null && (!fromrecord.equals(torecord))) {