Search code examples
pythongeojsonaltair

Altair and GeoJSON file


I'm using this website as guide:

https://www.districtdatalabs.com/altair-choropleth-viz/

However using slightly different data for London Boroughs:

https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london

Particularly this shape file: /ESRI/London_Borough_Excluding_MHW.shp

However when I'm trying to get the base layer I get a very weird output:

Weird Base Layer

When I use the data that's used on the tutorial I get exactly the same as the tutorial. So is it something in London Boroughs shape file?

My code so far:

map_df = gpd.read_file("/statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp")
#Change to match Borough names in previous DFs
Bnames = {"Kingston upon Thames" : "KingstonUponThames",
      "Richmond upon Thames" : "RichmondUponThames",
      "Hammersmith and Fulham" : "HammersmithFulham",
      "Kensington and Chelsea" : "KensingtonChelsea",
      "Tower Hamlets" : "TowerHamlets",
      "Barking and Dagenham" : "BarkingDagenham",
      "City of London" : "CityOfLondon"
     }
map_df['Borough'] = map_df['NAME'].map(Bnames)
map_df['Borough'] = map_df['Borough'].fillna(map_df['NAME'])


merged = map_df.merge(FreqMalCall[['Borough','MalCall']], how='left', on='Borough')

map_df['centroid_lon'] = map_df['geometry'].centroid.x
map_df['centroid_lat'] = map_df['geometry'].centroid.y


 choro_json = json.loads(map_df.to_json())
 choro_data = alt.Data(values=choro_json['features'])

def gen_base(geojson):
'''Generates baselayer of DC ANC map'''
base = alt.Chart(alt.Data(values=geojson)).mark_geoshape(
    stroke='black',
    strokeWidth=1
).encode(
).properties(
    width=400,
    height=400
)
return base


base_layer = gen_base(geojson=choro_json)
base_layer

Solution

  • Obvious mistake on my part. I needed to change the coordinate reference system:

     test_map = map_df.to_crs(epsg=4326)