-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse symbole and type #19
base: master
Are you sure you want to change the base?
Changes from all commits
d2e0199
d8bae87
8c5e1b1
27ceaba
bec7fa0
3d4342a
a2f8538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package org.surfsite.gexporter; | ||
|
||
import static org.apache.commons.lang3.StringUtils.defaultString; | ||
|
||
import android.annotation.SuppressLint; | ||
|
||
import com.garmin.fit.CourseMesg; | ||
|
@@ -26,20 +28,16 @@ | |
import org.xmlpull.v1.XmlPullParserFactory; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
public class Gpx2Fit { | ||
private static final Logger Log = LoggerFactory.getLogger(Gpx2Fit.class); | ||
|
@@ -168,13 +166,25 @@ private String readTrkName(XmlPullParser parser) throws IOException, XmlPullPars | |
return txt; | ||
} | ||
|
||
private String readName(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
parser.require(XmlPullParser.START_TAG, ns, "name"); | ||
private String readSimpleTextTag(XmlPullParser parser, String tagName) throws IOException, XmlPullParserException { | ||
parser.require(XmlPullParser.START_TAG, ns, tagName); | ||
String txt = readText(parser); | ||
parser.require(XmlPullParser.END_TAG, ns, "name"); | ||
parser.require(XmlPullParser.END_TAG, ns, tagName); | ||
return txt; | ||
} | ||
|
||
private String readName(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
return readSimpleTextTag(parser, "name"); | ||
} | ||
|
||
private String readType(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
return readSimpleTextTag(parser, "type"); | ||
} | ||
|
||
private String readSymbol(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
return readSimpleTextTag(parser, "sym"); | ||
} | ||
|
||
private void readTrkSeg(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { | ||
parser.require(XmlPullParser.START_TAG, ns, "trkseg"); | ||
while (parser.next() != XmlPullParser.END_TAG) { | ||
|
@@ -238,7 +248,7 @@ private void readTrkPt(XmlPullParser parser) throws XmlPullParserException, IOEx | |
break; | ||
} | ||
} | ||
trkPoints.add(new WayPoint(name, lat, lon, ele, time)); | ||
trkPoints.add(new WayPoint(name, lat, lon, ele, time, null, null)); | ||
} | ||
|
||
private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { | ||
|
@@ -248,6 +258,8 @@ private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOExce | |
double lat = Double.parseDouble(parser.getAttributeValue(null, "lat")); | ||
double lon = Double.parseDouble(parser.getAttributeValue(null, "lon")); | ||
double ele = Double.NaN; | ||
String type = null; | ||
String symbol = null; | ||
|
||
while (parser.next() != XmlPullParser.END_TAG) { | ||
if (parser.getEventType() != XmlPullParser.START_TAG) { | ||
|
@@ -264,12 +276,18 @@ private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOExce | |
case "name": | ||
name = readName(parser); | ||
break; | ||
case "type": | ||
type = readType(parser); | ||
break; | ||
case "sym": | ||
symbol = readSymbol(parser); | ||
break; | ||
default: | ||
skip(parser); | ||
break; | ||
} | ||
} | ||
wayPoints.add(new WayPoint(name, lat, lon, ele, time)); | ||
wayPoints.add(new WayPoint(name, lat, lon, ele, time, type, symbol)); | ||
} | ||
|
||
private void readRtePt(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { | ||
|
@@ -300,7 +318,7 @@ private void readRtePt(XmlPullParser parser) throws XmlPullParserException, IOEx | |
break; | ||
} | ||
} | ||
rtePoints.add(new WayPoint(name, lat, lon, ele, time)); | ||
rtePoints.add(new WayPoint(name, lat, lon, ele, time, null, null)); | ||
} | ||
|
||
private double readEle(XmlPullParser parser) throws IOException, XmlPullParserException { | ||
|
@@ -346,8 +364,9 @@ public String getName() { | |
* Grade adjusted pace based on a study by Alberto E. Minetti on the energy cost of | ||
* walking and running at extreme slopes. | ||
* <p> | ||
* see Minetti, A. E. et al. (2002). Energy cost of walking and running at extreme uphill and downhill slopes. | ||
* Journal of Applied Physiology 93, 1039-1046, http://jap.physiology.org/content/93/3/1039.full | ||
* see <a href="http://jap.physiology.org/content/93/3/1039.full">Minetti, A. E. et al. (2002). | ||
* Energy cost of walking and running at extreme uphill and downhill slopes. | ||
* Journal of Applied Physiology 93, 1039-1046</a> | ||
Comment on lines
-349
to
+369
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removes a warning |
||
*/ | ||
public double getWalkingGradeFactor(double g) { | ||
return 1.0 + (g * (19.5 + g * (46.3 + g * (-43.3 + g * (-30.4 + g * 155.4))))) / 3.6; | ||
|
@@ -539,14 +558,8 @@ public void writeFit(File outfile) { | |
lapMesg.setFieldValue(28, 0, (Integer) WayPoint.toSemiCircles(maxLong), '\uffff'); | ||
lapMesg.setFieldValue(29, 0, (Integer) WayPoint.toSemiCircles(minLat), '\uffff'); | ||
lapMesg.setFieldValue(30, 0, (Integer) WayPoint.toSemiCircles(minLong), '\uffff'); | ||
} catch (NoSuchMethodException e) { | ||
; | ||
} catch (IllegalAccessException e) { | ||
; | ||
} catch (InstantiationException e) { | ||
; | ||
} catch (InvocationTargetException e) { | ||
; | ||
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | | ||
InvocationTargetException ignored) { | ||
Comment on lines
-542
to
+562
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removes a warning |
||
} | ||
|
||
encode.write(lapMesg); | ||
|
@@ -568,39 +581,11 @@ public void writeFit(File outfile) { | |
|
||
|
||
if (!skipExtraCP && !wayPoints.isEmpty()) { | ||
for (WayPoint wpt : wayPoints) { | ||
CoursePointMesg cp = new CoursePointMesg(); | ||
cp.setLocalNum(0); | ||
|
||
cp.setPositionLat(wpt.getLatSemi()); | ||
cp.setPositionLong(wpt.getLonSemi()); | ||
String name = wpt.getName(); | ||
if (name != null) { | ||
cp.setName(name); | ||
} else { | ||
cp.setName(""); | ||
} | ||
cp.setType(CoursePoint.GENERIC); | ||
encode.write(cp); | ||
} | ||
writeWayPoints(encode, wayPoints); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reduced code duplication |
||
} | ||
|
||
if (!skipExtraCP && !rtePoints.isEmpty()) { | ||
for (WayPoint wpt : rtePoints) { | ||
CoursePointMesg cp = new CoursePointMesg(); | ||
cp.setLocalNum(0); | ||
|
||
cp.setPositionLat(wpt.getLatSemi()); | ||
cp.setPositionLong(wpt.getLonSemi()); | ||
String name = wpt.getName(); | ||
if (name != null) { | ||
cp.setName(name); | ||
} else { | ||
cp.setName(""); | ||
} | ||
cp.setType(CoursePoint.GENERIC); | ||
encode.write(cp); | ||
} | ||
writeWayPoints(encode, rtePoints); | ||
} | ||
|
||
{ | ||
|
@@ -746,4 +731,17 @@ public void writeFit(File outfile) { | |
|
||
encode.close(); | ||
} | ||
|
||
private void writeWayPoints(FileEncoder encode, List<WayPoint> points) { | ||
for (WayPoint wpt : points) { | ||
CoursePointMesg cp = new CoursePointMesg(); | ||
cp.setLocalNum(0); | ||
|
||
cp.setPositionLat(wpt.getLatSemi()); | ||
cp.setPositionLong(wpt.getLonSemi()); | ||
cp.setName(defaultString(wpt.getName())); | ||
cp.setType(wpt.getPointType()); | ||
encode.write(cp); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package org.surfsite.gexporter; | ||
|
||
import static org.apache.commons.lang3.StringUtils.isNumeric; | ||
import static org.apache.commons.lang3.math.NumberUtils.toShort; | ||
|
||
import com.garmin.fit.CoursePoint; | ||
|
||
import org.gavaghan.geodesy.Ellipsoid; | ||
import org.gavaghan.geodesy.GeodeticCalculator; | ||
import org.gavaghan.geodesy.GlobalCoordinates; | ||
|
@@ -17,14 +22,17 @@ public class WayPoint { | |
// select a reference ellipsoid | ||
private static final Ellipsoid reference = Ellipsoid.WGS84; | ||
|
||
private double lat = Double.NaN; | ||
private double lon = Double.NaN; | ||
private double ele = Double.NaN; | ||
private Date time = null; | ||
private double lat; | ||
private double lon; | ||
private double ele; | ||
private Date time; | ||
Comment on lines
-20
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. values are overridden in constructor so they don't have to be set here |
||
private double totaldist = Double.NaN; | ||
private String name = null; | ||
private String name; | ||
|
||
public WayPoint(String name, double lat, double lon, double ele, Date time) { | ||
private String type; | ||
private String symbol; | ||
|
||
public WayPoint(String name, double lat, double lon, double ele, Date time, String type, String symbol) { | ||
this.lat = lat; | ||
this.lon = lon; | ||
this.ele = ele; | ||
|
@@ -33,6 +41,8 @@ public WayPoint(String name, double lat, double lon, double ele, Date time) { | |
if (this.time == null) { | ||
this.time = RefDate; | ||
} | ||
this.type = type; | ||
this.symbol = symbol; | ||
} | ||
|
||
public String getName() { | ||
|
@@ -67,6 +77,15 @@ public double getEle() { | |
public Date getTime() { | ||
return time; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setLat(double lat) { | ||
this.lat = lat; | ||
} | ||
|
@@ -83,6 +102,14 @@ public void setTime(Date time) { | |
this.time = time; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public double distance(WayPoint other) { | ||
return geoCalc.calculateGeodeticCurve(reference, | ||
new GlobalCoordinates(getLat(), this.getLon()), | ||
|
@@ -110,4 +137,25 @@ public void setTotaldist(double totaldist) { | |
this.totaldist = totaldist; | ||
} | ||
|
||
public CoursePoint getPointType() { | ||
if (parseStringToCoursePoint(type) != null) { | ||
return parseStringToCoursePoint(type); | ||
} | ||
if (parseStringToCoursePoint(symbol) != null) { | ||
return parseStringToCoursePoint(symbol); | ||
} | ||
return CoursePoint.GENERIC; | ||
} | ||
|
||
private CoursePoint parseStringToCoursePoint(String s) { | ||
if (isNumeric(s)) { | ||
return CoursePoint.getByValue(toShort(s)); | ||
} else { | ||
try { | ||
return CoursePoint.valueOf(s); | ||
} catch (IllegalArgumentException exception) { | ||
return null; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the dependencies so one does not have to download oneself.
I don't know if you need the files locally for some other kind of debugging. If so I can leave the documentation unchanged.