Git Product home page Git Product logo

geojson-rewind's Introduction

Build Status

geojson-rewind

The GeoJSON specification is picky about winding order.

This helps you generate compliant Polygon and MultiPolygon geometries. Furthermore it lets you use Canvas and other drawing libraries's default behavior to color the interior rings of Polygon and MultiPolygon features.

Usage

As NPM module:

npm install --save @mapbox/geojson-rewind

As a console utility:

# install
npm install -g @mapbox/geojson-rewind
# use
geojson-rewind foo.geojson

As a browser library: geojson-rewind.js

API

rewind(geojson, clockwise)

Given a GeoJSON FeatureCollection, Feature, or Geometry, return a version with inner and outer rings of different winding orders.

If clockwise is true, the outer ring is clockwise, otherwise it is counterclockwise.

geojson-rewind's People

Contributors

andrewharvey avatar bhousel avatar chris48s avatar cowchris avatar fabid avatar gmaclennan avatar ingalls avatar mourner avatar nied avatar tmcw avatar trysound avatar tyrasd avatar watson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

geojson-rewind's Issues

Doesn't handle near-zero-area polygons properly

Hi there! I've got a set of polygons that I cannot rewind with geojson-rewind.

GeoJSON: https://gist.github.com/stepankuzmin/f12965a84eee3eed6df89e65d48696ae

Steps to reproduce:

const fs = require('fs');
const geojsonhint = require('@mapbox/geojsonhint');
const rewind = require('geojson-rewind');

const geojson = JSON.parse(fs.readFileSync('./invalid.geojson'));
const errors = geojsonhint.hint(geojson);
console.log(errors[0].message); // Polygons and MultiPolygons should follow the right-hand rule

const rewindedGeojson = rewind(geojson);
const newErrors = geojsonhint.hint(rewindedGeojson);
console.log(newErrors[0].message); // Polygons and MultiPolygons should follow the right-hand rule

geojson-rewind 0.3.1 and github:mapbox/geojson-rewind#v0.4.0
@mapbox/geojsonhint 2.1.0

Typing

For anyone, using this library within TypeScript (and together with d3), you can use

declare module "@mapbox/geojson-rewind" {
    import {ExtendedFeature, ExtendedFeatureCollection, ExtendedGeometryCollection, GeoGeometryObjects} from "d3-geo"

    export default function rewind(
        geojson: ExtendedFeature | ExtendedFeatureCollection | GeoGeometryObjects | ExtendedGeometryCollection,
        clockwise: boolean,
    ): ExtendedFeature | ExtendedFeatureCollection | GeoGeometryObjects | ExtendedGeometryCollection;
}

If people are interested, I guess I could submit a PR with this code in an index.d.ts.

Possible error in rewindRing error calculation

In the rewindRing function, the following line exists:

err += Math.abs(area) >= Math.abs(k) ? area - m + k : k - m + area;

I'm not familiar enough with the domain-specific calculations, but from a purely mathematical standpoint, the following computations are equivalent, regardless of the relative absolute values of area and k:

area - m + k
k - m + area

I don't know if one of those expressions is incorrect, and thus should be fixed, or if the comparison of absolute values can simply be eliminated and either computation can always be used.

Crashes on Geometries with empty coordinates

rewind currently doesn't handle a feature that looks like

    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": []
      }
    }

I'm not super familiar with GeoJSON (maybe empty coordinates is invalid?), but it worked for me on http://geojson.io

in case this isn't intentional, this seems like an easy enough fix

isRightHandRule function

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.