1

I'm building a flutter mobile app and I need to compute an area of some GeoJson geometries.

Say we have a GeoJson-like object:

final geo = {
    "type": "Polygon",
    "coordinates": [[
        [-122.085, 37.423],
        [-122.083, 37.423],
        [-122.083, 37.421],
        [-122.085, 37.421],
        [-122.085, 37.423]
    ]]
};

Assuming the projection is EPSG:4326, how do we get the actual area of the geometry using flutter or dart?

Tried to use dart-simple-features, but this is no longer maintained and requires SDK < 2.0.0.

Another option that came to my mind is to use some JavaScript library in combination with flutter_webview_plugin, but oh my... That seems like an overkill!

Also there is a possibility to use platform-specific code, but for the sake of development experience: let's avoid testing on multiple platforms if possible...

Any ideas? Or recommendations?

Ikar Pohorský
  • 4,617
  • 6
  • 39
  • 56

2 Answers2

4

Okay, no reply for almost a week... Created my first dart package:

https://pub.dev/packages/area

Simple to use:

import 'package:area/area.dart';

main() {
  const world = {
    'type': 'Polygon',
    'coordinates': [
      [
        [-180, -90],
        [-180, 90],
        [180, 90],
        [180, -90],
        [-180, -90]
      ]
    ]
  };

  print("The world area is: ${area(world)} m²");
}

Feel free to use, hate or love ;)

Ikar Pohorský
  • 4,617
  • 6
  • 39
  • 56
  • 1
    I was looking for one of these in go, but I was not able to find so I did the same as you. Thanks – g__n Feb 19 '20 at 19:06
2

You can use geojson_vi:

const world = {
  'type': 'Polygon',
  'coordinates': [
    [
      [-180, -90],
      [-180, 90],
      [180, 90],
      [180, -90],
      [-180, -90]
    ]
  ]
};
final geoJSONPolygon = GeoJSONPolygon.fromMap(world);
print(geoJSONPolygon.area);
chuyentt
  • 33
  • 1
  • 4