Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-jiao committed Mar 9, 2024
1 parent e8a0d4b commit b9b31a4
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 58 deletions.
10 changes: 6 additions & 4 deletions lib/src/kml_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,10 @@ class KmlReader {
await _readEnum(iterator, val.name, ColorMode.values);
break;
case KmlTag.fill:
polyStyle.fill = await _readInt(iterator, val.name, 16);
polyStyle.fill = await _readInt(iterator, val.name);
break;
case KmlTag.outline:
polyStyle.outline = await _readInt(iterator, val.name, 16);
polyStyle.outline = await _readInt(iterator, val.name);
break;
}
}
Expand Down Expand Up @@ -887,10 +887,12 @@ class KmlReader {
if (val is XmlStartElementEvent) {
switch (val.name) {
case KmlTag.bgColor:
balloonStyle.bgColor = await _readInt(iterator, val.name, 16);
balloonStyle.bgColor = await _readInt(iterator, val.name, 16)
?? balloonStyle.bgColor;
break;
case KmlTag.textColor:
balloonStyle.textColor = await _readInt(iterator, val.name, 16);
balloonStyle.textColor = await _readInt(iterator, val.name, 16)
?? balloonStyle.textColor;
break;
case KmlTag.text:
balloonStyle.text = await _readString(iterator, val.name) ?? '';
Expand Down
198 changes: 146 additions & 52 deletions lib/src/kml_writer.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:xml/xml.dart';

import 'model/geo_object.dart';
import 'model/geo_style.dart';
import 'model/geoxml.dart';
import 'model/gpx_tag.dart';
import 'model/kml_tag.dart';
import 'model/link.dart';
import 'model/metadata.dart';
import 'model/polygon.dart';
import 'model/rte.dart';
import 'model/trk.dart';
import 'model/wpt.dart';
Expand Down Expand Up @@ -53,23 +55,27 @@ class KmlWriter {
_writeMetadata(builder, geoXml.metadata!);
}

for (final style in geoXml.styles) {
_writeStyle(builder, style);
}

for (final wpt in geoXml.wpts) {
_writePoint(builder, KmlTag.placemark, wpt);
_writePoint(builder, wpt, geoXml);
}

// for (final polygon in gpx.polygons) {
// _writePolygon(builder, polygon);
// }
for (final polygon in geoXml.polygons) {
_writePolygon(builder, polygon, geoXml);
}

for (final rte in geoXml.rtes) {
_writeTrackRoute(builder, rte);
_writeTrackRoute(builder, rte, geoXml);
}

for (final trk in geoXml.trks) {
if (trk.trksegs
.expand((trkseg) => trkseg.trkpts)
.any((element) => element.time == null)) {
_writeTrackRoute(builder, trk);
_writeTrackRoute(builder, trk, geoXml);
} else {
_writeTrack(builder, trk);
}
Expand Down Expand Up @@ -113,12 +119,21 @@ class KmlWriter {
});
}

void _writeTrackRoute(XmlBuilder builder, GeoObject item) {
void _writeTrackRoute(XmlBuilder builder, GeoObject item, GeoXml geoXml) {
builder.element(KmlTag.placemark, nest: () {
_writeElement(builder, GpxTag.name, item.name);
_writeElement(builder, GpxTag.desc, item.desc);
_writeAtomLinks(builder, item.links);

if (item.style != null) {
if (item.style!.id != null && item.style!.id!.isNotEmpty
&& geoXml.styles.contains(item.style)) {
_writeElement(builder, KmlTag.styleUrl, '#${item.style!.id}');
} else {
_writeStyle(builder, item.style!);
}
}

builder.element(KmlTag.extendedData, nest: () {
_writeExtendedElement(builder, GpxTag.comment, item.cmt);
_writeExtendedElement(builder, GpxTag.type, item.type);
Expand Down Expand Up @@ -189,57 +204,136 @@ class KmlWriter {
});
}

// void _writePolygon(XmlBuilder builder, Polygon polygon) {
// builder.element(KmlTag.placemark, nest: () {
// _writeElement(builder, KmlTag.name, polygon.name);
// _writeElement(builder, KmlTag.desc, polygon.desc);
// _writeAtomLinks(builder, polygon.links);
//
// // Style the polygon.
// builder.element(KmlTag.style, nest: () {
// builder.element(KmlTag.lineStyle, nest: () {
// _writeElement(builder, KmlTag.color,
// polygon.outlineColor.toRadixString(16));
// _writeElement(builder, KmlTag.width, polygon.outlineWidth);
// });
// builder.element(KmlTag.polyStyle, nest: () {
// _writeElement(
// builder, KmlTag.color, polygon.fillColor.toRadixString(16));
// _writeElement(builder, KmlTag.outline, 0);
// });
// });
//
// builder.element(KmlTag.extendedData, nest: () {
// _writeExtendedElement(builder, GpxTag.comment, polygon.cmt);
// _writeExtendedElement(builder, GpxTag.type, polygon.type);
//
// _writeExtendedElement(builder, GpxTag.src, polygon.src);
// _writeExtendedElement(builder, GpxTag.number, polygon.number);
// });
//
// builder.element(KmlTag.polygon, nest: () {
// builder.element(KmlTag.outerBoundaryIs, nest: () {
// builder.element(KmlTag.linearRing, nest: () {
// _writeElement(
// builder,
// KmlTag.coordinates,
// polygon.points
// .map((wpt) => [wpt.lon, wpt.lat].join(','))
// .join('\n'));
// });
// });
// });
// });
// }

void _writePoint(XmlBuilder builder, String tagName, Wpt wpt) {
builder.element(tagName, nest: () {
void _writePolygon(XmlBuilder builder, Polygon polygon, GeoXml geoXml) {
builder.element(KmlTag.placemark, nest: () {
_writeElement(builder, KmlTag.name, polygon.name);
_writeElement(builder, KmlTag.desc, polygon.desc);
_writeAtomLinks(builder, polygon.links);

// Style the polygon.
if (polygon.style != null) {
if (polygon.style!.id != null && polygon.style!.id!.isNotEmpty
&& geoXml.styles.contains(polygon.style)) {
_writeElement(builder, KmlTag.styleUrl, '#${polygon.style!.id}');
} else {
_writeStyle(builder, polygon.style!);
}
}

builder.element(KmlTag.extendedData, nest: () {
_writeExtendedElement(builder, GpxTag.comment, polygon.cmt);
_writeExtendedElement(builder, GpxTag.type, polygon.type);

_writeExtendedElement(builder, GpxTag.src, polygon.src);
_writeExtendedElement(builder, GpxTag.number, polygon.number);
});

builder.element(KmlTag.polygon, nest: () {
builder.element(KmlTag.outerBoundaryIs, nest: () {
builder.element(KmlTag.linearRing, nest: () {
_writeElement(
builder,
KmlTag.coordinates,
polygon.points
.map((wpt) => [wpt.lon, wpt.lat].join(','))
.join('\n'));
});
});
});
});
}

void _writeStyle(XmlBuilder builder, GeoStyle style) {
builder.element(KmlTag.style,
attributes: style.id != null ? {KmlTag.id: style.id!} : {}, nest: () {
if (style.lineStyle != null) {
builder.element(KmlTag.lineStyle, nest: () {
_writeColorStyleElements(builder, style.lineStyle);
_writeElement(builder, KmlTag.width, style.lineStyle?.width);
});
}

if (style.polyStyle != null) {
builder.element(KmlTag.polyStyle, nest: () {
_writeColorStyleElements(builder, style.polyStyle);
_writeElement(builder, KmlTag.fill, style.polyStyle?.fill);
_writeElement(builder, KmlTag.outline, style.polyStyle?.outline);
});
}

if (style.iconStyle != null) {
builder.element(KmlTag.iconStyle, nest: () {
_writeColorStyleElements(builder, style.iconStyle);
if (style.iconStyle?.iconUrl != null) {
builder.element(KmlTag.icon, nest: () {
_writeElement(builder, KmlTag.href, style.iconStyle?.iconUrl);
});
}
_writeElement(builder, KmlTag.scale, style.iconStyle?.scale);
_writeElement(builder, KmlTag.heading, style.iconStyle?.heading);
if (style.iconStyle?.x != null &&
style.iconStyle?.y != null &&
style.iconStyle?.xunit != null &&
style.iconStyle?.yunit != null){
builder.element(KmlTag.hotSpot, attributes: {
KmlTag.hotSpotX: style.iconStyle!.x!.toString(),
KmlTag.hotSpotY: style.iconStyle!.y!.toString(),
KmlTag.xunits: style.iconStyle!.xunit!.name,
KmlTag.yunits: style.iconStyle!.yunit!.name,
});
}
});
}

if (style.labelStyle != null) {
builder.element(KmlTag.labelStyle, nest: () {
_writeColorStyleElements(builder, style.labelStyle);
_writeElement(builder, KmlTag.scale, style.labelStyle?.scale);
});
}

if (style.balloonStyle != null) {
builder.element(KmlTag.balloonStyle, nest: () {
_writeElement(builder, KmlTag.bgColor,
style.balloonStyle!.bgColor.toRadixString(16));
_writeElement(builder, KmlTag.textColor,
style.balloonStyle!.textColor.toRadixString(16));
_writeElement(builder, KmlTag.text,
style.balloonStyle!.text);
_writeElement(builder, KmlTag.displayMode,
style.balloonStyle!.show ? 'default' : 'hide');
});
}
});
}

void _writeColorStyleElements(XmlBuilder builder, ColorStyle? colorStyle) {
if (colorStyle == null) {
return;
}
_writeElement(builder,
KmlTag.color, colorStyle.color?.toRadixString(16));
_writeElement(builder,
KmlTag.colorMode, colorStyle.colorMode?.name);
}

void _writePoint(XmlBuilder builder, Wpt wpt, GeoXml geoXml) {
builder.element(KmlTag.placemark, nest: () {
_writeElement(builder, KmlTag.name, wpt.name);
_writeElement(builder, KmlTag.desc, wpt.desc);

_writeElementWithTime(builder, wpt.time);

_writeAtomLinks(builder, wpt.links);

if (wpt.style != null) {
if (wpt.style!.id != null && wpt.style!.id!.isNotEmpty
&& geoXml.styles.contains(wpt.style)) {
_writeElement(builder, KmlTag.styleUrl, '#${wpt.style!.id}');
} else {
_writeStyle(builder, wpt.style!);
}
}

builder.element(KmlTag.extendedData, nest: () {
_writeExtendedElement(builder, GpxTag.magVar, wpt.magvar);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/geo_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ class BalloonStyle {
/// overlay, you would specify the following: <bgColor>7fff0000</bgColor>,
/// where alpha=0x7f, blue=0xff, green=0x00, and red=0x00. The default is
/// opaque white (ffffffff).
int? bgColor;
int bgColor = 0xffffffff;

/// Foreground color for text. The default is black (ff000000).
int? textColor = 0xff000000;
int textColor = 0xff000000;

/// Text displayed in the balloon. If no text is specified, Google Earth
/// draws the default balloon (with the Feature <name> in boldface, the
Expand Down
6 changes: 6 additions & 0 deletions lib/src/model/geoxml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../kml_reader.dart';
import '../kml_writer.dart';
import 'geo_style.dart';
import 'metadata.dart';
import 'polygon.dart';
import 'rte.dart';
import 'trk.dart';
import 'wpt.dart';
Expand Down Expand Up @@ -35,6 +36,8 @@ class GeoXml {
/// A list of tracks.
List<Trk> trks = [];

List<Polygon> polygons = [];

List<GeoStyle> styles = [];

/// You can add extend GPX by adding your own elements from another schema
Expand All @@ -51,6 +54,7 @@ class GeoXml {
const ListEquality().equals(other.wpts, wpts) &&
const ListEquality().equals(other.rtes, rtes) &&
const ListEquality().equals(other.trks, trks) &&
const ListEquality().equals(other.polygons, polygons) &&
const ListEquality().equals(other.styles, styles) &&
const MapEquality().equals(other.extensions, extensions);
}
Expand All @@ -66,6 +70,7 @@ class GeoXml {
wpts,
rtes,
trks,
polygons,
styles,
extensions
].join(",")}]";
Expand All @@ -80,6 +85,7 @@ class GeoXml {
...trks,
...rtes,
...wpts,
...polygons,
...styles
]);

Expand Down
Loading

0 comments on commit b9b31a4

Please sign in to comment.