Polygon.fromWKT constructor

  1. @override
Polygon.fromWKT(
  1. String wkt
)

Creates a Polygon from a WKT String. Right now, cannot handle polygons with holes.

Implementation

@override
factory Polygon.fromWKT(String wkt) {
  final polygonType = wkt.startsWith('POLYGON');
  if (!polygonType) {
    throw ArgumentError('wkt is not a Polygon');
  }
  wkt = wkt.substring(wkt.indexOf('(') + 1, wkt.lastIndexOf(')')).trim();

  final rings = [];
  for (int i = 0; i < wkt.length; i++) {
    if (wkt[i] == '(') {
      final end = wkt.indexOf(')', i);
      rings.add(wkt.substring(i + 1, end));
      i = end + 1;
    }
  }

  return Polygon(rings
      .map((ring) => LinearRing(
            ring
                .split(',')
                .map<Coordinate>((c) => Coordinate.fromWKT(c.trim()))
                .toList(),
          ))
      .toList());
}