Blame view

src/services/road.service.ts 2.13 KB
229df284   Yarik   Big map commit
1
2
  import {Injectable} from '@angular/core';
  import {Http} from '@angular/http';
ad296a58   Yarik   Road
3
  
229df284   Yarik   Big map commit
4
  import {StatementBaseService} from './statement.base.service';
ad296a58   Yarik   Road
5
  
229df284   Yarik   Big map commit
6
  import {Road} from '../models/road';
dc9a0e3c   Yarik   Road fatality
7
8
9
  import {Way} from "../models/way";
  import {Node} from "../models/node";
  import {id} from "@swimlane/ngx-charts/release/utils/id";
ad296a58   Yarik   Road
10
11
12
  
  @Injectable()
  export class RoadService extends StatementBaseService {
229df284   Yarik   Big map commit
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      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;
      }
ad296a58   Yarik   Road
71
  }