Skip to content

Commit 92d4fe3

Browse files
authored
chores: Adds tests for raster clipping (#88)
* chores: Adds tests for raster clipping Signed-off-by: nagesh bansal <[email protected]>
1 parent 14846d9 commit 92d4fe3

12 files changed

+284
-55
lines changed

neonwranglerpy/fetcher/fetcher.py

+32-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async def _request(session, url):
3535
return await response.json()
3636

3737

38-
async def _download(session, url, filename, sem, month, size=None):
38+
async def _download(session, url, filename, sem, size=None):
3939
"""Asynchronous function to download file from url.
4040
4141
Parameters
@@ -58,6 +58,7 @@ async def _download(session, url, filename, sem, month, size=None):
5858
size = response.content_length if not size else size
5959
block = size
6060
copied = 0
61+
6162
with open(filename, mode='wb') as f:
6263
async for chunk in response.content.iter_chunked(block):
6364
f.write(chunk)
@@ -66,26 +67,44 @@ async def _download(session, url, filename, sem, month, size=None):
6667
# update_progressbar(progress, size)
6768

6869

69-
async def _fetcher(data, rate_limit, headers, files_to_stack_path="filesToStack"):
70+
async def _fetcher(data,
71+
rate_limit,
72+
headers,
73+
files_to_stack_path="filesToStack",
74+
data_type="vst"):
7075
"""Fetcher for downloading files."""
7176
sem = asyncio.Semaphore(rate_limit)
7277
data = data['data']
73-
dir_name = '.'.join(
74-
['NEON', data['productCode'], data['siteCode'], data['month'], data['release']])
75-
zip_dir_path = os.path.join(files_to_stack_path, f'{dir_name}')
76-
if not os.path.isdir(zip_dir_path):
77-
os.mkdir(zip_dir_path)
7878

7979
d_urls = [f['url'] for f in data["files"]]
8080
sizes = [f['size'] for f in data["files"]]
8181
f_names = [f['name'] for f in data["files"]]
82-
f_paths = [pjoin(zip_dir_path, name) for name in f_names]
83-
month = [data['month']]
82+
if data_type == "vst":
83+
dir_name = '.'.join([
84+
'NEON', data['productCode'], data['siteCode'], data['month'], data['release']
85+
])
86+
zip_dir_path = os.path.join(files_to_stack_path, f'{dir_name}')
87+
if not os.path.isdir(zip_dir_path):
88+
os.mkdir(zip_dir_path)
89+
f_paths = [pjoin(zip_dir_path, name) for name in f_names]
90+
else:
91+
f_paths = []
92+
zip_dir_path = os.path.join(files_to_stack_path, data['productCode'])
93+
if not os.path.isdir(zip_dir_path):
94+
os.mkdir(zip_dir_path)
95+
for i in range(len(d_urls)):
96+
split_path = d_urls[i].split('/')
97+
dir_path = '/'.join(split_path[4:len(split_path) - 1])
98+
save_dir_path = pjoin(zip_dir_path, dir_path)
99+
if not os.path.exists(save_dir_path):
100+
os.makedirs(save_dir_path)
101+
f_paths.append(os.path.join(save_dir_path, f_names[i]))
102+
84103
zip_url = zip(d_urls, f_paths, sizes)
85104
async with aiohttp.ClientSession() as session:
86105
tasks = []
87106
for url, name, sz in zip_url:
88-
task = asyncio.create_task(_download(session, url, name, sem, month, sz))
107+
task = asyncio.create_task(_download(session, url, name, sem, sz))
89108
tasks.append(task)
90109

91110
await asyncio.gather(*tasks)
@@ -94,7 +113,7 @@ async def _fetcher(data, rate_limit, headers, files_to_stack_path="filesToStack"
94113
async def vst_fetcher(item, rate_limit, headers, files_to_stack_path="filesToStack"):
95114
"""Vst fetcher gets the urls for the files of vst data."""
96115
data = requests.get(item).json()
97-
await _fetcher(data, rate_limit, headers, files_to_stack_path)
116+
await _fetcher(data, rate_limit, headers, files_to_stack_path, "vst")
98117

99118

100119
def fetcher(batch, data_type, rate_limit, headers, files_to_stack_path):
@@ -103,7 +122,8 @@ def fetcher(batch, data_type, rate_limit, headers, files_to_stack_path):
103122
if data_type == 'vst':
104123
asyncio.run(vst_fetcher(batch, rate_limit, headers, files_to_stack_path))
105124
elif data_type == 'aop':
106-
asyncio.run(_fetcher(batch, rate_limit, headers, files_to_stack_path))
125+
asyncio.run(
126+
_fetcher(batch, rate_limit, headers, files_to_stack_path, data_type))
107127

108128
except Exception as e:
109129
print(f"Error processing URLs: {e}")

neonwranglerpy/lib/crop_plot_data.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
def list_files(path):
1111
"""List all the files in a path of given format."""
12-
mask = path + '/**/*.[th][i5]'
13-
files = glob(mask, recursive=True)
12+
mask_tif = path + "/**/*.tif"
13+
mask_h5 = path + "/**/*.h5"
14+
files = glob(mask_tif, recursive=True) + glob(mask_h5, recursive=True)
1415
return files
1516

1617

@@ -23,12 +24,11 @@ def crop_data_to_plot(plt,
2324
parallelized=False,
2425
savepath=""):
2526
"""Create shapefiles out of a vegetation structure data with lat/lon coordinates."""
26-
dataset_path = os.path.normpath(dataset_path)
27+
dataset_path = os.path.abspath(dataset_path)
2728
full_files = list_files(dataset_path)
2829
files = [file for file in full_files if dpID in file]
2930
files = [file for file in files if str(target_year) in file]
3031

31-
# TODO: add check if files for targeted year and product is not present
3232
plots = plt[['plotID', 'subplotID', 'siteID', 'utmZone', 'easting', 'northing']]
3333

3434
plots = plots.groupby(['plotID', 'subplotID', 'siteID',

neonwranglerpy/lib/retrieve_aop_data.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ def retrieve_aop_data(data, year=2019, dpID=['DP3.30006.001'], savepath=""):
2323
"""
2424
coords_for_tiles = data[['plotID', 'siteID', 'utmZone', 'easting', 'northing']]
2525
# get tiles dimensions
26-
coords_for_tiles['easting'] = (coords_for_tiles[['easting']] /
27-
1000).astype(int) * 1000
28-
coords_for_tiles['northing'] = (coords_for_tiles[['northing']] /
29-
1000).astype(int) * 1000
26+
coords_for_tiles['easting'] = (coords_for_tiles[['easting']] / 1000).astype(
27+
int, errors='ignore') * 1000
28+
coords_for_tiles['northing'] = (coords_for_tiles[['northing']] / 1000).astype(
29+
int, errors='ignore') * 1000
3030
# if there are more than 1 row, drop duplicates
3131
if coords_for_tiles.easting.shape[0] > 1:
3232
# drop duplicates values

neonwranglerpy/utilities/byTileAOP.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ def by_tile_aop(dpID, site, year, easting, northing, buffer=0, savepath=None):
7777
if not len(month_urls):
7878
print(f"There is no data for site {site} and year {year}")
7979

80-
if isinstance(easting, (int, float)):
81-
easting = [easting]
82-
northing = [northing]
80+
# if isinstance(easting, (int, float)):
81+
# easting = [easting]
82+
# northing = [northing]
8383
# convert the easting and northing for BLAN site
8484
if site == "BLAN":
8585
if isinstance(easting, (int, list)):
@@ -124,7 +124,7 @@ def by_tile_aop(dpID, site, year, easting, northing, buffer=0, savepath=None):
124124
tile_northing = np.floor(northing / 1000).astype(int) * 1000
125125

126126
file_urls = get_tile_urls(month_urls, tile_easting, tile_northing)
127-
print(f"Tiles Found for Remote Sensing Data: {len(file_urls)}")
127+
# print(f"Tiles Found for Remote Sensing Data: {len(file_urls)}")
128128
if not savepath:
129129
savepath = os.path.normpath(os.path.join(os.getcwd(), dpID))
130130
else:
@@ -133,14 +133,14 @@ def by_tile_aop(dpID, site, year, easting, northing, buffer=0, savepath=None):
133133
if not os.path.isdir(savepath):
134134
os.makedirs(savepath)
135135

136-
files_to_stack_path = os.path.join(savepath, "filesToStack")
137-
if not os.path.isdir(files_to_stack_path):
138-
os.mkdir(files_to_stack_path)
136+
# files_to_stack_path = os.path.join(savepath, "filesToStack")
137+
# if not os.path.isdir(files_to_stack_path):
138+
# os.mkdir(files_to_stack_path)
139139

140-
if files_to_stack_path:
140+
if savepath:
141141
fetcher.run_threaded_batches(file_urls,
142142
'aop',
143143
rate_limit=2,
144144
headers=None,
145-
savepath=files_to_stack_path)
145+
savepath=savepath)
146146
return savepath

requirements.txt

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
deepforest
12
flake8-docstrings
23
geopandas
4+
h5py
5+
laspy
6+
laspy[lazrs,laszip]
7+
lazrs
8+
numpy
9+
opencv-python
310
pandas
411
pytest
512
pytest-cov
13+
rasterio
614
requests
15+
sphinx-press-theme
716
toml
817
tox
918
yapf
10-
sphinx-press-theme
11-
laspy
12-
lazrs
13-
opencv-python
14-
numpy
15-
rasterio
16-
deepforest
17-
laspy[lazrs,laszip]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<kml>
2+
<Document>
3+
<Style id="yellow">
4+
<LineStyle>
5+
<color>ff00ffff</color>
6+
<width>1</width>
7+
</LineStyle>
8+
</Style>
9+
<Style id="red">
10+
<LineStyle>
11+
<color>ffff0000</color>
12+
<width>2</width>
13+
</LineStyle>
14+
</Style>
15+
<Style id="green">
16+
<LineStyle>
17+
<color>ff00ff00</color>
18+
<width>1</width>
19+
</LineStyle>
20+
</Style>
21+
<Placemark>
22+
<styleUrl>#yellow</styleUrl>
23+
<MultiGeometry>
24+
<LineString>
25+
<coordinates>
26+
-122.23238135,45.78998225,472.42
27+
-122.23251581,45.78998319,502.60
28+
-122.23293157,45.78998597,495.12
29+
-122.23323219,45.78998800,528.25
30+
-122.23336215,45.78998883,533.17
31+
-122.23352100,45.78998984,529.48
32+
-122.23391686,45.78999247,536.98
33+
-122.23418289,45.78999436,550.95
34+
-122.23446446,45.78999622,557.39
35+
-122.23488887,45.78999910,579.25
36+
-122.23518130,45.79000103,596.16
37+
-122.23558094,45.79000370,596.65
38+
-122.23583161,45.79000538,607.72
39+
-122.23637528,45.79000902,614.77
40+
-122.23667629,45.79001104,630.26
41+
-122.23691821,45.79001267,627.68
42+
-122.23746663,45.79001634,638.23
43+
-122.23787007,45.79001903,647.24
44+
-122.23829064,45.79002180,649.80
45+
-122.23889580,45.79002587,642.38
46+
-122.23941458,45.79002932,647.60
47+
-122.24003466,45.79003344,620.49
48+
-122.24060095,45.79003719,602.50
49+
-122.24072702,45.79003803,616.02
50+
-122.24081019,45.79003854,616.17
51+
-122.24090217,45.79003900,630.50
52+
-122.24090741,45.79003863,605.05
53+
-122.24090941,45.79003769,619.30
54+
-122.24090961,45.79003546,624.19
55+
-122.24091087,45.78994774,632.77
56+
-122.24091276,45.78981042,627.01
57+
-122.24091613,45.78956400,629.13
58+
-122.24091874,45.78937174,622.10
59+
-122.24092179,45.78914701,649.52
60+
-122.24092677,45.78878024,663.03
61+
-122.24092683,45.78877532,662.99
62+
-122.24092733,45.78873915,662.55
63+
-122.24093202,45.78839299,670.26
64+
-122.24093390,45.78825512,658.97
65+
-122.24093661,45.78805501,677.56
66+
-122.24093793,45.78795761,671.62
67+
-122.24094121,45.78771630,697.13
68+
-122.24094387,45.78751962,696.45
69+
-122.24094775,45.78723222,691.93
70+
-122.24095073,45.78701475,716.12
71+
-122.24095427,45.78675446,739.77
72+
-122.24095765,45.78650539,741.78
73+
-122.24095810,45.78647169,740.04
74+
-122.24095917,45.78639010,728.65
75+
-122.24096435,45.78600930,755.07
76+
-122.24096779,45.78575778,774.91
77+
-122.24096874,45.78568832,774.25
78+
-122.24096986,45.78560544,753.52
79+
-122.24097462,45.78525376,772.82
80+
-122.24097634,45.78512721,795.48
81+
-122.24098101,45.78478381,801.61
82+
-122.24098664,45.78436899,819.95
83+
-122.24098998,45.78411981,819.61
84+
-122.24099274,45.78391684,831.97
85+
-122.24099807,45.78352698,840.98
86+
-122.24099995,45.78338830,840.10
87+
-122.24100301,45.78316303,845.75
88+
-122.24100597,45.78294461,851.62
89+
-122.24101049,45.78261037,830.82
90+
-122.24101301,45.78242328,831.80
91+
-122.24101683,45.78214237,827.47
92+
-122.24102209,45.78175510,845.19
93+
-122.24102355,45.78164921,840.07
94+
-122.24102398,45.78161755,838.33
95+
-122.24102423,45.78159943,845.91
96+
-122.24102809,45.78131481,832.99
97+
-122.24102980,45.78118832,825.68
98+
-122.24103104,45.78109728,840.28
99+
-122.24103152,45.78105487,831.92
100+
-122.24103161,45.78104560,830.48
101+
-122.24103108,45.78104047,834.11
102+
-122.24103014,45.78103998,837.98
103+
-122.24101558,45.78103942,835.67
104+
-122.24100614,45.78103927,832.36
105+
-122.24084005,45.78103811,844.06
106+
-122.24046370,45.78103560,842.94
107+
-122.23990230,45.78103188,820.38
108+
-122.23967972,45.78103041,832.94
109+
-122.23910563,45.78102657,823.25
110+
-122.23871273,45.78102397,823.67
111+
-122.23841314,45.78102196,806.22
112+
-122.23814754,45.78102018,811.12
113+
-122.23750748,45.78101593,804.22
114+
-122.23735086,45.78101489,806.80
115+
-122.23683190,45.78101142,782.21
116+
-122.23654231,45.78100949,775.20
117+
-122.23599912,45.78100588,769.10
118+
-122.23558946,45.78100310,761.54
119+
-122.23505218,45.78099952,753.16
120+
-122.23487799,45.78099834,760.59
121+
-122.23446789,45.78099560,748.59
122+
-122.23445889,45.78099554,761.53
123+
-122.23389307,45.78099184,760.91
124+
-122.23373284,45.78099073,745.26
125+
-122.23346330,45.78098889,744.87
126+
-122.23342892,45.78098979,743.23
127+
-122.23342211,45.78099023,731.96
128+
-122.23339356,45.78099616,732.85
129+
-122.23337332,45.78101520,731.03
130+
-122.23337611,45.78113084,732.19
131+
-122.23337897,45.78121380,733.23
132+
-122.23338068,45.78159885,735.48
133+
-122.23337276,45.78171767,734.30
134+
-122.23336741,45.78183789,733.26
135+
-122.23335587,45.78205088,731.29
136+
-122.23334934,45.78225623,729.59
137+
-122.23334104,45.78236337,728.07
138+
-122.23326144,45.78280977,709.15
139+
-122.23314301,45.78324149,682.22
140+
-122.23308463,45.78343722,666.49
141+
-122.23308419,45.78346469,666.52
142+
-122.23312231,45.78388315,676.06
143+
-122.23312488,45.78390203,676.86
144+
-122.23317638,45.78427732,688.30
145+
-122.23320198,45.78454484,693.25
146+
-122.23321142,45.78467600,695.20
147+
-122.23322496,45.78479141,697.67
148+
-122.23324568,45.78503386,702.08
149+
-122.23324782,45.78505210,702.60
150+
-122.23325844,45.78545998,703.32
151+
-122.23325252,45.78553470,701.97
152+
-122.23321130,45.78598143,690.07
153+
-122.23319094,45.78613482,684.61
154+
-122.23318346,45.78620583,682.11
155+
-122.23315809,45.78642558,675.12
156+
-122.23313964,45.78656417,669.62
157+
-122.23312010,45.78675973,663.72
158+
-122.23306359,45.78712179,648.35
159+
-122.23298096,45.78754079,625.42
160+
-122.23290970,45.78781744,606.94
161+
-122.23288090,45.78791713,598.96
162+
-122.23284534,45.78803543,589.93
163+
-122.23278066,45.78826695,573.07
164+
-122.23277595,45.78828580,571.45
165+
-122.23272403,45.78848753,558.93
166+
-122.23265525,45.78876980,542.39
167+
-122.23260206,45.78897986,530.45
168+
-122.23258008,45.78908361,525.32
169+
-122.23250474,45.78943420,506.75
170+
-122.23245574,45.78963089,494.85
171+
-122.23242285,45.78975558,487.07
172+
-122.23238606,45.78992532,475.85
173+
-122.23238135,45.78998225,472.42
174+
</coordinates>
175+
</LineString>
176+
</MultiGeometry>
177+
</Placemark>
178+
</Document>
179+
</kml>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PROJCS["WGS 84 / UTM 10N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]

0 commit comments

Comments
 (0)