Skip to content

Commit 9cf1561

Browse files
authored
Added unittest (opengeos#58)
* Add unittest * Add SamGeo tests * Add unittest for SamGeo * Update zoom level
1 parent 1ab60e0 commit 9cf1561

File tree

8 files changed

+131
-21
lines changed

8 files changed

+131
-21
lines changed

.github/FUNDING.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github: opengeos
2+
custom:
3+
- buymeacoffee.com/giswqs

.github/workflows/docs-build.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ jobs:
2828
pip install .
2929
- name: Discover typos with codespell
3030
run: codespell --skip="*.csv,*.geojson,*.json,*.js,*.html,*cff,*.pdf" --ignore-words-list="aci,acount,acounts,fallow,hart,hist,nd,ned,ois,wqs"
31-
- name: PKG-TEST
32-
run: |
33-
python -m unittest discover tests/
31+
# - name: PKG-TEST
32+
# run: |
33+
# python -m unittest discover tests/
3434
- run: mkdocs build
3535
- name: Deploy to Netlify
3636
uses: nwtgck/[email protected]

.github/workflows/docs.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ jobs:
1515
run: |
1616
python -m pip install --upgrade pip
1717
pip install --user --no-cache-dir Cython
18-
pip install --user -r requirements.txt
18+
pip install --user -r requirements.txt -r requirements_dev.txt
1919
pip install --user .
20-
- name: PKG-TEST
21-
run: |
22-
python -m unittest discover tests/
20+
# - name: PKG-TEST
21+
# run: |
22+
# python -m unittest discover tests/
2323
- run: python -m pip install --upgrade pip
2424
- run: pip install mkdocs-material mkdocstrings mkdocstrings-python-legacy mkdocs-git-revision-date-plugin mkdocs-jupyter ipykernel
2525
- run: mkdocs gh-deploy --force

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ private/
1212
**/*.dbf
1313
**/*.jpg
1414
**/*.png
15+
**/*.csv
1516

1617
# C extensions
1718
*.so

requirements_dev.txt

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1 @@
1-
pip
2-
bump2version
3-
wheel
4-
watchdog
5-
flake8
6-
tox
7-
coverage
8-
Sphinx
9-
twine
10-
grip
11-
1+
rio-cogeo

samgeo/common.py

+4
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,10 @@ def vector_to_geojson(filename, output=None, **kwargs):
653653
Returns:
654654
dict: The geojson dictionary.
655655
"""
656+
657+
if not filename.startswith("http"):
658+
filename = download_file(filename)
659+
656660
gdf = gpd.read_file(filename, **kwargs)
657661
if output is None:
658662
return gdf.__geo_interface__

tests/test_common.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
"""Tests for `samgeo` package."""
4+
5+
import os
6+
import unittest
7+
8+
from samgeo import samgeo
9+
10+
11+
class TestCommon(unittest.TestCase):
12+
"""Tests for the common.py module."""
13+
14+
def setUp(self):
15+
"""Set up test fixtures, if any."""
16+
17+
def tearDown(self):
18+
"""Tear down test fixtures, if any."""
19+
20+
def test_is_colab(self):
21+
self.assertFalse(samgeo.is_colab())
22+
23+
def test_check_file_path(self):
24+
self.assertTrue(samgeo.check_file_path("tests/test_common.py"))
25+
26+
def test_temp_file_path(self):
27+
self.assertFalse(os.path.exists(samgeo.temp_file_path(extension=".tif")))
28+
29+
def test_github_raw_url(self):
30+
self.assertEqual(
31+
samgeo.github_raw_url(
32+
"https://github.com/opengeos/segment-geospatial/blob/main/samgeo/samgeo.py"
33+
),
34+
"https://raw.githubusercontent.com/opengeos/segment-geospatial/main/samgeo/samgeo.py",
35+
)
36+
37+
def test_download_file(self):
38+
self.assertTrue(
39+
samgeo.download_file(
40+
url="https://github.com/opengeos/leafmap/raw/master/examples/data/world_cities.csv"
41+
)
42+
)
43+
44+
def test_image_to_cog(self):
45+
image = "https://github.com/opengeos/data/raw/main/raster/landsat7.tif"
46+
cog = "tests/data/landsat7_cog.tif"
47+
48+
samgeo.image_to_cog(image, cog)
49+
50+
self.assertTrue(os.path.exists(cog))
51+
52+
def test_vector_to_geojson(self):
53+
vector = "https://github.com/opengeos/leafmap/raw/master/examples/data/world_cities.geojson"
54+
self.assertIsInstance(samgeo.vector_to_geojson(vector), dict)
55+
56+
def test_tms_to_geotiff(self):
57+
bbox = [-95.3704, 29.6762, -95.368, 29.6775]
58+
image = "satellite.tif"
59+
samgeo.tms_to_geotiff(
60+
output=image, bbox=bbox, zoom=20, source="Satellite", overwrite=True
61+
)
62+
self.assertTrue(os.path.exists(image))

tests/test_samgeo.py

+53-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""Tests for `samgeo` package."""
44

5-
5+
import os
66
import unittest
77

88
from samgeo import samgeo
@@ -13,9 +13,59 @@ class TestSamgeo(unittest.TestCase):
1313

1414
def setUp(self):
1515
"""Set up test fixtures, if any."""
16+
bbox = [-122.1497, 37.6311, -122.1203, 37.6458]
17+
image = "satellite.tif"
18+
samgeo.tms_to_geotiff(
19+
output=image, bbox=bbox, zoom=15, source="Satellite", overwrite=True
20+
)
21+
self.source = image
22+
23+
out_dir = os.path.join(os.path.expanduser("~"), "Downloads")
24+
checkpoint = os.path.join(out_dir, "sam_vit_h_4b8939.pth")
25+
self.checkpoint = checkpoint
26+
27+
sam = samgeo.SamGeo(
28+
model_type="vit_h",
29+
checkpoint=checkpoint,
30+
sam_kwargs=None,
31+
)
32+
33+
self.sam = sam
1634

1735
def tearDown(self):
1836
"""Tear down test fixtures, if any."""
1937

20-
def test_000_something(self):
21-
"""Test something."""
38+
def test_generate(self):
39+
"""Test the automatic generation of masks and annotations.
40+
"""
41+
sam = self.sam
42+
source = self.source
43+
44+
sam.generate(source, output="masks.tif", foreground=True, unique=True)
45+
self.assertTrue(os.path.exists("masks.tif"))
46+
47+
sam.show_anns(axis="off", alpha=1, output="annotations.tif")
48+
self.assertTrue(os.path.exists("annotations.tif"))
49+
50+
sam.tiff_to_vector("masks.tif", "masks.gpkg")
51+
self.assertTrue(os.path.exists("masks.gpkg"))
52+
53+
54+
def test_predict(self):
55+
"""Test the prediction of masks and annotations based on input prompts.
56+
"""
57+
sam = samgeo.SamGeo(
58+
model_type="vit_h",
59+
checkpoint=self.checkpoint,
60+
automatic=False,
61+
sam_kwargs=None,
62+
)
63+
64+
sam.set_image(self.source)
65+
point_coords = [[-122.1419, 37.6383]]
66+
sam.predict(point_coords, point_labels=1, point_crs="EPSG:4326", output='mask1.tif')
67+
self.assertTrue(os.path.exists("mask1.tif"))
68+
69+
point_coords = [[-122.1464, 37.6431], [-122.1449, 37.6415], [-122.1451, 37.6395]]
70+
sam.predict(point_coords, point_labels=1, point_crs="EPSG:4326", output='mask2.tif')
71+
self.assertTrue(os.path.exists("mask2.tif"))

0 commit comments

Comments
 (0)