Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript

Home Page:https://turfjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ariel-salgado opened this issue · comments

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.

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.