Skip to content

Commit

Permalink
moving forward
Browse files Browse the repository at this point in the history
  • Loading branch information
armantorkzaban committed Jul 7, 2022
1 parent 63a319a commit 5c863e0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 48 deletions.
20 changes: 16 additions & 4 deletions lib/src/booleans/boolean_equal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import '../clean_coords.dart';

/// Determine whether two geometries of the same type have identical X,Y coordinate values.
/// See http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm
/// [precision]=6 sets decimal precision to use when comparing coordinates
/// [precision]=6 sets decimal precision to use when comparing coordinates.
/// With [direction] set to true, even if the [LineStrings] are reverse versions
/// of each other but the have similar [Position]s, they will be considered the same.
/// If [shiftedPolygon] is true, two [Polygon]s with shifted [Position]s are
/// considered the same.
/// Returns true if the objects are equal, false otherwise
/// example:
/// var pt1 = Point(coordinates: Position.of([0, 0]));
Expand All @@ -15,8 +19,13 @@ import '../clean_coords.dart';
/// //= true
/// booleanEqual(pt2, pt3);
/// //= false
bool booleanEqual(GeoJSONObject feature1, GeoJSONObject feature2,
{int precision = 6}) {
bool booleanEqual(
GeoJSONObject feature1,
GeoJSONObject feature2, {
int precision = 6,
bool direction = false,
bool shiftedPolygon = false,
}) {
if (!(precision >= 0)) {
throw Exception("precision must be a positive number");
}
Expand All @@ -27,6 +36,9 @@ bool booleanEqual(GeoJSONObject feature1, GeoJSONObject feature2,
var type2 = geom2!.type;
if (type1 != type2) return false;

var equality = Equality(precision: precision);
var equality = Equality(
precision: precision,
shiftedPolygon: shiftedPolygon,
direction: direction);
return equality.compare(cleanCoords(feature1), cleanCoords(feature2));
}
58 changes: 33 additions & 25 deletions test/booleans/equal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,49 @@ main() {
var inDir = Directory('./test/examples/booleans/equal/test/true');
for (var file in inDir.listSync(recursive: true)) {
if (file is File && file.path.endsWith('.geojson')) {
test(file.path, () {
// True Fixtures
var inSource = file.readAsStringSync();
var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource));
test(
file.path,
() {
// True Fixtures
var inSource = file.readAsStringSync();
var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource));

var feature1 = (inGeom as FeatureCollection).features[0];
var feature2 = inGeom.features[1];
Map<String, dynamic> json = jsonDecode(inSource);
var options = json['properties'];
var result = booleanEqual(feature1, feature2,
precision: options?['precision'] ?? 6);

expect(result, true);
});
var feature1 = (inGeom as FeatureCollection).features[0];
var feature2 = inGeom.features[1];
Map<String, dynamic> json = jsonDecode(inSource);
var options = json['properties'];
print(json['properties']?['direction']);
var result = booleanEqual(feature1, feature2,
precision: options?['precision'] ?? 6,
shiftedPolygon: options?['shiftedPolygon'] ?? false,
direction: options?['direction'] ?? false);
expect(result, true);
},
);
}
}
// False Fixtures
var inDir1 = Directory('./test/examples/booleans/equal/test/false');
for (var file in inDir1.listSync(recursive: true)) {
if (file is File && file.path.endsWith('.geojson')) {
test(file.path, () {
// True Fixtures
var inSource = file.readAsStringSync();
var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource));
test(
file.path,
() {
// True Fixtures
var inSource = file.readAsStringSync();
var inGeom = GeoJSONObject.fromJson(jsonDecode(inSource));

var feature1 = (inGeom as FeatureCollection).features[0];
var feature2 = inGeom.features[1];
var feature1 = (inGeom as FeatureCollection).features[0];
var feature2 = inGeom.features[1];

Map<String, dynamic> json = jsonDecode(inSource);
var options = json['properties'];
var result = booleanEqual(feature1, feature2,
precision: options?['precision'] ?? 6);
Map<String, dynamic> json = jsonDecode(inSource);
var options = json['properties'];
var result = booleanEqual(feature1, feature2,
precision: options?['precision'] ?? 6);

expect(result, false);
});
expect(result, false);
},
);
}
}
var pt = Point(coordinates: Position.of([9, 50]));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
{
"type": "FeatureCollection",
"properties": {
"precision": 6,
"shiftedPolygon": true,
"direction": false
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[1, 1],
[1, 0],
[0, 0],
[1, 1]
[
1,
1
],
[
1,
0
],
[
0,
0
],
[
1,
1
]
]
]
}
Expand All @@ -23,13 +39,25 @@
"type": "Polygon",
"coordinates": [
[
[0, 0],
[1, 1],
[1, 0],
[0, 0]
[
0,
0
],
[
1,
1
],
[
1,
0
],
[
0,
0
]
]
]
}
}
]
}
}
47 changes: 38 additions & 9 deletions test/examples/booleans/equal/test/true/reverse-polygons.geojson
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"type": "FeatureCollection",
"properties": {
"precision": 6,
"shiftedPolygon": true,
"direction": true
},
"features": [
{
"type": "Feature",
Expand All @@ -8,10 +13,22 @@
"type": "Polygon",
"coordinates": [
[
[-53.57, 28.28],
[-53.33, 28.29],
[-53.34, 28.43],
[-53.57, 28.28]
[
-53.57,
28.28
],
[
-53.33,
28.29
],
[
-53.34,
28.43
],
[
-53.57,
28.28
]
]
]
}
Expand All @@ -23,13 +40,25 @@
"type": "Polygon",
"coordinates": [
[
[-53.57, 28.28],
[-53.34, 28.43],
[-53.33, 28.29],
[-53.57, 28.28]
[
-53.57,
28.28
],
[
-53.34,
28.43
],
[
-53.33,
28.29
],
[
-53.57,
28.28
]
]
]
}
}
]
}
}

0 comments on commit 5c863e0

Please sign in to comment.