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

implement universal solution for kml viewpoint #771

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ set(SUPPORT
src/core/textstream.cc
src/core/usasciicodec.cc
src/core/vector3d.cc
src/core/welzl.cc
src/core/xmlstreamwriter.cc
)
if(${QT_VERSION_MAJOR} EQUAL "6")
Expand Down Expand Up @@ -193,6 +194,7 @@ set(HEADERS
src/core/textstream.h
src/core/usasciicodec.h
src/core/vector3d.h
src/core/welzl.h
src/core/xmlstreamwriter.h
src/core/xmltag.h
)
Expand Down
2 changes: 2 additions & 0 deletions GPSBabel.pro
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ SUPPORT = route.cc waypt.cc filter_vecs.cc util.cc vecs.cc mkshort.cc \
src/core/textstream.cc \
src/core/usasciicodec.cc \
src/core/vector3d.cc \
src/core/welzl.cc \
src/core/xmlstreamwriter.cc

versionAtLeast(QT_VERSION, 6.0): SUPPORT += src/core/codecdevice.cc
Expand Down Expand Up @@ -190,6 +191,7 @@ HEADERS = \
src/core/textstream.h \
src/core/usasciicodec.h \
src/core/vector3d.h \
src/core/welzl.h \
src/core/xmlstreamwriter.h \
src/core/xmltag.h

Expand Down
25 changes: 11 additions & 14 deletions kml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "src/core/datetime.h" // for DateTime
#include "src/core/file.h" // for File
#include "src/core/logging.h" // for Warning, Fatal
#include "src/core/welzl.h"
#include "src/core/xmlstreamwriter.h" // for XmlStreamWriter
#include "src/core/xmltag.h" // for xml_findfirst, xml_tag, fs_xml, xml_attribute, xml_findnext
#include "units.h" // for fmt_setunits, fmt_speed, fmt_altitude, fmt_distance, units_aviation, units_metric, units_nautical, units_statute
Expand Down Expand Up @@ -364,7 +365,7 @@ void KmlFormat::rd_deinit()
void KmlFormat::wr_init(const QString& fname)
{
char u = 's';
waypt_init_bounds(&kml_bounds);
kml_points.clear();
kml_time_min = QDateTime();
kml_time_max = QDateTime();

Expand Down Expand Up @@ -814,7 +815,7 @@ void KmlFormat::kml_recompute_time_bounds(const Waypoint* waypointp)

void KmlFormat::kml_add_to_bounds(const Waypoint* waypointp)
{
waypt_add_to_bounds(&kml_bounds, waypointp);
kml_points.push_back(gpsbabel::NVector(waypointp->latitude, waypointp->longitude));
kml_recompute_time_bounds(waypointp);
}

Expand Down Expand Up @@ -1682,6 +1683,8 @@ void KmlFormat::kml_write_AbstractView()
route_disp_all(nullptr, nullptr, kml_add_to_bounds_lambda);
}

gpsbabel::Circle bounds = gpsbabel::Welzl::welzl(kml_points);

writer->writeStartElement(QStringLiteral("LookAt"));

if (kml_time_min.isValid() || kml_time_max.isValid()) {
Expand All @@ -1704,25 +1707,19 @@ void KmlFormat::kml_write_AbstractView()
writer->writeEndElement(); // Close gx:TimeSpan tag
}

// If our BB spans the antemeridian, flip sign on one.
// This doesn't make our BB optimal, but it at least prevents us from
// zooming to the wrong hemisphere.
if (kml_bounds.min_lon * kml_bounds.max_lon < 0) {
kml_bounds.min_lon = -kml_bounds.max_lon;
}

writer->writeTextElement(QStringLiteral("longitude"), QString::number((kml_bounds.min_lon + kml_bounds.max_lon) / 2, 'f', precision));
writer->writeTextElement(QStringLiteral("latitude"), QString::number((kml_bounds.min_lat + kml_bounds.max_lat) / 2, 'f', precision));
writer->writeTextElement(QStringLiteral("longitude"), QString::number(bounds.center().longitude(), 'f', precision));
writer->writeTextElement(QStringLiteral("latitude"), QString::number(bounds.center().latitude(), 'f', precision));

// It turns out the length of the diagonal of the bounding box gives us a
// reasonable guess for setting the camera altitude.
double bb_size = gcgeodist(kml_bounds.min_lat, kml_bounds.min_lon,
kml_bounds.max_lat, kml_bounds.max_lon);
// TODO: we can easily calculate a range such that the camera sees a bit beyond the points
// for the cases that the range is reasonable.
double bb_size = 2.0 * bounds.radius();
// Clamp bottom zoom level. Otherwise, a single point zooms to grass.
if (bb_size < 1000) {
bb_size = 1000;
}
writer->writeTextElement(QStringLiteral("range"), QString::number(bb_size * 1.3, 'f', 6));
writer->writeTextElement(QStringLiteral("range"), QString::number(bb_size * 1.3, 'f', 3));

writer->writeEndElement(); // Close LookAt tag
}
Expand Down
4 changes: 3 additions & 1 deletion kml.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "format.h"
#include "src/core/datetime.h" // for DateTime
#include "src/core/file.h" // for File
#include "src/core/nvector.h"
#include "src/core/welzl.h"
#include "src/core/xmlstreamwriter.h" // for XmlStreamWriter
#include "xmlgeneric.h" // for cb_cdata, cb_end, cb_start, xg_callback, xg_string, xg_cb_type, xml_deinit, xml_ignore_tags, xml_init, xml_read, xg_tag_mapping

Expand Down Expand Up @@ -216,7 +218,7 @@ class KmlFormat : public Format
gpsbabel::XmlStreamWriter* writer{nullptr};

int realtime_positioning{};
bounds kml_bounds{};
gpsbabel::PointContainer kml_points;
gpsbabel::DateTime kml_time_max;
gpsbabel::DateTime kml_time_min;

Expand Down
8 changes: 4 additions & 4 deletions reference/LineStyles.kml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<Document>
<name>GPS device</name>
<LookAt>
<longitude>-122.275670</longitude>
<latitude>37.523278</latitude>
<range>5908.813619</range>
<longitude>-122.276195</longitude>
<latitude>37.523328</latitude>
<range>5678.471</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down Expand Up @@ -159,7 +159,7 @@
</Placemark>
<Placemark>
<name>NOTE 1</name>
<description>12 test lines - see notes with GPX sample!!four line widths for solid redtwo line widths for dashed redseven colors for medium widthNote that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that &quot;red med&quot; is a line label and &quot;Red Medium&quot; is a style label.Waypoints are generated by Topo to exactly match the first track point.Apparently only GPX and KML output from GPSBabel retain track descriptions??</description>
<description>12 test lines - see notes with GPX sample!!four line widths for solid redtwo line widths for dashed redseven colors for medium widthNote that line names have nothing at all to do with line style attributes!These are named for convenient cross-reference, but keep in mind that &quot;red med&quot; is a line label and &quot;Red Medium&quot; is a style label.Waypoints are generated by Topo to exactly match the first track point.Apparently only GPX and KML output from GPSBabel retain track descriptions??</description>
<styleUrl>#waypoint</styleUrl>
<Point>
<coordinates>-122.277670,37.522060</coordinates>
Expand Down
6 changes: 3 additions & 3 deletions reference/bounds-test.kml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<Document>
<name>GPS device</name>
<LookAt>
<longitude>-117.144015</longitude>
<latitude>36.438270</latitude>
<range>87257.203848</range>
<longitude>-117.155510</longitude>
<latitude>36.443045</latitude>
<range>68977.917</range>
</LookAt>
<!-- Normal route style -->
<Style id="route_n">
Expand Down
6 changes: 3 additions & 3 deletions reference/earth-expertgps-track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2001-06-02T00:18:13Z</begin>
<end>2002-05-25T19:05:57Z</end>
</gx:TimeSpan>
<longitude>-81.356770</longitude>
<latitude>36.257086</latitude>
<range>2979727.763965</range>
<longitude>-82.179826</longitude>
<latitude>36.694478</latitude>
<range>2974869.803</range>
</LookAt>
<!-- Normal route style -->
<Style id="route_n">
Expand Down
6 changes: 3 additions & 3 deletions reference/earth-expertgps.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2001-06-02T00:18:13Z</begin>
<end>2002-05-25T19:05:57Z</end>
</gx:TimeSpan>
<longitude>-81.356770</longitude>
<latitude>36.257086</latitude>
<range>2979727.763965</range>
<longitude>-82.179826</longitude>
<latitude>36.694478</latitude>
<range>2974869.803</range>
</LookAt>
<!-- Normal route style -->
<Style id="route_n">
Expand Down
16 changes: 8 additions & 8 deletions reference/earth-gc.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2002-08-15T07:00:00Z</begin>
<end>2003-06-29T07:00:00Z</end>
</gx:TimeSpan>
<longitude>-79.930833</longitude>
<latitude>41.027500</latitude>
<range>2109328.437865</range>
<longitude>-80.472308</longitude>
<latitude>41.234430</latitude>
<range>2106968.144</range>
</LookAt>
<!-- Normal waypoint style -->
<Style id="waypoint_n">
Expand Down Expand Up @@ -309,7 +309,7 @@ PICTURES of points and of the panels will follow soon. YOU CAN ONLY LOG ONE POIN
Good luck!]]></value>
</Data>
<Data name="gc_logs">
<value><![CDATA[<p><b>Found it</b> by Christopher R & Pooh B 2005-07-12<br />This marker is not in Quebec but it is a Geodesic marker in Clarenville, Newfoundland, Canada!
<value><![CDATA[<p><b>Found it</b> by Christopher R & Pooh B2005-07-12<br />This marker is not in Quebec but it is a Geodesic marker in Clarenville, Newfoundland, Canada!

Found this one while hunting a traditional cache and thought of this cache right away!

Expand All @@ -319,7 +319,7 @@ Smiles Pooh Bear

Ce marqueur n'est pas au Québec mais c'est un marqueur géodésique dans Clarenville, Terre-Neuve, Canada!

A trouvé celui-ci tandis que chasse une cachette traditionnelle et pensé à cette cachette tout de suite! Elle est située sur la montagne nue dans Clarenville - il y a aactually deux marqueurs à moins de 15 pieds d'un des autres sur la montagne nue... Ours De Pooh De Sourires</p><p><b>Found it</b> by TravelBen 2005-06-26<br />[:D] 14h22
A trouvé celui-ci tandis que chasse une cachette traditionnelle et pensé à cette cachette tout de suite! Elle est située sur la montagne nue dans Clarenville - il y a aactually deux marqueurs à moins de 15 pieds d'un des autres sur la montagne nue... Ours De Pooh De Sourires</p><p><b>Found it</b> by TravelBen2005-06-26<br />[:D] 14h22

Marqueur du Service de la Géodégie (c'est bien un &quot;g&quot; pas un &quot;s&quot;) du Québec.

Expand All @@ -329,7 +329,7 @@ UTM: 18T E 582877 N 5033250

Ce marqueur se trouve dans le ville de Senneville, sur un monument décrivant une page d'histoire du Québec, sur le bas côté avant droit.

Près de la cache: Exo-07 La Jumelle de Loudiver (GCP3VE)</p><p><b>Found it</b> by etasse 2005-06-03<br />MRN marker 94K4731 in Gatineau, QC. corner of Du Rhone and Gatineau Ave.
Près de la cache: Exo-07 La Jumelle de Loudiver (GCP3VE)</p><p><b>Found it</b> by etasse2005-06-03<br />MRN marker 94K4731 in Gatineau, QC. corner of Du Rhone and Gatineau Ave.

Position Average
N 45° 29.5247 W 075° 43.0049 59.49m
Expand All @@ -341,7 +341,7 @@ UTM 18T 0443996 5037868

This pole has everything: An underground cable warning, a geodesic mark, a bus stop and a garage sale sign.

Judging by the coordinates it looks like the coords should be 45°29'31.5&quot; -75°43'0&quot; I placed the GPS antenna right against the marker, to no avail.</p><p><b>Found it</b> by Katou 2005-06-03<br />Un bo point géodésique a Lotbinière..en allant faire une nouvelle cache a l'île richelieu ;-)</p><p><b>Found it</b> by Gps_Gulliver&DauphinBleu 2005-05-29<br />Point Geodesique situe near Port de Plaisance de Longueuil
Judging by the coordinates it looks like the coords should be 45°29'31.5&quot; -75°43'0&quot; I placed the GPS antenna right against the marker, to no avail.</p><p><b>Found it</b> by Katou2005-06-03<br />Un bo point géodésique a Lotbinière..en allant faire une nouvelle cache a l'île richelieu ;-)</p><p><b>Found it</b> by Gps_Gulliver&DauphinBleu2005-05-29<br />Point Geodesique situe near Port de Plaisance de Longueuil
sur le bord du fleuve st-laurent.
Il y a des sentiers et une grande piste cyclable
Enjoy !</p>]]></value>
Expand Down Expand Up @@ -426,7 +426,7 @@ Now that it's intuitively obvious to even the most casual observer where the cac
<img SRC="http://www.mtgc.org/mtgc_member-banner.gif" WIDTH="500" HEIGHT="40" ALT="Member of Middle Tennessee GeoCachers Club [www.mtgc.org]" BORDER="0"></a></p>]]></value>
</Data>
<Data name="gc_logs">
<value><![CDATA[<p><b>Found it</b> by littlepod 2005-07-03<br />Enjoyed the puzzle. We seemed to be about 50ft off though. TFTC.</p><p><b>Write note</b> by robertlipe 2005-04-29<br />TB Drop to show he's hanging out in Nashville until we blast off for Geowoodstock in a few weeks.</p><p><b>Found it</b> by Big Bumblebee 2005-04-18<br />Found it a while ago. Thanks.</p><p><b>Write note</b> by robertlipe 2005-03-27<br />I had to renew my permit with the CDC and in doing so, I trolled out here verified that the infectious ooze is fully within specification and industry accepted tolerance. Ooze On!</p><p><b>Found it</b> by Virtual Babe 2004-12-27<br />This was a great cache, however on this day I considered it a FIFM cache (Fun, Invigorating, Frustrating and Maddening), especially when the cache was not replaced in the proper spot by the previous cacher! Thanks anyway!!</p><p><b>Write note</b> by robertlipe 2004-01-12<br />I got a complaint from the CDC about oozy rat this weekend. I went out tonight in the dark and verified that the infectious ooze is fully within specification and industry accepted tolerance. (Although I realize now I did misstate the cache container to the reporting officer when confronted. It's, uuuuh, smaller than I said.)</p><p><b>Write note</b> by robertlipe 2003-10-04<br />In the expectation that this cache will get some traffic in the next 48 hours, Ryan and I checked it earlier today. The Rat is Oozing just as we planned it.</p><p><b>Write note</b> by robertlipe 2003-07-03<br />It won't earn him a smiley face, but I've confirmed that rickrich would have indeed sunk the battleship! Thanx for playing. You get a copy of the home game and some rice-a-roni...</p>]]></value>
<value><![CDATA[<p><b>Found it</b> by littlepod2005-07-03<br />Enjoyed the puzzle. We seemed to be about 50ft off though. TFTC.</p><p><b>Write note</b> by robertlipe2005-04-29<br />TB Drop to show he's hanging out in Nashville until we blast off for Geowoodstock in a few weeks.</p><p><b>Found it</b> by Big Bumblebee2005-04-18<br />Found it a while ago. Thanks.</p><p><b>Write note</b> by robertlipe2005-03-27<br />I had to renew my permit with the CDC and in doing so, I trolled out here verified that the infectious ooze is fully within specification and industry accepted tolerance. Ooze On!</p><p><b>Found it</b> by Virtual Babe2004-12-27<br />This was a great cache, however on this day I considered it a FIFM cache (Fun, Invigorating, Frustrating and Maddening), especially when the cache was not replaced in the proper spot by the previous cacher! Thanks anyway!!</p><p><b>Write note</b> by robertlipe2004-01-12<br />I got a complaint from the CDC about oozy rat this weekend. I went out tonight in the dark and verified that the infectious ooze is fully within specification and industry accepted tolerance. (Although I realize now I did misstate the cache container to the reporting officer when confronted. It's, uuuuh, smaller than I said.)</p><p><b>Write note</b> by robertlipe2003-10-04<br />In the expectation that this cache will get some traffic in the next 48 hours, Ryan and I checked it earlier today. The Rat is Oozing just as we planned it.</p><p><b>Write note</b> by robertlipe2003-07-03<br />It won't earn him a smiley face, but I've confirmed that rickrich would have indeed sunk the battleship! Thanx for playing. You get a copy of the home game and some rice-a-roni...</p>]]></value>
</Data>
</ExtendedData>
<Point>
Expand Down
2 changes: 1 addition & 1 deletion reference/realtime.kml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</gx:TimeSpan>
<longitude>-32.056227</longitude>
<latitude>30.474664</latitude>
<range>1728.780603</range>
<range>1726.846</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down
6 changes: 3 additions & 3 deletions reference/track/bounds-test-track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<Document>
<name>GPS device</name>
<LookAt>
<longitude>-117.144015</longitude>
<latitude>36.438270</latitude>
<range>87257.203848</range>
<longitude>-117.155510</longitude>
<latitude>36.443045</latitude>
<range>68977.917</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down
2 changes: 1 addition & 1 deletion reference/track/gpx_garmin_extensions-kml_track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</gx:TimeSpan>
<longitude>-0.035329</longitude>
<latitude>51.506248</latitude>
<range>1300.000000</range>
<range>1300.000</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down
24 changes: 12 additions & 12 deletions reference/track/gtrnctr_power-kml.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<begin>2010-05-28T01:16:36Z</begin>
<end>2010-05-28T02:41:44Z</end>
</gx:TimeSpan>
<longitude>-122.139608</longitude>
<latitude>37.382794</latitude>
<range>19819.321245</range>
<longitude>-122.131432</longitude>
<latitude>37.382993</latitude>
<range>18292.113</range>
</LookAt>
<!-- Normal track style -->
<!-- Normal track style -->
<Style id="track_n">
<IconStyle>
<scale>.5</scale>
Expand All @@ -23,7 +23,7 @@
<scale>0</scale>
</LabelStyle>
</Style>
<!-- Highlighted track style -->
<!-- Highlighted track style -->
<Style id="track_h">
<IconStyle>
<scale>1.2</scale>
Expand All @@ -42,7 +42,7 @@
<styleUrl>#track_h</styleUrl>
</Pair>
</StyleMap>
<!-- Normal multiTrack style -->
<!-- Normal multiTrack style -->
<Style id="multiTrack_n">
<IconStyle>
<Icon>
Expand All @@ -54,7 +54,7 @@
<width>6</width>
</LineStyle>
</Style>
<!-- Highlighted multiTrack style -->
<!-- Highlighted multiTrack style -->
<Style id="multiTrack_h">
<IconStyle>
<scale>1.2</scale>
Expand All @@ -77,15 +77,15 @@
<styleUrl>#multiTrack_h</styleUrl>
</Pair>
</StyleMap>
<!-- Normal waypoint style -->
<!-- Normal waypoint style -->
<Style id="waypoint_n">
<IconStyle>
<Icon>
<href>https://maps.google.com/mapfiles/kml/pal4/icon61.png</href>
</Icon>
</IconStyle>
</Style>
<!-- Highlighted waypoint style -->
<!-- Highlighted waypoint style -->
<Style id="waypoint_h">
<IconStyle>
<scale>1.2</scale>
Expand All @@ -112,13 +112,13 @@
</Style>
<Schema id="schema">
<gx:SimpleArrayField name="heartrate" type="int">
<displayName>Heart Rate</displayName>
<displayName>Heart Rate</displayName>
</gx:SimpleArrayField>
<gx:SimpleArrayField name="cadence" type="int">
<displayName>Cadence</displayName>
<displayName>Cadence</displayName>
</gx:SimpleArrayField>
<gx:SimpleArrayField name="power" type="float">
<displayName>Power</displayName>
<displayName>Power</displayName>
</gx:SimpleArrayField>
</Schema>
<Folder>
Expand Down
4 changes: 2 additions & 2 deletions reference/track/segmented_tracks-track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2007-07-27T05:24:05Z</begin>
<end>2007-07-27T05:35:00Z</end>
</gx:TimeSpan>
<longitude>-86.841461</longitude>
<longitude>-86.841519</longitude>
<latitude>35.831217</latitude>
<range>1944.793167</range>
<range>1901.182</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down
4 changes: 2 additions & 2 deletions reference/track/segmented_tracks.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2007-07-27T05:24:05Z</begin>
<end>2007-07-27T05:35:00Z</end>
</gx:TimeSpan>
<longitude>-86.841461</longitude>
<longitude>-86.841519</longitude>
<latitude>35.831217</latitude>
<range>1944.793167</range>
<range>1901.182</range>
</LookAt>
<!-- Normal track style -->
<Style id="track_n">
Expand Down
6 changes: 3 additions & 3 deletions reference/track/tracks~gpx.kml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<begin>2002-05-25T17:06:21.250Z</begin>
<end>2002-05-25T19:05:57.980Z</end>
</gx:TimeSpan>
<longitude>-91.600158</longitude>
<latitude>30.054150</latitude>
<range>3611.461364</range>
<longitude>-91.600224</longitude>
<latitude>30.055900</latitude>
<range>3264.506</range>
</LookAt>
<!-- Normal track-none style -->
<Style id="track-none_n">
Expand Down
Loading