0

I am working on a choropleth map and it is showing a white page instead of the map as shown here https://i.stack.imgur.com/boYKY.png

I have both the geojson and the excel file downloaded in the same folder.

geojson https://drive.google.com/file/d/1N-rp9yHqE1Rzn2VxoAAweJ8-5XIjk61j/view?usp=sharing excel https://docs.google.com/spreadsheets/d/1NKeUg20XxJe0jccMgjj9pMxrTIIWeuQk/edit?usp=sharing&ouid=100050178655652050254&rtpof=true&sd=true

Here is my code

import json
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.read_excel('kraje.xlsx', sheet_name='List1')

regions_json = json.load(open("KRAJE.geojson", "r"))

fig = px.choropleth(df,
             locations="K_KRAJ",
             geojson=regions_json,
             color='OB1506')
fig.show()

The console of my browser in which I am viewing the map shows this

I am using a jupyter notebook in the brave browser.

Can anyone please help me solve this? Thanks

EDIT:

I found the correct geojson file but now I have a different issue. Only one region is colored and not even in the correct color and the rest of the map even outside of my regions is colored in the same color. When I hover over my regions I can see that they are in the correct place but with a wrong color. And I also have no idea why the code colored the whole map and not only the regions from the geojson file. here is an image of the output

new (should be correct) geojson https://drive.google.com/file/d/1S03NX5Q0pqgAsbJnjqt8O5w8gUHH1rt_/view?usp=sharing

import json
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.read_excel('kraje.xlsx', sheet_name='List1')

regions_json = json.load(open("KRAJE.geojson", "r"))
for feature in regions_json['features']:
    feature["id"] = feature["properties"]["K_KRAJ"]

fig = px.choropleth(df,
             locations="K_KRAJ",
             geojson=regions_json,
             color='OB1506')
fig.update_geos(fitbounds="locations", visible=False)
fig.show()

SOLUTION

Thanks to Rob Raymond it finally works. There was an issue with the geojson file. I also had a ton of problems installing geopandas and the only tutorial that actually worked was installing each package separately (https://stackoverflow.com/a/69210111/17646343)

brownie
  • 121
  • 9
  • Please include your data in csv format and where you got the geojson from in your post. Makes it easier to debug. – Tobi208 Feb 09 '22 at 10:47
  • I added links to the files for debugging. thanks – brownie Feb 09 '22 at 11:06
  • I think the reason is that the coordinate data in the geojson file provided is not latitude and longitude information. If you have latitude and longitude information, please try it. – r-beginners Feb 09 '22 at 12:31
  • I found the correct geojson but now I have a different issue. I edited my question with the new problem. Does anyone know why does it do this? – brownie Feb 09 '22 at 21:29

1 Answers1

1
  • there are multiple issues with your geojson
    1. need to define the CRS, it's clearly not epsg:4326. Appears to be UTM CRS for Czech Republic
    2. even with this there are invalid polygons
  • with valid geojson, a few points you have missed
    • locations needs to be common across your data frame and geojson
    • featureidkey needs to be used to define you are joining on name
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd

files = {
    f.suffix: f
    for p in ["KRAJE*.*", "KRAJE*.*".lower()]
    for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")

# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".geojson"], "r"))
regions_json = (
    gpd.read_file(files[".geojson"])
    .dropna()
    .set_crs("EPSG:32633", allow_override=True)
    .to_crs("epsg:4326")
    .__geo_interface__
)

fig = px.choropleth(
    df,
    locations="N_KRAJ",
    featureidkey="properties.name",
    geojson=regions_json,
    color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig

updated

  • there are still issues with your geojson. Have fixed it using geopandas and buffer(0) (see Fix invalid polygon in Shapely)
  • with this and change to plotly parameters I can now generate a figure
import json
import numpy as np
import pandas as pd
import plotly.express as px
import geopandas as gpd
from pathlib import Path

files = {
    f.suffix: f
    for p in ["KRAJ_*.*", "KRAJE*.*".lower()]
    for f in Path.home().joinpath("Downloads").glob(p)
}
# df = pd.read_excel('kraje.xlsx', sheet_name='List1')
df = pd.read_excel(files[".xlsx"], sheet_name="List1")

# regions_json = json.load(open("KRAJE.geojson", "r"))
regions_json = json.load(open(files[".json"], "r"))
# geometry is still invalid!!! force it to valid by buffer(0)
regions_json = gpd.read_file(files[".json"]).assign(geometry=lambda d: d["geometry"].buffer(0)).__geo_interface__

fig = px.choropleth(
    df,
    locations="K_KRAJ",
    featureidkey="properties.K_KRAJ",
    geojson=regions_json,
    color="OB1506",
)
fig.update_geos(fitbounds="locations", visible=True)
fig

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Thanks. I found the correct geojson but now I have a different issue. I edited my question with the new problem. Does anyone know why does it do this? – brownie Feb 09 '22 at 21:29
  • updated - your geometry still has validation issues that need to be worked around – Rob Raymond Feb 09 '22 at 22:39
  • thanks. I am now stuck on installing geopandas because it takes too long and there are so many conflicts. About the geojson...I got it from mapshaper.org after inserting shape files and then transforming the coordinates to wgs84 format. So its weird that there was another issue :( – brownie Feb 10 '22 at 11:30
  • I'll take a look at getting geojson to work correctly from source – Rob Raymond Feb 10 '22 at 12:59
  • I can't find Czech regions as geojson. Found as Shapefile, but the way I would convert that to GeoJson is using geopandas... – Rob Raymond Feb 10 '22 at 14:41
  • Oh yes thanks Rob it finally works. After a whole day of trying to install geopandas :D. – brownie Feb 10 '22 at 21:30