|
20 | 20 | "metadata": {},
|
21 | 21 | "outputs": [],
|
22 | 22 | "source": [
|
23 |
| - "from pystac_client import Client\n", |
24 |
| - "\n", |
25 | 23 | "# set pystac_client logger to DEBUG to see API calls\n",
|
26 | 24 | "import logging\n",
|
27 |
| - "logging.basicConfig()\n", |
28 |
| - "logger = logging.getLogger('pystac_client')\n", |
29 |
| - "logger.setLevel(logging.INFO)\n", |
30 |
| - "\n", |
31 |
| - "# function for creating GeoDataFrame from Items\n", |
32 | 25 | "from copy import deepcopy\n",
|
| 26 | + "\n", |
33 | 27 | "import geopandas as gpd\n",
|
34 | 28 | "import pandas as pd\n",
|
35 | 29 | "from shapely.geometry import shape\n",
|
36 | 30 | "\n",
|
| 31 | + "from pystac_client import Client\n", |
| 32 | + "\n", |
| 33 | + "logging.basicConfig()\n", |
| 34 | + "logger = logging.getLogger(\"pystac_client\")\n", |
| 35 | + "logger.setLevel(logging.INFO)\n", |
| 36 | + "\n", |
| 37 | + "\n", |
37 | 38 | "# convert a list of STAC Items into a GeoDataFrame\n",
|
38 | 39 | "def items_to_geodataframe(items):\n",
|
39 | 40 | " _items = []\n",
|
40 | 41 | " for i in items:\n",
|
41 | 42 | " _i = deepcopy(i)\n",
|
42 |
| - " _i['geometry'] = shape(_i['geometry'])\n", |
| 43 | + " _i[\"geometry\"] = shape(_i[\"geometry\"])\n", |
43 | 44 | " _items.append(_i)\n",
|
44 | 45 | " gdf = gpd.GeoDataFrame(pd.json_normalize(_items))\n",
|
45 |
| - " for field in ['properties.datetime', 'properties.created', 'properties.updated']:\n", |
| 46 | + " for field in [\"properties.datetime\", \"properties.created\", \"properties.updated\"]:\n", |
46 | 47 | " if field in gdf:\n",
|
47 | 48 | " gdf[field] = pd.to_datetime(gdf[field])\n",
|
48 |
| - " gdf.set_index('properties.datetime', inplace=True)\n", |
| 49 | + " gdf.set_index(\"properties.datetime\", inplace=True)\n", |
49 | 50 | " return gdf"
|
50 | 51 | ]
|
51 | 52 | },
|
|
57 | 58 | "outputs": [],
|
58 | 59 | "source": [
|
59 | 60 | "# STAC API root URL\n",
|
60 |
| - "URL = 'https://planetarycomputer.microsoft.com/api/stac/v1'\n", |
| 61 | + "URL = \"https://planetarycomputer.microsoft.com/api/stac/v1\"\n", |
61 | 62 | "\n",
|
62 | 63 | "# custom headers\n",
|
63 | 64 | "headers = []\n",
|
|
82 | 83 | "metadata": {},
|
83 | 84 | "outputs": [],
|
84 | 85 | "source": [
|
| 86 | + "import json\n", |
| 87 | + "\n", |
| 88 | + "import hvplot.pandas # noqa: F401\n", |
| 89 | + "\n", |
85 | 90 | "# AOI around Delfzijl, in the north of The Netherlands\n",
|
86 | 91 | "geom = {\n",
|
87 | 92 | " \"type\": \"Polygon\",\n",
|
88 | 93 | " \"coordinates\": [\n",
|
89 |
| - " [\n", |
90 |
| - " [\n", |
91 |
| - " 6.42425537109375,\n", |
92 |
| - " 53.174765470134616\n", |
93 |
| - " ],\n", |
94 |
| - " [\n", |
95 |
| - " 7.344360351562499,\n", |
96 |
| - " 53.174765470134616\n", |
97 |
| - " ],\n", |
98 |
| - " [\n", |
99 |
| - " 7.344360351562499,\n", |
100 |
| - " 53.67393435835391\n", |
101 |
| - " ],\n", |
102 |
| - " [\n", |
103 |
| - " 6.42425537109375,\n", |
104 |
| - " 53.67393435835391\n", |
105 |
| - " ],\n", |
106 | 94 | " [\n",
|
107 |
| - " 6.42425537109375,\n", |
108 |
| - " 53.174765470134616\n", |
| 95 | + " [6.42425537109375, 53.174765470134616],\n", |
| 96 | + " [7.344360351562499, 53.174765470134616],\n", |
| 97 | + " [7.344360351562499, 53.67393435835391],\n", |
| 98 | + " [6.42425537109375, 53.67393435835391],\n", |
| 99 | + " [6.42425537109375, 53.174765470134616],\n", |
109 | 100 | " ]\n",
|
110 |
| - " ]\n", |
111 |
| - " ]\n", |
| 101 | + " ],\n", |
112 | 102 | "}\n",
|
113 | 103 | "\n",
|
114 | 104 | "params = {\n",
|
|
118 | 108 | " \"datetime\": \"2018-01-01/2020-12-31\",\n",
|
119 | 109 | "}\n",
|
120 | 110 | "\n",
|
121 |
| - "import hvplot.pandas\n", |
122 |
| - "import json\n", |
123 | 111 | "\n",
|
124 | 112 | "# reusable search function\n",
|
125 | 113 | "def search_fetch_plot(params, filt):\n",
|
126 | 114 | " # limit sets the # of items per page so we can see multiple pages getting fetched\n",
|
127 |
| - " params['filter'] = filt\n", |
| 115 | + " params[\"filter\"] = filt\n", |
128 | 116 | " search = cat.search(**params)\n",
|
129 |
| - " items = list(search.items_as_dicts()) # safe b/c we set max_items = 100\n", |
| 117 | + " items = list(search.items_as_dicts()) # safe b/c we set max_items = 100\n", |
130 | 118 | " # DataFrame\n",
|
131 | 119 | " items_df = pd.DataFrame(items_to_geodataframe(items))\n",
|
132 | 120 | " print(f\"{len(items_df.index)} items found\")\n",
|
133 |
| - " field = 'properties.eo:cloud_cover'\n", |
134 |
| - " return items_df.hvplot(y=field, label=json.dumps(filt), frame_height=500, frame_width=800) " |
| 121 | + " field = \"properties.eo:cloud_cover\"\n", |
| 122 | + " return items_df.hvplot(\n", |
| 123 | + " y=field, label=json.dumps(filt), frame_height=500, frame_width=800\n", |
| 124 | + " )" |
135 | 125 | ]
|
136 | 126 | },
|
137 | 127 | {
|
|
151 | 141 | "metadata": {},
|
152 | 142 | "outputs": [],
|
153 | 143 | "source": [
|
154 |
| - "filt = {\n", |
155 |
| - " \"op\": \"lte\",\n", |
156 |
| - " \"args\": [{\"property\": \"eo:cloud_cover\"}, 10]\n", |
157 |
| - "}\n", |
| 144 | + "filt = {\"op\": \"lte\", \"args\": [{\"property\": \"eo:cloud_cover\"}, 10]}\n", |
158 | 145 | "\n",
|
159 | 146 | "search_fetch_plot(params, filt)"
|
160 | 147 | ]
|
|
166 | 153 | "metadata": {},
|
167 | 154 | "outputs": [],
|
168 | 155 | "source": [
|
169 |
| - "filt = {\n", |
170 |
| - " \"op\": \"gte\",\n", |
171 |
| - " \"args\" : [{\"property\": \"eo:cloud_cover\"}, 80]\n", |
172 |
| - "}\n", |
| 156 | + "filt = {\"op\": \"gte\", \"args\": [{\"property\": \"eo:cloud_cover\"}, 80]}\n", |
173 | 157 | "\n",
|
174 | 158 | "search_fetch_plot(params, filt)"
|
175 | 159 | ]
|
|
181 | 165 | "metadata": {},
|
182 | 166 | "outputs": [],
|
183 | 167 | "source": [
|
184 |
| - "filt = { \n", |
| 168 | + "filt = {\n", |
185 | 169 | " \"op\": \"and\",\n",
|
186 | 170 | " \"args\": [\n",
|
187 |
| - " { \n", |
188 |
| - " \"op\":\"lte\", \n", |
189 |
| - " \"args\": [{\"property\": \"eo:cloud_cover\"}, 60]\n", |
190 |
| - " },\n", |
191 |
| - " { \n", |
192 |
| - " \"op\": \"gte\", \n", |
193 |
| - " \"args\": [{\"property\": \"eo:cloud_cover\"}, 40]\n", |
194 |
| - " }\n", |
195 |
| - " ]\n", |
| 171 | + " {\"op\": \"lte\", \"args\": [{\"property\": \"eo:cloud_cover\"}, 60]},\n", |
| 172 | + " {\"op\": \"gte\", \"args\": [{\"property\": \"eo:cloud_cover\"}, 40]},\n", |
| 173 | + " ],\n", |
196 | 174 | "}\n",
|
197 | 175 | "\n",
|
198 | 176 | "search_fetch_plot(params, filt)"
|
|
0 commit comments