Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AndreiKorchunov opened this issue Mar 20, 2025 · 0 comments

Comments

@AndreiKorchunov
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant