I'm very new to mapping, and to Altair/Vega. There's an example in the Altair documentation for how to make a map starting with an outline of US states, which is created basically with:
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
)
but I want to plot points in the British Isles, instead. Since there are only US and World maps in the vega data collections, I would have to create my own GeoJSON, no?
So I tried getting GeoJSON for the British Isles from a world map, by running some of the command-line commands from this blog post, namely,
ogr2ogr -f GeoJSON -where "adm0_a3 IN ('GBR','IRL','IMN','GGY','JEY','GBA')" subunits.json ne_10m_admin_0_map_subunits/ne_10m_admin_0_map_subunits.shp
This seems to have created a GeoJSON file, subunits.json, which probably represents the British Isles. But how can I get this into Altair? Or is there another way to make a map of the British Isles using Altair?
The example you refer to is using
topojson
structured data, while you havegeojson
structured data. So you probably need:For more insight read on
Explaining the differences between
geojson
andtopojson
structuredjson
files and their usage within AltairInline GeoJSON
We start with creating a collection containing two features, namely two adjacent polygons.
Example of the two polygons that we will create in the GeoJSON data format.:
Inspect the created GeoJSON by pretty print the variable
var_geojson
As can be seen, the two
Polygon
Features
are nested within thefeatures
object and thegeometry
is part of eachfeature
.Altair has the capability to parse nested
json
objects using theproperty
key withinformat
. The following is an example of such:Inline TopoJSON
TopoJSON is an extension of GeoJSON, where the
geometry
of thefeatures
are referred to from a top-level object namedarcs
. This makes it possible to apply a hash function on the geometry, so each sharedarc
should only be stored once.We can convert the
var_geojson
variable into atopojson
file format structure:Now the nested
geometry
objects are replaced byarcs
and refer by index to the top-levelarcs
object. Instead of having a singleFeatureCollection
we now can have multipleobjects
, where our convertedFeatureCollection
is stored within the keydata
as aGeometryCollection
.NOTE: the key-name
data
is arbitrary and differs in each dataset.Altair has the capability to parse the nested
data
object in thetopojson
formatted structure using thefeature
key withinformat
, while declaring it is atopojson
type
. The following is an example of such:TopoJSON from URL
There also exist a shorthand to extract the objects from a
topojson
file if this file is accessible by URL:Altair example where a
topojson
file is referred by URLGeoJSON from URL
But for
geojson
files accessible by URL there is no such shorthand and should be linked as follows:Altair example where a
geojson
file is referred by URLIn this example,
data.us_10m.url
is a string variable, where the string specifies the URL to a geojson file containing US state boundaries in thestate
feature. If you have a different geojson file you would like to use, you can substitute its URL in that example.