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

Fix kml bounding box issues. #761

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ set(SUPPORT
formspec.cc xmltag.cc cet.cc cet_util.cc fatal.cc rgbcolors.cc
inifile.cc garmin_fs.cc units.cc gbser.cc
gbfile.cc parse.cc session.cc main.cc globals.cc
src/core/nvector.cc
src/core/textstream.cc
src/core/usasciicodec.cc
src/core/vector3d.cc
src/core/xmlstreamwriter.cc
)
if(${QT_VERSION_MAJOR} EQUAL "6")
Expand Down Expand Up @@ -187,8 +189,10 @@ set(HEADERS
src/core/datetime.h
src/core/file.h
src/core/logging.h
src/core/nvector.h
src/core/textstream.h
src/core/usasciicodec.h
src/core/vector3d.h
src/core/xmlstreamwriter.h
src/core/xmltag.h
)
Expand Down
4 changes: 4 additions & 0 deletions GPSBabel.pro
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ SUPPORT = route.cc waypt.cc filter_vecs.cc util.cc vecs.cc mkshort.cc \
formspec.cc xmltag.cc cet.cc cet_util.cc fatal.cc rgbcolors.cc \
inifile.cc garmin_fs.cc units.cc gbser.cc \
gbfile.cc parse.cc session.cc main.cc globals.cc \
src/core/nvector.cc \
src/core/textstream.cc \
src/core/usasciicodec.cc \
src/core/vector3d.cc \
src/core/xmlstreamwriter.cc

versionAtLeast(QT_VERSION, 6.0): SUPPORT += src/core/codecdevice.cc
Expand Down Expand Up @@ -184,8 +186,10 @@ HEADERS = \
src/core/datetime.h \
src/core/file.h \
src/core/logging.h \
src/core/nvector.h \
src/core/textstream.h \
src/core/usasciicodec.h \
src/core/vector3d.h \
src/core/xmlstreamwriter.h \
src/core/xmltag.h

Expand Down
24 changes: 15 additions & 9 deletions kml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
#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/nvector.h" // for NVector
#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
#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

using gpsbabel::NVector;

// Icons provided and hosted by Google. Used with permission.
#define ICON_BASE "https://earth.google.com/images/kml-icons/"
Expand Down Expand Up @@ -1237,7 +1239,7 @@ QString KmlFormat::kml_geocache_get_logs(const Waypoint* wpt) const
if (logpart) {
gpsbabel::DateTime t = xml_parse_time(logpart->cdata);
if (t.isValid()) {
r += t.date().toString(Qt::ISODate);
r = r + " " + t.date().toString(Qt::ISODate);
}
}

Expand Down Expand Up @@ -1704,15 +1706,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;
}
// Uses NVectors to compute the middle of the bounding box.
// This works even if the BB spans the antimeridian.
// This prevents us from zooming to the wrong hemisphere.
// Gade, K. (2010). A Non-singular Horizontal Position Representation, The Journal of Navigation, Volume 63, Issue 03, pp 395-417, July 2010.
// 5.3.6. Horizontal geographical mean, equation 17.
NVector bb_1(kml_bounds.min_lat, kml_bounds.min_lon);
NVector bb_2(kml_bounds.min_lat, kml_bounds.max_lon);
NVector bb_3(kml_bounds.max_lat, kml_bounds.min_lon);
NVector bb_4(kml_bounds.max_lat, kml_bounds.max_lon);
NVector bb_mid = (bb_1 + bb_2 + bb_3 + bb_4).normalize();

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(bb_mid.longitude(), 'f', precision));
writer->writeTextElement(QStringLiteral("latitude"), QString::number(bb_mid.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.
Expand Down
4 changes: 2 additions & 2 deletions reference/LineStyles.kml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>GPS device</name>
<LookAt>
<longitude>-122.275670</longitude>
<latitude>37.523278</latitude>
<latitude>37.523279</latitude>
<range>5908.813619</range>
</LookAt>
<!-- Normal track style -->
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
2 changes: 1 addition & 1 deletion reference/bounds-test.kml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>GPS device</name>
<LookAt>
<longitude>-117.144015</longitude>
<latitude>36.438270</latitude>
<latitude>36.438594</latitude>
<range>87257.203848</range>
</LookAt>
<!-- Normal route style -->
Expand Down
2 changes: 1 addition & 1 deletion reference/earth-expertgps-track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<end>2002-05-25T19:05:57Z</end>
</gx:TimeSpan>
<longitude>-81.356770</longitude>
<latitude>36.257086</latitude>
<latitude>36.698053</latitude>
<range>2979727.763965</range>
</LookAt>
<!-- Normal route style -->
Expand Down
2 changes: 1 addition & 1 deletion reference/earth-expertgps.kml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<end>2002-05-25T19:05:57Z</end>
</gx:TimeSpan>
<longitude>-81.356770</longitude>
<latitude>36.257086</latitude>
<latitude>36.698053</latitude>
<range>2979727.763965</range>
</LookAt>
<!-- Normal route style -->
Expand Down
2 changes: 1 addition & 1 deletion reference/earth-gc.kml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<end>2003-06-29T07:00:00Z</end>
</gx:TimeSpan>
<longitude>-79.930833</longitude>
<latitude>41.027500</latitude>
<latitude>41.235699</latitude>
<range>2109328.437865</range>
</LookAt>
<!-- Normal waypoint style -->
Expand Down
2 changes: 1 addition & 1 deletion reference/track/bounds-test-track.kml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>GPS device</name>
<LookAt>
<longitude>-117.144015</longitude>
<latitude>36.438270</latitude>
<latitude>36.438594</latitude>
<range>87257.203848</range>
</LookAt>
<!-- Normal track style -->
Expand Down
20 changes: 10 additions & 10 deletions reference/track/gtrnctr_power-kml.kml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<end>2010-05-28T02:41:44Z</end>
</gx:TimeSpan>
<longitude>-122.139608</longitude>
<latitude>37.382794</latitude>
<latitude>37.382814</latitude>
Copy link
Collaborator

Choose a reason for hiding this comment

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

@viettaml , note this CL is going to change the LookAt (and thus, FlyTo) on files we handle. It's small on most common data and cases involving > hemisphere of polygons or places spanning hemispheres are subject to weird stuff already.
We can at least reduce OUR weird stuff.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note that the added test case in kml.cc demonstrates the existing code flying to the wrong place half way around the world. In working a another enhancement I got so sick of GE flying to the wrong side of the world I made a detour (this PR) to fix it.

The small changes in latitude are not due to finite precisions arithmetic, they are due to the span of longitude in the bounding box which moves the midpoint along a great circle diameter towards the pole.

Also note the code currently has COMPARE_BEARING_TO_GRTCIRC defined, which matches our grtcirc calculations, but uses a spherical earth with the equatorial radius. If we correct(undefine) that and use nvectors instead of grtcirc in the future a bunch of reference files will need to be tweaked.

<range>19819.321245</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
49 changes: 49 additions & 0 deletions reference/track/sim.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
utc_d,utc_t,lat,lon
2021/11/10,11:00:00 AM,66.000103228632,-179.97904
2021/11/10,11:00:01 AM,66.0001884601077,-179.97903
2021/11/10,11:00:02 AM,66.0000230237331,-179.97947
2021/11/10,11:00:03 AM,66.000045323685,-179.98008
2021/11/10,11:00:04 AM,66.0001767900345,-179.98048
2021/11/10,11:00:05 AM,66.0001092928285,-179.98067
2021/11/10,11:00:06 AM,66.0001668298279,-179.98197
2021/11/10,11:00:07 AM,66.0000748518391,-179.98320
2021/11/10,11:00:08 AM,66.0001341555094,-179.98429
2021/11/10,11:00:09 AM,66.0001213660596,-179.98519
2021/11/10,11:00:10 AM,66.0001967593428,-179.98558
2021/11/10,11:00:11 AM,66.0001580625671,-179.98671
2021/11/10,11:00:12 AM,66.0001378472268,-179.98805
2021/11/10,11:00:13 AM,66.0001709596168,-179.98906
2021/11/10,11:00:14 AM,66.0000502908297,-179.98970
2021/11/10,11:00:15 AM,66.000066383696,-179.99130
2021/11/10,11:00:16 AM,66.0000462879889,-179.99183
2021/11/10,11:00:17 AM,66.0001312399228,-179.99340
2021/11/10,11:00:18 AM,66.0000883525882,-179.99384
2021/11/10,11:00:19 AM,66.0001647357197,-179.99504
2021/11/10,11:00:20 AM,66.0001959985441,-179.99596
2021/11/10,11:00:21 AM,66.0001581122524,-179.99737
2021/11/10,11:00:22 AM,66.0001028337378,-179.99798
2021/11/10,11:00:23 AM,66.000105956278,-179.99903
2021/11/10,11:00:24 AM,66.0000574941283,-179.99956
2021/11/10,11:00:25 AM,66.0000416369344,179.99915
2021/11/10,11:00:26 AM,66.0001504925119,179.99831
2021/11/10,11:00:27 AM,66.0001927654035,179.99675
2021/11/10,11:00:28 AM,66.0001282065146,179.99621
2021/11/10,11:00:29 AM,66.0000911889429,179.99493
2021/11/10,11:00:30 AM,66.0001909788552,179.99362
2021/11/10,11:00:31 AM,66.0000568057737,179.99293
2021/11/10,11:00:32 AM,66.0000978383165,179.99211
2021/11/10,11:00:33 AM,66.0000403599515,179.99138
2021/11/10,11:00:34 AM,66.0001144849062,179.99045
2021/11/10,11:00:35 AM,66.0000769781374,179.98854
2021/11/10,11:00:36 AM,66.0000133158984,179.98783
2021/11/10,11:00:37 AM,66.0001758676388,179.98699
2021/11/10,11:00:38 AM,66.0000207460354,179.98606
2021/11/10,11:00:39 AM,66.0000931231469,179.98538
2021/11/10,11:00:40 AM,66.0001991309548,179.98429
2021/11/10,11:00:41 AM,66.0001841979519,179.98311
2021/11/10,11:00:42 AM,66.0001217081583,179.98251
2021/11/10,11:00:43 AM,66.0001017396611,179.98182
2021/11/10,11:00:44 AM,66.0001469324212,179.98183
2021/11/10,11:00:45 AM,66.0000706021879,179.98175
2021/11/10,11:00:46 AM,66.0000732617344,179.98178
2021/11/10,11:00:47 AM,66.0001673249007,179.98162
Loading