Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set the extent and zoom with a mark_geoshape? #632

Closed
palewire opened this issue Mar 22, 2018 · 2 comments
Closed

Is it possible to set the extent and zoom with a mark_geoshape? #632

palewire opened this issue Mar 22, 2018 · 2 comments

Comments

@palewire
Copy link
Contributor

palewire commented Mar 22, 2018

Say, for instance, I wanted to zoom in on Los Angeles County on map of U.S. states example included in the documentation.

visualization

Could I set the "scale" or otherwise change the extent so that LA, or any other location, was at the center of the map?

@palewire palewire changed the title Is it possible to set the extent and zoom in on a mark_geoshape? Is it possible to set the extent and zoom with a mark_geoshape? Mar 22, 2018
@jakevdp
Copy link
Collaborator

jakevdp commented Mar 22, 2018

No, this is not supported at the moment. See vega/vega-lite#3305

@kot-behemoth
Copy link

kot-behemoth commented Apr 9, 2019

I found a workaround, and wrote a small post about it here. I'll just paste the contents here, in case the link dies later.

"Zooming in" on a map in Altair

Altair doesn't allow making maps (aka mark_geoshape) interactive, since geo markers do not yet support interaction in Vega-Lite (vega/vega-lite#3306).

I stumbled upon this problem when I needed to only display information in only one US state, but the official example shows the full map of the US. Furthermore, it's not possible to set a fixed zoom, so that only one state is visible when the map is shown.

I found a solution by essentially only displaying the state I need, instead of the whole map. For that, I had to find the relevant geometry file. I found a GeoJson file for the state I was interested in from world.geo.json repo (state files are located here, below the folders). Then, I loaded it into Geojson.io geojson.io, and exported as TopoJson (I'm sure it could be done via better means, but this was the quickest). Finally, I simply used the local file in my notebook as such:

state = alt.topo_feature('./map.topojson', 'collection')

(
    alt.Chart(state)
    .mark_geoshape(
        fill='white',
        stroke='black'
    )
    .project(type='albersUsa')
)

Note that the second argument to alt.topo_feature can be found in the topojson file like this:

{
  "type": "Topology",
  "objects": {
    "collection": {     <--------- this is the argument
      "type": "GeometryCollection",
      "geometries": [
        {
          "type": "Polygon",
          "id": "USA-TX",
          "properties": {
            "fips": 48,
            "name": "Texas"
          },
          ...

Hope this helps while vega-lite gets the required functionality!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants