-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add_min_max_wavelength
- Loading branch information
Showing
16 changed files
with
217 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Transform Points | ||
|
||
Transform the points from a georeferenced shapefile/GeoJSON based on an image size. | ||
|
||
**plantcv.geospatial.transform_points**(*img, geojson*) | ||
|
||
**returns** list of transformed coordinates | ||
|
||
- **Parameters:** | ||
- img - Spectral image object, likely read in with [`geo.read_geotif`](read_geotif.md) | ||
- geojson - Path to the shapefile/GeoJSON containing the points. Can be Point or MultiPoint geometry. | ||
|
||
- **Context:** | ||
- Transformed points can be used downstream for Python analysis, such as defining ROIs. | ||
- **Example use:** | ||
- below to define plot boundaries | ||
|
||
|
||
```python | ||
import plantcv.geospatial as geo | ||
|
||
# Read geotif in | ||
spectral = geo.read_geotif(filename="./data/example_img.tif", bands="b,g,r,RE,NIR") | ||
coords = geo.transform_points(img=spectral, geojson="./experimental_bounds_2024.shp") | ||
roi_objects = pcv.roi.custom(img=spectral.pseudo_rgb, vertices=coords) | ||
|
||
``` | ||
|
||
**Source Code:** [Here](https://github.com/danforthcenter/plantcv-geospatial/blob/main/plantcv/geospatial/transform_points.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from importlib.metadata import version | ||
from plantcv.geospatial.read_geotif import read_geotif | ||
from plantcv.geospatial.transform_points import transform_points | ||
|
||
# Auto versioning | ||
__version__ = version("plantcv-geospatial") | ||
|
||
__all__ = [ | ||
"read_geotif" | ||
"read_geotif", | ||
"transform_points" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Transform georeferenced GeoJSON/shapefile points into python coordinates | ||
import fiona | ||
|
||
|
||
def transform_points(img, geojson): | ||
"""Takes a points-type shapefile/GeoJSON and transforms to numpy coordinates | ||
Inputs: | ||
img: A spectral object from read_geotif. | ||
geojson: Path to the shape file containing the points. | ||
Returns: | ||
coord: Transformed points as a list of numpy coordinates | ||
:param img: [spectral object] | ||
:param geojson: str | ||
:return coord: list | ||
""" | ||
geo_transform = img.geo_transform | ||
|
||
coord = [] | ||
with fiona.open(geojson, 'r') as shapefile: | ||
for i, _ in enumerate(shapefile): | ||
if type((shapefile[i].geometry["coordinates"])) is list: | ||
pixel_point = ~(geo_transform) * (shapefile[i].geometry["coordinates"][0]) | ||
if type((shapefile[i].geometry["coordinates"])) is tuple: | ||
pixel_point = ~(geo_transform) * (shapefile[i].geometry["coordinates"]) | ||
rounded = (int(pixel_point[0]), int(pixel_point[1])) | ||
coord.append(rounded) | ||
|
||
return coord |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Tests for geospatial.readd_geotif.""" | ||
|
||
from plantcv.geospatial import read_geotif, transform_points | ||
|
||
|
||
def test_geospatial_transform_points(test_data): | ||
"""Test for plantcv-geospatial.""" | ||
# read in small 5-band tif image | ||
img = read_geotif(filename=test_data.rgb_tif, bands="B,G,R") | ||
coords = transform_points(img=img, geojson=test_data.pts_geojson) | ||
assert len(coords) == 4 | ||
|
||
|
||
def test_geospatial_transform_single_points(test_data): | ||
"""Test for plantcv-geospatial.""" | ||
# read in small 5-band tif image | ||
img = read_geotif(filename=test_data.rgb_tif, bands="B,G,R") | ||
coords = transform_points(img=img, geojson=test_data.single_pts_geojson) | ||
assert len(coords) == 8 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"name": "point_crop", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32615" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 720198.797875224030577, 4302928.175875684246421 ] } }, | ||
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 720199.770441700122319, 4302928.186563228257 ] } }, | ||
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 720199.781129243900068, 4302927.310184645466506 ] } }, | ||
{ "type": "Feature", "properties": { }, "geometry": { "type": "Point", "coordinates": [ 720198.851312942570075, 4302927.352934820577502 ] } } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"name": "single_test_pts", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32615" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720201.455548222991638, 4302929.009466916322708 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720200.939437506720424, 4302928.515762113966048 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720200.359096014057286, 4302927.93337285425514 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720199.889628155739047, 4302927.349608448334038 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720199.406284277676605, 4302926.797856044024229 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720198.922733629238792, 4302926.185171222314239 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720198.442067798110656, 4302925.539512793533504 ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "Point", "coordinates": [ 720197.996919097611681, 4302925.057554029859602 ] } } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"name": "square_crop", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32615" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 720200.657507826690562, 4302928.218625859357417 ], [ 720199.535315738874488, 4302929.041566723957658 ], [ 720198.855781324207783, 4302928.114928886294365 ], [ 720199.977973412023857, 4302927.291988021694124 ], [ 720200.657507826690562, 4302928.218625859357417 ] ] ] } } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"name": "test_pts", | ||
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::32615" } }, | ||
"features": [ | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 720198.48000389453955, 4302927.004775654524565 ] ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 720199.890186188276857, 4302928.102779876440763 ] ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 720200.501309029874392, 4302929.21947811357677 ] ] } }, | ||
{ "type": "Feature", "properties": { "id": null }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 720197.757872894289903, 4302926.144173440523446 ] ] } } | ||
] | ||
} |