-
Notifications
You must be signed in to change notification settings - Fork 36
implement centerOfMass feature #221
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
Open
tiinsy
wants to merge
3
commits into
dartclub:main
Choose a base branch
from
deanpapas:feature-centerOfMass
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
library center_of_mass.dart; | ||
|
||
export 'package:geotypes/geotypes.dart'; | ||
export 'center_of_mass.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:turf/meta.dart'; | ||
import 'centroid.dart'; | ||
|
||
// Takes a Feature and returns its center of mass using the Centroid of Polygon formula | ||
// Link to formula: https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon | ||
Feature<Point> centerOfMass( | ||
GeoJSONObject geoJson, { | ||
Map<String, dynamic>? properties = const {}, | ||
}) { | ||
List<Position> coords = []; | ||
|
||
coordEach( | ||
geoJson, | ||
(Position? currentCoord, int? coordIndex, int? featureIndex, | ||
int? multiFeatureIndex, int? geometryIndex) { | ||
if (currentCoord != null && | ||
currentCoord[0] != null && | ||
currentCoord[1] != null) { | ||
coords.add(currentCoord); | ||
} | ||
}, | ||
); | ||
|
||
// Find centre of geoJSON object | ||
Feature<Point> centre = centroid(geoJson); | ||
Position translation = centre.geometry!.coordinates; | ||
|
||
// Set up areas with pre-defined variables | ||
double sx = 0; | ||
double sy = 0; | ||
double sArea = 0; | ||
|
||
List<Position> neutralizedPoints = coords.map((point) { | ||
return Position(point[0]! - translation[0]!, point[1]! - translation[1]!); | ||
}).toList(); | ||
|
||
// Compute signed area and weighted sums | ||
for (int i = 0; i < neutralizedPoints.length - 1; i++) { | ||
Position pi = neutralizedPoints[i]; | ||
Position pj = neutralizedPoints[i + 1]; | ||
|
||
double xi = pi[0]!.toDouble(), yi = pi[1]!.toDouble(); | ||
double xj = pj[0]!.toDouble(), yj = pj[1]!.toDouble(); | ||
|
||
double a = xi * yj - xj * yi; | ||
sArea += a; | ||
sx += (xi + xj) * a; | ||
sy += (yi + yj) * a; | ||
} | ||
|
||
if (sArea == 0) { | ||
return centre; | ||
} | ||
|
||
double areaFactor = 1 / (6 * sArea); | ||
List<double> finalCoordinates = [ | ||
translation[0]! + areaFactor * sx, | ||
translation[1]! + areaFactor * sy, | ||
]; | ||
// returns Point | ||
return Feature<Point>( | ||
geometry: Point( | ||
coordinates: Position(finalCoordinates[0], finalCoordinates[1])), | ||
properties: properties); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'package:turf/center_of_mass.dart'; | ||
import 'package:turf/meta.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:turf/src/center_of_mass.dart'; | ||
|
||
void main () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are those all the tests from Turf JS? If there are more, incl. file testing, then it would be great to implement those too |
||
|
||
final polygon = Feature<Polygon>( | ||
geometry: Polygon( | ||
coordinates: [ | ||
[ | ||
Position(1, 1), | ||
Position(1, -1), | ||
Position(-1, -1), | ||
Position(-1, 1), | ||
Position(1, 1) | ||
], | ||
], | ||
), | ||
); | ||
|
||
final expectedOutput = Position(0.0, 0.0); | ||
test('centerOfMass - simple polygon centered around (0,0):', () { | ||
expect(centerOfMass(polygon).geometry?.coordinates, equals(expectedOutput)); | ||
}); | ||
|
||
final polygon2 = Feature<Polygon>( | ||
geometry: Polygon( | ||
coordinates: [ | ||
[ | ||
Position(2, 3), | ||
Position(3, 3), | ||
Position(3, 2), | ||
Position(2, 2), | ||
Position(2, 3) | ||
], | ||
], | ||
) | ||
); | ||
|
||
final expectedOutput2 = Position(2.5, 2.5); | ||
|
||
test('center of mass - simple polygon centered around non-zero coord:', () { | ||
expect(centerOfMass(polygon2).geometry?.coordinates, equals(expectedOutput2)); | ||
}); | ||
|
||
final polygon3 = Feature<Polygon>( | ||
geometry: Polygon( | ||
coordinates: [ | ||
[ | ||
Position(43, 21), | ||
Position(27, 13), | ||
Position(21, 41), | ||
Position(43, 21) | ||
], | ||
], | ||
) | ||
); | ||
final expectedOutput3 = Position(30.333333333333332, 25); | ||
|
||
test('Center of mass - complex polygon', () { | ||
expect(centerOfMass(polygon3).geometry?.coordinates, equals(expectedOutput3)); | ||
}); | ||
|
||
final polygon4 = Feature<Polygon>( | ||
geometry: Polygon( | ||
coordinates: [ | ||
[ | ||
Position(40, 20), | ||
Position(39, 20), | ||
Position(40, 20) | ||
], | ||
], | ||
) | ||
); | ||
final expectedOutput4 = Position(39.5, 20); | ||
test('Center of mass - line polygon', () { | ||
expect(centerOfMass(polygon4).geometry?.coordinates, equals(expectedOutput4)); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.