road.service.ts 2.13 KB
import {Injectable} from '@angular/core';
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 {
    protected url: string = 'http://localhost:5000/road';

    constructor(protected http: Http) {
        super(http);
    }

    public createModel(): Object {
        return new Road();
    }

    public getRelation(id: number): Promise<any> {
        return this.http.get(this.url + '/relation?id=' + id, {headers: this.headers})
            .toPromise()
            .then(x => this.decodeWays(x.json()))
            .catch(this.handleError);
    }

    public getRoadByWay(id: number): Promise<any> {
        return this.http.get(this.url + '/roadbyway?id=' + id, {headers: this.headers})
            .toPromise()
            .then(x => this.parseModel(x.json()))
            .catch(this.handleError);
    };

    public getRoadByNode(id: number): Promise<any> {
        return this.http.get(this.url + '/roadbynode?id=' + id, {headers: this.headers})
            .toPromise()
            .then(x => this.parseModel(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;
    }
}