Calculate area of a clipped Voronoi region

523 views
Skip to first unread message

Gonzalo Bellver

unread,
May 19, 2014, 10:39:38 AM5/19/14
to d3...@googlegroups.com
Hi everyone,

I'm trying to calculate the area of a clipped (projected) Voronoi region over a map, to use it as a parameter for the color scale. My initial approach was using area() of d3.geo.path() with the polygon's path string, but it's always returning 0. I guess its because the feature is a MultiLine and the area of a MultiLine is always 0.

How can I calculate the area of the Voronoi region taking clipping into account?


Thanks!
Gonzalo

Jason Davies

unread,
May 19, 2014, 5:42:08 PM5/19/14
to d3...@googlegroups.com
On Mon, May 19, 2014 at 07:39:38AM -0700, Gonzalo Bellver wrote:
> I'm trying to calculate the area of a clipped (projected) Voronoi region
> over a map, to use it as a parameter for the color scale. My initial
> approach was using area()<https://github.com/mbostock/d3/wiki/Geo-Paths#path_area>of d3.geo.path() with the polygon's path string, but it's always returning
> 0. I guess its because the feature is a MultiLine and the area of a
> MultiLine is always 0.

The issue is that d3.geo.path.area expects a GeoJSON object, and you are
passing it the result of `polygon(d)`, which is an SVG path string.

If you want to compute the area of the Voronoi polygons, you can simply
use d3.geom.polygon.area:

https://github.com/mbostock/d3/wiki/Polygon-Geom#area

--
Jason Davies, http://www.jasondavies.com/

Gonzalo Bellver

unread,
May 19, 2014, 5:46:14 PM5/19/14
to d3...@googlegroups.com
Thanks, Jason

Will d3.geom.polygon.area consider clipping? Some of the polygons are clipped with a clipPath...
> --
> You received this message because you are subscribed to the Google Groups "d3-js" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jason Davies

unread,
May 19, 2014, 6:52:25 PM5/19/14
to d3...@googlegroups.com
On Mon, May 19, 2014 at 06:46:14PM -0300, Gonzalo Bellver wrote:
> Will d3.geom.polygon.area consider clipping? Some of the polygons are
> clipped with a clipPath...

Sorry, I missed that you were using an SVG clipPath.

It’s not that straightforward to do this at the moment for general
clipping paths. D3’s d3.geom.polygon.clip is an implementation of the
Sutherland–Hodgman algorithm, which only supports convex clip polygons,
and thus we can’t use the projected geographic boundary as the clip
polygon at the moment.

Forunately, we can exploit the fact that Voronoi regions are guaranteed
to be convex, and instead we can use each Voronoi region as a clip
polygon in turn, and clip the projected geographic boundary instead.
(Clipping is simply an intersection operation on shapes, so we can swap
the clip and subject polygons easily).

I’ve modified your example here:

http://bl.ocks.org/jasondavies/d70baf034448ef7a52d1

I’m also extracting the projected geographic boundary using a custom
stream to ensure we get any resampled points, but it would work just as
well without this due to the resolution of the data.

Note that we’re computing the projected (mercator) area here. This is
not really an issue for this example due to the scale, which means the
area distortion is very low, but it’s something to be conscious of when
reusing this method in other contexts, in which case an equal-area
projection would be more appropriate.
Reply all
Reply to author
Forward
0 new messages