Skip to content
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

added projjson if epsg not available #41

Merged
merged 7 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions rio_stac/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ def get_projection_info(
see: https://github.com/stac-extensions/projection/#item-properties-or-asset-fields

"""
try:
epsg = src_dst.crs.to_epsg() or None
except AttributeError:
projjson = None
if src_dst.crs is None:
epsg = None
elif not src_dst.crs.is_epsg_code:
epsg = None
try:
projjson = src_dst.crs.to_dict(projjson=True)
except AttributeError:
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
projjson = None
else:
epsg = src_dst.crs.to_epsg()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do something like 👇

projjson = None
epsg = None
if src_dst.crs is not None:
    epsg = src_dst.crs.to_epsg() if src_dst.crs.is_epsg_code else None
    try:
        projjson = src_dst.crs.to_dict(projjson=True)
    except AttributeError:
        pass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, thanks for the style improvement :)


meta = {
"epsg": epsg,
Expand All @@ -79,6 +86,8 @@ def get_projection_info(
"shape": [src_dst.height, src_dst.width],
"transform": list(src_dst.transform),
}
if projjson is not None:
meta["projjson"] = projjson
# rasterio can't reproduce wkt2
# ref: https://github.com/mapbox/rasterio/issues/2044
# if epsg is None:
Expand Down
Binary file added tests/fixtures/dataset_nocrs.tif
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ def test_rio_stac_cli(runner):
assert "datetime" in stac_item["properties"]
assert "proj:epsg" in stac_item["properties"]
assert "raster:bands" in stac_item["assets"]["asset"]

with runner.isolated_filesystem():
src_path = os.path.join(PREFIX, "dataset_nocrs.tif")
result = runner.invoke(stac, [src_path])
assert not result.exception
assert result.exit_code == 0
stac_item = json.loads(result.output)
assert stac_item["type"] == "Feature"
assert stac_item["assets"]["asset"]
assert stac_item["assets"]["asset"]["href"] == src_path
assert stac_item["links"] == []
assert stac_item["stac_extensions"] == [
"https://stac-extensions.github.io/projection/v1.0.0/schema.json",
"https://stac-extensions.github.io/raster/v1.1.0/schema.json",
"https://stac-extensions.github.io/eo/v1.0.0/schema.json",
]
assert "datetime" in stac_item["properties"]
assert "proj:epsg" in stac_item["properties"]
assert "proj:projjson" in stac_item["properties"]
assert "raster:bands" in stac_item["assets"]["asset"]
assert "eo:bands" in stac_item["assets"]["asset"]