Skip to content

Commit

Permalink
adds #area method for all GeoJSON objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Mar 29, 2020
1 parent 8e534b6 commit f567c60
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
11 changes: 11 additions & 0 deletions spec/geojson_area_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions src/geojson/object.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GeoJSON
class Object
def area : Float64
GeoJSON::Area.area(self)
end
end
end
1 change: 1 addition & 0 deletions src/geojson_area.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "geojson"
require "./geojson/area"
require "./geojson/object"

module GeoJSON::Area
VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }}
Expand Down

0 comments on commit f567c60

Please sign in to comment.