File size: 4,925 Bytes
597ee18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Color from './Color';
import Resolve from './Resolve';
import CSS from './CSS';
import { Scale, SVG, polyline, polygon, circle, rect, image } from './SVG';
import { Taal, Gewest, Gemeente, Straat } from './CRAB';
import fs from 'fs';
import { log } from './log';
import WMS from './WMS';
import request from 'request';
import { memoize } from './util';

const r = request.defaults({ encoding: null });
// import BBOX from './BBOX';
const EPSG = 'EPSG:31370';

const TAAL = 'Nederlands';
const GEWEST = 'Vlaams Gewest';
const GEMEENTE = 'Stabroek';
const STRAAT = 'Frans Oomsplein';// 'Markt';

const notNull = x => x;

const functions = funcs => object => funcs.map(fn => fn(object)).join('\n');

const css = new CSS({
  'rect.terrein': { stroke: 'black', fill: 'none', 'stroke-width': 2, 'stroke-opacity': 0.5 },
  'circle.wegobject': { stroke: 'red', fill: 'red', 'stroke-width': 10 },
  'circle.terrein': { stroke: 'green', fill: 'green', 'stroke-width': 5 },
  'circle.perceel': { stroke: 'purple', fill: 'purple', 'stroke-width': 3 },
  'polygon.gebouw': { stroke: Color.random().toHex(), fill: Color.random().toHex() },
});

const download = uri => new Promise((resolve, reject) => {
  r.get(uri.split('&').join('&'), (err, response, body) => {
    if (!err && response.statusCode === 200) {
      const contentType = response.headers['content-type'];
      const data = `data:${contentType};base64,${new Buffer(body).toString('base64')}`;
      log(uri);
      resolve(data);
    }
    reject(new Error(`failed download ${uri}`));
  });
});

const getUrl = memoize(uri => download(uri), 'GetUrl');

const createSVG = ({ gebouwen, percelen, terreinen, wegobjecten, wegsegmenten }) => {
  const coords = [];
  gebouwen.forEach(x => coords.push(...x));
  wegsegmenten.forEach(x => coords.push(...x));
  coords.push(...terreinen.map(x => x.center));
  coords.push(...terreinen.map(x => x.minimum));
  coords.push(...terreinen.map(x => x.maximum));
  coords.push(...wegobjecten.map(x => x.center));
  coords.push(...wegobjecten.map(x => x.minimum));
  coords.push(...wegobjecten.map(x => x.maximum));
  coords.push(...percelen);
  const list = coords.filter(notNull);
  const coordinates = {
    x: list.map(coord => coord[0]),
    y: list.map(coord => coord[1]),
  };
  const min = { x: Math.min(...coordinates.x), y: Math.min(...coordinates.y) };
  const max = { x: Math.max(...coordinates.x), y: Math.max(...coordinates.y) };
  const w = Math.ceil(max.x - min.x);
  const h = Math.ceil(max.y - min.y);
  const [W, H] = [w, h].map(o => o * Scale);
  const width = `width="${W}"`;
  const height = `height="${H}"`;
  const viewPort = `viewPort="0 0 ${W} ${H}"`;
  const ns = 'xmlns="http://www.w3.org/2000/svg"';
  const xlink = 'xmlns:xlink="http://www.w3.org/1999/xlink"';

  const svgDoc = new SVG({ width: W, height: H, css: css, children: [] });
  log(svgDoc.toString());

  const toRect = className => x => rect({ min, max, class: className })(x);
  const toCircle = className => x => circle({ min, max, r: 10, class: className })(x.center);

  const imgs = [];
  const terreinToImage = x => new Promise((resolve, reject) => {
    try {
      const [[left, top], [right, bottom]] = [x.minimum, x.maximum];
      const bbox = [left, top, right, bottom].join(',');
      const size = {
        width: Math.floor(right - left) * Scale,
        height: Math.floor(bottom - top) * Scale,
      };
      const url = WMS.getMap(size.width, size.height, EPSG, bbox).split('&').join('&');
      const prom = getUrl(url, `data/images/${bbox}.png`);
      return prom.then(src => {
        const i = image(url, min, max, src)(x);
        imgs.push(i);
        // log(imgs.length);
        return i;
      }).catch(log);
    } catch (e) {
      reject(new Error(e));
    }
  });

  const center = o => o.center;

  Promise.all(terreinen.map(terreinToImage)).then(ee => {
    log(ee);
    log(666);
    const svg = `<svg ${width} ${height} ${viewPort} version="1.1" ${ns} ${xlink}>
${CSS}
${gebouwen.map(polygon({ min, max, class: 'gebouw' })).join('\n')}
${wegsegmenten.map(polyline(min, max, 'gray', 1)).join('\n')}
${wegobjecten.map(rect({ min, max })).join('\n')}

${terreinen.map(functions([toRect('terrein'), toCircle('terrein')])).join('\n\n')}
${`_${imgs.join('\n')}_`}
${wegobjecten.map(center).map(circle({ min, max, class: 'wegobject' })).join('\n')}
${percelen.map(circle({ min, max, class: 'perceel' })).join('\n')}
</svg>`;
    fs.writeFileSync('test2.svg', svg);
    log('SVG generated');
  }).catch(log);
};

const getStraat = gemeente => Gemeente.get(gemeente)
  .then(Straat.find({ gemeenteId: gemeente.id, naam: STRAAT }));

Taal.find({ naam: TAAL })
.then(log)
.then(taal => Gewest.find({ taalCode: taal.code, naam: GEWEST }))
.then(log)
.then(gewest => Gemeente.find({ gewestId: gewest.id, naam: GEMEENTE }))
.then(log)
.then(gemeente => getStraat(gemeente))
.then(Resolve.Straat(createSVG));