I am creating an interactive map in Folium.
I am trying to set a Custom icon from my local disc (.svg or .png).
### Filepaths
fp1 = r"filepath.shp"
#Specify icon filepath
icon_home = folium.CustomIcon("house-svgrepo-com.svg")
#Read a file:
points = gpd.read_file(fp1)
#Create an empty map
map = folium.Map(
location=[53.23, 23.15],
# tiles="cartodb positron",
zoom_start=12,
control_scale=True)
#Add a point layer to a map
points_gjson = folium.features.GeoJson(
points,
icon=icon_home,
name="Near municipalities").add_to(map)
map.save(outputfilepath.html)
map
But during trying to accomplish this I receive an error:
File c:UsersKuba.condaenvsiFOSLibsite-packagesfoliumelements.py:25, in JSCSSMixin.render(self, **kwargs)
22 for name, url in self.default_css:
23 figure.header.add_child(CssLink(url), name=name)
---> 25 super().render(**kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesbrancaelement.py:685, in MacroElement.render(self, **kwargs)
682 figure.script.add_child(Element(script(self, kwargs)), name=self.get_name())
684 for name, element in self._children.items():
--> 685 element.render(**kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesfoliumfeatures.py:803, in GeoJson.render(self, **kwargs)
801 if self.highlight:
802 self.highlight_map = mapper.get_highlight_map(self.highlight_function)
--> 803 super().render()
File c:UsersKuba.condaenvsiFOSLibsite-packagesfoliummap.py:62, in Layer.render(self, **kwargs)
54 if self.show:
55 self.add_child(
56 ElementAddToElement(
57 element_name=self.get_name(),
(...)
60 name=self.get_name() + "_add",
61 )
---> 62 super().render(**kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesbrancaelement.py:682, in MacroElement.render(self, **kwargs)
680 script = self._template.module.__dict__.get("script", None)
681 if script is not None:
--> 682 figure.script.add_child(Element(script(self, kwargs)), name=self.get_name())
684 for name, element in self._children.items():
685 element.render(**kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesjinja2runtime.py:763, in Macro.__call__(self, *args, **kwargs)
757 elif len(args) > self._argument_count:
758 raise TypeError(
759 f"macro {self.name!r} takes not more than"
760 f" {len(self.arguments)} argument(s)"
761 )
--> 763 return self._invoke(arguments, autoescape)
File c:UsersKuba.condaenvsiFOSLibsite-packagesjinja2runtime.py:777, in Macro._invoke(self, arguments, autoescape)
774 if self._environment.is_async:
775 return self._async_invoke(arguments, autoescape) # type: ignore
--> 777 rv = self._func(*arguments)
779 if autoescape:
780 rv = Markup(rv)
File <template>:230, in macro(l_1_this, l_1_kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesjinja2filters.py:1688, in do_tojson(eval_ctx, value, indent)
1685 kwargs = kwargs.copy()
1686 kwargs["indent"] = indent
-> 1688 return htmlsafe_json_dumps(value, dumps=dumps, **kwargs)
File c:UsersKuba.condaenvsiFOSLibsite-packagesjinja2utils.py:658, in htmlsafe_json_dumps(obj, dumps, **kwargs)
654 if dumps is None:
655 dumps = json.dumps
657 return markupsafe.Markup(
--> 658 dumps(obj, **kwargs)
659 .replace("<", "\u003c")
660 .replace(">", "\u003e")
661 .replace("&", "\u0026")
662 .replace("'", "\u0027")
663 )
File c:UsersKuba.condaenvsiFOSLibjson__init__.py:238, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
232 if cls is None:
233 cls = JSONEncoder
234 return cls(
235 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
File c:UsersKuba.condaenvsiFOSLibjsonencoder.py:200, in JSONEncoder.encode(self, o)
196 return encode_basestring(o)
197 # This doesn't pass the iterator directly to ''.join() because the
198 # exceptions aren't as detailed. The list call should be roughly
199 # equivalent to the PySequence_Fast that ''.join() would do.
--> 200 chunks = self.iterencode(o, _one_shot=True)
201 if not isinstance(chunks, (list, tuple)):
202 chunks = list(chunks)
File c:UsersKuba.condaenvsiFOSLibjsonencoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot)
253 else:
254 _iterencode = _make_iterencode(
255 markers, self.default, _encoder, self.indent, floatstr,
256 self.key_separator, self.item_separator, self.sort_keys,
257 self.skipkeys, _one_shot)
--> 258 return _iterencode(o, 0)
File c:UsersKuba.condaenvsiFOSLibjsonencoder.py:180, in JSONEncoder.default(self, o)
161 def default(self, o):
162 """Implement this method in a subclass such that it returns
163 a serializable object for ``o``, or calls the base implementation
164 (to raise a ``TypeError``).
(...)
178
179 """
--> 180 raise TypeError(f'Object of type {o .__class__.__name__} '
181 f'is not JSON serializable')
**TypeError: Object of type CustomIcon is not JSON serializable**
The error:
TypeError: Object of type CustomIcon is not JSON serializable
I tried different filepaths “pathfilename”/”filename”, used the different icon formats, but could not find a similar solution.
Related issues:
https://python-visualization.github.io/folium/latest/user_guide/ui_elements/icons.html