Skip to content

Commit 374ccda

Browse files
authored
Add support for downloading layers (#7)
1 parent 7d1c00b commit 374ccda

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

Diff for: felt_python/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
refresh_url_layer,
1717
get_layer_details,
1818
update_layer_style,
19+
get_export_link,
20+
download_layer,
1921
)
2022
from .elements import (
2123
list_elements,
@@ -50,6 +52,8 @@
5052
"refresh_url_layer",
5153
"get_layer_details",
5254
"update_layer_style",
55+
"get_export_link",
56+
"download_layer",
5357
"AuthError",
5458
"list_elements",
5559
"list_element_groups",

Diff for: felt_python/layers.py

+43
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
REFRESH_TEMPLATE = urljoin(LAYER_TEMPLATE, "refresh")
2020
UPDATE_STYLE_TEMPLATE = urljoin(LAYER_TEMPLATE, "update_style")
2121
UPLOAD_TEMPLATE = urljoin(MAP_TEMPLATE, "upload")
22+
EXPORT_TEMPLATE = urljoin(LAYER_TEMPLATE, "get_export_link")
2223

2324

2425
def list_layers(map_id: str, api_token: str | None = None):
@@ -209,3 +210,45 @@ def update_layer_style(
209210
api_token=api_token,
210211
)
211212
return json.load(response)
213+
214+
215+
def get_export_link(
216+
map_id: str,
217+
layer_id: str,
218+
api_token: str | None = None,
219+
):
220+
"""Get a download link for a layer
221+
222+
Vector layers will be downloaded in GPKG format. Raster layers will be GeoTIFFs.
223+
"""
224+
response = make_request(
225+
url=EXPORT_TEMPLATE.format(
226+
map_id=map_id,
227+
layer_id=layer_id,
228+
),
229+
method="GET",
230+
api_token=api_token,
231+
)
232+
return json.load(response)["export_link"]
233+
234+
235+
def download_layer(
236+
map_id: str,
237+
layer_id: str,
238+
file_name: str | None = None,
239+
api_token: str | None = None,
240+
) -> str:
241+
"""Download a layer to a file.
242+
243+
Vector layers will be downloaded in GPKG format. Raster layers will be GeoTIFFs.
244+
If a specific file name is not provided, the default file name will be saved to
245+
the current working directory.
246+
"""
247+
export_link = get_export_link(map_id, layer_id, api_token)
248+
with urllib.request.urlopen(export_link) as response:
249+
if file_name is None:
250+
parsed_url = urllib.parse.urlparse(response.url)
251+
file_name = os.path.basename(parsed_url.path)
252+
with open(file_name, "wb") as file_obj:
253+
file_obj.write(response.read())
254+
return file_name

Diff for: testing_layers.ipynb

+23-1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,28 @@
286286
"list_layers(map_id)"
287287
]
288288
},
289+
{
290+
"cell_type": "markdown",
291+
"id": "6cc3e80a",
292+
"metadata": {},
293+
"source": [
294+
"# Downloading a layer\n",
295+
"\n",
296+
"Download the file uploaded layer as a GeoPackage"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"id": "eb3a3b0f",
303+
"metadata": {},
304+
"outputs": [],
305+
"source": [
306+
"from felt_python import download_layer\n",
307+
"\n",
308+
"download_layer(map_id, layer_id, file_name=\"exported.gpkg\")"
309+
]
310+
},
289311
{
290312
"cell_type": "markdown",
291313
"id": "e3574d06-bfbd-46bc-a86e-b70be7167cbf",
@@ -321,7 +343,7 @@
321343
"name": "python",
322344
"nbconvert_exporter": "python",
323345
"pygments_lexer": "ipython3",
324-
"version": "3.11.6"
346+
"version": "3.12.3"
325347
}
326348
},
327349
"nbformat": 4,

0 commit comments

Comments
 (0)