311 lines
10 KiB
HTML
311 lines
10 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>DXF to GeoJSON with OpenLayers</title>
|
||
<link rel="stylesheet" href="/static/js/ol/theme/default/ol.css">
|
||
<script src="/static/js/ol/lib/OpenLayers.js"></script>
|
||
<script src="/static/js/ol/ol-ext/ol-ext.min.js"></script>
|
||
<link rel="stylesheet" href="/static/js/ol/ol-ext/ol-ext.min.css">
|
||
<script src="/static/js/ol/dxf-parser.js"></script>
|
||
<script src="https://cdn.jsdelivr.net/npm/nurbs/dist/nurbs.min.js"></script>
|
||
|
||
<style>
|
||
#map {
|
||
width: 100%;
|
||
height: 90vh;
|
||
}
|
||
|
||
body {
|
||
margin: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
}
|
||
|
||
input[type="file"], button {
|
||
margin: 10px;
|
||
}
|
||
|
||
.controls {
|
||
position: absolute;
|
||
top: 10px;
|
||
left: 10px;
|
||
z-index: 1000;
|
||
background: white;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
|
||
}
|
||
|
||
.controls label {
|
||
display: block;
|
||
margin-bottom: 5px;
|
||
}
|
||
|
||
.controls input[type="range"] {
|
||
width: 100%;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<h2>DXF to GeoJSON Viewer with OpenLayers</h2>
|
||
<input type="file" id="fileInput" accept=".dxf">
|
||
<button id="exportGeoJSON">Экспортировать GeoJSON</button>
|
||
<div id="map"></div>
|
||
<div class="controls">
|
||
<label><input type="checkbox" id="lockMap"> Закрепить карту</label>
|
||
<label>Прозрачность <input type="range" id="opacitySlider" min="0" max="1" step="0.01" value="1"></label>
|
||
<button id="pinToMap">Закрепить на карте</button>
|
||
<button id="deleteLayer">Удалить слой</button>
|
||
</div>
|
||
<script>
|
||
let i = 0
|
||
scanLayer = new ol.layer.Tile({
|
||
source: new ol.source.XYZ({
|
||
url: '/media/map/orth/tiles/{z}/{x}/{y}.png',
|
||
}),
|
||
extent: ol.proj.transformExtent(
|
||
[
|
||
parseFloat('87.889212480'),
|
||
parseFloat('53.633875446'),
|
||
parseFloat('87.965792495'),
|
||
parseFloat('53.713365769')
|
||
],
|
||
'EPSG:4326', // Исходная проекция
|
||
'EPSG:3857' // Целевая проекция
|
||
)
|
||
});
|
||
|
||
|
||
scanLayer.set('name', 'Ортофотоплан')
|
||
|
||
const map = new ol.Map({
|
||
target: 'map',
|
||
layers: [
|
||
new ol.layer.Tile({
|
||
source: new ol.source.OSM()
|
||
}),
|
||
scanLayer
|
||
],
|
||
view: new ol.View({
|
||
center: ol.proj.fromLonLat([0, 0]),
|
||
zoom: 4
|
||
})
|
||
});
|
||
let vectorLayer;
|
||
let scale = 1; // Масштаб слоя
|
||
let isLocked = false;
|
||
document.getElementById('fileInput').addEventListener('change', function (event) {
|
||
const file = event.target.files[0];
|
||
if (file) {
|
||
const reader = new FileReader();
|
||
reader.onload = function (e) {
|
||
const parser = new DxfParser();
|
||
try {
|
||
const dxf = parser.parseSync(e.target.result);
|
||
const geojson = dxfToGeoJSON(dxf);
|
||
addGeoJSONToMap(geojson);
|
||
} catch (error) {
|
||
console.error('DXF Parsing Error:', error);
|
||
}
|
||
};
|
||
reader.readAsText(file);
|
||
}
|
||
});
|
||
document.getElementById('exportGeoJSON').addEventListener('click', function () {
|
||
if (vectorLayer) {
|
||
const features = vectorLayer.getSource().getFeatures();
|
||
const geojson = new ol.format.GeoJSON().writeFeatures(features, {
|
||
featureProjection: 'EPSG:3857'
|
||
});
|
||
downloadGeoJSON(geojson);
|
||
}
|
||
});
|
||
document.getElementById('lockMap').addEventListener('change', function () {
|
||
isLocked = this.checked;
|
||
if (isLocked) {
|
||
map.getView().setCenter(map.getView().getCenter());
|
||
map.getView().setZoom(map.getView().getZoom());
|
||
}
|
||
});
|
||
document.getElementById('opacitySlider').addEventListener('input', function () {
|
||
if (vectorLayer) {
|
||
vectorLayer.setOpacity(this.value);
|
||
}
|
||
});
|
||
document.getElementById('pinToMap').addEventListener('click', function () {
|
||
if (vectorLayer) {
|
||
vectorLayer.setZIndex(Infinity);
|
||
}
|
||
});
|
||
document.getElementById('deleteLayer').addEventListener('click', function () {
|
||
if (vectorLayer) {
|
||
map.removeLayer(vectorLayer);
|
||
vectorLayer = null;
|
||
}
|
||
});
|
||
|
||
function getCoordinates(entity) {
|
||
if (entity.type === 'LINE') {
|
||
return [[entity.vertices[0].x, entity.vertices[0].y], [entity.vertices[1].x, entity.vertices[1].y]];
|
||
}
|
||
if (entity.type === 'LWPOLYLINE' || entity.type === 'POLYLINE') {
|
||
return entity.vertices.map(v => [v.x, v.y]);
|
||
}
|
||
if (entity.type === 'CIRCLE') {
|
||
return [[entity.center.x, entity.center.y]];
|
||
}
|
||
if (entity.type === 'ARC') {
|
||
return arcToLine(entity);
|
||
}
|
||
if (entity.type === 'POINT') {
|
||
return [[entity.position.x, entity.position.y]];
|
||
}
|
||
return [];
|
||
}
|
||
|
||
function normalizeCoordinates(entities, dxfHeader) {
|
||
let minX = dxfHeader.$EXTMIN.x;
|
||
let minY = dxfHeader.$EXTMIN.y;
|
||
|
||
return entities.map(entity => {
|
||
const coords = getCoordinates(entity).map(([x, y]) => {
|
||
const normalizedX = x - minX;
|
||
const normalizedY = y - minY;
|
||
return convertToLonLat([normalizedX, normalizedY]);
|
||
});
|
||
return { ...entity, normalizedCoords: coords };
|
||
});
|
||
}
|
||
|
||
function dxfToGeoJSON(dxf) {
|
||
const normalizedEntities = normalizeCoordinates(dxf.entities, dxf.header);
|
||
const features = normalizedEntities.map(entity => {
|
||
if (entity.type === 'LINE') {
|
||
return createFeature('LineString', entity.normalizedCoords);
|
||
} else if (entity.type === 'LWPOLYLINE' || entity.type === 'POLYLINE') {
|
||
const coords = entity.normalizedCoords;
|
||
return createFeature(entity.closed ? 'Polygon' : 'LineString', entity.closed ? [coords] : coords);
|
||
} else if (entity.type === 'CIRCLE') {
|
||
return createFeature('Point', entity.normalizedCoords[0], { radius: entity.radius });
|
||
} else if (entity.type === 'ARC') {
|
||
return createFeature('LineString', entity.normalizedCoords);
|
||
} else if (entity.type === 'POINT') {
|
||
return createFeature('Point', entity.normalizedCoords[0]);
|
||
} else if (entity.type === 'SPLINE') {
|
||
return createFeature('LineString', entity.normalizedCoords);
|
||
}
|
||
return null;
|
||
}).filter(f => f !== null);
|
||
|
||
return { type: 'FeatureCollection', features: features };
|
||
}
|
||
|
||
function createFeature(type, coordinates, properties) {
|
||
i++
|
||
return {
|
||
type: 'Feature',
|
||
geometry: {type, coordinates},
|
||
properties
|
||
};
|
||
}
|
||
|
||
function arcToLine(arc) {
|
||
const coords = [];
|
||
const steps = 32;
|
||
const startAngle = (arc.startAngle * Math.PI) / 180;
|
||
const endAngle = (arc.endAngle * Math.PI) / 180;
|
||
const angleStep = (endAngle - startAngle) / steps;
|
||
for (let i = 0; i <= steps; i++) {
|
||
const angle = startAngle + i * angleStep;
|
||
coords.push([
|
||
arc.center.x + arc.radius * Math.cos(angle),
|
||
arc.center.y + arc.radius * Math.sin(angle)
|
||
]);
|
||
}
|
||
return coords;
|
||
}
|
||
|
||
function addGeoJSONToMap(geojson) {
|
||
if (vectorLayer) {
|
||
map.removeLayer(vectorLayer);
|
||
}
|
||
const vectorSource = new ol.source.Vector({
|
||
features: new ol.format.GeoJSON().readFeatures(geojson, {
|
||
featureProjection: 'EPSG:3857'
|
||
})
|
||
});
|
||
vectorLayer = new ol.layer.Vector({
|
||
source: vectorSource,
|
||
style: new ol.style.Style({
|
||
stroke: new ol.style.Stroke({
|
||
color: 'red',
|
||
width: 2
|
||
}),
|
||
image: new ol.style.Circle({
|
||
radius: 5,
|
||
fill: new ol.style.Fill({color: 'blue'})
|
||
})
|
||
})
|
||
});
|
||
map.addLayer(vectorLayer);
|
||
const group = new ol.Collection(vectorSource.getFeatures());
|
||
// Добавляем возможность перетаскивания
|
||
const translateInteraction = new ol.interaction.Translate({
|
||
features: group,
|
||
});
|
||
map.addInteraction(translateInteraction);
|
||
|
||
// Добавляем возможность масштабирования и перемещения с помощью маркеров
|
||
const transformInteraction = new ol.interaction.Transform({
|
||
features: group,
|
||
addCondition: ol.events.condition.shiftKeyOnly
|
||
});
|
||
map.addInteraction(transformInteraction);
|
||
|
||
// Выделение всех фичей
|
||
transformInteraction.setSelection(group)
|
||
|
||
map.on('pointermove', () => {
|
||
transformInteraction.setSelection(group)
|
||
}
|
||
)
|
||
|
||
const extent = vectorSource.getExtent();
|
||
if (!ol.extent.isEmpty(extent)) {
|
||
map.getView().fit(extent, {padding: [20, 20, 20, 20]});
|
||
} else {
|
||
console.warn('DXF не содержит геометрических объектов.');
|
||
}
|
||
}
|
||
|
||
function convertToLonLat([x, y]) {
|
||
|
||
const kx = 1.676;
|
||
const ky = 1.001;
|
||
|
||
console.log(`Координаты до конвертации [${x},${y}]`)
|
||
res = ol.proj.transform([x, y], 'EPSG:3857', 'EPSG:4326')
|
||
console.log(`Координаты после конвертации [${res[0]},${res[1]}]`)
|
||
res[0] = kx * res[0] + 87.915936 // ДОЛГОТА
|
||
res[1] = ky * res[1] + 53.688360 // ШИРОТА
|
||
return res;
|
||
}
|
||
|
||
function downloadGeoJSON(geojson) {
|
||
const blob = new Blob([geojson], {type: 'application/json'});
|
||
const url = URL.createObjectURL(blob);
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = 'export.geojson';
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
document.body.removeChild(a);
|
||
}
|
||
|
||
|
||
</script>
|
||
</body>
|
||
</html> |