Skip to content

Commit

Permalink
Corrected offset for annotation in GPX writer
Browse files Browse the repository at this point in the history
Issue #495
  • Loading branch information
mendhak committed Dec 30, 2016
1 parent da53c1e commit b2723fa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void run() {
return;
}

int startPosition = 339;


String wpt = getWaypointXml(loc, dateTimeString, description);

Expand All @@ -122,14 +122,14 @@ public void run() {

int written = 0;
int readSize;
byte[] buffer = new byte[startPosition];
byte[] buffer = new byte[Gpx10WriteHandler.INITIAL_XML_LENGTH];
while ((readSize = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readSize);
written += readSize;

System.out.println(written);

if (written == startPosition) {
if (written == Gpx10WriteHandler.INITIAL_XML_LENGTH) {
bos.write(wpt.getBytes());
buffer = new byte[20480];
}
Expand Down Expand Up @@ -181,6 +181,7 @@ class Gpx10WriteHandler implements Runnable {
Location loc;
private File gpxFile = null;
private boolean addNewTrackSegment;
static final int INITIAL_XML_LENGTH = 343;

public Gpx10WriteHandler(String dateTimeString, File gpxFile, Location loc, boolean addNewTrackSegment) {
this.dateTimeString = dateTimeString;
Expand All @@ -189,7 +190,6 @@ public Gpx10WriteHandler(String dateTimeString, File gpxFile, Location loc, bool
this.loc = loc;
}


@Override
public void run() {
synchronized (Gpx10FileLogger.lock) {
Expand All @@ -201,23 +201,17 @@ public void run() {
FileOutputStream initialWriter = new FileOutputStream(gpxFile, true);
BufferedOutputStream initialOutput = new BufferedOutputStream(initialWriter);

StringBuilder initialXml = new StringBuilder();
initialXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
initialXml.append("<gpx version=\"1.0\" creator=\"GPSLogger " + BuildConfig.VERSION_NAME + " - http://gpslogger.mendhak.com/\" ");
initialXml.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
initialXml.append("xmlns=\"http://www.topografix.com/GPX/1/0\" ");
initialXml.append("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 ");
initialXml.append("http://www.topografix.com/GPX/1/0/gpx.xsd\">");
initialXml.append("<time>").append(dateTimeString).append("</time>").append("<trk></trk></gpx>");
initialOutput.write(initialXml.toString().getBytes());
initialOutput.write(getBeginningXml(dateTimeString).getBytes());
initialOutput.write("<trk>".getBytes());
initialOutput.write(getEndXml().getBytes());
initialOutput.flush();
initialOutput.close();

//New file, so new segment.
addNewTrackSegment = true;
}

int offsetFromEnd = (addNewTrackSegment) ? 12 : 21;
int offsetFromEnd = (addNewTrackSegment) ? getEndXml().length() : getEndXmlWithSegment().length();
long startPosition = gpxFile.length() - offsetFromEnd;
String trackPoint = getTrackPointXml(loc, dateTimeString);

Expand All @@ -236,6 +230,26 @@ public void run() {

}

String getBeginningXml(String dateTimeString){
StringBuilder initialXml = new StringBuilder();
initialXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
initialXml.append("<gpx version=\"1.0\" creator=\"GPSLogger " + BuildConfig.VERSION_CODE + " - http://gpslogger.mendhak.com/\" ");
initialXml.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
initialXml.append("xmlns=\"http://www.topografix.com/GPX/1/0\" ");
initialXml.append("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 ");
initialXml.append("http://www.topografix.com/GPX/1/0/gpx.xsd\">");
initialXml.append("<time>").append(dateTimeString).append("</time>");
return initialXml.toString();
}

String getEndXml(){
return "</trk></gpx>";
}

String getEndXmlWithSegment(){
return "</trkseg></trk></gpx>";
}

String getTrackPointXml(Location loc, String dateTimeString) {

StringBuilder track = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import android.location.Location;
import android.test.suitebuilder.annotation.SmallTest;
import com.mendhak.gpslogger.BuildConfig;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.loggers.MockLocations;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Date;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

Expand Down Expand Up @@ -169,4 +173,38 @@ public void GetTrackPointXml_BundledGeoIdHeight_GeoIdHeightNode(){

assertThat("Trackpoint XML with a geoid height", actual, is(expected));
}

@Test
public void GetBeginningXml_Verify(){
Gpx10WriteHandler writeHandler = new Gpx10WriteHandler(null, null, null, true);


String actual = writeHandler.getBeginningXml(Strings.getIsoDateTime(new Date(1483054318298l)));
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><gpx version=\"1.0\" creator=\"GPSLogger "+ BuildConfig.VERSION_CODE +" - http://gpslogger.mendhak.com/\" 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\"><time>2016-12-29T23:31:58.298Z</time>";

assertThat("InitialXml matches", actual, is(expected));
assertThat("Initial XML Length is correct", actual.length(), is(343));
assertThat("Initial XML length constant is set for others to use", actual.length(), is(Gpx10WriteHandler.INITIAL_XML_LENGTH));
}

@Test
public void GetEndXml_Verify(){
Gpx10WriteHandler writeHandler = new Gpx10WriteHandler(null, null, null, true);
String expected = "</trk></gpx>";
String actual = writeHandler.getEndXml();

assertThat("End XML Matches", actual, is(expected));
assertThat("End XML length matches", actual.length(), is(12));
}

@Test
public void GetEndXmlWithSegment_Verify(){
Gpx10WriteHandler writeHandler = new Gpx10WriteHandler(null, null, null, true);
String expected = "</trkseg></trk></gpx>";
String actual = writeHandler.getEndXmlWithSegment();

assertThat("End Xml with track segment matches", actual, is(expected));
assertThat("End Xml length matches", actual.length(), is(21));

}
}

0 comments on commit b2723fa

Please sign in to comment.