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

Allow creation of Polygon from a Nested Array of Points #23

Closed
frastlin opened this issue Jun 29, 2019 · 5 comments
Closed

Allow creation of Polygon from a Nested Array of Points #23

frastlin opened this issue Jun 29, 2019 · 5 comments

Comments

@frastlin
Copy link

I have a number of arrays, such as [[1,1], [1,2], [2,2], [2,1]], and [[[1,1],[1,3],[2,3],[2,1]], [[2,2], [2,3], [4,3], [4,2]]].
That describe a single polygon and multipolygon respectively. The first function I need to add for any project I have is the code to convert from the nested points, to the Flatten polygons.
If there could be a function that would do this for me, it would make the library much easier to work with when implementing into a new project.

@alexbol99
Copy link
Owner

But this convert is pretty straightforward:

  let array = [[[1,1],[1,3],[2,3],[2,1]], [[2,2], [2,3], [4,3], [4,2]]];
  const polygon = new Polygon();
  for (let points of array) {
    let face = polygon.addFace(points.map(([x, y]) => new Point(x, y)));    
  }

Do you think it is not enough?

@frastlin
Copy link
Author

That doesn't work for both. The point is though, that this is probably an extremely common process, so there should be a method to handle it so the load to switch to Flatten is even easier. Here is my code:

shapeafy(poly, shape=null){
	//only pass in a multi-level array for poly, don't pass in shape. it returns the Flatten.js polygon object
	if(!shape){
		shape = new Polygon()
	}
	if(poly[0][0].length === undefined){
		const face = poly.map(p=> new Point(p))
		shape.addFace(face)
	} else {
		poly.forEach(arr=>this.shapeafy(arr, shape))
	}
	if(shape.isValid()){
		return shape
	} else {
		throw new Error("The shape is not valid: " + JSON.stringify(poly))
	}
}

@alexbol99
Copy link
Owner

It makes sense. Maybe I will add this ability to the library

alexbol99 added a commit that referenced this issue Jan 11, 2020
…e() method, updated typescript definition, tests and documentation

- Allow creation of polygon from array of numeric pairs, or from a nested array of numeric pairs (issue #23)
@alexbol99
Copy link
Owner

Available in @flatten-js/core v1.1.0

@alexbol99
Copy link
Owner

Available in v1.1.0.
Added ability to create polygon using class constructor and transfer array of numeric pairs or array of arrays.

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