@Test public void testSampleFile() throws URISyntaxException, NoSuchAuthorityCodeException, ServiceException, FactoryException { File file = new File(getClass().getResource("sample.gpx").toURI()); GPXParser parser = new GPXParser(); SimpleFeatureCollection fc = parser.parse(file, null, CRS.decode("EPSG:3067", true)); assertEquals(1, fc.size()); SimpleFeatureIterator it = fc.features(); try { SimpleFeature f = it.next(); assertEquals("trk name", f.getAttribute("name")); MultiLineString mls = (MultiLineString) f.getDefaultGeometry(); assertEquals(1, mls.getNumGeometries()); LineString ls = (LineString) mls.getGeometryN(0); assertEquals(4, ls.getNumPoints()); Coordinate c; c = ls.getCoordinateN(0); assertEquals(500000, c.x, 1e-7); assertEquals(6751648.982188291, c.y, 1e-7); assertEquals(135.0, c.z, 0.0); c = ls.getCoordinateN(1); assertEquals(500000, c.x, 1e-7); assertEquals(6863040.132332872, c.y, 1e-7); assertEquals(135.0, c.z, 0.0); c = ls.getCoordinateN(2); assertEquals(500000, c.x, 1e-7); assertEquals(6974447.589629403, c.y, 1e-7); assertEquals(135.0, c.z, 0.0); c = ls.getCoordinateN(3); assertEquals(500000, c.x, 1e-7); assertEquals(7085870.966995355, c.y, 1e-7); assertEquals(152.0, c.z, 0.0); } finally { it.close(); } }
@Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try { sourceCRS = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { throw new ServiceException("Failed to decode sourceCrs (EPSG:4326) for GPXParser"); } try { SimpleFeatureCollection parsed; parsed = parse(file, GPX.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } return parsed; } catch (Exception e) { throw new UserLayerException("Failed to parse GPX: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } }
GPXParser implements FeatureCollectionParser { @Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try { sourceCRS = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { throw new ServiceException("Failed to decode sourceCrs (EPSG:4326) for GPXParser"); } try { SimpleFeatureCollection parsed; parsed = parse(file, GPX.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } return parsed; } catch (Exception e) { throw new UserLayerException("Failed to parse GPX: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } } }
GPXParser implements FeatureCollectionParser { @Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try { sourceCRS = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { throw new ServiceException("Failed to decode sourceCrs (EPSG:4326) for GPXParser"); } try { SimpleFeatureCollection parsed; parsed = parse(file, GPX.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } return parsed; } catch (Exception e) { throw new UserLayerException("Failed to parse GPX: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } }  }
GPXParser implements FeatureCollectionParser { @Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try { sourceCRS = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { throw new ServiceException("Failed to decode sourceCrs (EPSG:4326) for GPXParser"); } try { SimpleFeatureCollection parsed; parsed = parse(file, GPX.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } return parsed; } catch (Exception e) { throw new UserLayerException("Failed to parse GPX: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } }  @Override SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS); @Override String getSuffix(); }
GPXParser implements FeatureCollectionParser { @Override public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS) throws ServiceException { try { sourceCRS = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { throw new ServiceException("Failed to decode sourceCrs (EPSG:4326) for GPXParser"); } try { SimpleFeatureCollection parsed; parsed = parse(file, GPX.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.trkType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.rteType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse(file, GPX.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } parsed = parse10(file, GPX10.wptType); if (!parsed.isEmpty()) { return new ReprojectingFeatureCollection(parsed, sourceCRS, targetCRS); } return parsed; } catch (Exception e) { throw new UserLayerException("Failed to parse GPX: " + e.getMessage(), UserLayerException.ErrorType.PARSER, UserLayerException.ErrorType.INVALID_FORMAT); } }  @Override SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS); @Override String getSuffix(); static final String SUFFIX; }