Spaces:
Sleeping
Sleeping
Update pages/MipMAP.py
Browse files- pages/MipMAP.py +150 -1
pages/MipMAP.py
CHANGED
|
@@ -4,7 +4,156 @@ from streamlit_folium import folium_static
|
|
| 4 |
from folium.plugins import Draw, MousePosition
|
| 5 |
from branca.element import Template, MacroElement
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def main():
|
| 10 |
st.title("SwissScape Advanced")
|
|
|
|
| 4 |
from folium.plugins import Draw, MousePosition
|
| 5 |
from branca.element import Template, MacroElement
|
| 6 |
|
| 7 |
+
# Configuration de la page Streamlit
|
| 8 |
+
st.set_page_config(layout="wide", page_title="SwissScape Advanced")
|
| 9 |
+
|
| 10 |
+
# Formats de papier standard (en mètres)
|
| 11 |
+
PAPER_FORMATS = {
|
| 12 |
+
"A4": (0.210, 0.297),
|
| 13 |
+
"A3": (0.297, 0.420),
|
| 14 |
+
"A2": (0.420, 0.594),
|
| 15 |
+
"A1": (0.594, 0.841),
|
| 16 |
+
"A0": (0.841, 1.189),
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Taille du bloc d'impression en mètres
|
| 20 |
+
PRINT_BLOCK_SIZE = 0.4 # 40 cm
|
| 21 |
+
|
| 22 |
+
class AdvancedMapControl(MacroElement):
|
| 23 |
+
def __init__(self):
|
| 24 |
+
super(AdvancedMapControl, self).__init__()
|
| 25 |
+
self._template = Template("""
|
| 26 |
+
{% macro script(this, kwargs) %}
|
| 27 |
+
var advancedMapControl = L.control({position: 'topright'});
|
| 28 |
+
advancedMapControl.onAdd = function (map) {
|
| 29 |
+
var div = L.DomUtil.create('div', 'advanced-map-control');
|
| 30 |
+
div.innerHTML = `
|
| 31 |
+
<select id="paper-format">
|
| 32 |
+
<option value="A4">A4</option>
|
| 33 |
+
<option value="A3">A3</option>
|
| 34 |
+
<option value="A2">A2</option>
|
| 35 |
+
<option value="A1">A1</option>
|
| 36 |
+
<option value="A0">A0</option>
|
| 37 |
+
</select>
|
| 38 |
+
<input type="number" id="scale" value="1000" min="1" step="100">
|
| 39 |
+
<button id="apply-format">Appliquer Format</button>
|
| 40 |
+
<button id="calculate-blocks">Calculer Blocs</button>
|
| 41 |
+
<div id="block-result"></div>
|
| 42 |
+
<button id="export-geojson">Exporter GeoJSON</button>
|
| 43 |
+
`;
|
| 44 |
+
L.DomEvent.disableClickPropagation(div);
|
| 45 |
+
return div;
|
| 46 |
+
};
|
| 47 |
+
advancedMapControl.addTo({{ this._parent.get_name() }});
|
| 48 |
+
|
| 49 |
+
var rectangle;
|
| 50 |
+
var grid;
|
| 51 |
+
|
| 52 |
+
document.getElementById('apply-format').addEventListener('click', function() {
|
| 53 |
+
var format = document.getElementById('paper-format').value;
|
| 54 |
+
var scale = parseInt(document.getElementById('scale').value);
|
| 55 |
+
var center = {{ this._parent.get_name() }}.getCenter();
|
| 56 |
+
var paperSizes = {
|
| 57 |
+
'A4': [0.210, 0.297],
|
| 58 |
+
'A3': [0.297, 0.420],
|
| 59 |
+
'A2': [0.420, 0.594],
|
| 60 |
+
'A1': [0.594, 0.841],
|
| 61 |
+
'A0': [0.841, 1.189]
|
| 62 |
+
};
|
| 63 |
+
var size = paperSizes[format];
|
| 64 |
+
var widthMeters = size[0] * scale;
|
| 65 |
+
var heightMeters = size[1] * scale;
|
| 66 |
+
var bounds = [
|
| 67 |
+
[center.lat - heightMeters/2/111320, center.lng - widthMeters/2/(111320*Math.cos(center.lat*Math.PI/180))],
|
| 68 |
+
[center.lat + heightMeters/2/111320, center.lng + widthMeters/2/(111320*Math.cos(center.lat*Math.PI/180))]
|
| 69 |
+
];
|
| 70 |
+
|
| 71 |
+
if (rectangle) {
|
| 72 |
+
{{ this._parent.get_name() }}.removeLayer(rectangle);
|
| 73 |
+
}
|
| 74 |
+
if (grid) {
|
| 75 |
+
{{ this._parent.get_name() }}.removeLayer(grid);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
rectangle = L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo({{ this._parent.get_name() }});
|
| 79 |
+
{{ this._parent.get_name() }}.fitBounds(bounds);
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
document.getElementById('calculate-blocks').addEventListener('click', function() {
|
| 83 |
+
if (!rectangle) {
|
| 84 |
+
alert("Veuillez d'abord dessiner une bbox ou appliquer un format de papier.");
|
| 85 |
+
return;
|
| 86 |
+
}
|
| 87 |
+
var scale = parseInt(document.getElementById('scale').value);
|
| 88 |
+
var bounds = rectangle.getBounds();
|
| 89 |
+
var widthMeters = (bounds.getEast() - bounds.getWest()) * 111320 * Math.cos(bounds.getCenter().lat * Math.PI / 180);
|
| 90 |
+
var heightMeters = (bounds.getNorth() - bounds.getSouth()) * 111320;
|
| 91 |
+
|
| 92 |
+
var blockSizeMeters = 0.4 * scale; // 40 cm * scale
|
| 93 |
+
var blocksX = Math.ceil(widthMeters / blockSizeMeters);
|
| 94 |
+
var blocksY = Math.ceil(heightMeters / blockSizeMeters);
|
| 95 |
+
|
| 96 |
+
var result = `Blocs nécessaires : ${blocksX} x ${blocksY} (${blocksX * blocksY} au total)`;
|
| 97 |
+
document.getElementById('block-result').innerText = result;
|
| 98 |
+
|
| 99 |
+
if (grid) {
|
| 100 |
+
{{ this._parent.get_name() }}.removeLayer(grid);
|
| 101 |
+
}
|
| 102 |
+
grid = L.layerGroup().addTo({{ this._parent.get_name() }});
|
| 103 |
+
|
| 104 |
+
var latStep = blockSizeMeters / 111320;
|
| 105 |
+
var lngStep = blockSizeMeters / (111320 * Math.cos(bounds.getCenter().lat * Math.PI / 180));
|
| 106 |
+
|
| 107 |
+
for (var lat = bounds.getSouth(); lat <= bounds.getNorth(); lat += latStep) {
|
| 108 |
+
L.polyline([[lat, bounds.getWest()], [lat, bounds.getEast()]], {color: "#ff7800", weight: 1}).addTo(grid);
|
| 109 |
+
}
|
| 110 |
+
for (var lng = bounds.getWest(); lng <= bounds.getEast(); lng += lngStep) {
|
| 111 |
+
L.polyline([[bounds.getSouth(), lng], [bounds.getNorth(), lng]], {color: "#ff7800", weight: 1}).addTo(grid);
|
| 112 |
+
}
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
document.getElementById('export-geojson').addEventListener('click', function() {
|
| 116 |
+
if (rectangle) {
|
| 117 |
+
var bounds = rectangle.getBounds();
|
| 118 |
+
var geojson = {
|
| 119 |
+
"type": "Feature",
|
| 120 |
+
"properties": {
|
| 121 |
+
"format": document.getElementById('paper-format').value,
|
| 122 |
+
"scale": parseInt(document.getElementById('scale').value),
|
| 123 |
+
"printBlockSize": 0.4 // 40 cm in meters
|
| 124 |
+
},
|
| 125 |
+
"geometry": {
|
| 126 |
+
"type": "Polygon",
|
| 127 |
+
"coordinates": [[
|
| 128 |
+
[bounds.getWest(), bounds.getSouth()],
|
| 129 |
+
[bounds.getEast(), bounds.getSouth()],
|
| 130 |
+
[bounds.getEast(), bounds.getNorth()],
|
| 131 |
+
[bounds.getWest(), bounds.getNorth()],
|
| 132 |
+
[bounds.getWest(), bounds.getSouth()]
|
| 133 |
+
]]
|
| 134 |
+
}
|
| 135 |
+
};
|
| 136 |
+
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(geojson));
|
| 137 |
+
var downloadAnchorNode = document.createElement('a');
|
| 138 |
+
downloadAnchorNode.setAttribute("href", dataStr);
|
| 139 |
+
downloadAnchorNode.setAttribute("download", "export.geojson");
|
| 140 |
+
document.body.appendChild(downloadAnchorNode);
|
| 141 |
+
downloadAnchorNode.click();
|
| 142 |
+
downloadAnchorNode.remove();
|
| 143 |
+
} else {
|
| 144 |
+
alert("Veuillez d'abord dessiner une bbox ou appliquer un format de papier.");
|
| 145 |
+
}
|
| 146 |
+
});
|
| 147 |
+
|
| 148 |
+
{{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function (e) {
|
| 149 |
+
if (rectangle) {
|
| 150 |
+
{{ this._parent.get_name() }}.removeLayer(rectangle);
|
| 151 |
+
}
|
| 152 |
+
rectangle = e.layer;
|
| 153 |
+
{{ this._parent.get_name() }}.addLayer(rectangle);
|
| 154 |
+
});
|
| 155 |
+
{% endmacro %}
|
| 156 |
+
""")
|
| 157 |
|
| 158 |
def main():
|
| 159 |
st.title("SwissScape Advanced")
|