Making choropleth map from dataframe using folium - python

I'm trying to make a choropleth map using pandas to read csv data. I followed a tutorial online, but didn't get my code to work.
Tutorial link: https://towardsdatascience.com/choropleth-maps-with-folium-1a5b8bcdd392. Here is the code i'm trying to get to work:
# CHOLOPLETH MAP
import json
kunnat_geo = r'kunnat.geojson'
coords = pd.read_csv('taulu2.csv')
map_cholo = folium.Map(location=[65,26], zoom_start=4, tiles='stamenwatercolor')
map_cholo.choropleth(
geo_data=kunnat_geo,
data=coords,
columns=['ALUE', 'MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018'],
key_on='features.properties.Kunta',
fill_color='YlGnBu',
fill_opacity=1,
line_opacity=1,
legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
smooth_factor=0)
map_cholo
However it gives me an attribute error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-74-026c8c7bfd66> in <module>
14 line_opacity=1,
15 legend_name='MIELENTERVEYDEN KUNTOUTUSKOTIEN ASIAKKAAT VUONNA 2018',
---> 16 smooth_factor=0)
17
18 map_cholo
D:\Anaconda3\lib\site-packages\folium\folium.py in choropleth(self, *args, **kwargs)
416 )
417 from folium.features import Choropleth
--> 418 self.add_child(Choropleth(*args, **kwargs))
419
420 def keep_in_front(self, *args):
D:\Anaconda3\lib\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)
1249 style_function=style_function,
1250 smooth_factor=smooth_factor,
-> 1251 highlight_function=highlight_function if highlight else None)
1252
1253 self.add_child(self.geojson)
D:\Anaconda3\lib\site-packages\folium\features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup)
456 self.convert_to_feature_collection()
457 if self.style:
--> 458 self._validate_function(style_function, 'style_function')
459 self.style_function = style_function
460 self.style_map = {}
D:\Anaconda3\lib\site-packages\folium\features.py in _validate_function(self, func, name)
521 """
522 test_feature = self.data['features'][0]
--> 523 if not callable(func) or not isinstance(func(test_feature), dict):
524 raise ValueError('{} should be a function that accepts items from '
525 'data[\'features\'] and returns a dictionary.'
D:\Anaconda3\lib\site-packages\folium\features.py in style_function(x)
1223
1224 def style_function(x):
-> 1225 color, opacity = color_scale_fun(x)
1226 return {
1227 'weight': line_weight,
D:\Anaconda3\lib\site-packages\folium\features.py in color_scale_fun(x)
1204
1205 def color_scale_fun(x):
-> 1206 key_of_x = get_by_key(x, key_on)
1207 if key_of_x is None:
1208 raise ValueError("key_on `{!r}` not found in GeoJSON.".format(key_on))
D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
1202 get_by_key(obj.get(key.split('.')[0], None),
-> 1203 '.'.join(key.split('.')[1:])))
1204
1205 def color_scale_fun(x):
D:\Anaconda3\lib\site-packages\folium\features.py in get_by_key(obj, key)
1200 def get_by_key(obj, key):
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
-> 1202 get_by_key(obj.get(key.split('.')[0], None),
1203 '.'.join(key.split('.')[1:])))
1204
AttributeError: 'NoneType' object has no attribute 'get'
Here is the geojson file im using: https://raw.githubusercontent.com/varmais/maakunnat/master/kunnat.geojson
And the data: https://pastebin.com/SdEXDM89

The geojson you are using contains multiple flaws. Just an example, look at
{"id":49,"type":"Feature","geometry":{"coordinates":[[[[25.134789950981,65.0613390595066],[24.9509796014637,65.1411214235741],[24.1149334366251,65.1739425977789],[24.1374948169974,65.2416484128856],[24.6071512739822,65.211492673253],[25.2249108920834,65.2366422852267],[25.319454962569,65.2626286706296],[25.5524286749097,65.2529182323504],[25.6623616596278,65.260748036272],[25.7723806903971,65.2919894627541],[25.9132907485861,65.2931966157892],[26.0266782370841,65.2703735338041],[25.993210256336,65.1914324603082],[25.736352838218,65.2201254797071],[25.5637124399186,65.1919505955019],[25.635197316715,65.1390540421563],[25.6086182968484,65.0816350751541],[25.3775958184099,65.1070832092577],[25.2892903033333,65.0817828240486],[25.134789950981,65.0613390595066]]]],"type":"MultiPolygon"},"properties":{"nationalCode":84,"Country":"FI","name_fi":"Haukipudas","name_se":"Haukipudas"}}
It does not have Kunta in properties. This makes your geojson bad-formed. I suggest you to replace that geojson, or edit it as the very last resort.

Related

GeoDataFrame Value Error: 'data' should be a 1-dimensional array of geometry objects'

I want to quantify some geolocations with osmnx using the nearest_edges-function. I get a value error message when running this code and don't know what I'm doing wrong:
# project graph and points
G_proj = ox.project_graph(G)
gdf_loc_p = gdf_loc["geometry"].to_crs(G_proj.graph["crs"])
ne, d = ox.nearest_edges(
G_proj, X=gdf_loc_p.x.values, Y=gdf_loc_p.y.values, return_dist=True
)
# reindex points based on results from nearest_edges
gdf_loc = (
gdf_loc.set_index(pd.MultiIndex.from_tuples(ne, names=["u", "v", "key"]))
.assign(distance=d)
.sort_index()
)
# join geometry from edges back to points
# aggregate so have number of accidents on each edge
gdf_bad_roads = (
gdf_edges.join(gdf_loc, rsuffix="_loc", how="inner")
.groupby(["u", "v", "key"])
.agg(geometry = ("geometry", "first"), number=("osmid", "size"))
.set_crs(gdf_edges.crs)
)
When running it tells me in the line .agg(geometry)# we require a list, but not a 'str' and from there on couple more issues leading to a value error data' should be a 1-dimensional array of geometry objects. I attached the whole Traceback. Thanks for your help!
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/jy/1f2tlvb965g30zhw9q3cvdw07r5rb_/T/ipykernel_82991/3621029527.py in <module>
2 # aggregate so have number of accidents on each edge
3 gdf_bad_roads = (
----> 4 gdf_edges.join(gdf_loc, rsuffix="_loc", how="inner")
5 .groupby(["u", "v", "key"])
6 .agg(geometry = ("geometry", "first"), number=("osmid", "size"))
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/groupby/generic.py in aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
977
978 op = GroupByApply(self, func, args, kwargs)
--> 979 result = op.agg()
980 if not is_dict_like(func) and result is not None:
981 return result
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/apply.py in agg(self)
159
160 if is_dict_like(arg):
--> 161 return self.agg_dict_like()
162 elif is_list_like(arg):
163 # we require a list, but not a 'str'
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/apply.py in agg_dict_like(self)
457
458 axis = 0 if isinstance(obj, ABCSeries) else 1
--> 459 result = concat(
460 {k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use
461 )
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
312
313 return wrapper
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
305 )
306
--> 307 return op.get_result()
308
309
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/reshape/concat.py in get_result(self)
537
538 cons = sample._constructor
--> 539 return cons(new_data).__finalize__(self, method="concat")
540
541 def _get_result_dim(self) -> int:
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/geodataframe.py in __init__(self, data, geometry, crs, *args, **kwargs)
155 try:
156 if (
--> 157 hasattr(self["geometry"].values, "crs")
158 and self["geometry"].values.crs
159 and crs
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/geodataframe.py in __getitem__(self, key)
1325 GeoDataFrame.
1326 """
-> 1327 result = super().__getitem__(key)
1328 geo_col = self._geometry_column_name
1329 if isinstance(result, Series) and isinstance(result.dtype, GeometryDtype):
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/frame.py in __getitem__(self, key)
3424 if self.columns.is_unique and key in self.columns:
3425 if isinstance(self.columns, MultiIndex):
-> 3426 return self._getitem_multilevel(key)
3427 return self._get_item_cache(key)
3428
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/frame.py in _getitem_multilevel(self, key)
3511 result_columns = maybe_droplevels(new_columns, key)
3512 if self._is_mixed_type:
-> 3513 result = self.reindex(columns=new_columns)
3514 result.columns = result_columns
3515 else:
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
322 #wraps(func)
323 def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 324 return func(*args, **kwargs)
325
326 kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/frame.py in reindex(self, *args, **kwargs)
4770 kwargs.pop("axis", None)
4771 kwargs.pop("labels", None)
-> 4772 return super().reindex(**kwargs)
4773
4774 #deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"])
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/generic.py in reindex(self, *args, **kwargs)
4816
4817 # perform the reindex on the axes
-> 4818 return self._reindex_axes(
4819 axes, level, limit, tolerance, method, fill_value, copy
4820 ).__finalize__(self, method="reindex")
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/frame.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
4589 columns = axes["columns"]
4590 if columns is not None:
-> 4591 frame = frame._reindex_columns(
4592 columns, method, copy, level, fill_value, limit, tolerance
4593 )
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/frame.py in _reindex_columns(self, new_columns, method, copy, level, fill_value, limit, tolerance)
4634 new_columns, method=method, level=level, limit=limit, tolerance=tolerance
4635 )
-> 4636 return self._reindex_with_indexers(
4637 {1: [new_columns, indexer]},
4638 copy=copy,
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/pandas/core/generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
4895 new_data = new_data.copy()
4896
-> 4897 return self._constructor(new_data).__finalize__(self)
4898
4899 def filter(
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/geodataframe.py in __init__(self, data, geometry, crs, *args, **kwargs)
162 _crs_mismatch_warning()
163 # TODO: raise error in 0.9 or 0.10.
--> 164 self["geometry"] = _ensure_geometry(self["geometry"].values, crs)
165 except TypeError:
166 pass
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/geodataframe.py in _ensure_geometry(data, crs)
44 return GeoSeries(out, index=data.index, name=data.name)
45 else:
---> 46 out = from_shapely(data, crs=crs)
47 return out
48
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/array.py in from_shapely(data, crs)
149
150 """
--> 151 return GeometryArray(vectorized.from_shapely(data), crs=crs)
152
153
~/opt/anaconda3/envs/pyproj_env/lib/python3.10/site-packages/geopandas/array.py in __init__(self, data, crs)
278 )
279 elif not data.ndim == 1:
--> 280 raise ValueError(
281 "'data' should be a 1-dimensional array of geometry objects."
282 )
ValueError: 'data' should be a 1-dimensional array of geometry objects.
Edit: thank you! Unfortunately it doesnt work. I downgraded Python to 3.9 (and upgraded Panda to 1.4 but have same issue). I added the Traceback of the other code as well.
----
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [4], in <cell line: 4>()
2 gdf_bad_roads = gdf_edges.join(gdf_loc, rsuffix="_loc", how="inner")
3 # aggregate so have number of accidents on each edge
----> 4 gdf_bad_roads_agg = gdf_bad_roads.groupby(["u", "v", "key"]).agg(
5 geometry=("geometry", "first"), number=("osmid", "size")
6 ).set_crs(gdf_edges.crs)
8 print(f"""
9 pandas: {pd.__version__}
10 geopandas: {gpd.__version__}
11 osmnx: {ox.__version__}""")
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/groupby/generic.py:869, in DataFrameGroupBy.aggregate(self, func, engine, engine_kwargs, *args, **kwargs)
866 func = maybe_mangle_lambdas(func)
868 op = GroupByApply(self, func, args, kwargs)
--> 869 result = op.agg()
870 if not is_dict_like(func) and result is not None:
871 return result
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/apply.py:168, in Apply.agg(self)
165 return self.apply_str()
167 if is_dict_like(arg):
--> 168 return self.agg_dict_like()
169 elif is_list_like(arg):
170 # we require a list, but not a 'str'
171 return self.agg_list_like()
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/apply.py:498, in Apply.agg_dict_like(self)
495 keys_to_use = ktu
497 axis = 0 if isinstance(obj, ABCSeries) else 1
--> 498 result = concat(
499 {k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use
500 )
501 elif any(is_ndframe):
502 # There is a mix of NDFrames and scalars
503 raise ValueError(
504 "cannot perform both aggregation "
505 "and transformation operations "
506 "simultaneously"
507 )
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/util/_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
305 if len(args) > num_allow_args:
306 warnings.warn(
307 msg.format(arguments=arguments),
308 FutureWarning,
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/reshape/concat.py:359, in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
155 """
156 Concatenate pandas objects along a particular axis with optional set logic
157 along the other axes.
(...)
344 ValueError: Indexes have overlapping values: ['a']
345 """
346 op = _Concatenator(
347 objs,
348 axis=axis,
(...)
356 sort=sort,
357 )
--> 359 return op.get_result()
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/reshape/concat.py:599, in _Concatenator.get_result(self)
596 new_data._consolidate_inplace()
598 cons = sample._constructor
--> 599 return cons(new_data).__finalize__(self, method="concat")
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/geodataframe.py:157, in GeoDataFrame.__init__(self, data, geometry, crs, *args, **kwargs)
154 index = self.index
155 try:
156 if (
--> 157 hasattr(self["geometry"].values, "crs")
158 and self["geometry"].values.crs
159 and crs
160 and not self["geometry"].values.crs == crs
161 ):
162 _crs_mismatch_warning()
163 # TODO: raise error in 0.9 or 0.10.
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/geodataframe.py:1327, in GeoDataFrame.__getitem__(self, key)
1321 def __getitem__(self, key):
1322 """
1323 If the result is a column containing only 'geometry', return a
1324 GeoSeries. If it's a DataFrame with a 'geometry' column, return a
1325 GeoDataFrame.
1326 """
-> 1327 result = super().__getitem__(key)
1328 geo_col = self._geometry_column_name
1329 if isinstance(result, Series) and isinstance(result.dtype, GeometryDtype):
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/frame.py:3473, in DataFrame.__getitem__(self, key)
3471 if self.columns.is_unique and key in self.columns:
3472 if isinstance(self.columns, MultiIndex):
-> 3473 return self._getitem_multilevel(key)
3474 return self._get_item_cache(key)
3476 # Do we have a slicer (on rows)?
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/frame.py:3560, in DataFrame._getitem_multilevel(self, key)
3558 result_columns = maybe_droplevels(new_columns, key)
3559 if self._is_mixed_type:
-> 3560 result = self.reindex(columns=new_columns)
3561 result.columns = result_columns
3562 else:
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/util/_decorators.py:324, in rewrite_axis_style_signature.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
322 #wraps(func)
323 def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 324 return func(*args, **kwargs)
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/frame.py:4798, in DataFrame.reindex(self, *args, **kwargs)
4796 kwargs.pop("axis", None)
4797 kwargs.pop("labels", None)
-> 4798 return super().reindex(**kwargs)
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/generic.py:4974, in NDFrame.reindex(self, *args, **kwargs)
4971 return self._reindex_multi(axes, copy, fill_value)
4973 # perform the reindex on the axes
-> 4974 return self._reindex_axes(
4975 axes, level, limit, tolerance, method, fill_value, copy
4976 ).__finalize__(self, method="reindex")
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/frame.py:4611, in DataFrame._reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
4609 columns = axes["columns"]
4610 if columns is not None:
-> 4611 frame = frame._reindex_columns(
4612 columns, method, copy, level, fill_value, limit, tolerance
4613 )
4615 index = axes["index"]
4616 if index is not None:
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/frame.py:4656, in DataFrame._reindex_columns(self, new_columns, method, copy, level, fill_value, limit, tolerance)
4643 def _reindex_columns(
4644 self,
4645 new_columns,
(...)
4651 tolerance=None,
4652 ):
4653 new_columns, indexer = self.columns.reindex(
4654 new_columns, method=method, level=level, limit=limit, tolerance=tolerance
4655 )
-> 4656 return self._reindex_with_indexers(
4657 {1: [new_columns, indexer]},
4658 copy=copy,
4659 fill_value=fill_value,
4660 allow_dups=False,
4661 )
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/pandas/core/generic.py:5054, in NDFrame._reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
5051 if copy and new_data is self._mgr:
5052 new_data = new_data.copy()
-> 5054 return self._constructor(new_data).__finalize__(self)
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/geodataframe.py:164, in GeoDataFrame.__init__(self, data, geometry, crs, *args, **kwargs)
162 _crs_mismatch_warning()
163 # TODO: raise error in 0.9 or 0.10.
--> 164 self["geometry"] = _ensure_geometry(self["geometry"].values, crs)
165 except TypeError:
166 pass
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/geodataframe.py:46, in _ensure_geometry(data, crs)
44 return GeoSeries(out, index=data.index, name=data.name)
45 else:
---> 46 out = from_shapely(data, crs=crs)
47 return out
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/array.py:151, in from_shapely(data, crs)
135 def from_shapely(data, crs=None):
136 """
137 Convert a list or array of shapely objects to a GeometryArray.
138
(...)
149
150 """
--> 151 return GeometryArray(vectorized.from_shapely(data), crs=crs)
File ~/opt/anaconda3/envs/pyproj_env/lib/python3.9/site-packages/geopandas/array.py:280, in GeometryArray.__init__(self, data, crs)
275 raise TypeError(
276 "'data' should be array of geometry objects. Use from_shapely, "
277 "from_wkb, from_wkt functions to construct a GeometryArray."
278 )
279 elif not data.ndim == 1:
--> 280 raise ValueError(
281 "'data' should be a 1-dimensional array of geometry objects."
282 )
283 self.data = data
285 self._crs = None
ValueError: 'data' should be a 1-dimensional array of geometry objects.
pandas: 1.4.1
geopandas: 0.10.2
osmnx: 1.1.2
have changed this to a MWE
have separated out join() and groupby() / agg()
have included versions
one difference I can see - python 3.9 vs 3.10
import osmnx as ox
import geopandas as gpd
import pandas as pd
import io
df = pd.read_csv(
io.StringIO(
"""AccidentUID,AccidentLocation_CHLV95_E,AccidentLocation_CHLV95_N
99BA5D383B96D02AE0430A865E33D02A,2663985,1213215
9B25C4871C909022E0430A865E339022,2666153,1211303
9B71AB601D948092E0430A865E338092,2666168,1211785
9C985CF7710A60C0E0430A865E3360C0,2663991,1213203
9EA9548660AB3002E0430A865E333002,2666231,1210786
9B2E8B25D5C29094E0430A865E339094,2666728,1210404
9C87C10FB73A905EE0430A865E33905E,2666220,1211811
9E30F39D35CA1058E0430A865E331058,2664599,1212960
9BC2EA43E0BFC068E0430A865E33C068,2665533,1212617
9C0BB9332AB30044E0430A865E330044,2666852,1211964"""
)
)
gdf_loc = gpd.GeoDataFrame(
data=df,
geometry=gpd.points_from_xy(
df["AccidentLocation_CHLV95_E"], df["AccidentLocation_CHLV95_N"]
),
crs="EPSG:2056",
).to_crs("epsg:4326")
# get OSM data for investigated location
G = ox.graph_from_place("Luzern, Switzerland", network_type="drive")
G_proj = ox.project_graph(G)
gdf_nodes, gdf_edges = ox.utils_graph.graph_to_gdfs(G_proj)
# project graph and points
gdf_loc_p = gdf_loc["geometry"].to_crs(G_proj.graph["crs"])
ne, d = ox.nearest_edges(
G_proj, X=gdf_loc_p.x.values, Y=gdf_loc_p.y.values, return_dist=True
)
# reindex points based on results from nearest_edges
gdf_loc = (
gdf_loc.set_index(pd.MultiIndex.from_tuples(ne, names=["u", "v", "key"]))
.assign(distance=d)
.sort_index()
)
# join geometry from edges back to points
gdf_bad_roads = gdf_edges.join(gdf_loc, rsuffix="_loc", how="inner")
# aggregate so have number of accidents on each edge
gdf_bad_roads_agg = gdf_bad_roads.groupby(["u", "v", "key"]).agg(
geometry=("geometry", "first"), number=("osmid", "size")
).set_crs(gdf_edges.crs)
print(f"""
pandas: {pd.__version__}
geopandas: {gpd.__version__}
osmnx: {ox.__version__}""")
pandas: 1.4.0
geopandas: 0.10.2
osmnx: 1.1.2
Alternative aggregate syntax. Has been confirmed both work
hence conclusion is that named aggregations are failing. Possibly should be raised as an issue on pandas, but is not failing on all environments
groupby()/apply() is doing a first on shared edges and also necessary to set CRS again
dissolve() is doing a unary union on geometries. Conceptually should be the same, but is giving slightly different geometry. (A unary union of identical geometries IMHO is an instance of one of the geometries)
gdf_bad_roads.groupby(["u", "v", "key"]).agg({"geometry":"first", "AccidentUID":"size"}).set_crs(gdf_edges.crs).explore(color="blue")
gdf_bad_roads.dissolve(["u", "v", "key"], aggfunc={"AccidentUID":"size"}).explore(color="blue")

How can I resolve all these errors from folium?

I am trying to create a map following this tutorial. I am using almost the exact same code as in the tutorial, but get a series I am unsure how to begin resolving, anyone have any thoughts on how I could start resolving?
Here is my code below. I think my table merge worked okay because it appears correctly when I print the table, but I think things start going downhill with the choropleth function.
'''
import geopandas
import pandas as pd
import folium
url = 'https://en.wikipedia.org/wiki/List_of_countries_by_meat_consumption'
tables = pd.read_html(url)
table = tables[0]
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
table = world.merge(table, how='left', left_on=['name'], right_on=['Country'])
pd.options.display.width = 4000
pd.set_option('display.max_rows', 188)
table = table.dropna(subset=['kg/person (2002)[9][note 1]'])
meat_map = folium.Map()
folium.Choropleth(
geo_data=table,
name='choropleth',
data=table,
columns=['Country', 'kg/person (2002)[9][note 1]'],
key_on ='feature.properties.name',
fill_color ='YlGnBu',
fill_opacity = 0.7,
line_opacity = 0.2,
legend_name = "Meat consumption in kg/person"
).add_to(meat_map)
here is my error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-b9be599a18fa> in <module>
10 meat_map = folium.Map()
11
---> 12 folium.Choropleth(
13 geo_data=table,
14 name='choropleth',
~\anaconda3\envs\geo_env\lib\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)
1287 smooth_factor=smooth_factor)
1288 else:
-> 1289 self.geojson = GeoJson(
1290 geo_data,
1291 style_function=style_function,
~\anaconda3\envs\geo_env\lib\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:
~\anaconda3\envs\geo_env\lib\site-packages\folium\features.py in process_data(self, data)
540 if hasattr(data, 'to_crs'):
541 data = data.to_crs('EPSG:4326')
--> 542 return json.loads(json.dumps(data.__geo_interface__))
543 else:
544 raise ValueError('Cannot render objects with any missing geometries'
~\anaconda3\envs\geo_env\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder
~\anaconda3\envs\geo_env\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\anaconda3\envs\geo_env\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
~\anaconda3\envs\geo_env\lib\json\encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type MultiPolygon is not JSON serializable
'''

What is causing a circular reference when plotting a map using ArcGIS API for Python?

Following the documentation for the ArcGIS API for Python, I'm attempting to plot coordinates from a spatially enabled dataframe. Perhaps my understanding of a circular reference is off, but I can't see how the object would be referencing itself.
import pandas as pd
from arcgis.gis import GIS
## importing geocoded data from pickle
prop_sdf = pd.read_pickle("./recent_geocoded.pkl")
prop_sdf = pd.DataFrame.spatial.from_xy(prop_sdf, 'lat','long')
prop_sdf.head()
This is what my resulting dataframe looks like:
unique_id lat long
0 43432884 41.701011 -70.019244
2 43400770 41.641784 -70.366659
3 43425636 41.701954 -70.146602
4 43427274 41.720506 -70.021849
5 43427818 41.649288 -70.490767
I'm able to render the interactive map
m1 = GIS().map("United States")
m1.zoom = 4
m1.center = [39,-98]
But hit an error when plotting:
prop_sdf.spatial.plot(map_widget=m1)
m1
Trace:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-30-d48a9f223ff4> in <module>
----> 1 prop_sdf.spatial.plot(map_widget=m1)
2 m1
~\anaconda3\lib\site-packages\arcgis\features\geo\_accessor.py in plot(self, map_widget, **kwargs)
2002 self._data.columns = [c.replace(" ", "_") for c in self._data.columns]
2003 # plot and be merry
-> 2004 _plot_map_widget(map_widget)
2005 self._data.columns = orig_col
2006 return True
~\anaconda3\lib\site-packages\arcgis\features\geo\_accessor.py in _plot_map_widget(mp_wdgt)
1984 colors=kwargs.pop('cmap', None) or kwargs.pop('colors', None) or kwargs.pop('pallette', 'jet'),
1985 alpha=kwargs.pop('alpha', 1),
-> 1986 **kwargs)
1987
1988 # small helper to address zoom level
~\anaconda3\lib\site-packages\arcgis\features\geo\_viz\mapping.py in plot(df, map_widget, name, renderer_type, symbol_type, symbol_style, col, colors, alpha, **kwargs)
191 fc.layer['layerDefinition']['drawingInfo']['renderer'] = renderer
192 if map_exists:
--> 193 map_widget.add_layer(fc, options={'title':name})
194 else:
195 map_widget.add_layer(fc, options={'title':name})
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in add_layer(self, item, options)
1051 _is_geoenabled(item):
1052 item = item.spatial.to_feature_collection()
-> 1053 self._add_layer_to_widget(item, options)
1054
1055 def _add_layer_to_webmap(self, item, options):
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in _add_layer_to_widget(self, item, options)
1091 # (i.e., do what was done for ImageryLayer for all major Layers)
1092 # 'No type' layer just means that we'll figure it out at JS time
-> 1093 _lyr = _make_jsonable_dict(item._lyr_json)
1094 if ('type' in _lyr and \
1095 _lyr['type'] == 'MapImageLayer') and \
~\anaconda3\lib\site-packages\arcgis\widgets\_mapview\_mapview.py in _make_jsonable_dict(obj)
60 will later be used to delete all values with this value"""
61 return flag
---> 62 dict_ = json.loads(json.dumps(obj, default=default_func))
63 return { k:v for k, v in dict_.items() if v != flag }
64
~\anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240
~\anaconda3\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
ValueError: Circular reference detected

Basic on installation: PyAssertion error

%pylab
x = linspace(-10., 10., 1000)
plot(x, sin(x))
PyAssertionError Traceback (most recent call last)
<ipython-input-41-c9b44dd20001> in <module>()
----> 1 plot(x, sin(x))
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in plot(*args, **kwargs)
3305 #_autogen_docstring(Axes.plot)
3306 def plot(*args, **kwargs):
-> 3307 ax = gca()
3308 # Deprecated: allow callers to override the hold state
3309 # by passing hold=True|False
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in gca(**kwargs)
948 matplotlib.figure.Figure.gca : The figure's gca method.
949 """
--> 950 return gcf().gca(**kwargs)
951
952 # More ways of creating axes:
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in gcf()
584 return figManager.canvas.figure
585 else:
--> 586 return figure()
587
588
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
533 frameon=frameon,
534 FigureClass=FigureClass,
--> 535 **kwargs)
536
537 if figLabel:
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wxagg.pyc in new_figure_manager(num, *args, **kwargs)
114 fig = FigureClass(*args, **kwargs)
115
--> 116 return new_figure_manager_given_figure(num, fig)
117
118
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wxagg.pyc in new_figure_manager_given_figure(num, figure)
121 Create a new figure manager instance for the given figure.
122 """
--> 123 frame = FigureFrameWxAgg(num, figure)
124 figmgr = frame.get_figure_manager()
125 if matplotlib.is_interactive():
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wx.pyc in __init__(self, num, fig)
1262 # of the frame - so appearance is closer to GTK version
1263
-> 1264 self.toolbar = self._get_toolbar(statbar)
1265
1266 if self.toolbar is not None:
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wxagg.pyc in _get_toolbar(self, statbar)
26 def _get_toolbar(self, statbar):
27 if matplotlib.rcParams['toolbar'] == 'toolbar2':
---> 28 toolbar = NavigationToolbar2WxAgg(self.canvas)
29 toolbar.set_status_bar(statbar)
30 else:
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wx.pyc in __init__(self, canvas)
1569 def __init__(self, canvas):
1570 wx.ToolBar.__init__(self, canvas.GetParent(), -1)
-> 1571 NavigationToolbar2.__init__(self, canvas)
1572 self.canvas = canvas
1573 self._idle = True
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backend_bases.pyc in __init__(self, canvas)
2750 self._active = None
2751 self._lastCursor = None
-> 2752 self._init_toolbar()
2753 self._idDrag = self.canvas.mpl_connect(
2754 'motion_notify_event', self.mouse_move)
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wx.pyc in _init_toolbar(self)
1595 self.wx_ids[text] = wx.NewId()
1596 wxc._AddTool(self, self.wx_ids, text,
-> 1597 _load_bitmap(image_file + '.png'),
1598 tooltip_text)
1599
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\backends\backend_wx.pyc in _load_bitmap(filename)
1423 raise IOError('Could not find bitmap file "%s"; dying' % bmpFilename)
1424
-> 1425 bmp = wx.Bitmap(bmpFilename)
1426 return bmp
1427
C:\Users\RD\AppData\Local\Enthought\Canopy\User\lib\site-packages\wx\_gdi.pyc in __init__(self, *args, **kwargs)
646 Loads a bitmap from a file.
647 """
--> 648 _gdi_.Bitmap_swiginit(self,_gdi_.new_Bitmap(*args, **kwargs))
649 __swig_destroy__ = _gdi_.delete_Bitmap
650 __del__ = lambda self : None;
PyAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1449) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!
I am trying to plot the results.How can I plot the results? I am getting the PyAssertion error. How can I solve the error?

Cannot Load From HDF Data Store

I am using pandas 0.11.0 on Windows 64 Bit.
I have a datastore:
store = pandas.io.pytables.HDFStore( r"""C:\data.h5""" )
It has some data in it:
print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: C:\data.h5
/region frame (shape->[30,2])
/sector frame (shape->[116,2])
When I try to load 'sector' from the store I get:
store['/sector']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-7fea069742ed> in <module>()
----> 1 store['/sector']
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in __getitem__(self, key)
236
237 def __getitem__(self, key):
--> 238 return self.get(key)
239
240 def __setitem__(self, key, value):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in get(self, key)
369 if group is None:
370 raise KeyError('No object named %s in the file' % key)
--> 371 return self._read_group(group)
372
373 def select(self, key, where=None, start=None, stop=None, columns=None, iterator=False, chunksize=None, **kwargs):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in _read_group(self, group, **kwargs)
876 s = self._create_storer(group)
877 s.infer_axes()
--> 878 return s.read(**kwargs)
879
880 class TableIterator(object):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read(self, **kwargs)
1982 blocks = []
1983 for i in range(self.nblocks):
-> 1984 blk_items = self.read_index('block%d_items' % i)
1985 values = self.read_array('block%d_values' % i)
1986 blk = make_block(values, blk_items, items)
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read_index(self, key)
1615 return self.read_sparse_intindex(key)
1616 elif variety == 'regular':
-> 1617 _, index = self.read_index_node(getattr(self.group, key))
1618 return index
1619 else: # pragma: no cover
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read_index_node(self, node)
1733 **kwargs)
1734 else:
-> 1735 index = factory(_unconvert_index(data, kind), **kwargs)
1736
1737 index.name = name
TypeError: __new__() got an unexpected keyword argument 'freq'
What is this "freq" argument? And why is it stopping me from loading the data?

Categories