1+ import pytest
2+ import grass .script as gs
3+ from grass .tools import Tools
4+ from grass .jupyter .utils import *
5+
6+
7+ def test_get_region (session_with_data ):
8+ """Check that get_region returns a dictionary with region bounds"""
9+ region = get_region (env = session_with_data .env )
10+
11+ assert isinstance (region , dict )
12+ assert "north" in region
13+ assert "south" in region
14+ assert "east" in region
15+ assert "west" in region
16+
17+ def test_get_location_proj_string (tmp_path ):
18+ """Test that get_location_proj_string returns the projection of the environment in PROJ.4 format."""
19+ project = tmp_path / "test_project_proj"
20+ gs .create_project (project )
21+ with gs .setup .init (project ):
22+ gs .run_command ("g.proj" , flags = "c" , epsg = "4326" )
23+ projection = get_location_proj_string ()
24+ assert "+proj=" in projection
25+
26+ def test_reproject_region (session_with_data ):
27+ """Check that a region is correctly reprojected between two projections"""
28+ reg = {"north" : 100000.0 , "south" : 0.0 , "east" : 100000.0 , "west" : 0.0 , "rows" : 10 , "cols" : 10 }
29+ p_in = "+proj=merc +a=6378137 +b=6378137 +units=m +type=crs"
30+ p_out = "+proj=longlat +datum=WGS84 +type=crs"
31+
32+ res = reproject_region (reg , p_in , p_out , env = session_with_data .env )
33+
34+ assert isinstance (res , dict ) and "north" in res
35+ assert res ["north" ] != reg ["north" ] and res ["east" ] != reg ["east" ]
36+ assert - 90 <= res ["north" ] <= 90 and - 180 <= res ["east" ] <= 180
37+
38+ def test_set_target_region (session_with_data , tmp_path ):
39+ """Check setting a target region in a newly created environment"""
40+ gisdbase = str (tmp_path )
41+
42+ # Helper to quickly spin up isolated GRASS locations
43+ def make_env (loc_name , epsg ):
44+ _ , env = gs .create_environment (gisdbase , loc_name , "PERMANENT" )
45+ gs .create_location (gisdbase , loc_name , epsg = epsg , overwrite = True )
46+ full_env = session_with_data .env .copy ()
47+ full_env ["GISRC" ] = env ["GISRC" ]
48+ return full_env
49+
50+ # 1. Setup Source and Target
51+ src_env = make_env ("src_loc" , "3857" )
52+ tgt_env = make_env ("tgt_loc" , "4326" )
53+
54+ # 2. Add data to source, then execute function
55+ Tools (env = src_env ).g_region (n = 1000 , s = 0 , e = 1000 , w = 0 , rows = 10 , cols = 10 )
56+ set_target_region (src_env , tgt_env )
57+
58+ # 3. Verify
59+ res = get_region (env = tgt_env )
60+ assert isinstance (res , dict ) and "north" in res and "south" in res
0 commit comments