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

Gpx wpt mappoints #452

Merged
merged 10 commits into from
Mar 11, 2022
15 changes: 15 additions & 0 deletions demo/images/emptydirectory/unique_wpt.gpx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<gpx version="1.1" creator="OsmAnd~ 4.1.11" xmlns="http://www.topografix.com/GPX/1/1" xmlns:osmand="https://osmand.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<name>favourites</name>
</metadata>
<wpt lat="46.1234" lon="10.1234">
<time>2022-02-12T19:31:58Z</time>
<name>Location name</name>
<extensions>
<osmand:icon>special_star</osmand:icon>
<osmand:background>circle</osmand:background>
<osmand:color>#eecc22</osmand:color>
</extensions>
</wpt>
</gpx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<gpx version="1.1" creator="OsmAnd~ 4.1.11" xmlns="http://www.topografix.com/GPX/1/1" xmlns:osmand="https://osmand.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<name>favourites</name>
</metadata>
<wpt lat="39.1234" lon="-0.54332">
<time>2022-02-04T00:12:22Z</time>
<name>Location 1</name>
<extensions>
<osmand:icon>special_star</osmand:icon>
<osmand:background>circle</osmand:background>
<osmand:color>#eecc22</osmand:color>
</extensions>
</wpt>
<wpt lat="39.54321" lon="-0.1234">
<time>2022-02-04T00:12:22Z</time>
<name>Location 2</name>
<extensions>
<osmand:address>Street 1, Town</osmand:address>
<osmand:icon>special_star</osmand:icon>
<osmand:background>circle</osmand:background>
<osmand:color>#eecc22</osmand:color>
</extensions>
</wpt>
<wpt lat="12.54321" lon="10.1234">
<time>2022-02-04T00:12:22Z</time>
<name>Location 3</name>
<extensions>
<osmand:address>Street 2, Town</osmand:address>
<osmand:icon>special_star</osmand:icon>
<osmand:background>circle</osmand:background>
<osmand:color>#eecc22</osmand:color>
</extensions>
</wpt>
</gpx>
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,32 @@ export class GalleryMapLightboxComponent implements OnChanges {
for (let i = 0; i < this.gpxFiles.length; i++) {
const file = this.gpxFiles[i];
const path = await this.mapService.getMapPath(file);
const wpoints = await this.mapService.getMapPoints(file);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the code from getMapPoints to getMapPath, as both functions do a network call and that is wasteful (probably the browser would serve the second call from cache, but still). Also the two function has some common code. You can rename the function to something more generic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've made it a single getMapCoordinates[][] instead, which returns the array of arrays coordinates = [path,wpoints] - so there's only one network call to the GPX file, but 2 calls to getElementsByTagName in getMapCoordinates(). I hope that's good enough :)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is perfect, thank you.

if (file !== this.gpxFiles[i]) { // check race condition
return;
}
if (path.length === 0) {
continue;
if (path.length !== 0) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clean up this part a bit? removing unnecessary comments, etc. Otherwise this PR looks good.

this.mapLayersControlOption.overlays.Paths.addLayer(marker(path[0] as LatLng));
this.mapLayersControlOption.overlays.Paths.addLayer(polyline(path as LatLng[]));
// console.log('Trk path is not empty, in file ' + i + ', path[0]=' + JSON.stringify(path[0]));
// continue;
} else {
// console.log('No trk path in file ' + i);
// continue;
}
if (wpoints.length !== 0) {
// console.log('Wpt exist in file ' + i + ', wpoints[0]=' + JSON.stringify(wpoints[0]) +', wpoints.length=' + wpoints.length);
wpoints_loop: for (let wpt_i = 0; i < wpoints.length; wpt_i++) {
// console.log('item number ' + wpt_i + ' out of ' + wpoints.length + ', coord=' + JSON.stringify(wpoints[wpt_i]));
if (wpoints[wpt_i] === undefined) {
break wpoints_loop;
}
this.mapLayersControlOption.overlays.Paths.addLayer(marker(wpoints[wpt_i] as LatLng));
}
// console.log('Wpoints plotted.');
} else {
// console.log('No wpt points in file ' + i);
}
this.mapLayersControlOption.overlays.Paths.addLayer(marker(path[0] as LatLng));
this.mapLayersControlOption.overlays.Paths.addLayer(polyline(path as LatLng[]));
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/frontend/app/ui/gallery/map/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ export class MapService {
}
return points;
}

// Waypoints <wpt> from GPX files:
public async getMapPoints(file: FileDTO): Promise<MapPoints[]> {
const filePath = Utils.concatUrls(file.directory.path, file.directory.name, file.name);
const gpx = await this.networkService.getXML('/gallery/content/' + filePath);
const elements = gpx.getElementsByTagName('wpt');
const wpoints: MapPoints[] = [];
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < elements.length; i++) {
wpoints.push({
lat: parseFloat(elements[i].getAttribute('lat')),
lng: parseFloat(elements[i].getAttribute('lon'))
});
}
console.log('From file ' + filePath + ', wpoints=' + JSON.stringify(wpoints));
return wpoints;
}

}

Expand All @@ -92,3 +109,8 @@ export interface MapPath {
lat: number;
lng: number;
}

export interface MapPoints {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reuse the interface above. Maybe refactor it to something like MapCoordinates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

lat: number;
lng: number;
}