Coder Social home page Coder Social logo

android-gpx-parser's Introduction

Android Arsenal

Android GPX Parser

Table of Contents:

  1. Description
  2. Module Structures
  3. Projects using this library
  4. Download
  5. Dependencies
  6. Usage
  7. License

Description

A library to parse GPX files, built for Android. The reference schema is the Topografix GPX 1.1.
In addition, it parses the speed extension, when provided as a double number.

GPX is an XML file format to represent GPS data: coordinates, routes, waypoints, and more.

Pull requests are welcome! Please check the issues and open a pull request when done: you will have made the world a better place.

Module Structure

  1. App: an example usage of the library.
  2. Parser: the library itself.

Projects using this library:

To have your project listed here, please send me an email or open a PR.

Download

Add the Jitpack repository to your root build file. The way you do this depends on the Gradle plugin you are using.

If using Gradle plugin 7.0.0. or newer (default for new apps since Android Studio Artic Fox)

// in settings.gradle
dependencyResolutionManagement {
    ...
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

For older verions of Gradle:

// in project-level build.gradle
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Finally, in your dependencies list

dependencies {
    implementation 'com.github.ticofab:android-gpx-parser:2.3.1'
}

Dependencies

The following is an open github repo that has date and time handling. Keep in mind that the following dependency also has dependencies of its own.

Usage

In Java:

GPXParser parser = new GPXParser(); // consider injection
Gpx parsedGpx = null;
try {
    InputStream in = getAssets().open("test.gpx");
    parsedGpx = parser.parse(in); // consider using a background thread
} catch (IOException | XmlPullParserException e) {
    // do something with this exception
    e.printStackTrace();
}
if (parsedGpx == null) {
    // error parsing track
} else {
    // do something with the parsed track
    // see included example app and tests
}

In Kotlin:

val parser = GPXParser() // consider injection
try {
    val input: InputStream = getAssets().open("test.gpx")
    val parsedGpx: Gpx? = parser.parse(input) // consider using a background thread
    parsedGpx?.let {
        // do something with the parsed track
        // see included example app and tests
    } ?: {
        // error parsing track
    }
} catch (e: IOException) {
    // do something with this exception
    e.printStackTrace()
} catch (e: XmlPullParserException) {
    // do something with this exception
    e.printStackTrace()
}

License

Copyright 2015 - 2023 Fabio Tiriticco - Fabway

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-gpx-parser's People

Contributors

abinpaul1 avatar andreasflosdorf avatar gabrielezereik avatar herberlin avatar msbit avatar neevasethi avatar pygmalion69 avatar sebaslogen avatar stustirling avatar ticofab avatar tkorri avatar vincenzovitale-tomtom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-gpx-parser's Issues

How to deal with missing extensions?

Hi, thanks so much for this nice library! Quick question, my GPX files also have a HR field under TrackPointExtension (to go along with the usual lat/long/time fields). I'm trying to figure out the best way to go add the HR field to the TrackPoints returned by the library.

Add Testing

At the moment there is no testing. I suggest getting some unit tests written.

What would be the suggested pattern for this? We could have a test GPX file that we read in from the assets in the parser module, run it through the parser and check the track result at the end?

The only issue with this is it's not very small and not really a unit test more of an end-to-end test? Would this be ok if we had separate tests for each thing?

not sucessed

<name>2020-03-02</name>
<copyright author="etx365.cn">
  <year>2020</year>
  <license>etx365.cn</license>
  <author>LAVEN</author>
</copyright>
2020-03-02 2020 etx365.cn LAVEN

I use java code like :

Metadata metadata = parsedGpx2.getMetadata();
String name = metadata.getName();
String desc = metadata.getDesc();
Copyright copyright = metadata.getCopyright();
String author = copyright.getAuthor();
String license = copyright.getLicense();
Log.d(TAG, author + license);

D/TTT: etx365.cn etx365.cn
it succed like D/TTT: LAVEN etx365.cn
How could I do something ? Thank you

Problem with JodaTime

Hi, I'm getting java.lang.NoClassDefFoundError: org.joda.time.format.ISODateTimeFormat. I have Joda dependencie added to my build.gradle and annotation multiDexEnabled true but its not working :/

Parser causing exception for <gpsies:property> tag

Hello @ticofab, I hope you are well.
Wanted to thank you again for this library.

Recently a user shared a GPX which contained this data under the block

<extensions>
      <gpsies:property>round trip</gpsies:property>
      <gpsies:trackLengthMeter>26131.64726969568</gpsies:trackLengthMeter>
      <gpsies:totalAscentMeter>339.0</gpsies:totalAscentMeter>
      <gpsies:totalDescentMeter>338.0</gpsies:totalDescentMeter>
      <gpsies:minHeightMeter>215.0</gpsies:minHeightMeter>
      <gpsies:maxHeightMeter>279.0</gpsies:maxHeightMeter>
    </extensions>

And the library threw this exception -

org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null}extensions (position:START_TAG <{https://www.gpsies.com/GPX/1/0}gpsies:property>@18:24 in java.io.InputStreamReader@1145afa)

Would be great if you could look into it. Thanks.

General support for gpx extensions

Description:
The current implementation of the library supports parsing a specific extension, but does not provide a mechanism to parse other extensions that can be found within the <extension> tag in a GPX file. This limitation restricts the library's flexibility and compatibility with GPX files that contain various custom or third-party extensions.

Expected behavior:
The library should parse all data contained within the <extension> tag, regardless of the extension's name or structure. The parsed data should be made accessible to users in a structured and user-friendly format, such as a map or dictionary. Users should have the ability to iterate through or query the parsed extensions and retrieve their values.

Error while parsing GPX file from filepath

java.lang.IllegalArgumentException: Invalid format: "" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:947) at io.ticofab.androidgpxparser.parser.GPXParser.readTime(GPXParser.java:500) at io.ticofab.androidgpxparser.parser.GPXParser.readPoint(GPXParser.java:319) at io.ticofab.androidgpxparser.parser.GPXParser.readTrackPoint(GPXParser.java:461) at io.ticofab.androidgpxparser.parser.GPXParser.readSegment(GPXParser.java:233) at io.ticofab.androidgpxparser.parser.GPXParser.readTrack(GPXParser.java:149) at io.ticofab.androidgpxparser.parser.GPXParser.readGpx(GPXParser.java:117) at io.ticofab.androidgpxparser.parser.GPXParser.parse(GPXParser.java:83) at com.ideveloper.ui.activity.MapActivity.gpxTrackPoints(MapActivity.java:390) at com.ideveloper.ui.activity.MapActivity.access$1100(MapActivity.java:108) at com.ideveloper.ui.activity.MapActivity$6.onDownloadComplete(MapActivity.java:855) at com.downloader.request.DownloadRequest$2.run(DownloadRequest.java:260) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7870) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Possible answers please drop.

Parser code
GPXParser parser = new GPXParser(); // consider injection Gpx parsedGpx = null; try { // FileInputStream in = new FileInputStream(files); FileInputStream in = new FileInputStream(filePath); Log.d(TAG, "---->"+filePath); parsedGpx = parser.parse(in); // consider using a background thread } catch (IOException | XmlPullParserException e) { e.printStackTrace(); }

Publishing to Maven central

Would there be any interest in publishing this library to Maven central instead of requiring Jitpack? I'd love to be able to remove the Jitpack dependency if possible, and would be happy to help in adding this support ๐Ÿ‘

How to get the speed from track points?

Hi, i am trying to use your library and i need the speed of the trackpoint from your sample gpx file
<ns2:extensions> <speed>1.4887607411448586791635761983343400061130523681640625</speed> </ns2:extensions>

Improve Library Release Structure

At the moment there is a lot of code within the library module's build.gradle file that I assume is related to the library's release.

Is there a way to remove a lot of this? I haven't done many library releases but the one I have done hasn't need any of this code. There is one module in particular that causes an issue and thats classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'. I believe this is because you need to apply the plugin in the module that depends on it which isn't ideal. I am not sure what this is for so can't definitely remove it.

Parse waypoint description

The library (1.1.2) still not parse the (description) field from waypoint!
Can you please add it in the next version?
thanks in advance.

// part from real gpx file. 2015-09-14T06:54:35Z -=1=- 150 A text description of the element. Holds additional information about the element intended for the user, not the GPS.

Cannot access time

I cannot access point.time because of the following error:

Cannot access class 'org.joda.time.DateTime'. Check your module classpath for missing or conflicting dependencies

I am pretty sure that this is because

implementation 'net.danlew:android.joda:2.10.9.1'

(from the library's build.gradle) does not expose org.joda.time classes to apps integrating your library. Changing it to

api 'net.danlew:android.joda:2.10.9.1'

should fix this without requiring apps to add joda to their dependencies.

I need to first download

Hi,

I am making http get request to download gpx file from server (i need to download it. it is a requirement)

private void callAPI(String routeId) {

    NetworkBuilder.getAppNetwork().getRoute(routeId).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

            Log.e("@@@@@@@@@@@", "API SUCCESS");
            try {
                File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File file = new File(path, "cyclingRoute.gpx");
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                IOUtils.write(response.body().bytes(), fileOutputStream);
                parseRoute(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            
            Log.e("@@@@@@@@@@@", "API FAILS" + t.getMessage());
        }
    });
}

with parseRoute function i am trying to parse gpx but input stream "is" throws a null pointer exception.
I also tried implementing

InputStream in = getAssets().open("cyclingRoute.gpx"); but it said no file or directory found.

private void parseRoute(File file){

    GPXParser mParser = new GPXParser();
    Gpx parsedGpx = null;
    try {
        InputStream  is= getClass().getResourceAsStream(file.getName());
        parsedGpx = mParser.parse(is);
    } catch (IOException | XmlPullParserException e) {
        // do something with this exception
        e.printStackTrace();
    }
    if (parsedGpx == null) {
        // error parsing track
    } else {
        // do something with the parsed track
        List<Track> tracks = parsedGpx.getTracks();
        for (int i = 0; i < tracks.size(); i++) {
            Track track = tracks.get(i);
            Log.d(TAG, "track " + i + ":");
            List<TrackSegment> segments = track.getTrackSegments();
            for (int j = 0; j < segments.size(); j++) {
                TrackSegment segment = segments.get(j);
                Log.d(TAG, "  segment " + j + ":");
                for (TrackPoint trackPoint : segment.getTrackPoints()) {
                    Log.d(TAG, "    point: lat " + trackPoint.getLatitude() + ", lon " + trackPoint.getLongitude());
                }
            }
        }
    }
}

any help will be appriciated!!!

Is it planned to make a writer?

Hello, I wanted to know if it was planned to do in both directions, reading but also writing?

Thank you in advance for your answer.

Access to gpx extensions

The parser implements the standard Topografix GPX 1.1, in which the attribute 'speed' is not defined, unlike in 1.0. Nevertheless, it is possible as an optional extension. There is also a test case, that includes this attribute, see:
https://github.com/ticofab/android-gpx-parser/blob/master/app/src/main/assets/test.gpx

In the API (io.ticofab.androidgpxparser.parser.domain.TrackPoint) there is no way to access extensions to get the attribute 'speed' if it is present in the gpx file. Speed can be calculated by using existing data (distance and time differences), but if this information is availlavle in the gpx file, there are reasons to use this information. An extension to the API that allows access to GPX Extensions would be great.

parse <ele/> throw exception

When there's a "<ele/>" in gpx file, it throw a exception:
GPXParse.readElevation(XmlPullParser parser): line 160
Double ele = Double.valueOf(readText(parser));

I try to fix it:
final String text = readText(parser);
Double ele;
if (TextUtils.isEmpty(text))
{
ele = 0d;
}
else
{
ele = Double.valueOf(text);
}
but another exception throw from GPXParse.readTime(XmlPullParser parser), and I can't catch it.

Write to file

I have built out a GPX file using the Builder methods. I am also parsing separately. How do I write the resultant Gpx object to a file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.