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

add proj centroid #38

Merged
merged 22 commits into from
May 3, 2023
7 changes: 5 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ jobs:
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
environment-file: environment.yml
python-version: ${{ matrix.python-version }}
- name: Update Conda's environemnt
run: conda env update -f environment.yml -n test
- name: update certs so PROJ can download the us_noaa_cshpgn.tif file
run: |
sudo mkdir -p /etc/pki/tls/certs
sudo curl https://curl.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt
philvarner marked this conversation as resolved.
Show resolved Hide resolved
- name: Execute linters and test suites
run: ./scripts/cibuild
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

- Add Projection Extension field proj:centroid ([#38](https://github.com/stactools-packages/naip/pull/38))

## [v0.3.1] - 2023-02-24

### Fixed
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ packages = find_namespace:
install_requires =
stactools >= 0.4.3
pystac >= 1.5 # for GridExtension class

[options.packages.find]
where = src
12 changes: 9 additions & 3 deletions src/stactools/naip/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fsspec
import pystac
import rasterio as rio
import shapely
from lxml import etree
from pystac.extensions.eo import EOExtension
from pystac.extensions.grid import GridExtension
Expand All @@ -15,7 +16,6 @@
from pystac.extensions.raster import DataType, RasterBand, RasterExtension
from pystac.extensions.scientific import CollectionScientificExtension, Publication
from pystac.utils import str_to_datetime
from shapely.geometry import box, mapping, shape
from stactools.core.io import read_text
from stactools.core.io.xml import XmlElement
from stactools.core.projection import reproject_geom
Expand Down Expand Up @@ -138,7 +138,10 @@ def create_item(
original_bbox = list(ds.bounds)
transform = list(ds.transform)
geom = reproject_geom(
ds.crs, "epsg:4326", mapping(box(*ds.bounds)), precision=6
ds.crs,
"epsg:4326",
shapely.geometry.mapping(shapely.geometry.box(*ds.bounds)),
precision=6,
)

if fgdc_metadata_href is not None:
Expand Down Expand Up @@ -198,7 +201,9 @@ def create_item(

item_id = naip_item_id(state, resource_desc)

bounds = list(shape(geom).bounds)
shapely_shape = shapely.geometry.shape(geom)
bounds = list(shapely_shape.bounds)
centroid = shapely_shape.centroid

dt = dt + timedelta(hours=16) # UTC is +4 ET, so is around 9-12 AM in CONUS
properties = {"naip:state": state, "naip:year": year}
Expand All @@ -222,6 +227,7 @@ def create_item(
projection.shape = image_shape
projection.bbox = original_bbox
projection.transform = transform
projection.centroid = {"lat": round(centroid.y, 5), "lon": round(centroid.x, 5)}

# Grid Extension
grid = GridExtension.ext(item, add_if_missing=True)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_stac.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
from datetime import datetime

import pytest
from pystac.extensions.projection import ProjectionExtension
from pystac.extensions.raster import DataType, RasterExtension
from pystac.extensions.scientific import CollectionScientificExtension

Expand Down Expand Up @@ -38,6 +40,10 @@ def test_create_item_txt(self):
self.assertEqual(raster_band.data_type, DataType.UINT8)
self.assertEqual(raster_band.unit, "none")

projection = ProjectionExtension.ext(item)
projection.centroid["lat"] == pytest.approx(30.96876)
projection.centroid["lon"] == pytest.approx(-85.90624)

# test stac on year = 2020
def test_create_item_xml(self):
item = create_item(
Expand Down Expand Up @@ -101,7 +107,6 @@ def test_create_item_xml_ext(self):
"data-files/m_3211605_ne_11_060_20200415_20200730.xml",
"data-files/m_3211605_ne_11_060_20200415_20200730_missing_resource_desc.xml",
]:

item = create_item(
"ca",
"2020",
Expand Down