question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access GeoPandas points' lat/long in encode() ?

See original GitHub issue

Hi,

Is there an elegant way to adress the lat and long component of points in the geometry column of a Geopandas Dataframe in Altair’s encode method to link it to latitude and longitude? I could not find anything on this in the documentation.

I understand that it transparently does it for mark_geoshape(), but for the case of graduated symbol maps, e.g., via mark_circle(), it would be nice to have a bit more control and not have to generate separate columns in the DataFrame just to point at it.

Here is a relatively minimal example:


import altair as alt
import pandas as pd
import geopandas as gpd

districts = gpd.read_file("https://opendata.potsdam.de/explore/dataset/statistische-bezirke-in-potsdam/download/?format=geojson")
schools = gpd.read_file("https://opendata.potsdam.de/explore/dataset/schulen/download/?format=geojson")

# i would like to skip this step:
schools['lon'] = schools['geometry'].x
schools['lat'] = schools['geometry'].y

basemap = alt.Chart(districts).mark_geoshape(fill="lightgray", stroke="darkgray"
                    ).encode(tooltip="bezeichnun")

pois = alt.Chart(schools).mark_circle().encode(
    latitude='lat:Q',
    longitude='lon:Q',
    tooltip="schulname",
    size="planungsra:Q"
)

basemap + pois

altair_graduated_symbols ipynb

Thanks a lot, Marian

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jakevdpcommented, Jun 10, 2020

Actually, I take that back. There is a shortcut here, using vega-lite’s syntax for nested data encodings:

import altair as alt
import pandas as pd
import geopandas as gpd

districts = gpd.read_file("https://opendata.potsdam.de/explore/dataset/statistische-bezirke-in-potsdam/download/?format=geojson")
schools = gpd.read_file("https://opendata.potsdam.de/explore/dataset/schulen/download/?format=geojson")

basemap = alt.Chart(districts).mark_geoshape(fill="lightgray", stroke="darkgray"
                    ).encode(tooltip="bezeichnun")

pois = alt.Chart(schools).mark_circle().encode(
    latitude='geometry.coordinates[1]:Q',
    longitude='geometry.coordinates[0]:Q',
    tooltip="schulname",
    size="planungsra:Q"
)

basemap + pois

visualization (5)

0reactions
mattijncommented, Jun 11, 2020

Yeah also just realized that it should be possible like this. It’s a bit better then assigning new lat, lon columns👍.

Side note: You could not have known this from the Python side, since there is no schools.geometry.coordinates in the GeoDataFrame. The GeoDataFrame is serialised as (slightly custom) GeoJSON for Vega-Lite insertion and referred to in the Altair spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

geopandas.points_from_xy
Generate GeometryArray of shapely Point geometries from x, y(, z) coordinates. In case of geographic coordinates, it is assumed that longitude is captured...
Read more >
Reverse Geocoding in Python - Towards Data Science
Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name.
Read more >
2. More Advanced Python - Using Shapefiles with GeoPandas
We include points_from_xy() when we specify the geometry. This takes the latitude and longitude columns and uses them to create point objects that...
Read more >
Introduction to Geospatial Raster and Vector Data with Python
Raster datasets can usually only encode one data type, for example you can't have a multiband ... Load point, line, and polygon vector...
Read more >
Perform a Spatial Join in Python - Coiled
For example, you can join a point-based dataset with a polygon-based ... A neat trick to know about GeoPandas is that calling .plot()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found