有没有一种在地图上自动创建细分六边形以可视化六边形的好方法? [英] Is there a good way to automatically create tessellating hexagons on maps to visualise hexbins?

查看:83
本文介绍了有没有一种在地图上自动创建细分六边形以可视化六边形的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个版本之前,Tableau引入了 HexBin 函数,以允许将地理信息(或实际上是按x-y坐标分组的任何信息)以相等大小的六边形单位分组在一起.这在创建基础信息在地理位置上分布不均的地图时非常有用.

可以将六边形的中心点直接绘制为点图,但这并不像在六边形平铺平面时实际绘制图那样令人满意.一些博客作者建议使用形状而不是点来创建类似所需结果的东西.

六边形公式相似.

数据集本身包含一个名为PointID的字段,该字段包含从1到6的值.(有多种方法可以实现此目的,但是在数据集中执行此操作比在tableau中执行更容易,因为许多数据库都包含可确保随机分布的散列函数的整数,可以通过简单的mod函数将其转换为1到6的数字).我根据PointID中的值定义了一个名为angle的新字段(以帮助定义六边形的顶点):

现在,我可以添加需要绘制的点以定义六边形多边形:

经度类似:

此时,数据集应在每个十六进制中包含足够的行,以确保每个十六进制包含的行中的每个值都为1-6,从而定义了六边形的所有点.

要绘制六边形,请适当设置"lon"和"lat lat"字段的地理角色",然后依次双击每个字段.然后将hexbinx和hexbiny拖动到详细信息,然后将图表类型从自动更改为多边形.这将给您带来一些可怕的麻烦,通过将PointID拖动到Path可以解决此问题.这应该可以做到这一点(我还添加了对六边形上色的行数以及已调整的颜色和透明度):

这表明基本技术有效.但这也显示出一个关键问题:六边形多边形变形.如果您可以忍受这种情况(靠近赤道或在较小区域内的问题较少),那就坚持下去.

但是,如果您关心变形(视觉效果以及六边形不覆盖相等区域的事实),那么您就必须做一些更复杂的事情.我在下面描述的内容基于Tableau的Sarah Battersby的工作.

下面的调整涉及简单解决方案以外的一些额外步骤:

  • 基于距离生成新的比例因子
  • 在Web Mercator系统中,根据距离而非纬度将纬度值转换为X-Y坐标
  • 在新坐标系中使用距离而不是角度生成六边形
  • 将基于距离的六边形转换回拉脱坐标,以定义六边形的中心
  • 使用距离坐标添加六边形顶点,然后转换回lat lon

您还需要为地球半径添加一个参数,该参数是坐标转换中的转换因子.

这是它的工作方式.首先转换为X-Y坐标:

现在,我们可以在新坐标上使用hexbin分组了:

将这些坐标转换回lat-lon的公式如下(如果您只想绘制中心,则很有用):

请注意,纬度公式才是复杂的公式.

但是要使六边形多边形在绘制时正常工作,必须在 转换之前将六个顶点添加额外的点,然后再将它们转换回经纬度坐标.转换与上面的公式相同,最终公式如下所示:

额外的部分仅基于六边形缩放比例为六边形中心的坐标添加固定距离(而六边形基于六个角度值).

以与上一个多边形图相同的方式绘制时,它应该看起来像这样(整理后):

现在,六边形的外观和实际大小都更好.

对于包含以上计算的Tableau Public工作簿看到这里.

PS Tableau可以并且应该将大部分此功能内置到产品中,因为它将大大简化有用的技术.

A few versions ago Tableau introduced the HexBin functions to allow geographic information (or, actually, any information grouped by x-y coordinates) to be grouped together in equally-sized hexagonal units. This can be very useful when creating maps where the underlying information isn't evenly spread geographically.

The centre points of hexbins can be plotted directly as point maps but this isn't as satisfactory as actually plotting a map where the hexagons tile the plane. Some bloggers have suggested using shapes instead of points to create something like the desired outcome. This Tableau blog uses hex bins but plots them using non-hexagonal shapes, for example. This blog suggests the use of custom hexagonal shapes, but they don't tesselate and managing them when the hexbin scale varies (the same scale doesn't work for everything) is annoying.

So is there a good way to automatically create tessellating hexagons to plot on a map that allows different hexagon densities?

Supplementary questions: why didn't Tableau build in this functionality? How to adjust for the shape of the earth when hexagons are based on Lat/lon?

解决方案

Yes it can be done in simple ways that work and more complicated ways that correct for the shape of the earth

The hexbin function in Tableau works on any X-Y data to bin the raw coordinates into hexagonal areas.

On maps, the data usually comes as Lat-Lon coordinates expressed in degrees. Tableau can plot this data directly but usually does this using the somewhat distorting Web Mercator projection (areas far away from the equator are greatly enlarged). This means that hexagonal bins defined on lat-lon will not be equally sized (one degree in an east-west direction represents a much smaller distance on the Earth's surface when the latitude is high and far from the equator but a degree on a north-south line is always about 111km long).

Simple versions ignore this distortion and generate the hexbins from lat-lon coordinates. I'll describe methods based on this simple technique first. I'm basing this on a dataset containing the locations of every postcode in the UK (~2.5m rows with a wide range of density in different geographies).

The basic method involves several steps:

  • Generate a scaling factor using a parameter that allows adjustment of the size of the hexagons
  • Generate an value to define the extra points for the vertices of the hexagon (I achieve this by adding a value to each row on the dataset that consists of a random number from 1 to 6)
  • Generate the lat-lon coordinates for the hexbin centres
  • Add the 6 points that define the vertices for the hexagons
  • Plot the hexagons as polygons on a map (they can then be coloured by any aggregate based on the underlying data)

Here is a step-by-step guide.

I define a parameter hexbin size that represents the radius of the hexbin in degrees. Then the hex bins are defined in the following way based on the Latitude and Longitude values in each row of the dataset:

The hexbiny formula is similar.

The dataset itself contains a field called PointID containing values from 1 to 6. (there are multiple ways to achieve this but doing it in the dataset is easier than doing it in tableau as many databases contain hashing functions that guarantee a random distribution of integers which can be turned into numbers from 1 to 6 by a simple mod function). I define a new field called angle based on the value in PointID (to help define the hexagon's vertices):

Now I can add the points that need to be plotted to define the hexagonal polygons:

The longitude is similar:

At this point the dataset should contain enough rows within each hexbin to guarantee that each hexbin contains rows with every value from 1-6 so all the points of the hexagon are defined.

To plot the hexagons, make the Geographic Role for the plot lon and plot lat fields is set appropriately and double-click each in turn. Then drag the hexbinx and hexbiny to detail and change the chart type from automatic to Polygon. This will give some horrible mess which is fixed by dragging PointID to Path. This should give this (i've also added a count of the number of rows to colour the hexagons and adjusted colours and transparency):

This shows that the basic technique works. But it also shows a key problem with it: the hexagonal polygons are distorted. If you can live with this (it is less of a problem close to the equator or over small areas) thens stick with it.

But if you care about the distortions (the visual as well as the fact that the hexagons don't cover equal areas) then you have to do something more complex. What I describe below is based on work by Sarah Battersby of Tableau.

The adjustments below involve some extra steps over and above the simple solution:

  • Generate a new scaling factor based on distance
  • Convert the lat-lon values into X-Y coordinates in the Web Mercator system based on distance not lat-lon degrees
  • Generate the hexbins using distance not angle in the new coordinate system
  • Convert the distance-based hexbins back to the lat-lon coordinates to define the hexbin centres
  • Add the hexagon vertices using the distance coordinates and then convert back to lat-lon

You also need to add a parameter for the radius of the earth which is a conversion factor in the coordinate transformations.

Here is how that works. First the conversion to X-Y coordinates:

Now we can use hexbin grouping on the new coordinates:

The formulae for converting these coordinates back to lat-lon are below (useful if you want to just plot the centres):

Note that it is the latitude formula that is the complex one.

But to make the hexagonal polygons work properly when plotted, you have to add the extra points for the 6 vertices before transforming them back to lat-lon coordinates. The conversion is the same as the formulae above and the final formulae look like this:

The extra part just adds a fixed distance to the coordinate for the hexbin centre based on a hexagon sized by the scaling factor (and there are six of these based on the six values of Angle).

When plotted in the same way as the previous polygon plot it should look like this (when tidied up):

Now both the visual appearance and the actual size of the hexagons are better.

For a Tableau Public workbook containing the above calculations see here.

PS Tableau could and should build most of this functionality into the product as it would dramatically simplify a useful technique.

这篇关于有没有一种在地图上自动创建细分六边形以可视化六边形的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆