dartclub / turf_dart

A turf.js-like geospatial analysis library working with GeoJSON, written in pure Dart.
https://pub.dev/packages/turf
MIT License
63 stars 29 forks source link

Ports lineToPolygon, polygonToLine, and their tests #104

Closed armantorkzaban closed 2 years ago

armantorkzaban commented 2 years ago

Implements parts of #101

lukas-h commented 2 years ago

Should I already give a review?

armantorkzaban commented 2 years ago

Should I already give a review?

Would be nice if you could review the lineToPolygon and its test.

lukas-h commented 2 years ago

Just fyi, my incomplete implementation attempt for lineToPolygon. It does not contain "closing" of the polygon (first==last coord), etc.

import 'package:turf/helpers.dart';
import 'package:turf/meta.dart';

Feature lineToPolygon(GeoJSONObject geom) {
  if (geom is Feature) {
    return _toPolygonFeature(geom);
  } else if (geom is FeatureCollection) {
    return _toPolygonFeatureCollection(geom);
  } else {
    return _toPolygonSimple(geom);
  }
}

Feature _toPolygonFeatureCollection(FeatureCollection coll) {
  var multiPolygon = MultiPolygon(
    coordinates: [],
  );
  geomEach(
    coll,
    (
      GeometryType? currentGeometry,
      int? featureIndex,
      Map<String, dynamic>? featureProperties,
      BBox? featureBBox,
      dynamic featureId,
    ) {
      if (currentGeometry is LineString) {
        multiPolygon.coordinates.add([currentGeometry.coordinates]);
      } else if (currentGeometry is MultiLineString) {
        multiPolygon.coordinates.add(currentGeometry.coordinates);
      } else {
        throw Exception('unsupported type');
      }
    },
  );
  return Feature(geometry: multiPolygon);
}

Feature _toPolygonFeature(Feature feature) {
  return _toPolygonSimple(feature.geometry);
}

Feature _toPolygonSimple(
  GeoJSONObject? geom, [
  Map<String, dynamic>? properties,
  dynamic id,
]) {
  if (geom is LineString) {
    return Feature(
      id: id,
      properties: properties,
      geometry: Polygon(coordinates: [geom.coordinates]),
    );
  } else if (geom is MultiLineString) {
    return Feature(
      id: id,
      properties: properties,
      geometry: Polygon(coordinates: geom.coordinates),
    );
  } else {
    throw Exception('unsupported type');
  }
}
lukas-h commented 2 years ago

BTW, once review requested, please change the PR from draft to a normal PR. @armantorkzaban

lukas-h commented 2 years ago

Let's narrow this PR down to just lineToPolygon and polygonToLine. This way we can merge faster, without having to implement the rest of the conversion methods here

lukas-h commented 2 years ago

@arman What is the status on this one? Looks to me also like an easy to finish thing. Let's finish this asap, so we can reduce the number of tasks that we work on at the same time.