Skip to content

Commit

Permalink
Merge pull request #13 from geocrystal/ring_area
Browse files Browse the repository at this point in the history
use geocrystal/ring_area for area
  • Loading branch information
mamantoha authored Apr 11, 2024
2 parents c9f4ae9 + 36d788a commit caa2a85
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies:
github: geocrystal/geohash
geo_bearing:
github: geocrystal/geo_bearing
ring_area:
github: geocrystal/ring_area

development_dependencies:
ameba:
Expand Down
16 changes: 10 additions & 6 deletions spec/geo/polygon_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,19 @@ describe Geo::Polygon do

describe "#area" do
coords = [
Geo::Coord.new(-3, -2),
Geo::Coord.new(-1, 4),
Geo::Coord.new(6, 1),
Geo::Coord.new(3, 10),
Geo::Coord.new(-4, 9),
Geo::Coord.new(-15, 125),
Geo::Coord.new(-22, 113),
Geo::Coord.new(-37, 117),
Geo::Coord.new(-33, 130),
Geo::Coord.new(-39, 148),
Geo::Coord.new(-27, 154),
Geo::Coord.new(-15, 144),
Geo::Coord.new(-15, 125),
]

polygon = Geo::Polygon.new(coords)

it { polygon.area.should eq(60.0) }
it { polygon.area.should eq(7748891609977.457) }
end

describe "comparisons" do
Expand Down
1 change: 1 addition & 0 deletions src/geo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require "convex_hull"
require "geohash"
require "geo_bearing"
require "haversine"
require "ring_area"
require "./geo/utils"
require "./geo/coord"
require "./geo/polygon"
Expand Down
4 changes: 0 additions & 4 deletions src/geo/coord.cr
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,6 @@ module Geo
true
end

def shoelace(other : Geo::Coord)
lat * other.lng - lng * other.lat
end

private def guard_seconds(pattern : String, result : String) : Array(String)?
if m = pattern.match(/<(lat|lng)s>/)
return [result, ""] unless m && result.starts_with?("60")
Expand Down
6 changes: 4 additions & 2 deletions src/geo/polygon.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ module Geo
@coords
end

# Evaluate area of a polygon using shoelace formula
# Return the approximate signed geodesic area of the polygon in square meters.
def area
@coords.each_cons_pair.sum { |lat, lng| lat.shoelace(lng) }.abs.fdiv(2)
coordinates = @coords.map { |coord| [coord.lng, coord.lat] }

RingArea.ring_area(coordinates)
end

# Order coords in lexicographical order.
Expand Down

0 comments on commit caa2a85

Please sign in to comment.