Skip to content

Commit

Permalink
add polygon support and some new attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-jiao committed Mar 9, 2024
1 parent df440b1 commit 3bae1ff
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [2.5.3]

* Add `Style` support for kml.
* Add `Polygon` support for kml. Polygon may previously be parsed as rte, the relevant code may need to be updated.
* Update dependencies.

## [2.5.2]
Expand Down
1 change: 1 addition & 0 deletions lib/geoxml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export 'src/model/geoxml.dart';
export 'src/model/link.dart';
export 'src/model/metadata.dart';
export 'src/model/person.dart';
export 'src/model/polygon.dart';
export 'src/model/rte.dart';
export 'src/model/trk.dart';
export 'src/model/trkseg.dart';
Expand Down
113 changes: 102 additions & 11 deletions lib/src/kml_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import 'model/kml_tag.dart';
import 'model/link.dart';
import 'model/metadata.dart';
import 'model/person.dart';
import 'model/polygon.dart';
import 'model/rte.dart';
import 'model/trk.dart';
import 'model/trkseg.dart';
import 'model/wpt.dart';
import 'tools/bool_converter.dart';
import 'tools/stream_converter.dart';

/// Read Gpx from string
Expand Down Expand Up @@ -71,6 +73,8 @@ class KmlReader {
geoXml.wpts.add(item);
} else if (item is Rte) {
geoXml.rtes.add(item);
} else if (item is Polygon) {
geoXml.polygons.add(item);
}
break;
case KmlTag.folder:
Expand Down Expand Up @@ -142,9 +146,11 @@ class KmlReader {
final elm = iterator.current;
GeoStyle? style;
DateTime? time;
bool? tessellate;
Wpt? ext;
Wpt? wpt;
Rte? rte;
Polygon? polygon;

if ((elm is XmlStartElementEvent) && !elm.isSelfClosing) {
while (await iterator.moveNext()) {
Expand Down Expand Up @@ -188,6 +194,32 @@ class KmlReader {
case KmlTag.gxTrack:
rte = await _readGxTrack(iterator, val.name);
break;
case KmlTag.polygon:
polygon = Polygon();
break;
case KmlTag.extrude:
item.extrude =
(await _readInt(iterator, val.name))?.toBool();
break;
case KmlTag.tessellate:
tessellate =
(await _readInt(iterator, val.name))?.toBool();
break;
case KmlTag.altitudeMode:
item.altitudeMode =
await _readEnum(iterator, val.name, AltitudeMode.values);
break;
case KmlTag.outerBoundaryIs:
polygon = polygon ?? Polygon();
polygon.outerBoundaryIs = Rte();
polygon.outerBoundaryIs.rtepts =
await _readCoordinate(iterator, val.name);
break;
case KmlTag.innerBoundaryIs:
polygon = polygon ?? Polygon();
polygon.innerBoundaryIs.addAll((await _readCoordinates(iterator,
val.name)).map((e) => Rte(rtepts: e)));
break;
case KmlTag.style:
style = await _readStyle(iterator, val.name);
break;
Expand Down Expand Up @@ -215,6 +247,8 @@ class KmlReader {
wpt.name = item.name;
wpt.desc = item.desc;
wpt.links = item.links;
wpt.extrude = item.extrude;
wpt.altitudeMode = item.altitudeMode;
if (time != null) {
wpt.time = time;
}
Expand All @@ -234,15 +268,47 @@ class KmlReader {
wpt.number = ext.number;
}

if (style != null) {
wpt.style = style;
}
wpt.style = style;

return wpt;
} else if (polygon is Polygon) {
polygon.name = item.name;
polygon.desc = item.desc;
polygon.links = item.links;
polygon.extrude = item.extrude;
polygon.tessellate = tessellate;
polygon.altitudeMode = item.altitudeMode;
if (time != null) {
for (final wpt in polygon.outerBoundaryIs.rtepts) {
wpt.time = time;
}
}

if (time != null) {
for (final rte in polygon.innerBoundaryIs) {
for (final wpt in rte.rtepts) {
wpt.time = time;
}
}
}

if (ext != null) {
polygon.src = ext.src;
polygon.cmt = ext.cmt;
polygon.type = ext.type;
polygon.number = ext.number;
}

polygon.style = style;

return polygon;
} else if (rte is Rte) {
rte.name = item.name;
rte.desc = item.desc;
rte.links = item.links;
rte.extrude = item.extrude;
rte.tessellate = tessellate;
rte.altitudeMode = item.altitudeMode;
if (time != null) {
for (final wpt in rte.rtepts) {
wpt.time = time;
Expand All @@ -256,16 +322,12 @@ class KmlReader {
rte.number = ext.number;
}

if (style != null) {
rte.style = style;
}
rte.style = style;

return rte;
}

if (style != null) {
item.style = style;
}
item.style = style;

return item;
}
Expand Down Expand Up @@ -542,6 +604,34 @@ class KmlReader {
return Rte(rtepts: wpts);
}

Future<List<List<Wpt>>> _readCoordinates(
StreamIterator<XmlEvent> iterator, String tagName) async {
final coords = <List<Wpt>>[];
final elm = iterator.current;

if ((elm is XmlStartElementEvent) && !elm.isSelfClosing) {
while (await iterator.moveNext()) {
final val = iterator.current;

if (val is XmlStartElementEvent) {
switch (val.name) {
case KmlTag.altitudeMode:
break;
case KmlTag.coordinates:
coords.add(await _readCoordinate(iterator, val.name));
break;
}
}

if (val is XmlEndElementEvent && val.name == tagName) {
break;
}
}
}

return coords;
}

Future<List<Wpt>> _readCoordinate(
StreamIterator<XmlEvent> iterator, String tagName) async {
final wpts = <Wpt>[];
Expand Down Expand Up @@ -771,10 +861,11 @@ class KmlReader {
await _readEnum(iterator, val.name, ColorMode.values);
break;
case KmlTag.fill:
polyStyle.fill = await _readInt(iterator, val.name);
polyStyle.fill = (await _readInt(iterator, val.name))?.toBool();
break;
case KmlTag.outline:
polyStyle.outline = await _readInt(iterator, val.name);
polyStyle.outline =
(await _readInt(iterator, val.name))?.toBool();
break;
}
}
Expand Down
15 changes: 5 additions & 10 deletions lib/src/kml_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import 'model/polygon.dart';
import 'model/rte.dart';
import 'model/trk.dart';
import 'model/wpt.dart';

/// KML 2.2 AltitudeMode values
enum AltitudeMode {
absolute,
clampToGround,
relativeToGround,
}
import 'tools/bool_converter.dart';

/// Convert Gpx into KML
class KmlWriter {
Expand Down Expand Up @@ -234,7 +228,7 @@ class KmlWriter {
_writeElement(
builder,
KmlTag.coordinates,
polygon.points
polygon.outerBoundaryIs.rtepts
.map((wpt) => [wpt.lon, wpt.lat].join(','))
.join('\n'));
});
Expand All @@ -256,8 +250,9 @@ class KmlWriter {
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);
_writeElement(builder, KmlTag.fill, style.polyStyle?.fill?.toInt());
_writeElement(builder,
KmlTag.outline, style.polyStyle?.outline?.toInt());
});
}

Expand Down
13 changes: 13 additions & 0 deletions lib/src/model/geo_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ class GeoObject {

/// Kml style
GeoStyle? style;

bool? extrude;

AltitudeMode? altitudeMode;
}


enum AltitudeMode {
clampToGround,
relativeToGround,
absolute,
relativeToSeaFloor,
clampToSeaFloor,
}
4 changes: 2 additions & 2 deletions lib/src/model/geo_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ class LineStyle extends ColorStyle {
/// like solid fences).
class PolyStyle extends ColorStyle {
/// Boolean value. Specifies whether to fill the polygon.
int? fill;
bool? fill;

/// Boolean value. Specifies whether to outline the polygon.
/// Polygon outlines use the current LineStyle.
int? outline;
bool? outline;

@override
// ignore: type_annotate_public_apis
Expand Down
1 change: 1 addition & 0 deletions lib/src/model/geoxml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../gpx_reader.dart';
import '../gpx_writer.dart';
import '../kml_reader.dart';
import '../kml_writer.dart';
import 'geo_object.dart';
import 'geo_style.dart';
import 'metadata.dart';
import 'polygon.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/src/model/kml_tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class KmlTag {

static const extendedData = 'ExtendedData';
static const data = 'Data';

static const author = 'atom:author';
static const authorName = 'atom:name';
static const email = 'atom:email';
Expand Down
Loading

0 comments on commit 3bae1ff

Please sign in to comment.