folium.Choropleth Where is my mistake?

Hello all,

The main question: at the moment, is it basically possible to use “folium.Choropleth” without any trouble.

Folium alone does not cause any problems,


MAP_LOC = [48.88774545, 10.092337] # here Ostalbkreis
map_kreisverband = folium.Map(
location=MAP_LOC,
zoom_start=11,
#tiles = ‘Stamen Terrain’,
width=“100%” ,
height=“100%”
)
folium.TileLayer(
name=“county”,
control=True,
).add_to(map_kreisverband)

map_kreisverband


outputs a nice map.


I open the files as follows:


from pyodide.http import open_url ## Get the data

#------ Daten --------------------------------------------------
DATA_URL_JSON = (“Daten\kv-csv\json\kv_ellwangen_aalen.geojson”)
DATA_URL_DF = (“Daten\kv-csv\kv-mitglieder-summe-final-copy.csv”)
DATA_URL_DF_KV = (“Daten\kv-csv\kv-mitglieder-lat-lon.csv”)

url_content_json = open_url(DATA_URL_JSON)
url_content_df = open_url(DATA_URL_DF)
url_content_df_kv = open_url(DATA_URL_DF_KV)

df = pd.read_csv(url_content_df)
df_kv =pd.read_csv(url_content_df_kv)


If I include the GEOJSON file,


MAP_LOC = [48.88774545, 10.092337] # in this case Ostalbkreis
map_kreisverband = folium.Map(
location=MAP_LOC,
zoom_start=11,
#tiles = ‘Stamen Terrain’,
width=“100%” ,
height=“100%”
)
folium.TileLayer(
name=“county”,
control=True,
).add_to(map_kreisverband)
folium.Choropleth(
geo_data=url_content_json, #<----- the JSON/GEOJSON-File
name=“Anzeige nach Mitglieder je Ort”, #Ostalbkreis Daten
key_on=“feature.properties.GEN”,
data=df,
columns=[“GEN”, “members”],
.
.
.
.


I get the error:


logger.ts:39 [pyscript/base] PythonError: Traceback (most recent call last):
File “/lib/python3.10/asyncio/futures.py”, line 201, in result
raise self._exception
File “/lib/python3.10/asyncio/tasks.py”, line 232, in __step
result = coro.send(None)
File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 506, in eval_code_async
await CodeRunner(
File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 357, in run_async
coroutine = eval(self.code, globals, locals)
File “”, line 38, in
File “/lib/python3.10/site-packages/folium/features.py”, line 1290, in init
self.geojson = GeoJson(
File “/lib/python3.10/site-packages/folium/features.py”, line 497, in init
self.data = self.process_data(data)
File “/lib/python3.10/site-packages/folium/features.py”, line 542, in process_data
raise ValueError(‘Cannot render objects with any missing geometries’
ValueError: Cannot render objects with any missing geometries: <_io.StringIO object at 0x2da3fb0>


As a normal Python script the program runs without errors.

I hope I could make my problem understandable.

Greetings

Walter

Walter, would it be possible to share your full source code? I’m afraid without a testable example, it’s difficult to diagnose what’s happening here. Perhaps in a pastebin?

Hello Jeff, I’ll get to it tomorrow, here in Germany the bed is calling. If it is ok for this forum, I also explain where the whole thing should lead.

Hello Walter, I’m curious about this. Could you give more context as to where your files are located? Also how are you opening and reading them?

I might be wrong, but you received the error ValueError: Cannot render objects with any missing geometries: <_io.StringIO object at 0x2da3fb0> makes me think that perhaps you need to do a .read() in these files (or perhaps pandas failed to read them, but I would be surprised if it didnt throw an exception)

I hope I can make my problem clear, but first the link to the files on Onedrive.

KV-pyscript-01.html is started from inside VS-Code with its server (Go live).

The problem, if I put my JSON data here

folium.Choropleth( geo_data=url_content_json, …, …

I get the error
ValueError: Cannot render objects with any missing geometries: <_io.StringIO object at 0x2a2ef80>.

I suspect it is something along the lines of -->io.StringIO object<–, but I am unfortunately still too inexperienced as far as Python and JS etc are concerned.

I also have a streamlit version of the map display in the directory link above, it works there. If you want to try the streamlit version, you should install the libraries

Pip install streamlit and pip install streamlit_folium

If the Onedrive-link does not work as intended, please let me know.

Briefly about the project, it is a “Citizen Science” project for fine dust measurement / traffic counting.

  • With a Rasberry Zero WH and sensors, various environmental values are recorded.
  • The data will be analyzed and published on the WWW, hence the map display.
  • There are probably 5 measuring stations

Hello,

a small step in the right direction.

If I wrap the geo data in a python class


class MyClass:
json_data = ‘{“type”:“FeatureCollection”,“features”:[{“type”:“Feature”,“properties”:{“ADE”:6,“GF”:4,“BSG”:1,“RS”:“081360045045”,“AGS”:“08136045”,“SDV_RS”: “081360045045”,“GEN”:“Neresheim”,“BEZ”:“Stadt”,“IBZ”:61,“BEM”:“–”,“NBD”:“ja”,“SN_L”:“08”,“SN_R”:1,“SN_K”:36,“SN_V1”:“00”,“SN_V2”:45,“SN_G”:“045”,“FK_S3”:“R”,"N. …]]]}}]}’


and then


geo_from_class = MyClass()

.
.
.
folium.Choropleth(
geo_data=geo_from_class.json_data,


I get the map ( see in the link in the previous post above: pythonmodule\pyscript-folium.py for the complete code) displayed without errors.

Definitely not the best solution so far, but it is one :slight_smile:

Hello again!

I had a look at your example and also looked at the pyodide documentation. The io.StringIO that you get is expected since open_url returns an io.StringIO - see the docs here, so you will need to add a .read to your url_content_json

Something along the lines of:

url_content = open_url(DATA_URL_GEOJSON)
data = url_content.read()

or

url_content_json = open_url(DATA_URL_GEOJSON).read()

Oh and this will return a string, so if you want JSON blob you will need to do something like:

import json
url_content =  open_url(DATA_URL_GEOJSON).read()
url_content_json = json.loads(url_content)

Let me know if this helps :smile:

1 Like

Hallo Fabio,
“.read” did the trick.

As written at the beginning, I am just a beginner, the more a huge thanks to you for your help.

Again what learned, especially in case of problems to consult the “pyodide documentation”.

The problem is solved for me.

That’s great news! Glad that you got it work :smile:

1 Like