如何使用Altair中的GeoJSON数据制作地图?

2024-05-20 22:53:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我对地图和牛郎星/织女星都很陌生。有一个an example in the Altair documentation for how to make a map starting with an outline of US states,它基本上是用以下方法创建的:

states = alt.topo_feature(data.us_10m.url, feature='states')

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
)

但我想在不列颠群岛上画点。由于vega数据集合中只有美国和世界地图,所以我必须创建自己的GeoJSON,不是吗?在

所以我试着从世界地图上获取不列颠群岛的GeoJSON,通过运行一些命令行命令from this blog post,即

^{pr2}$

这似乎创建了一个GeoJSON文件,子单元.json可能代表了不列颠群岛。但我怎么才能把这个带进牛郎星呢?或者有没有其他方法可以用牛郎星绘制不列颠群岛的地图?在


Tags: the方法inanexamplegeojson地图alt
2条回答

在本例中,data.us_10m.url是一个字符串变量,其中字符串指定指向state功能中包含美国州边界的geojson file的URL。如果您想使用不同的geojson文件,可以在该示例中替换其URL。在

您引用的示例是使用topojson结构化数据,而您有geojson结构化数据。所以你可能需要:

# remote geojson data object
url_geojson = 'https://raw.githubusercontent.com/mattijn/datasets/master/two_polygons.geo.json'
data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='features',type='json'))

# chart object
alt.Chart(data_geojson_remote).mark_geoshape(
).encode(
    color="properties.name:N"
).properties(
    projection={'type': 'identity', 'reflectY': True}
)

chart

欲了解更多详情,请继续阅读


解释geojsontopojson结构化json文件之间的区别及其在Altair

中的使用 ^{pr2}$

内联GeoJSON

我们首先创建一个包含两个特征的集合,即两个相邻的多边形。在

我们将以GeoJSON数据格式创建的两个多边形示例:

FeatureCollection with two Features

feature_1 = geojson.Feature(
    geometry=geojson.Polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]),
    properties={"name":"abc"}
)
feature_2 = geojson.Feature(
    geometry=geojson.Polygon([[[1, 0], [2, 0], [2, 1], [1, 1], [1, 0]]]),
    properties={"name":"def"}
)
var_geojson = geojson.FeatureCollection([feature_1, feature_2])

通过漂亮地打印变量var_geojson检查创建的GeoJSON

pprint.pprint(var_geojson)
{'features': [{'geometry': {'coordinates': [[[0, 0],
                                             [1, 0],
                                             [1, 1],
                                             [0, 1],
                                             [0, 0]]],
                            'type': 'Polygon'},
               'properties': {'name': 'abc'},
               'type': 'Feature'},
              {'geometry': {'coordinates': [[[1, 0],
                                             [2, 0],
                                             [2, 1],
                                             [1, 1],
                                             [1, 0]]],
                            'type': 'Polygon'},
               'properties': {'name': 'def'},
               'type': 'Feature'}],
 'type': 'FeatureCollection'}

可以看出,这两个PolygonFeatures嵌套在features对象中,geometry是每个{}的一部分。在

Altair能够使用format中的property键解析嵌套的json对象。以下是这样一个例子:

# inline geojson data object
data_geojson = alt.InlineData(values=var_geojson, format=alt.DataFormat(property='features',type='json')) 

# chart object
alt.Chart(data_geojson).mark_geoshape(
).encode(
    color="properties.name:N"
).properties(
    projection={'type': 'identity', 'reflectY': True}
)

chart

内联拓扑JSON

TopoJSON是GeoJSON的一个扩展,其中featuresgeometry是从一个名为arcs的顶级对象引用的。这使得在几何体上应用散列函数成为可能,因此每个共享的arc应该只存储一次。在

我们可以将var_geojson变量转换为topojson文件格式结构:

var_topojson = topojson.Topology(var_geojson, prequantize=False).to_json()
var_topojson
{'arcs': [[[1.0, 1.0], [0.0, 1.0], [0.0, 0.0], [1.0, 0.0]],
          [[1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [1.0, 1.0]],
          [[1.0, 1.0], [1.0, 0.0]]],
 'objects': {'data': {'geometries': [{'arcs': [[-3, 0]],
                                      'properties': {'name': 'abc'},
                                      'type': 'Polygon'},
                                     {'arcs': [[1, 2]],
                                      'properties': {'name': 'def'},
                                      'type': 'Polygon'}],
                      'type': 'GeometryCollection'}},
 'type': 'Topology'}

现在嵌套的geometry对象被arcs替换,并通过索引引用顶层的arcs对象。我们现在可以使用多个FeatureCollection,而不是一个objects,其中转换后的FeatureCollection作为GeometryCollection存储在键data中。在

注意:键名data是任意的,并且在每个数据集中都是不同的。在

Altair能够使用format中的feature键解析topojson格式结构中的嵌套data对象,同时声明它是topojsontype。以下是这样一个例子:

# inline topojson data object
data_topojson = alt.InlineData(values=var_topojson, format=alt.DataFormat(feature='data',type='topojson')) 

# chart object
alt.Chart(data_topojson).mark_geoshape(
).encode(
    color="properties.name:N"
).properties(
    projection={'type': 'identity', 'reflectY': True}
)

chart

来自URL

如果可以通过URL访问topojson文件,还可以使用速记从topojson文件中提取对象:

alt.topo_feature(url, feature)

Altair示例,其中URL引用topojson文件

# remote topojson data object
url_topojson = 'https://raw.githubusercontent.com/mattijn/datasets/master/two_polygons.topo.json'
data_topojson_remote = alt.topo_feature(url=url_topojson, feature='data')

# chart object
alt.Chart(data_topojson_remote).mark_geoshape(
).encode(
    color="properties.name:N"
).properties(
    projection={'type': 'identity', 'reflectY': True}
)

chart

来自URL

但是对于可通过URL访问的geojson文件,没有这样的速记,应该按如下方式链接:

alt.Data(url, format)

Altair示例,其中URL引用geojson文件

# remote geojson data object
url_geojson = 'https://raw.githubusercontent.com/mattijn/datasets/master/two_polygons.geo.json'
data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='features',type='json'))

# chart object
alt.Chart(data_geojson_remote).mark_geoshape(
).encode(
    color="properties.name:N"
).properties(
    projection={'type': 'identity', 'reflectY': True}
)

chart

相关问题 更多 >