Compare commits

...

5 Commits

Author SHA1 Message Date
98f5be7c74 Change polygons to feature and remove hardcoded geoJSON 2026-04-28 09:51:06 +02:00
57e3c4d3ad Merge branch 'main' of https://git.olemenke.com/Ol86/HKA-OSM 2026-04-27 14:15:03 +02:00
7fb116ab2f Change name and add new query function 2026-04-27 14:15:01 +02:00
David Sp
b1464cc32e Re-implement and fix detail side section 2026-04-27 14:10:41 +02:00
David Sp
50b261e8ba Move initialization into ngOnInit
Co-authored-by: Copilot <copilot@github.com>
2026-04-27 14:08:01 +02:00
6 changed files with 1818 additions and 161 deletions

1657
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,67 +0,0 @@
import * as G from 'geojson';
// Custom GeoJSON style for the building
export const buildingSytle = {
color: '#d62305', // Red border
weight: 2, // Border width
fillColor: '#d4caca', // Red fill
fillOpacity: 1, // Semi-transparent fill
};
// Example GeoJSON data
export const examplePolygon: G.Polygon = {
type: 'Polygon',
coordinates: [
[
[8.3897067, 49.0149349], // Southwest corner
[8.3904111, 49.0149078], // Southeast corner
[8.3904345, 49.0151542], // Northeast corner
[8.3897302, 49.0151832], // Northwest corner
[8.3897067, 49.0149349], // Closing the polygon by repeating the first point
],
],
};
export const buildings: G.FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
name: 'E',
building: 'university',
},
geometry: {
type: 'Polygon',
coordinates: [
[
[8.3897067, 49.0149349], // Southwest corner
[8.3904111, 49.0149078], // Southeast corner
[8.3904345, 49.0151542], // Northeast corner
[8.3897302, 49.0151832], // Northwest corner
[8.3897067, 49.0149349], // Closing the polygon by repeating the first point
],
],
},
},
{
type: 'Feature',
properties: {
name: 'F',
building: 'university',
},
geometry: {
type: 'Polygon',
coordinates: [
[
[8.3897585, 49.015502], // Southwest corner
[8.3904592, 49.0154731], // Southeast corner
[8.3904833, 49.0157255], // Northeast corner
[8.3897827, 49.0157543], // Northwest corner
[8.3897585, 49.015502], // Closing the polygon by repeating the first point
],
],
},
},
],
};

View File

@@ -1,8 +1,15 @@
<div class="map-wrapper"> <div class="map-wrapper">
<div <div
class="map-container" class="map-container"
[ngClass]="isFullscreen ? 'map-fullscreen' : 'map-details'"
leaflet leaflet
[leafletOptions]="options" [leafletOptions]="options"
(leafletMapReady)="onMapReady($event)" (leafletMapReady)="onMapReady($event)"
></div> ></div>
@if (!isFullscreen && details) {
<div class="details">
<h2>{{ details.name }}</h2>
<p>{{ details.building }}</p>
</div>
}
</div> </div>

View File

@@ -1,9 +1,9 @@
.map-container { .map-container {
padding: 0; padding: 0;
margin: 0; margin: 0;
position: absolute; position: fixed;
top: 54px; top: 54px;
height: calc(100vh - 54px); height: 100%;
width: 100%; width: 100%;
} }
@@ -13,14 +13,14 @@
} }
.map-details { .map-details {
left: 10%; left: 25%;
width: 90%; width: 75%;
} }
.details { .details {
position: absolute; position: fixed;
top: 54px; top: 54px;
left: 0; left: 25px;
width: 10%; width: 25%;
height: 100%; height: 100%;
} }

View File

@@ -1,12 +1,12 @@
import { Component, signal } from '@angular/core'; import { Component, signal, OnInit, ChangeDetectorRef } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { LeafletModule } from '@bluehalo/ngx-leaflet'; import { LeafletModule } from '@bluehalo/ngx-leaflet';
import { center } from '@turf/turf'; import { center } from '@turf/turf';
import { Overpass } from '../overpass'; import { Overpass } from '../overpass';
import * as L from 'leaflet'; import * as L from 'leaflet';
import * as G from 'geojson';
import * as D from './details'; import * as D from './details';
import * as E from './geojson';
@Component({ @Component({
selector: 'app-map', selector: 'app-map',
@@ -14,107 +14,112 @@ import * as E from './geojson';
templateUrl: './map.html', templateUrl: './map.html',
styleUrl: './map.scss', styleUrl: './map.scss',
}) })
export class OSMMap { export class OSMMap implements OnInit {
private buildingLayer?: L.LayerGroup; private buildingLayer?: L.LayerGroup;
protected readonly title = signal('HKA-OSM'); protected readonly title = signal('HKA-OSM');
map?: L.Map; map?: L.Map;
details?: D.Details;
osm?: L.TileLayer;
options: L.MapOptions = {};
layerControl?: L.Control.Layers;
isFullscreen = true; isFullscreen = true;
osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { constructor(
maxNativeZoom: 19, private overpass: Overpass,
maxZoom: 21, private cdr: ChangeDetectorRef,
minZoom: 18, ) {}
attribution: '© OpenStreetMap contributors',
});
// Define map options ngOnInit() {
options: L.MapOptions = { this.osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
layers: [this.osm], maxNativeZoom: 19,
zoom: 18, // Zoom level maxZoom: 21,
center: this.getCenter(), // Center of the map minZoom: 18,
maxBounds: L.latLngBounds( attribution: '© OpenStreetMap contributors',
L.latLng(49.014442, 8.387954), // Southwest corner });
L.latLng(49.017847, 8.395448), // Northeast corner
),
maxBoundsViscosity: 1.0, // Prevent panning outside bounds
};
layerControl = L.control.layers({ // Define map options
OpenStreetMap: this.osm, this.options = {
}); layers: [this.osm],
zoom: 18, // Zoom level
center: this.getCenter(), // Center of the map
};
constructor(private overpass: Overpass) {} this.layerControl = L.control.layers({
OpenStreetMap: this.osm,
onMapReady(map: L.Map) {
this.map = map;
this.layerControl.addTo(this.map);
this.loadBuildings().addTo(this.map);
console.log('Map is ready');
}
loadBuildings() {
return L.geoJSON(E.buildings, {
style: E.buildingSytle,
onEachFeature: (feature, layer) => {
layer.on({
click: (e) => {
this.isFullscreen = !this.isFullscreen;
if (!this.isFullscreen) {
this.map?.setView(this.getCenter(feature.geometry), 20);
this.buildingLayer = this.queryBuildings(feature.properties.name);
this.buildingLayer.addTo(this.map!);
} else {
this.map?.setView(this.getCenter(), 18);
if (this.buildingLayer) {
this.map?.removeLayer(this.buildingLayer);
}
}
console.log('Set isFullscreen to:', this.isFullscreen);
console.log('Clicked on feature:', feature.properties?.name);
},
});
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
permanent: false,
direction: 'top',
});
}
},
}); });
} }
queryBuildings(building_name: string) { onMapReady(map: L.Map) {
this.map = map;
this.layerControl?.addTo(this.map);
this.queryBuildingData().addTo(this.map);
console.log('Map is ready');
}
queryBuildingData() {
// Query the Overpass API for building data and add it to the map as a GeoJSON layer.
const buildingLayer = new L.LayerGroup();
this.overpass.fetchBuildingData().subscribe((data) => {
console.log('Received building data:', data);
L.geoJSON(this.createFeatureCollection(data), {
style: { color: '#d62305', weight: 2, fillColor: '#d4caca', fillOpacity: 1 },
onEachFeature: (feature, layer) => {
layer.on({
click: (e) => {
this.isFullscreen = !this.isFullscreen;
if (!this.isFullscreen) {
this.map?.setView(this.getCenter(feature.geometry), 20);
this.buildingLayer = this.queryIndoorData(feature.properties.name);
this.buildingLayer.addTo(this.map!);
this.details = new D.Details(feature.properties.name, feature.properties.building);
} else {
this.map?.setView(this.getCenter(), 18);
if (this.buildingLayer) {
this.map?.removeLayer(this.buildingLayer);
}
}
console.log('Set isFullscreen to:', this.isFullscreen);
console.log('Clicked on feature:', feature.properties?.name);
this.cdr.detectChanges();
},
});
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
permanent: false,
direction: 'top',
});
}
},
}).addTo(buildingLayer);
});
return buildingLayer;
}
queryIndoorData(building_name: string): L.LayerGroup {
// Get the building name from the feature properties and query the Overpass API for detailed building data, // Get the building name from the feature properties and query the Overpass API for detailed building data,
// including indoor features. // including indoor features.
// Create a new LayerGroup to hold the building polygons // Create a new LayerGroup to hold the building polygons
const resultingLayer = new L.LayerGroup(); const resultingLayer = new L.LayerGroup();
this.overpass.fetchBuildings(building_name).subscribe((data) => {
console.log('Fetched building data from Overpass API:', data);
// Create a mapping of node IDs to their coordinates this.overpass.fetchIndoorData(building_name).subscribe((data) => {
const nodes: Map<number, L.LatLng> = new Map(); console.log('Received indoor data for building:', building_name, data);
data.elements.forEach((element: any) => { L.geoJSON(this.createFeatureCollection(data), {
if (element.type === 'node') { style: { color: '#999999', fillColor: '#e0e0e0', fillOpacity: 1 },
nodes.set(element.id, new L.LatLng(element.lat, element.lon)); onEachFeature: (feature, layer) => {
} if (feature.properties) {
}); layer.bindTooltip(feature.properties.ref ?? 'Test', {
permanent: false,
// Process ways and create polygons direction: 'top',
// TODO: Change to features, to also add the meta data of the building, e.g. the name of the room });
data.elements.forEach((element: any) => { }
if (element.type === 'way') { },
const coordinates: L.LatLng[] = []; }).addTo(resultingLayer);
element.nodes.forEach((nodeId: number) => {
coordinates.push(nodes.get(nodeId)!);
});
L.polygon(coordinates, { color: '#999999', fillColor: '#e0e0e0', fillOpacity: 1 }).addTo(
resultingLayer,
);
}
});
}); });
return resultingLayer; return resultingLayer;
} }
@@ -130,4 +135,46 @@ export class OSMMap {
const result = new L.LatLng(centerPoint[0], centerPoint[1]); const result = new L.LatLng(centerPoint[0], centerPoint[1]);
return result; return result;
} }
private createFeatureCollection(data: any): G.FeatureCollection {
const buildingData: G.FeatureCollection = {
type: 'FeatureCollection',
features: [],
};
// Create a mapping of node IDs to their coordinates
const nodes: Map<number, L.LatLng> = new Map();
data.elements.forEach((element: any) => {
if (element.type === 'node') {
nodes.set(element.id, new L.LatLng(element.lat, element.lon));
}
});
// Process ways and create polygons
// TODO: Change to features, to also add the meta data of the building, e.g. the name of the room
data.elements.forEach((element: any) => {
if (element.type === 'way') {
const coordinates: L.LatLng[] = [];
element.nodes.forEach((nodeId: number) => {
coordinates.push(nodes.get(nodeId)!);
});
buildingData.features.push(
this.createFeature(new Map(Object.entries(element.tags || {})), coordinates),
// this.createFeature(new Map(Object.entries({ name: 'Test' })), coordinates),
);
}
});
return buildingData;
}
private createFeature(properties: Map<string, string>, coordinates: L.LatLng[]): G.Feature {
return {
type: 'Feature',
properties: Object.fromEntries(properties),
geometry: {
type: 'Polygon',
coordinates: [coordinates.map((coord) => [coord.lng, coord.lat])],
},
};
}
} }

View File

@@ -28,11 +28,11 @@ export class Overpass {
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {}
fetchBuildings(building_name: string) { fetchIndoorData(building_name: string) {
const header = { 'Content-Type': 'text/plain' }; const header = { 'Content-Type': 'text/plain' };
const query = ` const query = `
[out:json][timeout:25]; [out:json][timeout:90];
nwr["building"]["operator"="Hochschule Karlsruhe"]["name"="${building_name}"](49.014442, 8.387954, 49.017847, 8.395448)->.building; nwr["building"]["operator"="Hochschule Karlsruhe"]["name"="${building_name}"]->.building;
( (
nwr["indoor"="room"](area.building); nwr["indoor"="room"](area.building);
); );
@@ -42,4 +42,17 @@ export class Overpass {
return this.http.post<OverpassResponse>(this.apiUrl, query, { headers: header }); return this.http.post<OverpassResponse>(this.apiUrl, query, { headers: header });
} }
fetchBuildingData() {
const header = { 'Content-Type': 'text/plain' };
const query = `
[out:json][timeout:90];
(
way["building"="university"]["operator"="Hochschule Karlsruhe"];
);
(._;>;);
out body;
`;
return this.http.post<OverpassResponse>(this.apiUrl, query, { headers: header });
}
} }