Skip to content

Commit

Permalink
Merge pull request #269 from stac-utils/fix/rde/268
Browse files Browse the repository at this point in the history
Fix issue with projection extension setters handling None values.
  • Loading branch information
lossyrob authored Mar 3, 2021
2 parents ad06c61 + 4c14300 commit eebfe89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

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

### Changed

Expand Down
70 changes: 42 additions & 28 deletions pystac/extensions/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ def get_wkt2(self, asset=None):
else:
return asset.properties.get('proj:wkt2')

def set_wkt2(self, wkt2, asset=None):
def set_wkt2(self, value, asset=None):
"""Set an Item or an Asset wkt2.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:wkt2'] = wkt2
key = 'proj:wkt2'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:wkt2'] = wkt2
target[key] = value

@property
def projjson(self):
Expand Down Expand Up @@ -188,16 +190,18 @@ def get_projjson(self, asset=None):
else:
return asset.properties.get('proj:projjson')

def set_projjson(self, projjson, asset=None):
def set_projjson(self, value, asset=None):
"""Set an Item or an Asset projjson.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:projjson'] = projjson
key = 'proj:projjson'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:projjson'] = projjson
target[key] = value

@property
def geometry(self):
Expand Down Expand Up @@ -233,16 +237,18 @@ def get_geometry(self, asset=None):
else:
return asset.properties.get('proj:geometry')

def set_geometry(self, geometry, asset=None):
def set_geometry(self, value, asset=None):
"""Set an Item or an Asset projection geometry.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:geometry'] = geometry
key = 'proj:geometry'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:geometry'] = geometry
target[key] = value

@property
def bbox(self):
Expand Down Expand Up @@ -279,16 +285,18 @@ def get_bbox(self, asset=None):
else:
return asset.properties.get('proj:bbox')

def set_bbox(self, bbox, asset=None):
def set_bbox(self, value, asset=None):
"""Set an Item or an Asset projection bbox.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:bbox'] = bbox
key = 'proj:bbox'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:bbox'] = bbox
target[key] = value

@property
def centroid(self):
Expand Down Expand Up @@ -324,16 +332,18 @@ def get_centroid(self, asset=None):
else:
return asset.properties.get('proj:centroid')

def set_centroid(self, centroid, asset=None):
def set_centroid(self, value, asset=None):
"""Set an Item or an Asset centroid.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:centroid'] = centroid
key = 'proj:centroid'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:centroid'] = centroid
target[key] = value

@property
def shape(self):
Expand Down Expand Up @@ -367,16 +377,18 @@ def get_shape(self, asset=None):
else:
return asset.properties.get('proj:shape')

def set_shape(self, shape, asset=None):
def set_shape(self, value, asset=None):
"""Set an Item or an Asset shape.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:shape'] = shape
key = 'proj:shape'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:shape'] = shape
target[key] = value

@property
def transform(self):
Expand Down Expand Up @@ -413,16 +425,18 @@ def get_transform(self, asset=None):
else:
return asset.properties.get('proj:transform')

def set_transform(self, transform, asset=None):
def set_transform(self, value, asset=None):
"""Set an Item or an Asset transform.
If an Asset is supplied, sets the property on the Asset.
Otherwise sets the Item's value.
"""
if asset is None:
self.item.properties['proj:transform'] = transform
key = 'proj:transform'
target = self.item.properties if asset is None else asset.properties
if value is None:
target.pop(key, None)
else:
asset.properties['proj:transform'] = transform
target[key] = value

@classmethod
def _object_links(cls):
Expand Down
8 changes: 8 additions & 0 deletions tests/extensions/test_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def test_apply(self):
shape=[100, 100],
transform=[30.0, 0.0, 224985.0, 0.0, -30.0, 6790215.0, 0.0, 0.0, 1.0])

def test_partial_apply(self):
proj_item = pystac.read_file(self.example_uri)

proj_item.ext.projection.apply(epsg=1111)

self.assertEqual(proj_item.ext.projection.epsg, 1111)
proj_item.validate()

def test_validate_proj(self):
item = pystac.read_file(self.example_uri)
item.validate()
Expand Down

0 comments on commit eebfe89

Please sign in to comment.