Skip to content

Commit fe605dd

Browse files
authored
enh: endpoint for raster DL (#538)
1 parent cb68336 commit fe605dd

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

back/api/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
TileView.as_view(),
2626
name="retrieve-tile",
2727
),
28-
# L'URL spécifique doit venir AVANT l'URL générale avec <id>
28+
# Specific URL should be BEFORE general URL with <id>
2929
path(
3030
"tiles/<datatype>/in-polygon/",
3131
ScoresInPolygonView.as_view(),
@@ -44,9 +44,9 @@
4444
path("feedback/", FeedbackView.as_view(), name="create-feedback"),
4545
path("qpv/", QPVListView.as_view(), name="qpv-list"),
4646
path(
47-
"rasters/plantability/",
47+
"rasters/<str:raster_type>/",
4848
RasterDownloadView.as_view(),
49-
name="download-plantability-raster",
49+
name="download-raster",
5050
),
5151
path("dashboard/", DashboardView.as_view(), name="dashboard"),
5252
path("", include(router.urls)),

back/api/views/raster_views.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,40 @@
33
from django.conf import settings
44
from rest_framework.views import APIView
55

6+
RASTER_MAP = {
7+
"plantability": ("rasters/plantability.tif", "plantability_2025.tif"),
8+
"vegestrate": (
9+
"rasters/vegestrate_lyonmetro_02m_postprocessed.tif",
10+
"vegestrate_2023_02m.tif",
11+
),
12+
}
13+
614

715
class RasterDownloadView(APIView):
8-
"""
9-
API endpoint to download the plantability raster file.
10-
Provides a stable URL for accessing the raster regardless of internal file structure.
16+
"""API endpoint to download the plantability and vegestrate raster files.
1117
12-
Example: GET /api/rasters/plantability/
18+
Example: GET /api/rasters/plantability/ or GET /api/rasters/vegestrate/
1319
"""
1420

15-
def get(self, request):
16-
"""Serve the plantability raster file as a download."""
17-
raster_path = os.path.join(settings.MEDIA_ROOT, "rasters", "plantability.tif")
21+
def get(self, request, raster_type):
22+
if raster_type not in RASTER_MAP:
23+
raise Http404(
24+
"Raster does not exist, only plantability or vegestrate are available."
25+
)
1826

19-
if not os.path.exists(raster_path):
20-
raise Http404("Plantability raster file not found")
27+
relative_path, filename = RASTER_MAP[raster_type]
28+
raster_path = os.path.join(settings.MEDIA_ROOT, relative_path)
2129

22-
file_handle = open(raster_path, "rb")
30+
if not os.path.exists(raster_path):
31+
raise Http404(
32+
"Raster file not found. Please send an email to contact@telescoop.fr"
33+
)
2334

2435
response = FileResponse(
25-
file_handle,
36+
open(raster_path, "rb"),
2637
content_type="image/tiff",
2738
as_attachment=True,
28-
filename="plantability.tif",
39+
filename=filename,
2940
)
30-
3141
response["Cache-Control"] = "public, max-age=3600"
32-
3342
return response

front/cypress/components/SidebarDownload.cy.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import MapSidePanelDownload from "@/components/map/panels/sidepanel/MapSidePanelDownload.vue"
22
import { useMapStore } from "@/stores/map"
3-
import { DataType } from "@/utils/enum"
3+
import { DataType, DataTypeToDownloadLink } from "@/utils/enum"
44

55
describe("MapSidePanelDownload", () => {
66
beforeEach(() => {
@@ -28,7 +28,7 @@ describe("MapSidePanelDownload", () => {
2828

2929
cy.get("@windowOpen").should(
3030
"have.been.calledWith",
31-
"https://data.grandlyon.com/portail/en/jeux-de-donnees/calque-plantabilite-metropole-lyon/info",
31+
DataTypeToDownloadLink[DataType.PLANTABILITY],
3232
"_blank"
3333
)
3434
})
@@ -47,7 +47,7 @@ describe("MapSidePanelDownload", () => {
4747

4848
cy.get("@windowOpen").should(
4949
"have.been.calledWith",
50-
"https://www.data.gouv.fr/datasets/cartographie-des-zones-climatiques-locales-lcz-des-88-aires-urbaines-de-plus-de-50-000-habitants-de-france-metropolitaine/#/resources/e0c0f5e4-c8bb-4d33-aec9-ba16b5736102",
50+
DataTypeToDownloadLink[DataType.CLIMATE_ZONE],
5151
"_blank"
5252
)
5353
})
@@ -66,7 +66,45 @@ describe("MapSidePanelDownload", () => {
6666

6767
cy.get("@windowOpen").should(
6868
"have.been.calledWith",
69-
"https://data.grandlyon.com/portail/en/jeux-de-donnees/exposition-et-vulnerabilite-aux-fortes-chaleurs-dans-la-metropole-de-lyon/info",
69+
DataTypeToDownloadLink[DataType.VULNERABILITY],
70+
"_blank"
71+
)
72+
})
73+
74+
it("should open correct download link for PLANTABILITY_VULNERABILITY data type", () => {
75+
cy.window().then(() => {
76+
const store = useMapStore()
77+
store.selectedDataType = DataType.PLANTABILITY_VULNERABILITY
78+
})
79+
80+
cy.window().then((win) => {
81+
cy.stub(win, "open").as("windowOpen")
82+
})
83+
84+
cy.get('[data-cy="download-data"]').click()
85+
86+
cy.get("@windowOpen").should(
87+
"have.been.calledWith",
88+
DataTypeToDownloadLink[DataType.PLANTABILITY_VULNERABILITY],
89+
"_blank"
90+
)
91+
})
92+
93+
it("should open correct download link for VEGETATION data type", () => {
94+
cy.window().then(() => {
95+
const store = useMapStore()
96+
store.selectedDataType = DataType.VEGETATION
97+
})
98+
99+
cy.window().then((win) => {
100+
cy.stub(win, "open").as("windowOpen")
101+
})
102+
103+
cy.get('[data-cy="download-data"]').click()
104+
105+
cy.get("@windowOpen").should(
106+
"have.been.calledWith",
107+
DataTypeToDownloadLink[DataType.VEGETATION],
70108
"_blank"
71109
)
72110
})

front/src/utils/enum.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ export const VulnerabilityCategoryOrder = [
9898
]
9999

100100
export const DataTypeToDownloadLink: Record<DataType, string> = {
101-
[DataType.PLANTABILITY]:
102-
"https://data.grandlyon.com/portail/en/jeux-de-donnees/calque-plantabilite-metropole-lyon/info",
101+
[DataType.PLANTABILITY]: "https://carte.iarbre.fr/api/rasters/plantability/",
103102
[DataType.CLIMATE_ZONE]:
104103
"https://www.data.gouv.fr/datasets/cartographie-des-zones-climatiques-locales-lcz-des-88-aires-urbaines-de-plus-de-50-000-habitants-de-france-metropolitaine/#/resources/e0c0f5e4-c8bb-4d33-aec9-ba16b5736102",
105104
[DataType.VULNERABILITY]:
106105
"https://data.grandlyon.com/portail/en/jeux-de-donnees/exposition-et-vulnerabilite-aux-fortes-chaleurs-dans-la-metropole-de-lyon/info",
107106
[DataType.PLANTABILITY_VULNERABILITY]:
108107
"https://data.grandlyon.com/portail/en/jeux-de-donnees/calque-plantabilite-metropole-lyon/info",
109-
[DataType.VEGETATION]: "https://github.com/IGNF/FLAIR-HUB"
108+
[DataType.VEGETATION]: "https://carte.iarbre.fr/api/rasters/vegestrate/"
110109
}

0 commit comments

Comments
 (0)