sen2p 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sen2p-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.4
2
+ Name: sen2p
3
+ Version: 0.0.1
4
+ Summary: Download Sentinel-2 data from Microsoft Planetary Computer
5
+ Home-page: https://github.com/tnmthai/sen2p
6
+ Author: Thai Tran
7
+ Author-email: ThaiTran@outlook.co.nz
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Topic :: Scientific/Engineering :: GIS
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: planetary-computer
16
+ Requires-Dist: pystac-client
17
+ Requires-Dist: rioxarray
18
+ Requires-Dist: xarray
19
+ Requires-Dist: tqdm
20
+ Requires-Dist: geopandas
21
+ Requires-Dist: shapely
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: requires-dist
29
+ Dynamic: requires-python
30
+ Dynamic: summary
31
+
32
+ # Sentinel Downloader
33
+
34
+ This Python library allows you to download Sentinel-2 data from Microsoft Planetary Computer.
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ pip install -e .
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ ```python
45
+ from sen2p import download
46
+
47
+ location = [172.1, -43.5]
48
+
49
+ results = download(
50
+ start_date="2023-06-01",
51
+ end_date="2023-06-10",
52
+ location=location,
53
+ bands=["B02", "B03", "B04"],
54
+ output_dir="test_outputs",
55
+ merge_bands=True,
56
+ max_items=3 # download up to 3 images
57
+ )
58
+
59
+ for r in results:
60
+ print("Downloaded:", r)
61
+ ```
62
+ ```python
63
+ from sen2p import download
64
+
65
+ # Path to your shapefile
66
+ shapefile_path = "Site.shp" # Update with your actual shapefile path
67
+
68
+ # Call the function
69
+ results = download(
70
+ start_date="2023-06-01",
71
+ end_date="2023-06-30",
72
+ location=shapefile_path,
73
+ bands=["B02", "B03", "B04"],
74
+ output_dir="test_output",
75
+ merge_bands=True,
76
+ max_items=5 # Download up to 5 matching images
77
+ )
78
+ ```
sen2p-0.0.1/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Sentinel Downloader
2
+
3
+ This Python library allows you to download Sentinel-2 data from Microsoft Planetary Computer.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install -e .
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from sen2p import download
15
+
16
+ location = [172.1, -43.5]
17
+
18
+ results = download(
19
+ start_date="2023-06-01",
20
+ end_date="2023-06-10",
21
+ location=location,
22
+ bands=["B02", "B03", "B04"],
23
+ output_dir="test_outputs",
24
+ merge_bands=True,
25
+ max_items=3 # download up to 3 images
26
+ )
27
+
28
+ for r in results:
29
+ print("Downloaded:", r)
30
+ ```
31
+ ```python
32
+ from sen2p import download
33
+
34
+ # Path to your shapefile
35
+ shapefile_path = "Site.shp" # Update with your actual shapefile path
36
+
37
+ # Call the function
38
+ results = download(
39
+ start_date="2023-06-01",
40
+ end_date="2023-06-30",
41
+ location=shapefile_path,
42
+ bands=["B02", "B03", "B04"],
43
+ output_dir="test_output",
44
+ merge_bands=True,
45
+ max_items=5 # Download up to 5 matching images
46
+ )
47
+ ```
@@ -0,0 +1 @@
1
+ from .download import download
@@ -0,0 +1,157 @@
1
+ import os
2
+ from typing import List, Optional, Dict, Any, Union
3
+ from tqdm import tqdm
4
+ import xarray as xr
5
+ import rioxarray as rxr
6
+ import geopandas as gpd
7
+ from shapely.geometry import mapping, Point
8
+ from pystac_client import Client
9
+ import planetary_computer as pc
10
+ from rasterio.enums import Resampling
11
+
12
+ def download(
13
+ start_date: str,
14
+ end_date: str,
15
+ location: Union[List[float], Dict[str, Any], str],
16
+ bands: Optional[List[str]] = None,
17
+ output_dir: Optional[str] = None,
18
+ show_progress: bool = True,
19
+ merge_bands: bool = False,
20
+ merged_filename: Optional[str] = None,
21
+ overwrite: bool = False,
22
+ cell_size: Optional[float] = None,
23
+ max_items: int = 10,
24
+ collection: str = "sentinel-2-l2a"
25
+ ) -> List[Dict[str, Any]]:
26
+ '''
27
+ Download Sentinel-2 data from Planetary Computer using search parameters.
28
+
29
+ Args:
30
+ start_date (str): Start date (e.g., "2023-06-01").
31
+ end_date (str): End date (e.g., "2023-06-30").
32
+ location (list, dict, or str): [lon, lat], GeoJSON geometry, or path to shapefile.
33
+ bands (list, optional): List of bands to download (e.g., ["B02", "B03"]).
34
+ output_dir (str, optional): Where to save output files.
35
+ show_progress (bool): Show download progress.
36
+ merge_bands (bool): Merge into single file.
37
+ merged_filename (str, optional): Filename for merged file.
38
+ overwrite (bool): Overwrite existing files.
39
+ cell_size (float, optional): Target resolution.
40
+ max_items (int): How many items to fetch.
41
+ collection (str): STAC collection name.
42
+
43
+ Returns:
44
+ List of result dicts, one per STAC item.
45
+ '''
46
+
47
+ catalog = Client.open("https://planetarycomputer.microsoft.com/api/stac/v1", modifier=pc.sign_inplace)
48
+
49
+ if isinstance(location, list):
50
+ geometry = mapping(Point(location[0], location[1]))
51
+ elif isinstance(location, dict):
52
+ geometry = location
53
+ elif isinstance(location, str) and location.endswith(".shp"):
54
+ gdf = gpd.read_file(location)
55
+ geometry = mapping(gdf.unary_union)
56
+ else:
57
+ raise ValueError("Location must be [lon, lat], GeoJSON geometry, or shapefile path")
58
+
59
+ search = catalog.search(
60
+ collections=[collection],
61
+ intersects=geometry,
62
+ datetime=f"{start_date}/{end_date}",
63
+ max_items=max_items,
64
+ )
65
+
66
+ items = list(search.get_items())
67
+ if not items:
68
+ raise ValueError("No Sentinel-2 items found for the given location and time range.")
69
+
70
+ all_results = []
71
+
72
+ for item in items:
73
+ item_id = item.id
74
+ available_assets = list(item.assets.keys())
75
+
76
+ if bands is None:
77
+ bands_to_download = [asset for asset in available_assets if asset.startswith("B")]
78
+ else:
79
+ missing_bands = [band for band in bands if band not in available_assets]
80
+ if missing_bands:
81
+ print(f"Skipping {item_id}: Missing bands {missing_bands}")
82
+ continue
83
+ bands_to_download = bands
84
+
85
+ item_output_dir = os.path.join(output_dir, item_id) if output_dir else None
86
+ if item_output_dir and not os.path.exists(item_output_dir):
87
+ os.makedirs(item_output_dir)
88
+
89
+ result = {}
90
+ band_data_arrays = []
91
+ resampled_arrays = []
92
+ band_names = []
93
+ progress_iter = tqdm(bands_to_download, desc=f"{item_id}") if show_progress else bands_to_download
94
+
95
+ for band in progress_iter:
96
+ band_url = item.assets[band].href
97
+ file_path = os.path.join(item_output_dir, f"{item.id}_{band}.tif") if item_output_dir else None
98
+
99
+ if file_path and os.path.exists(file_path) and not overwrite:
100
+ if show_progress:
101
+ progress_iter.write(f"{file_path} exists, skipping.")
102
+ if merge_bands:
103
+ band_data = rxr.open_rasterio(file_path)
104
+ band_data_arrays.append((band, band_data))
105
+ band_names.append(band)
106
+ result[band] = file_path
107
+ continue
108
+
109
+ if show_progress:
110
+ progress_iter.set_description(f"{item_id}: {band}")
111
+
112
+ band_data = rxr.open_rasterio(band_url)
113
+ if merge_bands:
114
+ band_data_arrays.append((band, band_data))
115
+ band_names.append(band)
116
+
117
+ if item_output_dir:
118
+ band_data.rio.to_raster(file_path)
119
+ result[band] = file_path
120
+ else:
121
+ result[band] = band_data
122
+
123
+ if merge_bands and item_output_dir:
124
+ merged_path = os.path.join(item_output_dir, merged_filename or f"{item.id}_merged.tif")
125
+ if os.path.exists(merged_path) and not overwrite:
126
+ if show_progress:
127
+ print(f"Merged file {merged_path} exists, skipping.")
128
+ result["merged"] = merged_path
129
+ else:
130
+ if cell_size is None and band_data_arrays:
131
+ cell_size = abs(band_data_arrays[0][1].rio.transform()[0])
132
+ elif cell_size is None:
133
+ cell_size = 10
134
+
135
+ for band_name, data_array in band_data_arrays:
136
+ current_res = abs(data_array.rio.transform()[0])
137
+ if abs(current_res - cell_size) > 0.01:
138
+ resampling_method = Resampling.bilinear if current_res < cell_size else Resampling.nearest
139
+ resampled = data_array.rio.reproject(
140
+ data_array.rio.crs,
141
+ resolution=(cell_size, cell_size),
142
+ resampling=resampling_method,
143
+ )
144
+ resampled_arrays.append(resampled)
145
+ else:
146
+ resampled_arrays.append(data_array)
147
+
148
+ merged_data = xr.concat(resampled_arrays, dim="band")
149
+ merged_data.attrs["description"] = f"Merged bands: {', '.join(band_names)}"
150
+ merged_data.rio.to_raster(merged_path, tags={"BAND_NAMES": ",".join(band_names)}, descriptions=band_names)
151
+ result["merged"] = merged_path
152
+ if show_progress:
153
+ print(f"Merged file written: {merged_path}")
154
+
155
+ all_results.append(result)
156
+
157
+ return all_results
@@ -0,0 +1,78 @@
1
+ Metadata-Version: 2.4
2
+ Name: sen2p
3
+ Version: 0.0.1
4
+ Summary: Download Sentinel-2 data from Microsoft Planetary Computer
5
+ Home-page: https://github.com/tnmthai/sen2p
6
+ Author: Thai Tran
7
+ Author-email: ThaiTran@outlook.co.nz
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Topic :: Scientific/Engineering :: GIS
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: planetary-computer
16
+ Requires-Dist: pystac-client
17
+ Requires-Dist: rioxarray
18
+ Requires-Dist: xarray
19
+ Requires-Dist: tqdm
20
+ Requires-Dist: geopandas
21
+ Requires-Dist: shapely
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: requires-dist
29
+ Dynamic: requires-python
30
+ Dynamic: summary
31
+
32
+ # Sentinel Downloader
33
+
34
+ This Python library allows you to download Sentinel-2 data from Microsoft Planetary Computer.
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ pip install -e .
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ ```python
45
+ from sen2p import download
46
+
47
+ location = [172.1, -43.5]
48
+
49
+ results = download(
50
+ start_date="2023-06-01",
51
+ end_date="2023-06-10",
52
+ location=location,
53
+ bands=["B02", "B03", "B04"],
54
+ output_dir="test_outputs",
55
+ merge_bands=True,
56
+ max_items=3 # download up to 3 images
57
+ )
58
+
59
+ for r in results:
60
+ print("Downloaded:", r)
61
+ ```
62
+ ```python
63
+ from sen2p import download
64
+
65
+ # Path to your shapefile
66
+ shapefile_path = "Site.shp" # Update with your actual shapefile path
67
+
68
+ # Call the function
69
+ results = download(
70
+ start_date="2023-06-01",
71
+ end_date="2023-06-30",
72
+ location=shapefile_path,
73
+ bands=["B02", "B03", "B04"],
74
+ output_dir="test_output",
75
+ merge_bands=True,
76
+ max_items=5 # Download up to 5 matching images
77
+ )
78
+ ```
@@ -0,0 +1,10 @@
1
+ README.md
2
+ setup.py
3
+ sen2p/__init__.py
4
+ sen2p/download.py
5
+ sen2p.egg-info/PKG-INFO
6
+ sen2p.egg-info/SOURCES.txt
7
+ sen2p.egg-info/dependency_links.txt
8
+ sen2p.egg-info/requires.txt
9
+ sen2p.egg-info/top_level.txt
10
+ tests/test_download.py
@@ -0,0 +1,7 @@
1
+ planetary-computer
2
+ pystac-client
3
+ rioxarray
4
+ xarray
5
+ tqdm
6
+ geopandas
7
+ shapely
@@ -0,0 +1 @@
1
+ sen2p
sen2p-0.0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
sen2p-0.0.1/setup.py ADDED
@@ -0,0 +1,34 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ # Optional: load README.md as the long description
4
+ with open("README.md", "r", encoding="utf-8") as f:
5
+ long_description = f.read()
6
+
7
+ setup(
8
+ name="sen2p",
9
+ version="0.0.1",
10
+ description="Download Sentinel-2 data from Microsoft Planetary Computer",
11
+ long_description=long_description,
12
+ long_description_content_type="text/markdown",
13
+ author="Thai Tran",
14
+ author_email="ThaiTran@outlook.co.nz",
15
+ url="https://github.com/tnmthai/sen2p",
16
+ packages=find_packages(),
17
+ install_requires=[
18
+ "planetary-computer",
19
+ "pystac-client",
20
+ "rioxarray",
21
+ "xarray",
22
+ "tqdm",
23
+ "geopandas",
24
+ "shapely"
25
+ ],
26
+ python_requires=">=3.8",
27
+ classifiers=[
28
+ "Programming Language :: Python :: 3",
29
+ "License :: OSI Approved :: MIT License",
30
+ "Operating System :: OS Independent",
31
+ "Intended Audience :: Science/Research",
32
+ "Topic :: Scientific/Engineering :: GIS",
33
+ ],
34
+ )
@@ -0,0 +1,6 @@
1
+ from sentinel_downloader import download_pc_stac_item
2
+
3
+ def test_function_runs():
4
+ item_url = "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2A_35UNU_20230827_0_L2A"
5
+ result = download_pc_stac_item(item_url, bands=["B02"], output_dir="output_test", show_progress=False)
6
+ assert "B02" in result