From f567c602ffb63578a568b4abda195e356e6c3460 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Sun, 29 Mar 2020 19:10:28 +0300 Subject: [PATCH] adds #area method for all GeoJSON objects --- README.md | 15 ++++++++++++++- spec/geojson_area_spec.cr | 11 +++++++++++ src/geojson/object.cr | 7 +++++++ src/geojson_area.cr | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/geojson/object.cr diff --git a/README.md b/README.md index dc4af3d..efcc4ec 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,20 @@ Calculate the area inside of any [GeoJSON](https://github.com/geocrystal/geojson require "geojson_area" ``` -`GeoJSON::Area.area()` accept any `GeoJSON` object, and returns contained area as square meters. +This adds `area` method for all `GeoJSON` objects: + +```crystal +polygon = GeoJSON::Polygon.new([ + [[-10.0, -10.0], [10.0, -10.0], [10.0, 10.0], [-10.0,-10.0]], + [[-1.0, -2.0], [3.0, -2.0], [3.0, 2.0], [-1.0,-2.0]] +]) + +polygon.area +# => 2366726096087.807 +``` + +Also you can use `GeoJSON::Area.area()` directly. +This method accept any `GeoJSON` object, and returns contained area as square meters. ```crystal polygon = GeoJSON::Polygon.new([ diff --git a/spec/geojson_area_spec.cr b/spec/geojson_area_spec.cr index 6ef0faf..3782026 100644 --- a/spec/geojson_area_spec.cr +++ b/spec/geojson_area_spec.cr @@ -58,4 +58,15 @@ describe GeoJSON::Area do area = GeoJSON::Area.area(polygon) area.should eq(2366726096087.807) end + + describe GeoJSON::Object do + it "#area" do + polygon = GeoJSON::Polygon.new([ + [[-10.0, -10.0], [10.0, -10.0], [10.0, 10.0], [-10.0, -10.0]], + [[-1.0, -2.0], [3.0, -2.0], [3.0, 2.0], [-1.0, -2.0]], + ]) + + polygon.area.should eq(2366726096087.807) + end + end end diff --git a/src/geojson/object.cr b/src/geojson/object.cr new file mode 100644 index 0000000..87ada7a --- /dev/null +++ b/src/geojson/object.cr @@ -0,0 +1,7 @@ +module GeoJSON + class Object + def area : Float64 + GeoJSON::Area.area(self) + end + end +end diff --git a/src/geojson_area.cr b/src/geojson_area.cr index 9098102..f28fc3c 100644 --- a/src/geojson_area.cr +++ b/src/geojson_area.cr @@ -1,5 +1,6 @@ require "geojson" require "./geojson/area" +require "./geojson/object" module GeoJSON::Area VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}