首页 > 解决方案 > 在 FOLIUM python 中处理缺失的几何图形

问题描述

GeoJSON 来自这里

country_geodf = gpd.read_file("https://datahub.io/core/geo-countries/r/countries.geojson")

我正在尝试使用上述 geoJSON 数据和 Kaggle 的 2021 ML 和 DS 调查数据来创建等值线图。但反复面对这些错误。有没有办法处理丢失的几何图形?

Folium 可以处理缺失值,但不能处理缺失几何。

错误跟踪

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_42/729212434.py in <module>
     16     show=False,
     17     overlay=True,
---> 18     nan_fill_color = "White"
     19 ).add_to(country_map)
     20 

/opt/conda/lib/python3.7/site-packages/folium/features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
   1291                 style_function=style_function,
   1292                 smooth_factor=smooth_factor,
-> 1293                 highlight_function=highlight_function if highlight else None)
   1294 
   1295         self.add_child(self.geojson)

/opt/conda/lib/python3.7/site-packages/folium/features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup, zoom_on_click, marker)
    497         self.marker = marker
    498 
--> 499         self.data = self.process_data(data)
    500 
    501         if self.style or self.highlight:

/opt/conda/lib/python3.7/site-packages/folium/features.py in process_data(self, data)
    543         else:
    544             raise ValueError('Cannot render objects with any missing geometries'
--> 545                              ': {!r}'.format(data))
    546 
    547     def convert_to_feature_collection(self):

ValueError: Cannot render objects with any missing geometries:    ISO_A3     Q3                     ADMIN  \
0     IND  30.18                     India   
1     USA  10.76  United States of America   
2     JPN   3.74                     Japan   
3     CHN   3.30                     China   
4     BRA   3.05                    Brazil   
..    ...    ...                       ...   
59    KAZ   0.18                Kazakhstan   
60    NOR   0.18                    Norway   
61    DZA   0.18                   Algeria   
62    IRQ   0.17                      Iraq   
63    ETH   0.17                  Ethiopia   

                                             geometry  
0   MULTIPOLYGON (((93.85532 7.21418, 93.86500 7.2...  
1   MULTIPOLYGON (((-155.60652 20.13796, -155.5863...  
2   MULTIPOLYGON (((141.46795 24.21377, 141.46315 ...  
3   MULTIPOLYGON (((111.20460 15.77924, 111.19654 ...  
4   MULTIPOLYGON (((-48.54259 -27.81666, -48.55187...  
..                                                ...  
59  MULTIPOLYGON (((50.65952 44.93895, 50.67799 44...  
60  MULTIPOLYGON (((3.38258 -54.44931, 3.36451 -54...  
61  POLYGON ((8.60251 36.93951, 8.60566 36.91305, ...  
62  POLYGON ((42.89674 37.32491, 42.93705 37.32015...  
63  POLYGON ((38.13331 14.67776, 38.19026 14.68732...  

[64 rows x 4 columns]

标签: pythongeojsonfolium

解决方案


推荐阅读