Corriger le sens de la flèche du nord, éviter les débordements de texte et hachurer les mailles sans donnée de la planche PDF

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Antoine Jacquin
2026-09-27 16:00:04 +02:00
parent 1336571da7
commit b85c29e5f1
2 changed files with 154 additions and 32 deletions

View File

@ -324,12 +324,66 @@ def _hex(rgb):
def _convergence_deg(cx, cy):
"""Angle (°) du nord géographique par rapport au nord du quadrillage L93."""
"""Convergence du méridien : azimut (°) du nord du quadrillage L93 mesuré
depuis le nord géographique (sens horaire), positif à l'est du méridien
central (3°E)."""
lat0, lon0 = _to_wgs84(cx, cy)
lat1, lon1 = _to_wgs84(cx, cy + 100.0)
return math.degrees(math.atan2((lon1 - lon0) * math.cos(math.radians(lat0)), lat1 - lat0))
def _north_arrow_angle(cx, cy):
"""Angle de rotation reportlab (sens antihoraire, `Canvas.rotate`) pour que
la flèche du nord — dessinée vers le haut, c'est-à-dire vers le nord du
quadrillage — pointe vers le nord géographique."""
return _convergence_deg(cx, cy)
def _wrap_text(c, text, font, size, max_width, sep=" "):
"""Découpe `text` sur `sep` en lignes tenant chacune dans `max_width` (pt)."""
parts = text.split(sep)
lines, cur = [], parts[0]
for part in parts[1:]:
candidate = cur + sep + part
if c.stringWidth(candidate, font, size) <= max_width:
cur = candidate
else:
lines.append(cur)
cur = part
lines.append(cur)
return lines
def _fit_title(c, text, font, size, max_width, min_size=7.0):
"""Réduit la taille de police jusqu'à `min_size` puis tronque avec des
points de suspension pour tenir `text` dans `max_width` (pt)."""
while size > min_size and c.stringWidth(text, font, size) > max_width:
size -= 0.5
if c.stringWidth(text, font, size) > max_width:
while text and c.stringWidth(text + "...", font, size) > max_width:
text = text[:-1]
text = text.rstrip() + "..."
return text, size
def _draw_hatch(c, x, y, w, h, mm, step=3.0):
"""Hachures grises sur fond blanc (maille sans donnée), comme sur la carte."""
from reportlab.lib.colors import Color, white
if w <= 0 or h <= 0:
return
c.saveState()
p = c.beginPath(); p.rect(x * mm, y * mm, w * mm, h * mm)
c.clipPath(p, stroke=0, fill=0)
c.setFillColor(white)
c.rect(x * mm, y * mm, w * mm, h * mm, stroke=0, fill=1)
c.setStrokeColor(Color(0.75, 0.75, 0.75)); c.setLineWidth(0.3)
n = int((w + h) / step) + 2
for k in range(-n, n):
x0 = x + k * step
c.line(x0 * mm, y * mm, (x0 + h) * mm, (y + h) * mm)
c.restoreState()
def _panel_boxes(lay):
"""Rectangles (x, y, w, h) mm du bandeau : légende, qualité, cartouche."""
gap = 4.0
@ -379,7 +433,8 @@ def _draw_grid(c, lay, bbox, mm):
((bbox[0], bbox[1]), (lay.map_x, lay.map_y - 6.2, "l")),
((bbox[2], bbox[1]), (lay.map_x + lay.map_w, lay.map_y - 6.2, "r"))):
lat, lon = _to_wgs84(gx, gy)
txt = f"{lat:.5f} N {lon:.5f} E"
hemi = "E" if lon >= 0 else "O"
txt = f"{lat:.5f} N {abs(lon):.5f} {hemi}"
(c.drawString if align == "l" else c.drawRightString)(px * mm, py * mm, txt)
c.setFont("Helvetica", 5.5)
c.drawString(lay.map_x * mm, (lay.map_y + lay.map_h + 6.2) * mm,
@ -414,11 +469,12 @@ def _draw_legend(c, box, mm):
start, 5.2, stroke=0, fill=1)
c.setFillColor(white)
c.circle(rcx * mm, rcy * mm, r_in * mm, stroke=0, fill=1)
c.setFillColor(black); c.setFont("Helvetica-Bold", 5.5)
for label, deg in (("N", 0), ("E", 90), ("S", 180), ("O", 270)):
c.setFillColor(black); c.setFont("Helvetica-Bold", 5.0)
for label, deg in (("N", 0), ("NE", 45), ("E", 90), ("SE", 135), ("S", 180),
("SO", 225), ("O", 270), ("NO", 315)):
a = math.radians(deg)
c.drawCentredString((rcx + (r_out + 2) * math.sin(a)) * mm,
(rcy + (r_out + 2) * math.cos(a) - 0.8) * mm, label)
c.drawCentredString((rcx + (r_out + 2.4) * math.sin(a)) * mm,
(rcy + (r_out + 2.4) * math.cos(a) - 0.8) * mm, label)
c.setFont("Helvetica", 6)
c.drawString((rcx + r_out + 5) * mm, (rcy + 2) * mm, _pdf_text("Teinte = orientation"))
c.drawString((rcx + r_out + 5) * mm, (rcy - 1) * mm, _pdf_text("de la pente"))
@ -435,7 +491,7 @@ def _draw_legend(c, box, mm):
def _draw_quality(c, box, bbox, zq, mm):
"""Encart qualité : miniature de densité sol + chiffres clés."""
from reportlab.lib.colors import HexColor, black, Color
from reportlab.lib.colors import HexColor, black, white
x, y, w, h = box
c.setFillColor(black); c.setFont("Helvetica-Bold", 8)
c.drawString(x * mm, (y + h - 4) * mm, _pdf_text("Qualité des données"))
@ -444,8 +500,7 @@ def _draw_quality(c, box, bbox, zq, mm):
k = min(avail_w / (bbox[2] - bbox[0]), avail_h / (bbox[3] - bbox[1]))
mw, mh = (bbox[2] - bbox[0]) * k, (bbox[3] - bbox[1]) * k
mx, my = x, y + h - 7 - mh
c.setFillColor(Color(0.85, 0.85, 0.85))
c.rect(mx * mm, my * mm, mw * mm, mh * mm, stroke=0, fill=1) # gris = non renseigné
_draw_hatch(c, mx, my, mw, mh, mm) # hachures = non renseigné
for gx0, gy0, gx1, gy1, v in zq["grid_cells"]:
x0, x1 = max(gx0, bbox[0]), min(gx1, bbox[2])
y0, y1 = max(gy0, bbox[1]), min(gy1, bbox[3])
@ -456,14 +511,17 @@ def _draw_quality(c, box, bbox, zq, mm):
c.rect(mx * mm, my * mm, mw * mm, mh * mm, stroke=1, fill=0)
# Classes
lx, ly = x + mw + 3, y + h - 8
c.setFont("Helvetica", 5.8)
c.setFillColor(black); c.setFont("Helvetica", 5.8)
c.drawString(lx * mm, ly * mm, _pdf_text("Points sol / m²"))
for low, label, color in DENSITY_CLASSES:
ly -= 3.2
c.setFillColor(HexColor(color)); c.rect(lx * mm, ly * mm, 3 * mm, 2.2 * mm, stroke=0, fill=1)
c.setFillColor(black); c.drawString((lx + 4) * mm, (ly + 0.4) * mm, _pdf_text(label))
ly -= 3.2
c.setFillColor(Color(0.85, 0.85, 0.85)); c.rect(lx * mm, ly * mm, 3 * mm, 2.2 * mm, stroke=0, fill=1)
c.setFillColor(white); c.rect(lx * mm, ly * mm, 3 * mm, 2.2 * mm, stroke=0, fill=1)
_draw_hatch(c, lx, ly, 3.0, 2.2, mm, step=1.4)
c.setStrokeColor(black); c.setLineWidth(0.3)
c.rect(lx * mm, ly * mm, 3 * mm, 2.2 * mm, stroke=1, fill=0)
c.setFillColor(black); c.drawString((lx + 4) * mm, (ly + 0.4) * mm, _pdf_text("non renseigné"))
# Chiffres clés
lines = []
@ -481,25 +539,31 @@ def _draw_quality(c, box, bbox, zq, mm):
label = "Acquisition" if zq["acq_sources"] == {"gps"} else "Date de production du fichier"
lines.append(f"{label} : {period}")
if zq["missing_relief"]:
lines.append("Sans relief : " + ", ".join(f"{c_}_{r_}" for c_, r_ in zq["missing_relief"]))
lines.append("Donnée manquante (sans relief) : " + ", ".join(
f"{c_}_{r_}" for c_, r_ in zq["missing_relief"]))
if zq["missing_quality"]:
lines.append("Qualité non renseignée : " + ", ".join(
f"{c_}_{r_}" for c_, r_ in zq["missing_quality"]))
ty = min(my, ly) - 3.5
c.setFont("Helvetica", 5.8)
font_q, size_q = "Helvetica", 5.8
c.setFillColor(black); c.setFont(font_q, size_q)
max_w = w * mm
for line in lines:
if ty < y + 1:
break
c.drawString(x * mm, ty * mm, _pdf_text(line))
ty -= 2.9
sep = ", " if ", " in line else " "
for part in _wrap_text(c, line, font_q, size_q, max_w, sep=sep):
if ty < y + 1:
return
c.drawString(x * mm, ty * mm, _pdf_text(part))
ty -= 2.9
def _draw_cartouche(c, box, lay, bbox, cx, cy, title, now, mm):
"""Titre, échelle graphique et numérique, nord, date, source."""
from reportlab.lib.colors import black, white
x, y, w, h = box
c.setFillColor(black); c.setFont("Helvetica-Bold", 10)
c.drawString(x * mm, (y + h - 5) * mm, _pdf_text(title)[:80])
txt, title_size = _fit_title(c, _pdf_text(title), "Helvetica-Bold", 10, w * mm)
c.setFillColor(black); c.setFont("Helvetica-Bold", title_size)
c.drawString(x * mm, (y + h - 5) * mm, txt)
c.setFont("Helvetica", 7)
c.drawString(x * mm, (y + h - 9) * mm,
_pdf_text(f"Échelle 1:{_fmt_int(lay.scale)} - {lay.paper} {lay.orient} - {lay.dpi} dpi"))
@ -515,27 +579,32 @@ def _draw_cartouche(c, box, lay, bbox, cx, cy, title, now, mm):
c.drawString(bx * mm, (by - 2.8) * mm, "0")
c.drawRightString((bx + bar_mm) * mm, (by - 2.8) * mm, f"{_fmt_int(length)} m")
# Flèche du nord géographique (la carte est orientée nord du quadrillage)
gamma = _convergence_deg(cx, cy)
gamma = _north_arrow_angle(cx, cy)
ax, ay = x + w - 8, y + h - 12
c.saveState(); c.translate(ax * mm, ay * mm); c.rotate(-gamma)
c.saveState(); c.translate(ax * mm, ay * mm); c.rotate(gamma)
p = c.beginPath(); p.moveTo(0, 5 * mm); p.lineTo(-1.8 * mm, -3 * mm); p.lineTo(0, -1.5 * mm)
p.lineTo(1.8 * mm, -3 * mm); p.close()
c.drawPath(p, stroke=0, fill=1)
c.setFont("Helvetica-Bold", 6); c.drawCentredString(0, 6 * mm, "N")
c.restoreState()
c.setFont("Helvetica", 5.5)
c.drawRightString((x + w) * mm, (ay - 6) * mm,
_pdf_text(f"convergence L93 {gamma:+.2f}°".replace(".", ",")))
# Note d'orientation : ligne à part (jamais sur la même hauteur que l'échelle
# numérique — évite le chevauchement des deux textes alignés à droite/gauche).
side = "ouest" if gamma >= 0 else "est"
north_label = f"nord géographique à {abs(gamma):.2f}° à l'{side} du quadrillage".replace(".", ",")
ty = by - 7
c.setFont("Helvetica", 6)
for line in (f"Centre L93 : X {_fmt_int(round(cx))} m Y {_fmt_int(round(cy))} m",
font_c, size_c = "Helvetica", 6
c.setFillColor(black); c.setFont(font_c, size_c)
max_w_c = w * mm
for line in (north_label,
f"Centre L93 : X {_fmt_int(round(cx))} m Y {_fmt_int(round(cy))} m",
f"Zone : {_fmt_int(round(bbox[2] - bbox[0]))} x {_fmt_int(round(bbox[3] - bbox[1]))} m",
f"Exporté le {now:%d/%m/%Y %H:%M}",
"Source : LiDAR HD (c) IGN - rendu lidar_rendu"):
if ty < y + 1:
break
c.drawString(x * mm, ty * mm, _pdf_text(line))
ty -= 3.0
for part in _wrap_text(c, line, font_c, size_c, max_w_c):
if ty < y + 1:
return
c.drawString(x * mm, ty * mm, _pdf_text(part))
ty -= 3.0
def build_pdf(output_dir, lat, lon, paper="A4", orient="paysage", scale=2000,