Skip to content
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

Add a way to build a GpxBoundingBox from a list of points #17

Open
HarelM opened this issue Aug 11, 2018 · 0 comments
Open

Add a way to build a GpxBoundingBox from a list of points #17

HarelM opened this issue Aug 11, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@HarelM
Copy link
Contributor

HarelM commented Aug 11, 2018

Some GPX file do not have bounds property filled.
In some cases filling this can help out when using the GPX object.
Below is the code I'm using in order to update the GPX bounds, I thought this library might be a good fit to it. If not, I can always have this code in my repository.

        public static GpxMainObject UpdateBounds(this GpxMainObject gpx)
        {
            if (gpx.Metadata?.Bounds != null &&
                gpx.Metadata.Bounds.MinLatitude.Value != 0 &&
                gpx.Metadata.Bounds.MaxLatitude.Value != 0 &&
                gpx.Metadata.Bounds.MinLongitude.Value != 0 &&
                gpx.Metadata.Bounds.MaxLongitude.Value != 0)
            {
                return gpx;
            }
            var points = (gpx.Routes ?? new List<GpxRoute>()).Where(r => r.Waypoints != null).SelectMany(r => r.Waypoints).ToArray();
            points = points.Concat(gpx.Waypoints ?? new List<GpxWaypoint>()).ToArray();
            points = points.Concat((gpx.Tracks ?? new List<GpxTrack>()).Where(r => r.Segments != null).SelectMany(t => t.Segments).SelectMany(s => s.Waypoints)).ToArray();
            if (!points.Any())
            {
                return gpx;
            }

            var boundingBox = new GpxBoundingBox(
                minLatitude: new GpxLatitude(points.Min(p => p.Latitude.Value)), 
                maxLatitude: new GpxLatitude(points.Max(p => p.Latitude.Value)),
                minLongitude: new GpxLongitude(points.Min(p => p.Longitude.Value)), 
                maxLongitude: new GpxLongitude(points.Max(p => p.Longitude.Value))
            );
            gpx.Metadata = gpx.Metadata == null 
                ? new GpxMetadata(null, null, null, null, null, ImmutableArray<GpxWebLink>.Empty, null, null, boundingBox, null) 
                : new GpxMetadata(gpx.Metadata.Creator, gpx.Metadata.Name, gpx.Metadata.Description, gpx.Metadata.Author, gpx.Metadata.Copyright, gpx.Metadata.Links, gpx.Metadata.CreationTime, gpx.Metadata.Keywords, boundingBox, gpx.Metadata.Extensions);
            return gpx;
        }
@airbreather airbreather added the enhancement New feature or request label Aug 11, 2018
@airbreather airbreather changed the title Update GPX bounds Add a way to build a GpxBoundingBox from a list of points Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants