From dc9a0e3c13dbf0773530a20152c26c7eb7451201 Mon Sep 17 00:00:00 2001 From: yarik Date: Fri, 24 Feb 2017 16:32:56 +0200 Subject: [PATCH] Road fatality --- src/app/data/road/road.component.html | 2 +- src/app/data/road/road.component.ts | 11 +++++++++-- src/components/road-map.component.ts | 44 +++++++++++++++++++++++++++++++++++++++++++- src/models/node.ts | 23 +++++++++++++++++++++++ src/models/way.ts | 8 ++++++++ src/services/road.service.ts | 28 ++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/models/node.ts create mode 100644 src/models/way.ts diff --git a/src/app/data/road/road.component.html b/src/app/data/road/road.component.html index e4f311c..074ae8b 100644 --- a/src/app/data/road/road.component.html +++ b/src/app/data/road/road.component.html @@ -2,7 +2,7 @@
diff --git a/src/app/data/road/road.component.ts b/src/app/data/road/road.component.ts index 7cdbc4b..2499d6e 100644 --- a/src/app/data/road/road.component.ts +++ b/src/app/data/road/road.component.ts @@ -13,6 +13,7 @@ import { RendererComponent } from '../../../helpers/renderer.component'; import { RoadTypeSelectList } from '../../../models/road-type-select-list'; import { routerTransition } from '../../../animations/router.animation'; +import {Way} from "../../../models/way"; @Component({ // tslint:disable-next-line:component-selector @@ -35,8 +36,14 @@ export class RoadComponent extends StatementBase { } public showOnMap(): void { - console.log(this.gridOptions.api.getSelectedRows()[0].id); - this.mapComponent.showMap(); + let selectedRows: any[] = this.gridOptions.api.getSelectedRows(); + if (selectedRows.length) { + let id = selectedRows[0].id; + this.service.getRelation(id).then(x => { + this.mapComponent.setWays(x); + return x; + }); + } } protected createColumnDefs(): any[] { diff --git a/src/components/road-map.component.ts b/src/components/road-map.component.ts index 8710b7b..dab513c 100644 --- a/src/components/road-map.component.ts +++ b/src/components/road-map.component.ts @@ -1,5 +1,6 @@ import { Component, Input, AfterViewInit } from '@angular/core'; import * as L from 'leaflet'; +import { Way } from '../models/way'; @Component({ // tslint:disable-next-line:component-selector @@ -9,6 +10,7 @@ import * as L from 'leaflet'; export class RoadMapComponent implements AfterViewInit { protected isVisible: boolean = false; protected map: L.Map; + protected ways: Way[]; @Input() identificator: string = 'mapID'; public ngAfterViewInit(): void { @@ -21,6 +23,46 @@ export class RoadMapComponent implements AfterViewInit { this.isVisible = true; } public getHeight(): string { - return this.isVisible ? '100%' : '0'; + return this.isVisible ? '100%' : '100%'; + } + public setWays(ways: Way[]): void { + this.ways = ways; + this.showWays(); + } + protected showWays(): void { + let minLat: number = 0; + let maxLat: number = 0; + let minLon: number = 0; + let maxLon: number = 0; + console.log(this.ways); + this.ways.forEach((way) => { + let nodes: L.LatLng[] = []; + way.nodes.forEach((node) => { + if (minLat == 0) { + minLat = node.lat; + maxLat = node.lat; + } else { + minLat = (minLat > node.lat)?node.lat:minLat; + maxLat = (maxLat < node.lat)?node.lat:maxLat; + } + if (minLon == 0) { + minLon = node.lon; + maxLon = node.lon; + } else { + minLon = (minLon > node.lon)?node.lon:minLon; + maxLon = (maxLon < node.lon)?node.lon:maxLon; + } + let latLng = node.getLatLng(); + nodes.push(latLng); + }); + way.polyline = L.polyline(nodes, {color: 'red'}).addTo(this.map); + console.log(way.polyline); + }); + this.map.fitBounds([ + [minLat, minLon], + [maxLat, maxLon] + ]); + console.log(minLat, maxLat, minLon, maxLon); + this.showMap(); } } diff --git a/src/models/node.ts b/src/models/node.ts new file mode 100644 index 0000000..0730538 --- /dev/null +++ b/src/models/node.ts @@ -0,0 +1,23 @@ +import * as L from 'leaflet'; + +export class Node { + id: number; + index: number; + lat: number; + lon: number; + latLng: L.LatLng; + protected createLatLng(): void { + this.latLng = L.latLng(this.lat, this.lon); + } + public getLatLng(): L.LatLng { + if (this.latLng == undefined) { + this.createLatLng(); + } + return this.latLng; + } + public setLatLng(lat: number, lon: number): void { + this.lat = lat; + this.lon = lon; + this.createLatLng(); + } +} \ No newline at end of file diff --git a/src/models/way.ts b/src/models/way.ts new file mode 100644 index 0000000..6ec6450 --- /dev/null +++ b/src/models/way.ts @@ -0,0 +1,8 @@ +import { Node } from './node'; +import * as L from 'leaflet'; + +export class Way { + id: number; + nodes: Node[]; + polyline: L.Polyline; +} \ No newline at end of file diff --git a/src/services/road.service.ts b/src/services/road.service.ts index 8b49e44..e6270c6 100644 --- a/src/services/road.service.ts +++ b/src/services/road.service.ts @@ -4,6 +4,9 @@ import { Http } from '@angular/http'; import { StatementBaseService } from './statement.base.service'; import { Road } from '../models/road'; +import {Way} from "../models/way"; +import {Node} from "../models/node"; +import {id} from "@swimlane/ngx-charts/release/utils/id"; @Injectable() export class RoadService extends StatementBaseService { @@ -14,10 +17,35 @@ export class RoadService extends StatementBaseService { public createModel(): Object { return new Road(); } + public getRelation(id: number): Promise { + return this.http.get(this.url + '/relation?id=' + id, { headers: this.headers }) + .toPromise() + .then(x => this.decodeWays(x.json())) + .catch(this.handleError); + } protected parseModels(json: any): any[] { return json.roadEditDsM as Road[]; }; protected parseModel(json: any): any { return json as Road; }; + protected decodeWays(json: Object[]): Way[] { + let result: Way[] = []; + json.forEach((way: Way) => { + let nodes: Node[] = []; + way.nodes.forEach((node: Node) => { + let nodeObj: Node = new Node(); + nodeObj.id = node.id; + nodeObj.index = node.index; + nodeObj.lat = node.lat; + nodeObj.lon = node.lon; + nodes.push(nodeObj); + }); + let wayObj: Way = new Way(); + wayObj.id = way.id; + wayObj.nodes = nodes; + result.push(wayObj); + }); + return result; + } } -- libgit2 0.21.4