Skip to content

Commit

Permalink
Merge pull request #750 from MTES-MCT/fix-gpu
Browse files Browse the repository at this point in the history
Remplace les cartes statiques par leurs équivalents dynamiques
  • Loading branch information
alexisig authored Nov 28, 2024
2 parents 1b81314 + a427adc commit 1ab70b0
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 183 deletions.
19 changes: 16 additions & 3 deletions assets/scripts/map_libre/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class MapLibre
this.mapCenter = _options.mapCenter
this.defaultZoom = _options.defaultZoom
this.data = _options.data
this.bounds = _options.bounds

if (!this.targetElement)
{
Expand Down Expand Up @@ -87,6 +88,20 @@ export default class MapLibre

// Set controls
this.map.addControl(new maplibregl.NavigationControl(), 'top-left')
const fullScreenControl = new maplibregl.FullscreenControl()
this.map.addControl(fullScreenControl, 'bottom-right')
this.map.addControl(this.tabs, 'top-right')
this.map.scrollZoom.disable()

fullScreenControl.on('fullscreenstart', () => this.map.scrollZoom.enable())
fullScreenControl.on('fullscreenend', () => this.map.scrollZoom.disable())

if (this.bounds)
{
this.map.fitBounds(this.bounds, {
padding: 50,
})
}
})
}

Expand Down Expand Up @@ -122,9 +137,7 @@ export default class MapLibre
)
}

this.tabs = new Tabs({
tabList,
})
this.tabs = new Tabs({ tabList })
}

setSources()
Expand Down
12 changes: 12 additions & 0 deletions assets/scripts/map_libre/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export default class Tabs

this.buttonsNode = document.createElement('div')
this.buttonsNode.classList.add('tablist')
this.buttonsNode.style.pointerEvents = 'auto'
this.buttonsNode.style.height = '100vh'
this.buttonsNode.style.width = '50px'
this.buttonsNode.setAttribute('role', 'tablist')
this.buttonsNode.setAttribute('aria-label', 'Paramètres de la carte')

Expand Down Expand Up @@ -46,6 +49,10 @@ export default class Tabs

// Create tab
const tabNode = document.createElement('div')
tabNode.style.width = '50vw'
tabNode.style.height = '100vh'
tabNode.style.right = '50px'
tabNode.style.pointerEvents = 'auto'
tabNode.id = `${_tab.id}-tab`
tabNode.classList.add('tab')
tabNode.setAttribute('role', 'tabpanel')
Expand Down Expand Up @@ -97,4 +104,9 @@ export default class Tabs
{
return this.tabsNode.querySelector(`[aria-labelledby="${_id}"]`)
}

onAdd()
{
return this.tabsNode
}
}
11 changes: 10 additions & 1 deletion carto/templates/carto/map_libre.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
{% load highcharts_tags %}
{% load sri %}

{% block top_navigation %}
{% endblock top_navigation %}

{% block title %}
{{ map_name }}
{% endblock title %}

{% block pagetitle %}
{{ map_name }}
{% endblock pagetitle %}
Expand Down Expand Up @@ -40,13 +47,15 @@
{{ data|json_script:"data" }}

<script language="javascript" nonce="[NONCE_PLACEHOLDER]">
new MapLibre ({
new MapLibre({
targetElement: document.getElementById('map__container'),
debug: {{ debug|yesno:"true,false" }},
mapCenter: [{{ center_lng }}, {{ center_lat }}],
defaultZoom: {{ default_zoom }},
bounds: {{ bounds|default:'null' }},
data: JSON.parse(document.getElementById('data').textContent)
})

</script>
{% endlocalize %}
{% endblock bodyend %}
4 changes: 4 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,7 @@ def show_toolbar(request):
# CRISP

CRISP_WEBHOOK_SECRET_KEY = env.str("CRISP_WEBHOOK_SECRET_KEY")

X_FRAME_OPTIONS = "SAMEORIGIN"

XS_SHARING_ALLOWED_METHODS = ["POST", "GET", "OPTIONS", "PUT", "DELETE"]
5 changes: 0 additions & 5 deletions project/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ def response_change(self, request, obj):
messages.add_message(request, messages.INFO, msg)
return HttpResponseRedirect(".")

elif "_generate-gpu" in request.POST:
tasks.generate_theme_map_gpu.delay(obj.id)
messages.add_message(request, messages.INFO, "Génération de l'image des zonages d'urbanismes en cours")
return HttpResponseRedirect(".")

elif "_generate-fill-gpu" in request.POST:
tasks.generate_theme_map_fill_gpu.delay(obj.id)
messages.add_message(
Expand Down
3 changes: 1 addition & 2 deletions project/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

def pre_create_historical_record_callback(sender, **kwargs):
project_history = kwargs["history_instance"]
if project_history.async_theme_map_gpu_done is None:
project_history.async_theme_map_gpu_done = False
if project_history.async_theme_map_fill_gpu_done is None:
project_history.generate_theme_map_fill_gpu = False


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.13 on 2024-11-21 10:31

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("public_data", "0191_landconso_landconsocomparison_landconsostats_landpop_and_more"),
("project", "0101_remove_projectcommune_commune_insee"),
]

operations = [
migrations.RemoveField(
model_name="historicalproject",
name="async_theme_map_gpu_done",
),
migrations.RemoveField(
model_name="historicalproject",
name="theme_map_gpu",
),
migrations.RemoveField(
model_name="project",
name="async_theme_map_gpu_done",
),
migrations.RemoveField(
model_name="project",
name="theme_map_gpu",
),
]
12 changes: 12 additions & 0 deletions project/migrations/0103_merge_20241128_1446.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Django 4.2.13 on 2024-11-28 13:46

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("project", "0102_alter_projectcommune_commune"),
("project", "0102_remove_historicalproject_async_theme_map_gpu_done_and_more"),
]

operations = []
2 changes: 0 additions & 2 deletions project/models/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ def map_tasks(project_id: str) -> List[celery.Task]: # noqa: C901
map_tasks.append(tasks.generate_theme_map_understand_artif.si(project.id))

if project.has_complete_uniform_ocsge_coverage and project.has_zonage_urbanisme:
if not project.async_theme_map_gpu_done:
map_tasks.append(tasks.generate_theme_map_gpu.si(project.id))
if not project.async_theme_map_fill_gpu_done:
map_tasks.append(tasks.generate_theme_map_fill_gpu.si(project.id))

Expand Down
12 changes: 1 addition & 11 deletions project/models/project_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,6 @@ def get_public_key(self) -> str:
storage=PublicMediaStorage(),
)

theme_map_gpu = models.ImageField(
upload_to=upload_in_project_folder,
blank=True,
null=True,
storage=PublicMediaStorage(),
)

theme_map_fill_gpu = models.ImageField(
upload_to=upload_in_project_folder,
blank=True,
Expand All @@ -307,7 +300,6 @@ def get_public_key(self) -> str:
async_generate_theme_map_conso_done = models.BooleanField(default=False)
async_generate_theme_map_artif_done = models.BooleanField(default=False)
async_theme_map_understand_artif_done = models.BooleanField(default=False)
async_theme_map_gpu_done = models.BooleanField(default=False)
async_theme_map_fill_gpu_done = models.BooleanField(default=False)
async_ocsge_coverage_status_done = models.BooleanField(default=False)

Expand Down Expand Up @@ -348,9 +340,7 @@ def async_complete(self) -> bool:
static_maps_ready = static_maps_ready and self.async_theme_map_understand_artif_done

if self.has_zonage_urbanisme and self.has_complete_uniform_ocsge_coverage:
static_maps_ready = (
static_maps_ready and self.async_theme_map_gpu_done and self.async_theme_map_fill_gpu_done
)
static_maps_ready = static_maps_ready and self.async_theme_map_fill_gpu_done

return calculations_and_extend_ready and static_maps_ready

Expand Down
59 changes: 0 additions & 59 deletions project/tasks/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,65 +695,6 @@ def generate_theme_map_understand_artif(self, project_id) -> None:
logger.info("End generate_theme_map_understand_artif, project_id=%d", project_id)


@shared_task(bind=True, max_retries=5)
def generate_theme_map_gpu(self, project_id) -> None:
logger.info("Start generate_theme_map_gpu, project_id=%d", project_id)
try:
diagnostic = Project.objects.get(id=int(project_id))

geom = diagnostic.combined_emprise.transform("3857", clone=True)
srid, wkt = geom.ewkt.split(";")
polygons = shapely.wkt.loads(wkt)
gdf_emprise = geopandas.GeoDataFrame({"geometry": [polygons]}, crs="EPSG:3857")

data = {"color": [], "geometry": []}

for zone_urba in ZoneUrba.objects.intersect(diagnostic.combined_emprise):
srid, wkt = zone_urba.intersection.ewkt.split(";")
polygons = shapely.wkt.loads(wkt)
data["geometry"].append(polygons)
data["color"].append((*[_ / 255 for _ in zone_urba.get_color()], 0.9))

zone_urba_gdf = geopandas.GeoDataFrame(data, crs="EPSG:4326").to_crs(epsg=3857)

fig, ax = plt.subplots(figsize=(15, 10))
plt.axis("off")
fig.set_dpi(150)

zone_urba_gdf.plot(ax=ax, color=zone_urba_gdf["color"])
gdf_emprise.plot(ax=ax, facecolor="none", edgecolor="black")
ax.add_artist(ScaleBar(1))
ax.set_title(
f"Les zones d'urbanisme du territoire «{diagnostic.territory_name}» en {diagnostic.analyse_end_date}"
)
cx.add_basemap(ax, source=settings.OPENSTREETMAP_URL)
cx.add_attribution(ax, text=settings.OPENSTREETMAP_ATTRIBUTION)

img_data = io.BytesIO()
plt.savefig(img_data, bbox_inches="tight", format="jpg")
plt.close()
img_data.seek(0)

race_protection_save_map(
diagnostic.pk,
"async_theme_map_gpu_done",
"theme_map_gpu",
f"theme_map_gpu_{project_id}.jpg",
img_data,
)

except Project.DoesNotExist:
logger.error(f"project_id={project_id} does not exist")

except Exception as exc:
logger.error(exc)
logger.exception(exc)
self.retry(exc=exc, countdown=300)

finally:
logger.info("End generate_theme_map_gpu, project_id=%d", project_id)


@shared_task(bind=True, max_retries=5)
def generate_theme_map_fill_gpu(self, project_id) -> None:
logger.info("Start generate_theme_map_fill_gpu, project_id=%d", project_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<h1 class="mb-3">Détail de la zone urbaine</h1>
<h4 class="mb-3">Détail de la zone urbaine</h1>
<ul class="fr-tags-group">
<li>
<p class="fr-tag fr-tag--sm">Surface de la zone:&nbsp;<strong>{{ surface|floatformat:1 }} ha</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,19 @@ <h2>Aperçu de l'artificialisation</h2>
<h3>Evolution de l'artificialisation entre {{ first_millesime }} et {{ last_millesime }}</h3>

<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col-12 fr-col-lg-8">
<div class="fr-col-12">
<div class="bg-white fr-p-2w h-100">
<div class="d-flex justify-content-end align-items-center fr-mb-2w">
{% include "project/components/widgets/chart_buttons.html" with chart="chart_waterfall" %}
</div>
<div id="chart_waterfall"></div>
</div>
</div>
<div class="fr-col-12 fr-col-lg-4">
{% url 'project:theme-my-artif' project.pk as dynamic_map_url %}
{% include "project/components/widgets/static_map.html" with dynamic_map_url=dynamic_map_url static_map=project.theme_map_understand_artif title="Carte comprendre l'artificialisation de son territoire" %}
</div>
<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col-12">
{% url 'project:theme-my-artif' project.pk as map_url %}
{% include "project/components/map/iframe_map.html" with src=map_url title="Carte comprendre l'artificialisation" %}
</div>
</div>

Expand Down
14 changes: 8 additions & 6 deletions project/templates/project/components/dashboard/consommation.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<h3 id="conso-annuelle">Consommation d'espace annuelle sur le territoire</h3>

<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col-12{% if not is_commune %} fr-col-lg-8{% endif %}">
<div class="fr-col-12">
<div class="bg-white fr-p-2w h-100">
<div class="d-flex justify-content-end align-items-center fr-mb-2w">
{% include "project/components/widgets/chart_buttons.html" with chart="annual_total_conso_chart" %}
Expand All @@ -22,13 +22,15 @@ <h3 id="conso-annuelle">Consommation d'espace annuelle sur le territoire</h3>
<div id="annual_total_conso_chart"></div>
</div>
</div>
{% if not is_commune %}
<div class="fr-col-12 fr-col-lg-4">
{% url 'project:theme-city-conso' project.pk as dynamic_map_url %}
{% include "project/components/widgets/static_map.html" with dynamic_map_url=dynamic_map_url static_map=project.theme_map_conso title="Carte consommation d'espaces des communes du territoire sur la période (en ha)" %}
</div>
{% if not is_commune %}
<div class="fr-grid-row fr-grid-row--gutters">
<div class="fr-col-12">
{% url 'project:theme-city-conso' project.pk as map_url %}
{% include "project/components/map/iframe_map.html" with src=map_url title="ECarte consommation d'espaces des communes du territoire sur la période (en ha)" %}
</div>
{% endif %}
</div>
{% endif %}

<div class="fr-notice bg-white fr-mt-2w">
<div class="fr-px-2w">
Expand Down
Loading

0 comments on commit 1ab70b0

Please sign in to comment.