Skip to content

Commit eebfe89

Browse files
authored
Merge pull request #269 from stac-utils/fix/rde/268
Fix issue with projection extension setters handling None values.
2 parents ad06c61 + 4c14300 commit eebfe89

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fix handling of optional properties when using apply on view extension ([#259](https://github.com/stac-utils/pystac/pull/259))
8+
- Fixed issue with setting None into projection extension fields that are not required breaking validation ([#269](https://github.com/stac-utils/pystac/pull/269))
89

910
### Changed
1011

pystac/extensions/projection.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ def get_wkt2(self, asset=None):
141141
else:
142142
return asset.properties.get('proj:wkt2')
143143

144-
def set_wkt2(self, wkt2, asset=None):
144+
def set_wkt2(self, value, asset=None):
145145
"""Set an Item or an Asset wkt2.
146146
147147
If an Asset is supplied, sets the property on the Asset.
148148
Otherwise sets the Item's value.
149149
"""
150-
if asset is None:
151-
self.item.properties['proj:wkt2'] = wkt2
150+
key = 'proj:wkt2'
151+
target = self.item.properties if asset is None else asset.properties
152+
if value is None:
153+
target.pop(key, None)
152154
else:
153-
asset.properties['proj:wkt2'] = wkt2
155+
target[key] = value
154156

155157
@property
156158
def projjson(self):
@@ -188,16 +190,18 @@ def get_projjson(self, asset=None):
188190
else:
189191
return asset.properties.get('proj:projjson')
190192

191-
def set_projjson(self, projjson, asset=None):
193+
def set_projjson(self, value, asset=None):
192194
"""Set an Item or an Asset projjson.
193195
194196
If an Asset is supplied, sets the property on the Asset.
195197
Otherwise sets the Item's value.
196198
"""
197-
if asset is None:
198-
self.item.properties['proj:projjson'] = projjson
199+
key = 'proj:projjson'
200+
target = self.item.properties if asset is None else asset.properties
201+
if value is None:
202+
target.pop(key, None)
199203
else:
200-
asset.properties['proj:projjson'] = projjson
204+
target[key] = value
201205

202206
@property
203207
def geometry(self):
@@ -233,16 +237,18 @@ def get_geometry(self, asset=None):
233237
else:
234238
return asset.properties.get('proj:geometry')
235239

236-
def set_geometry(self, geometry, asset=None):
240+
def set_geometry(self, value, asset=None):
237241
"""Set an Item or an Asset projection geometry.
238242
239243
If an Asset is supplied, sets the property on the Asset.
240244
Otherwise sets the Item's value.
241245
"""
242-
if asset is None:
243-
self.item.properties['proj:geometry'] = geometry
246+
key = 'proj:geometry'
247+
target = self.item.properties if asset is None else asset.properties
248+
if value is None:
249+
target.pop(key, None)
244250
else:
245-
asset.properties['proj:geometry'] = geometry
251+
target[key] = value
246252

247253
@property
248254
def bbox(self):
@@ -279,16 +285,18 @@ def get_bbox(self, asset=None):
279285
else:
280286
return asset.properties.get('proj:bbox')
281287

282-
def set_bbox(self, bbox, asset=None):
288+
def set_bbox(self, value, asset=None):
283289
"""Set an Item or an Asset projection bbox.
284290
285291
If an Asset is supplied, sets the property on the Asset.
286292
Otherwise sets the Item's value.
287293
"""
288-
if asset is None:
289-
self.item.properties['proj:bbox'] = bbox
294+
key = 'proj:bbox'
295+
target = self.item.properties if asset is None else asset.properties
296+
if value is None:
297+
target.pop(key, None)
290298
else:
291-
asset.properties['proj:bbox'] = bbox
299+
target[key] = value
292300

293301
@property
294302
def centroid(self):
@@ -324,16 +332,18 @@ def get_centroid(self, asset=None):
324332
else:
325333
return asset.properties.get('proj:centroid')
326334

327-
def set_centroid(self, centroid, asset=None):
335+
def set_centroid(self, value, asset=None):
328336
"""Set an Item or an Asset centroid.
329337
330338
If an Asset is supplied, sets the property on the Asset.
331339
Otherwise sets the Item's value.
332340
"""
333-
if asset is None:
334-
self.item.properties['proj:centroid'] = centroid
341+
key = 'proj:centroid'
342+
target = self.item.properties if asset is None else asset.properties
343+
if value is None:
344+
target.pop(key, None)
335345
else:
336-
asset.properties['proj:centroid'] = centroid
346+
target[key] = value
337347

338348
@property
339349
def shape(self):
@@ -367,16 +377,18 @@ def get_shape(self, asset=None):
367377
else:
368378
return asset.properties.get('proj:shape')
369379

370-
def set_shape(self, shape, asset=None):
380+
def set_shape(self, value, asset=None):
371381
"""Set an Item or an Asset shape.
372382
373383
If an Asset is supplied, sets the property on the Asset.
374384
Otherwise sets the Item's value.
375385
"""
376-
if asset is None:
377-
self.item.properties['proj:shape'] = shape
386+
key = 'proj:shape'
387+
target = self.item.properties if asset is None else asset.properties
388+
if value is None:
389+
target.pop(key, None)
378390
else:
379-
asset.properties['proj:shape'] = shape
391+
target[key] = value
380392

381393
@property
382394
def transform(self):
@@ -413,16 +425,18 @@ def get_transform(self, asset=None):
413425
else:
414426
return asset.properties.get('proj:transform')
415427

416-
def set_transform(self, transform, asset=None):
428+
def set_transform(self, value, asset=None):
417429
"""Set an Item or an Asset transform.
418430
419431
If an Asset is supplied, sets the property on the Asset.
420432
Otherwise sets the Item's value.
421433
"""
422-
if asset is None:
423-
self.item.properties['proj:transform'] = transform
434+
key = 'proj:transform'
435+
target = self.item.properties if asset is None else asset.properties
436+
if value is None:
437+
target.pop(key, None)
424438
else:
425-
asset.properties['proj:transform'] = transform
439+
target[key] = value
426440

427441
@classmethod
428442
def _object_links(cls):

tests/extensions/test_projection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ def test_apply(self):
9797
shape=[100, 100],
9898
transform=[30.0, 0.0, 224985.0, 0.0, -30.0, 6790215.0, 0.0, 0.0, 1.0])
9999

100+
def test_partial_apply(self):
101+
proj_item = pystac.read_file(self.example_uri)
102+
103+
proj_item.ext.projection.apply(epsg=1111)
104+
105+
self.assertEqual(proj_item.ext.projection.epsg, 1111)
106+
proj_item.validate()
107+
100108
def test_validate_proj(self):
101109
item = pystac.read_file(self.example_uri)
102110
item.validate()

0 commit comments

Comments
 (0)