Skip to content

Discard points without coordinates during parsing #86

@Sibyx

Description

@Sibyx

Problem

PointParser::parse() does not validate that points have latitude and longitude. Malformed GPX data with missing lat/lon attributes will produce Point objects with null coordinates, which can cause errors downstream in:

  • GeoHelper::getRawDistance()deg2rad(null)
  • BoundsAnalyzer — comparing null coordinates
  • Any distance/elevation calculations

Current code

// src/phpGPX/Parsers/PointParser.php (2.x develop)
$point->latitude = isset($node['lat']) ? ((float) $node['lat']) : null;
$point->longitude = isset($node['lon']) ? ((float) $node['lon']) : null;
// ... continues processing even if both are null

Proposed fix

Return null from PointParser::parse() when a point has no coordinates, and filter nulls in callers:

// PointParser::parse()
$point->latitude = isset($node['lat']) ? ((float) $node['lat']) : null;
$point->longitude = isset($node['lon']) ? ((float) $node['lon']) : null;

if ($point->latitude === null && $point->longitude === null) {
    return null;
}

Callers (SegmentParser, RouteParser) need to filter null results:

// SegmentParser::parse() example
foreach ($node->trkpt as $trkpt) {
    $point = PointParser::parse($trkpt);
    if ($point !== null) {
        $segment->points[] = $point;
    }
}

Notes

  • This is a defensive parsing measure — real-world GPX files occasionally contain malformed points
  • The check uses && (both null), not || — a point with only one coordinate is also invalid but rarer

Related discussion: #67 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions