стабильно но неправильно

This commit is contained in:
zakhy 2025-02-28 09:12:47 +07:00
parent a5820d4930
commit 6c501a74be
2 changed files with 83 additions and 19 deletions

View File

@ -121,6 +121,10 @@
<script>
let centerLat = parseFloat('{{lat}}')
let centerLon = parseFloat('{{lon}}')
let zeroLon = parseFloat("{{ zeroLon }}") ? parseFloat("{{ zeroLon }}") : centerLon
let zeroLat = parseFloat("{{ zeroLat }}") ? parseFloat("{{ zeroLat }}") : centerLat
let transformInteraction = {}
let translateInteraction = {}
let i = 0
@ -153,31 +157,56 @@
const iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([centerLon, centerLat])),
name: 'Null Island',
population: 4000,
rainfall: 500,
});
const iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: '/static/img/earthquake.png',
width: 20,
height: 20,
}),
});
function getCoordsByFeature(feature, resoluiton) {
const coords = feature.getGeometry().getCoordinates()
const coordsIn4326 = ol.proj.transform(coords, 'EPSG:3857', 'EPSG:4326');
return {
lat: coordsIn4326[0],
lon: coordsIn4326[1],
formatted: `${coordsIn4326[0]} ${coordsIn4326[1]}`,
}
}
function iconStyle(feature, resolution) {
return new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: '/static/img/earthquake.png',
width: 20,
height: 20,
}),
text: new ol.style.Text({
textAlign: 'center',
textBaseline: 'middle',
text: getCoordsByFeature(feature, resolution).formatted,
fill: new ol.style.Fill({color: '#000'}),
offsetX: 0,
offsetY: -25,
placement: 'point',
maxAngle: '0.7853981633974483',
overflow: 'false',
rotation: parseFloat('0'),
backgroundFill: new ol.style.Fill({color: '#fff'}),
backgroundStroke: new ol.style.Stroke({color: '#000', width: 1}),
padding: [3, 3, 3, 3],
})
});
}
iconFeature.setStyle(iconStyle);
const exampleSource = new ol.source.Vector({
features: [iconFeature],
});
const exampleLayer = new ol.layer.Vector({
source: exampleSource,
});
layers.push(exampleLayer)
// const exampleLayer = new ol.layer.Vector({
// source: exampleSource,
// style: iconStyle
// });
// layers.push(exampleLayer)
const map = new ol.Map({
target: 'map',
layers: layers,
@ -197,6 +226,11 @@
map.addInteraction(transformInteraction);
let vectorLayer;
// Обработчик клика на карту
map.on('click', function (event) {
const features = map.getFeaturesAtPixel(event.pixel); // Получаем объекты в точке клика
console.log(getCoordsByFeature(features[0]).formatted)
});
// const ctrl = new ol.control.Status();
// map.addControl(ctrl);
// // Show information on each frame
@ -260,6 +294,16 @@
color: feature.get("color"),
width: 1,
}),
image: new ol.style.Circle({
radius: 10, // Размер точки
fill: new ol.style.Fill({
color: 'red', // Цвет заливки
}),
stroke: new ol.style.Stroke({
color: 'white', // Цвет обводки
width: 2, // Толщина обводки
}),
}),
});
};
@ -322,6 +366,10 @@
}
return null;
}).filter(f => f !== null);
features.push(
createFeature('Point', [zeroLat, zeroLon], {zeroPoint: true})
)
console.log(features)
return {type: 'FeatureCollection', features: features};
}
@ -431,8 +479,10 @@
const ky = 1.001;
let res = ol.proj.transform([x, y], 'EPSG:3857', 'EPSG:4326')
res[0] = kx * res[0] + 87.915936 // ДОЛГОТА
res[1] = ky * res[1] + 53.688360 // ШИРОТА
res[0] = kx * res[0]
+ zeroLat // ДОЛГОТА
res[1] = ky * res[1]
+ zeroLon // ШИРОТА
return res;
}
@ -460,6 +510,14 @@
formData.append('file', new Blob([geojson], {type: 'application/json'}), 'export.geojson');
formData.append('name', name.value)
const zeroPointFeatures = features.filter((feature) => feature.get('zeroPoint'))
if (zeroPointFeatures.length > 0) {
console.log(zeroPointFeatures)
const zeroCoords = getCoordsByFeature(zeroPointFeatures[0])
formData.append('zeroLat', zeroCoords.lat)
formData.append('zeroLon', zeroCoords.lon)
}
// Отправляем данные на сервер
fetch(window.location.href, {
method: 'POST',

View File

@ -44,6 +44,12 @@ def create_layer(request):
context = generalmodule.get_context_template()
if request.method == "POST":
form = StaticLayerForm(request.POST, request.FILES)
zeroLat = request.POST.get('zeroLat')
zeroLon = request.POST.get('zeroLon')
if zeroLon and zeroLat:
settingsmodule.set_settings("zeroLat", zeroLat)
settingsmodule.set_settings("zeroLon", zeroLon)
if form.is_valid():
form.save()
return redirect("mainindex")