question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

coordEach indexing bug

See original GitHub issue

I noticed this evening that when using coordEach that it doesn’t really deal with holes properly.

So for example I pass in a poly with a hole like

[ [ [ 100, 0 ], [ 101, 0 ], [ 101, 1 ], [ 100, 1 ], [ 100, 0 ] ],
  [ [ 100.2, 0.2 ], [ 100.8, 0.2 ], [ 100.8, 0.8 ],[ 100.2, 0.8 ],[ 100.2, 0.2 ] ]
]

if I do something like

    coordEach(poly, function (currentCoord, currentIndex, featureIndex, featureSubIndex) {
        console.log(currentCoord)
        console.log(currentIndex, featureIndex, featureSubIndex)

I get

[ 100, 0 ]
0 0 0
[ 101, 0 ]
1 0 0
[ 101, 1 ]
2 0 0
[ 100, 1 ]
3 0 0
[ 100.2, 0.2 ]
4 0 0

So the hole get’s the same featureIndex and featureSubIndex as the rest of the poly which means I can’t detect that the coord belongs to a hole.

Would be great to add some polys with holes to the test fixtures to those meta modules.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
DenisCarrierecommented, Nov 17, 2017

👍 Agreed, I’ve added a 3rd index (geometryIndex) which indicates the index when iterating over the geometry (used in Polygon & MultiPolygon geometries).

There are 3 major index tiers which are broken down like so:

  • featureIndex => multiFeatureIndex => geometryIndex

Using those 3 indexes you should be able to find your way back to the exact geometry part of the feature.

turf.coordEach(features, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  //=currentCoord
  //=coordIndex
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
});

@rowanwins Have a go at it and let me know if you are having any issues with the new update. (Close issue whenever you confirm it’s 👍 )

0reactions
DenisCarrierecommented, Dec 10, 2017

I know… Handling Polygons with holes is brutal! Don’t hesitate to test the other @turf/meta modules, we can do another big revamp at the next major release to make it easier to work with by returning Objects in the callbacks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

meta package - github.com/tomchavakis/turf-go/meta/coordEach ...
CoordEach iterate over coordinates in any Geojson object and apply the callbackFn geojson can be a FeatureCollection | Feature | Geometry callbackFn is...
Read more >
Top 5 @turf/helpers Code Examples - Snyk
Learn more about how to use @turf/helpers, based on @turf/helpers code examples created from the most popular ways it is used in public...
Read more >
@turf/meta | Yarn - Package Manager
coordIndex number The current index of the coordinate being processed. Starts at index 0, if an initialValue is provided, and at index 1...
Read more >
Advanced geospatial analysis - Turf.js
Error - if it encounters an unknown geometry type ... The properties object will contain three values: index : closest point was found...
Read more >
JavaScript @turf/meta coordEach Examples
coordEach extracted from open source projects. ... File: index.js Project: OlympicsORG/turf ... 'object') throw new Error('options must be an object'); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found