我使用jenetics在java中读取GPX文件。效果很好。gpx文件是从我的手表,从运行的会话中获取的。我正在阅读跟踪点,并想知道是否有一种方法来确定,在GPX文件,什么时候停止手表休息。例:如果我运行15分钟,然后停止手表5分钟,然后再启动它,然后运行15分钟,对gpx文件的解析将给我35分钟。但我想录下30分钟。
你知道能不能检测到这个吗?
发布于 2022-07-11 21:33:40
如果您暂停运行,您的手表应该创建一个新的轨道段,如
<trk>
<trkseg>
<trkpt lat="48.19949341" lon="16.40377444"></trkpt>
<trkpt lat="48.19948359" lon="16.40371021"></trkpt>
...
</trkseg>
<trkseg>
<trkpt lat="48.19949341" lon="16.40377444"></trkpt>
<trkpt lat="48.19948359" lon="16.40371021"></trkpt>
...
</trkseg>
</trk>JPX库允许您通过Gpx -> getTracks -> getTrackSegments访问跟踪段。在您的例子中,您的手表应该创建一个轨道与两个轨道段。轨道段将包含停站前后的轨道点.
更新: TrackSegment收集器
下面的代码允许您将路径点收集到跟踪段中.
import static java.lang.String.format;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import io.jenetics.jpx.GPX;
import io.jenetics.jpx.Track;
import io.jenetics.jpx.TrackSegment;
import io.jenetics.jpx.WayPoint;
public final class TrackSegments {
private TrackSegments() {
}
/**
* Return a new collector, which collects a given way point list into a list
* of {@link TrackSegment}s. All way points without a timestamp are filtered
* out. A new segment is created if the timestamp of two consecutive points
* are greater than the give {@code maxGap} duration. Each segment will
* contain at least {@code minSegmentSize} points.
*
* @param maxGap the maximal allowed gap between two points within a
* track segment. If two points exceed the given gap, a new segment
* is created.
* @param minSegmentSize the minimal number of way points a segment must
* consist
* @return a new track segment collector
* @throws NullPointerException if the given {@code maxGap} is {@code null}
* @throws IllegalArgumentException if the {@code maxGap} or
* {@code minSegmentSize} is negative
*/
public static Collector<WayPoint, ?, List<TrackSegment>>
toTrackSegments(final Duration maxGap, final int minSegmentSize) {
if (maxGap.isNegative()) {
throw new IllegalArgumentException(format(
"The maximal allowed point gap must not be negative: %s",
maxGap
));
}
if (minSegmentSize < 1) {
throw new IllegalArgumentException(format(
"The minimal track segment size must be greater 0, but was %d.",
minSegmentSize
));
}
return Collectors.collectingAndThen(
Collectors.toList(),
points -> toTrackSegments(points, maxGap, minSegmentSize)
);
}
private static List<TrackSegment> toTrackSegments(
final List<WayPoint> points,
final Duration gap,
final int minSegmentSize
) {
final List<WayPoint> wps = points.stream()
.filter(wp -> wp.getTime().isPresent())
.toList();
if (wps.size() < minSegmentSize) {
return List.of();
}
final List<TrackSegment> segments = new ArrayList<>();
Instant last = wps.get(0).getTime().orElseThrow();
TrackSegment.Builder segment = TrackSegment.builder();
for (final WayPoint point : wps) {
final Instant zdt = point.getTime().orElseThrow();
if (last.plusNanos(gap.toNanos()).isAfter(zdt)) {
segment.addPoint(point);
} else {
if (segment.points().size() >= minSegmentSize) {
segments.add(segment.build());
}
segment = TrackSegment.builder();
}
last = zdt;
}
if (segment.points().size() >= minSegmentSize) {
segments.add(segment.build());
}
return segments;
}
public static void main(String[] args) throws IOException {
final GPX gpx = GPX.Reader.DEFAULT.read("some_file.gpx");
final Stream<WayPoint> points = gpx.tracks()
.flatMap(Track::segments)
.flatMap(TrackSegment::points);
final List<TrackSegment> segments = points
.collect(toTrackSegments(Duration.ofMinutes(1), 10));
}
}https://stackoverflow.com/questions/72944526
复制相似问题