mapbox / mapbox/geojson-rewind

isRightHandRule function

Open
#4 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
133
Forks
35
PR merge metrics
No merged PRs in 30d

Description

It looks like the current implementation only exports a rewind function that returns (and sometimes mutates) a geojson object.

I need a function that will check for the "Right Hand Rule" - ensure polygon geoms have counter-clockwise external rings, clockwise internal rings. I don't have any need for the altered geometry, just the boolean.

The best I've got so far:

var rewind = require("geojson-rewind");
function isRightHandRule(geom) {
    // Hack to deepcopy geom and avoid mutation
    geomstring = JSON.stringify(geom);
    geomcopy = JSON.parse(geomstring);

    // Does the original geometry match the clockwise-wound geometry
    return geomstring === JSON.stringify(rewind(geomcopy, true));
}

But this has some major flaws: it needs to deepcopy the geom via JSON strings, it relies on string comparison and it needs to rewind the entire geometry rather than failing fast on the first linear ring that doesn't match.

Any ideas on a better way to implement this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the exported rewind function and trace how it visits polygon geometries and their linear rings. Define the boolean check around the existing ring-winding behavior, then verify that it reports whether external rings are counter-clockwise and internal rings clockwise without altering the input or processing beyond the first mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.