Module: RGeo::Geographic::SphericalPolygonMethods

Included in:
SphericalPolygonImpl
Defined in:
lib/rgeo/geographic/spherical_feature_methods.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#centroidObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rgeo/geographic/spherical_feature_methods.rb', line 217

def centroid
  return super unless num_interior_rings == 0

  centroid_lat = 0.0
  centroid_lng = 0.0
  signed_area = 0.0

  exterior_ring.points.each_cons(2) do |p0, p1|
    area = (p0.x * p1.y) - (p1.x * p0.y)
    signed_area += area
    centroid_lat += (p0.x + p1.x) * area
    centroid_lng += (p0.y + p1.y) * area
  end

  signed_area *= 0.5
  centroid_lat /= (6.0 * signed_area)
  centroid_lng /= (6.0 * signed_area)

  factory.point(centroid_lat, centroid_lng)
end