Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Panz99 committed Jul 23, 2024
1 parent 001e585 commit c5b1dcc
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/src/circle.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:turf/helpers.dart';
import 'package:turf/invariant.dart';
import 'package:turf/src/booleans/boolean_helper.dart';
import 'destination.dart';

/// Takes a [Point] and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
/// Takes a [Point] or a [Feature<Point>] and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
///
/// example:
/// ```dart
Expand All @@ -16,17 +18,26 @@ import 'destination.dart';
/// );
/// ```
Feature<Polygon> circle(
Point center,
GeoJSONObject center,
num radius, {
num? steps = 64,
Unit? unit = Unit.kilometers,
Map<String, dynamic>? properties = const {},
num? steps,
Unit? unit,
Map<String, dynamic>? properties,
}) {
steps ??= 64;
unit ??= Unit.kilometers;
Point origin;
final geometry = getGeom(center);
if (geometry is Point) {
origin = geometry;
} else {
throw GeometryNotSupported(geometry);
}
properties ??=
center is Feature && center.properties != null ? center.properties : {};
final List<Position> coordinates = [];
for (var i = 0; i < steps; i++) {
final c = destination(center, radius, (i * -360) / steps, unit).coordinates;
final c = destination(origin, radius, (i * -360) / steps, unit).coordinates;
coordinates.add(c);
}
coordinates.add(coordinates[0]);
Expand Down
59 changes: 59 additions & 0 deletions test/components/circle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'dart:convert';
import 'dart:io';

import 'package:test/test.dart';
import 'package:turf/circle.dart';
import 'package:turf/src/truncate.dart';
import 'package:turf_equality/turf_equality.dart';

import '../context/helper.dart';

void main() {
group(
'circle',
() {
test('circle -- add properties', () {
final point = Point(coordinates: Position(0, 0));
final out = circle(point, 10, properties: {'foo': 'bar'});

final isEqual = out.properties?['foo'] == 'bar';
expect(isEqual, true);
});

Directory inDir = Directory('./test/examples/circle/in');
for (var file in inDir.listSync(recursive: true)) {
if (file is File && file.path.endsWith('.geojson')) {
test(
file.path,
() {
final inSource = file.readAsStringSync();
final feature = Feature.fromJson(jsonDecode(inSource));
final properties = feature.properties ?? {};
final radius = properties['radius'] ?? 5;
final steps = properties['steps'] ?? 64;
final unit = properties['units'];

final C = truncate(circle(feature, radius,
properties: {"marker-symbol": "circle"},
steps: steps,
unit: unit)) as Feature<Polygon>;

final results = featureCollection([feature, C]);

Directory outDir = Directory('./test/examples/centroid/out');
for (var file2 in outDir.listSync(recursive: true)) {
if (file2 is File &&
file2.uri.pathSegments.last == file.uri.pathSegments.last) {
var outSource = file2.readAsStringSync();
var outGeom = GeoJSONObject.fromJson(jsonDecode(outSource));
Equality eq = Equality();
expect(eq.compare(results, outGeom), true);
}
}
},
);
}
}
},
);
}
10 changes: 10 additions & 0 deletions test/examples/circle/in/circle1.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "Feature",
"properties": {
"radius": 5
},
"geometry": {
"type": "Point",
"coordinates": [-75.343, 39.984]
}
}
93 changes: 93 additions & 0 deletions test/examples/circle/out/circle1.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"radius": 5
},
"geometry": {
"type": "Point",
"coordinates": [-75.343, 39.984]
}
},
{
"type": "Feature",
"properties": {
"radius": 5
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-75.343, 40.028966],
[-75.348756, 40.028749],
[-75.354456, 40.028101],
[-75.360046, 40.027029],
[-75.365472, 40.025541],
[-75.37068, 40.023653],
[-75.375622, 40.021383],
[-75.380248, 40.018753],
[-75.384516, 40.015788],
[-75.388383, 40.012517],
[-75.391813, 40.008972],
[-75.394772, 40.005185],
[-75.397232, 40.001195],
[-75.399169, 39.997039],
[-75.400565, 39.992758],
[-75.401406, 39.988393],
[-75.401685, 39.983985],
[-75.401399, 39.979578],
[-75.40055, 39.975213],
[-75.399148, 39.970934],
[-75.397204, 39.96678],
[-75.39474, 39.962792],
[-75.391777, 39.959008],
[-75.388345, 39.955465],
[-75.384477, 39.952197],
[-75.380211, 39.949235],
[-75.375586, 39.946608],
[-75.370648, 39.94434],
[-75.365444, 39.942455],
[-75.360025, 39.940969],
[-75.354442, 39.939897],
[-75.348748, 39.93925],
[-75.343, 39.939034],
[-75.337252, 39.93925],
[-75.331558, 39.939897],
[-75.325975, 39.940969],
[-75.320556, 39.942455],
[-75.315352, 39.94434],
[-75.310414, 39.946608],
[-75.305789, 39.949235],
[-75.301523, 39.952197],
[-75.297655, 39.955465],
[-75.294223, 39.959008],
[-75.29126, 39.962792],
[-75.288796, 39.96678],
[-75.286852, 39.970934],
[-75.28545, 39.975213],
[-75.284601, 39.979578],
[-75.284315, 39.983985],
[-75.284594, 39.988393],
[-75.285435, 39.992758],
[-75.286831, 39.997039],
[-75.288768, 40.001195],
[-75.291228, 40.005185],
[-75.294187, 40.008972],
[-75.297617, 40.012517],
[-75.301484, 40.015788],
[-75.305752, 40.018753],
[-75.310378, 40.021383],
[-75.31532, 40.023653],
[-75.320528, 40.025541],
[-75.325954, 40.027029],
[-75.331544, 40.028101],
[-75.337244, 40.028749],
[-75.343, 40.028966]
]
]
}
}
]
}

0 comments on commit c5b1dcc

Please sign in to comment.