Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomly generated polygons do not comply with the right-hand rule. #2605

Open
ariel-salgado opened this issue May 12, 2024 · 1 comment
Open

Comments

@ariel-salgado
Copy link

I am currently testing some functionalities in my app, of which one is to check if an object is a valid GeoJSON. I'm using @mapbox/geojsonhint for this.

import { hint } from '@mapbox/geojsonhint';
import { randomPolygon } from '@turf/random';

const feature = randomPolygon(1).features[0];

function isValidGeoJSON(geojson: any): { valid: boolean; errors: any[] } {
	const errors = hint(geojson);
	return {
		errors,
		valid: errors.length === 0
	};
}

isValidGeoJSON(feature);

To test this function I generate random polygons with @turf/random. The problem is that all polygons randomly generated by the randomPolygon function fail due to the right-hand rule.

The RFC 7946 states here:

A linear ring MUST follow the right-hand rule with respect to the area it bounds, i.e., exterior rings are counterclockwise, and holes are clockwise.

@mapbox/geojsonhint is not the only one complaining about this, tools like geojson.io and geojsonlint also throw the same error.

I'm using @turf/random v6.5.0.

Random feature generated by randomPolygon:

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -158.66509293153055,
          -73.08040644678542
        ],
        [
          -158.97038620807854,
          -74.68046996596513
        ],
        [
          -153.1212960672334,
          -72.72257839813791
        ],
        [
          -155.32806726918324,
          -76.44993243660309
        ],
        [
          -159.07024019343282,
          -75.21488784692389
        ],
        [
          -159.01662503629552,
          -78.17553374285981
        ],
        [
          -160.06913205531168,
          -75.78662828914291
        ],
        [
          -167.94347021160297,
          -79.55249722610885
        ],
        [
          -163.71367843734242,
          -73.78563223508077
        ],
        [
          -164.07509676069895,
          -67.48211236028651
        ],
        [
          -158.66509293153055,
          -73.08040644678542
        ]
      ]
    ]
  }
}

To reproduce generate a new feature using the randomPolygon function from @turf/random or copy the example above and paste it into geojson.io or geojsonlint.

@smallsaucepan
Copy link
Member

Yikes. That's not ideal. Thanks for reporting this @ariel-salgado. If you need a workaround take a look at @turf/rewind - it should re-wind your polygons into compliance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants