Skip to content

DEM

该模块用于alos-dem数据下载

数据源: Microsoft Planetary Computer

数据目录: https://planetarycomputer.microsoft.com/api/stac/v1/

数据说明: https://www.eorc.jaxa.jp/ALOS/en/aw3d30/aw3d30v3.2_product_e_e1.2.pdf

Alos_DEM

Source code in hydro_opendata/downloader/dem.py
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Alos_DEM:
    def __init__(self):
        self._sources = {
            "microsoft": {
                "url": "https://planetarycomputer.microsoft.com/api/stac/v1/",
                "collection_name": "alos-dem",
            },
        }
        self._hrefs = []

    def list_sources(self):
        """
        返回stac列表

        Returns:
            sources (list): stac列表
        """
        return self._sources

    def add_source(self, source_name, url, collection_name):
        """
        添加自定义stac内容

        Args:
            source_name (str): 源名称
            url (list): STAC根路径地址
            collection_name (str): 数据集名称

        Returns:
            source (dict): 符合条件的url地址列表
        """

        new = {source_name: {"url": url, "collection_name": collection_name}}
        self._sources[source_name] = {"url": url, "collection_name": collection_name}

        return new

    def search(self, source="microsoft", bbox=None, intersects=None):
        """
        搜索符合条件的dem,并返回url地址列表

        Args:
            catalog_url (str): STAC根路径地址,默认为[微软planetarycomputer](https://planetarycomputer.microsoft.com)
            bbox (list): 搜索的矩形范围
            intersects (str): 搜索范围,应符合GeoJSON的geometry属性格式

        Returns:
            hrefs (list): 符合条件的url地址列表
        """

        if bbox is None:
            bbox = [115, 38, 136, 54]
        if source not in self._sources.keys():
            return

        stac = self._sources[source]["url"]
        stac_response = requests.get(stac).json()
        catalog_links = stac_response["links"]
        search = [l["href"] for l in catalog_links if l["rel"] == "search"][0]

        params = {"collections": [self._sources[source]["collection_name"]]}
        if bbox is not None:
            params["bbox"] = bbox
        if intersects is not None:
            params["intersects"] = intersects

        query = requests.post(search, json=params).json()

        features = query["features"]

        for feat in features:
            self._hrefs.append(feat["assets"]["data"]["href"])

        return self._hrefs

    def download(self, save_dir=".", cover=False):
        """
        下载列表中的dem数据

        Args:
            save_dir (str): 本地保存目录
            cover (bool): 若文件已存在,是否覆盖

        """

        if self._hrefs is not None:
            for href in self._hrefs:
                url = requests.utils.urlparse(href)
                file_path = os.path.join(save_dir, url.path.split("/")[-1])

                if os.path.exists(file_path) and not cover:
                    continue

                if not os.path.exists(os.path.dirname(file_path)):
                    os.makedirs(os.path.dirname(file_path))

                download_sigletasking(href, file_path)

add_source(source_name, url, collection_name)

添加自定义stac内容

Parameters:

Name Type Description Default
source_name str

源名称

required
url list

STAC根路径地址

required
collection_name str

数据集名称

required

Returns:

Name Type Description
source dict

符合条件的url地址列表

Source code in hydro_opendata/downloader/dem.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def add_source(self, source_name, url, collection_name):
    """
    添加自定义stac内容

    Args:
        source_name (str): 源名称
        url (list): STAC根路径地址
        collection_name (str): 数据集名称

    Returns:
        source (dict): 符合条件的url地址列表
    """

    new = {source_name: {"url": url, "collection_name": collection_name}}
    self._sources[source_name] = {"url": url, "collection_name": collection_name}

    return new

download(save_dir='.', cover=False)

下载列表中的dem数据

Parameters:

Name Type Description Default
save_dir str

本地保存目录

'.'
cover bool

若文件已存在,是否覆盖

False
Source code in hydro_opendata/downloader/dem.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def download(self, save_dir=".", cover=False):
    """
    下载列表中的dem数据

    Args:
        save_dir (str): 本地保存目录
        cover (bool): 若文件已存在,是否覆盖

    """

    if self._hrefs is not None:
        for href in self._hrefs:
            url = requests.utils.urlparse(href)
            file_path = os.path.join(save_dir, url.path.split("/")[-1])

            if os.path.exists(file_path) and not cover:
                continue

            if not os.path.exists(os.path.dirname(file_path)):
                os.makedirs(os.path.dirname(file_path))

            download_sigletasking(href, file_path)

list_sources()

返回stac列表

Returns:

Name Type Description
sources list

stac列表

Source code in hydro_opendata/downloader/dem.py
30
31
32
33
34
35
36
37
def list_sources(self):
    """
    返回stac列表

    Returns:
        sources (list): stac列表
    """
    return self._sources

search(source='microsoft', bbox=None, intersects=None)

搜索符合条件的dem,并返回url地址列表

Parameters:

Name Type Description Default
catalog_url str

STAC根路径地址,默认为微软planetarycomputer

required
bbox list

搜索的矩形范围

None
intersects str

搜索范围,应符合GeoJSON的geometry属性格式

None

Returns:

Name Type Description
hrefs list

符合条件的url地址列表

Source code in hydro_opendata/downloader/dem.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def search(self, source="microsoft", bbox=None, intersects=None):
    """
    搜索符合条件的dem,并返回url地址列表

    Args:
        catalog_url (str): STAC根路径地址,默认为[微软planetarycomputer](https://planetarycomputer.microsoft.com)
        bbox (list): 搜索的矩形范围
        intersects (str): 搜索范围,应符合GeoJSON的geometry属性格式

    Returns:
        hrefs (list): 符合条件的url地址列表
    """

    if bbox is None:
        bbox = [115, 38, 136, 54]
    if source not in self._sources.keys():
        return

    stac = self._sources[source]["url"]
    stac_response = requests.get(stac).json()
    catalog_links = stac_response["links"]
    search = [l["href"] for l in catalog_links if l["rel"] == "search"][0]

    params = {"collections": [self._sources[source]["collection_name"]]}
    if bbox is not None:
        params["bbox"] = bbox
    if intersects is not None:
        params["intersects"] = intersects

    query = requests.post(search, json=params).json()

    features = query["features"]

    for feat in features:
        self._hrefs.append(feat["assets"]["data"]["href"])

    return self._hrefs

Last update: 2023-08-30