Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
geo / geo-polygon-areaOpenRuns on Liberas

Polygon area

The area enclosed by ring on a sphere of radius R = 6,371,008.8 m, in square metres, by the line-integral formula of Chamberlain & Duquette (JPL, 2007) as GeoJSON tools use it. If the ring has at least 2 vertices and its last vertex equals its first (lat = lat and lon = lon numerically), the last is dropped; n vertices remain, and fewer than 3 give 0. Otherwise S = Σ over i = 0 … n − 1, accumulated in that order from +0, of (rad(lon[(i + 1) mod n]) − rad(lon[(i − 1) mod n])) × sin(rad(lat[i])); the result is round(|S| × R × R ÷ 2). Either orientation gives the same area; edges run in plain longitude, so a ring may not cross the antimeridian (it would be measured the long way round); a self-intersecting ring gives the magnitude of its signed sum. A point is {lat, lon} in degrees; it is an error unless both are finite with −90 ≤ lat ≤ 90 and −180 ≤ lon ≤ 180 (NaN and ±Infinity are errors). A list has at most 100,000 points and each must be a valid point, else an error. rad(v) = v × π ÷ 180 and deg(v) = v × 180 ÷ π, left to right, with π the binary64 nearest π. Arithmetic is IEEE 754 binary64, each operation rounded to nearest-even and evaluated in exactly the order written (left to right; no fused multiply-add). sin, cos, tan, asin, atan, atan2, exp and ln are the platform's own: the result is an integer, round half away from zero, so any libm accurate to a few ulps gives the same answer except within a hair of a rounding boundary.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init geo-polygon-area --lang rust
cd geo-polygon-area && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)21 lines›
geo-polygon-area.wit
package root:component;

world root {
  import liberas:geo/types@0.1.0;

  export liberas:geo/polygon-area@0.1.0;
}
package liberas:geo@0.1.0 {
  interface types {
    record point {
      lat: f64,
      lon: f64,
    }
  }
  interface polygon-area {
    use types.{point};

    area: func(ring: list<point>) -> result<u64, string>;
  }
}
Test vectors18›
geo-polygon-area.cases.json
[
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 0,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 5
        },
        {
          "lat": 5,
          "lon": 5
        },
        {
          "lat": 5,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": -10,
          "lon": -170
        },
        {
          "lat": -10,
          "lon": 170
        },
        {
          "lat": 20,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 80,
          "lon": -180
        },
        {
          "lat": 80,
          "lon": -90
        },
        {
          "lat": 80,
          "lon": 0
        },
        {
          "lat": 80,
          "lon": 90
        },
        {
          "lat": 80,
          "lon": 180
        },
        {
          "lat": 90,
          "lon": 180
        },
        {
          "lat": 90,
          "lon": -180
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 0.0001
        },
        {
          "lat": 0.0001,
          "lon": 0.0001
        },
        {
          "lat": 0.0001,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 1,
          "lon": 1
        },
        {
          "lat": 2,
          "lon": 2
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 1,
          "lon": 1
        },
        {
          "lat": 2,
          "lon": 2
        }
      ]
    }
  },
  {
    "args": {
      "ring": []
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": "Infinity"
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": "NaN",
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": "Infinity"
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 90.0000001,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": -91,
          "lon": 0
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 180.5
        }
      ]
    }
  },
  {
    "args": {
      "ring": [
        {
          "lat": 0,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 10
        },
        {
          "lat": 10,
          "lon": 0
        },
        {
          "lat": 0,
          "lon": "-Infinity"
        }
      ]
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 18 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 60,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.