notebooks icon indicating copy to clipboard operation
notebooks copied to clipboard

TypeError: Object of type Feature is not JSON serializable при вызове save_geojson(features, ...)

Open AndreiKorchunov opened this issue 7 months ago • 0 comments

Hello. I'm trying to run notebook ..\PlanetLabs\notebooks\jupyter-notebooks\use-cases\crop-segmentation-and-classification\crop-segmentation\1-datasets-identify.ipynb. Everything is executed until the last line

# save ground truth data
save_geojson(features, os.path.join(predata_dir, 'ground-truth.geojson'))

when executed, an error is thrown:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[56], line 8
5 save_geojson(aoi_test, os.path.join(predata_dir, 'aoi-test.geojson'))
7 # save ground truth data
----> 8 save_geojson(features, os.path.join(predata_dir, 'ground-truth.geojson'))

Cell In[55], line 3, in save_geojson(features, filename)
1 def save_geojson(features, filename):
2 with open(filename, "w") as f:
----> 3 f.write(json.dumps(features))

File C:\Python312\Lib\json\__init__.py:231, in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
 226# cached encoder
 227 if (not skipkeys and ensure_ascii and
 228 check_circular and allow_nan and
 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

File C:\Python312\Lib\json\encoder.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:\Python312\Lib\json\encoder.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:\Python312\Lib\json\encoder.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 Feature is not JSON serializable

features were loaded from the code above: features = load_ground_truth(survey_shapefile)

Python 3.12

How to fix it?

AndreiKorchunov avatar Mar 20 '25 07:03 AndreiKorchunov