Skip to content

Commit da73a9b

Browse files
committed
expose the area of the geometry as *geometry*.area
It can be used, for example, to filter out "frames", i.e. the whole rectangle returned for a value < min(values), by testing geometry.area > n * m - 3/4 https://observablehq.com/d/0cef46faf6b9c53c See #14
1 parent 0606516 commit da73a9b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ The returned geometry objects are typically passed to [d3.geoPath](https://githu
8787

8888
<a name="contours_contour" href="#contours_contour">#</a> <i>contours</i>.<b>contour</b>(<i>values</i>, <i>threshold</i>) [<>](https://github.com/d3/d3-contour/blob/master/src/contours.js "Source")
8989

90-
Computes a single contour, returning a [GeoJSON](http://geojson.org/geojson-spec.html) [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) [geometry object](http://geojson.org/geojson-spec.html#geometry-objects) representing the area where the input <i>values</i> are greater than or equal to the given [*threshold* value](#contours_thresholds); the threshold value for each geometry object is exposed as <i>geometry</i>.value.
90+
Computes a single contour, returning a [GeoJSON](http://geojson.org/geojson-spec.html) [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) [geometry object](http://geojson.org/geojson-spec.html#geometry-objects) representing the area where the input <i>values</i> are greater than or equal to the given [*threshold* value](#contours_thresholds); the threshold value for each geometry object is exposed as <i>geometry</i>.value, and its area exposed as <i>geometry</i>.area.
9191

9292
The input *values* must be an array of length <i>n</i>×<i>m</i> where [<i>n</i>, <i>m</i>] is the contour generator’s [size](#contours_size); furthermore, each <i>values</i>[<i>i</i> + <i>jn</i>] must represent the value at the position ⟨<i>i</i>, <i>j</i>⟩. See [*contours*](#_contours) for an example.
9393

src/contours.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ export default function() {
5252
// Based on https://github.com/mbostock/shapefile/blob/v0.6.2/shp/polygon.js
5353
function contour(values, value) {
5454
var polygons = [],
55-
holes = [];
55+
holes = [],
56+
a = 0;
5657

5758
isorings(values, value, function(ring) {
5859
smooth(ring, values, value);
59-
if (area(ring) > 0) polygons.push([ring]);
60+
var ar = area(ring);
61+
if (ar > 0) polygons.push([ring]);
6062
else holes.push(ring);
63+
a += ar / 2;
6164
});
6265

6366
holes.forEach(function(hole) {
@@ -72,6 +75,7 @@ export default function() {
7275
return {
7376
type: "MultiPolygon",
7477
value: value,
78+
area: a,
7579
coordinates: polygons
7680
};
7781
}

test/contours-test.js

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tape("contours(values) returns the expected result for an empty polygon", functi
1818
{
1919
"type": "MultiPolygon",
2020
"value": 0.5,
21+
"area": 0,
2122
"coordinates": []
2223
}
2324
]);
@@ -41,6 +42,7 @@ tape("contours(values) returns the expected result for a simple polygon", functi
4142
{
4243
"type": "MultiPolygon",
4344
"value": 0.5,
45+
"area": 14.5,
4446
"coordinates": [
4547
[
4648
[[6, 7.5], [6, 6.5], [6, 5.5], [6, 4.5], [6, 3.5], [5.5, 3], [4.5, 3],
@@ -69,6 +71,7 @@ tape("contours(values).contour(value) returns the expected result for a simple p
6971
], 0.5), {
7072
"type": "MultiPolygon",
7173
"value": 0.5,
74+
"area": 14.5,
7275
"coordinates": [
7376
[
7477
[[6, 7.5], [6, 6.5], [6, 5.5], [6, 4.5], [6, 3.5], [5.5, 3], [4.5, 3],
@@ -97,6 +100,7 @@ tape("contours.smooth(false)(values) returns the expected result for a simple po
97100
{
98101
"type": "MultiPolygon",
99102
"value": 0.5,
103+
"area": 14.5,
100104
"coordinates": [
101105
[
102106
[[6, 7.5], [6, 6.5], [6, 5.5], [6, 4.5], [6, 3.5], [5.5, 3], [4.5, 3],
@@ -126,6 +130,7 @@ tape("contours(values) returns the expected result for a polygon with a hole", f
126130
{
127131
"type": "MultiPolygon",
128132
"value": 0.5,
133+
"area": 12,
129134
"coordinates": [
130135
[
131136
[[6, 7.5], [6, 6.5], [6, 5.5], [6, 4.5], [6, 3.5], [5.5, 3], [4.5, 3],
@@ -157,6 +162,7 @@ tape("contours(values) returns the expected result for a multipolygon", function
157162
{
158163
"type": "MultiPolygon",
159164
"value": 0.5,
165+
"area": 14,
160166
"coordinates": [
161167
[
162168
[[5, 7.5], [5, 6.5], [5, 5.5], [5, 4.5], [5, 3.5], [4.5, 3], [3.5, 3],
@@ -190,6 +196,7 @@ tape("contours(values) returns the expected result for a multipolygon with holes
190196
{
191197
"type": "MultiPolygon",
192198
"value": 0.5,
199+
"area": 16,
193200
"coordinates": [
194201
[
195202
[[4, 5.5], [4, 4.5], [4, 3.5], [3.5, 3], [2.5, 3], [1.5, 3], [1, 3.5],

0 commit comments

Comments
 (0)