-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconvertSVG.js
More file actions
275 lines (209 loc) · 6.76 KB
/
convertSVG.js
File metadata and controls
275 lines (209 loc) · 6.76 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/****
*
* This script is called from Node.js to process a SVG floorplan into:
* 1) a high-res PNG that will be used as a background image
* 2) a new SVG that will contain only overlay/interactive features:
* a) interior spaces
* b) labels
*
* It uses D3.js to process the SVG DOM and PhantomJS to render PNGs
*
* In the future it can be parameterized and turned into a web service
* in order to dynamically change the background image.
* We may also look into using something like Mapnik or TileStache to
* split the high-res into tiles for improved performance
*
*
* Note: I had to pull down this fork of the phantomjs-node module
* https://github.com/sebv/phantomjs-node
*
*/
'use strict';
var PNGPath = '/app/media/png/',
SVGPath = '/app/media/svg/';
var ConvertSVG = function (filename) {
this.filename = filename;
};
ConvertSVG.prototype = {
filename: null,
/**
* Create PNG
* @param fileSource
* @param fileTarget
* @param width
* @param height
* @param resolutionMultiplier
* @param tileTarget
*/
createPNG: function (fileSource, fileTarget, tileTarget, width, height, resolutionMultiplier) {
//setup phantomJS to render PNG
var phantom = require('phantom');
phantom.create(function (ph) {
return ph.createPage(function (page) {
//set page dimensions
var pageWidth = width * resolutionMultiplier,
pageHeight = height * resolutionMultiplier;
page.set('viewportSize', {width: pageWidth, height: pageHeight});
console.log("PNG Dimensions Width:" + pageWidth + " Height:" + pageHeight);
return page.open(fileSource, function (status) {
if (status !== 'success') {
console.log('Unable to load: ' + fileSource);
ph.exit();
} else {
page.render(fileTarget);
console.log('Render Complete!');
ph.exit();
var gm = require('gm'),
fs = require('fs'),
cropHeight = pageHeight / 2,
cropWidth = pageWidth / 2;
//upperLeft
gm(fileTarget).crop(cropWidth, cropHeight, 0, 0)
.background('white')
.flatten()
.matteColor('white')
.write(tileTarget + "-bg-ul.jpg", function (err) {
if (!err) {
console.log(' Saved ul tile ');
} else {
console.log(err);
}
});
//lowerLeft
gm(fileTarget).crop(cropWidth, cropHeight, 0, cropHeight)
.background('white')
.flatten()
.matteColor('white')
.write(tileTarget + "-bg-ll.jpg", function (err) {
if (!err) {
console.log(' Saved ll tile ');
} else {
console.log(err);
}
});
//upperRight
gm(fileTarget).crop(cropWidth, cropHeight, cropWidth, 0)
.background('white')
.flatten()
.matteColor('white')
.write(tileTarget + "-bg-ur.jpg", function (err) {
if (!err) {
console.log(' Saved ur tile ');
} else {
console.log(err);
}
});
//lowerRight
gm(fileTarget).crop(cropWidth, cropHeight, cropWidth, cropHeight)
.background('white')
.flatten()
.matteColor('white')
.write(tileTarget + "-bg-lr.jpg", function (err) {
if (!err) {
console.log(' Saved lr tile ');
} else {
console.log(err);
}
});
}
});
});
});
},
/**
* Write data to file
* @param data
* @param fileTarget
*/
writeFile: function (data, fileTarget) {
var fs = require('fs');
//write to file
fs.writeFileSync(fileTarget, data);
},
//Not using any of this stuff for now
/*mapValue: function (value, minTo, maxTo, minFrom, maxFrom) {
return minTo + (maxTo - minTo) * ((value - minFrom) / (maxFrom - minFrom));
},
projectCADToLatLng: function (latlng, minX, minY, maxX, maxY) {
var targetMinX = -10.0,
targetMinY = -5.0,
targetMaxX = 10.0,
targetMaxY = 5.0,
mappedLat = this.mapValue(latlng.lat, targetMinY, targetMaxY, minY, maxY),
mappedLng = this.mapValue(latlng.lng, targetMinX, targetMaxX, minX, maxX);
return {lat: mappedLat, lng: mappedLng};
},
createGDALTranslateCall: function (viewBox, target) {
//gdal_translate -of GTiff -a_ullr ullat ullon lrlat lelon -a_srs EPSG:4269 input.tif output.tif
// ulx uly lrx lry
var ullaty = Number(viewBox.yMin) + Number(viewBox.height),
ullonx = viewBox.xMin,
lrlaty = viewBox.yMin,
lrlonx = Number(viewBox.xMin) + Number(viewBox.width),
ul = this.projectCADToLatLng({lat: ullaty, lng: ullonx}, ullonx, lrlaty, lrlonx, ullaty),
lr = this.projectCADToLatLng({lat: lrlaty, lng: lrlonx}, ullonx, lrlaty, lrlonx, ullaty),
output = "gdal_translate -of GTiff -a_ullr " +
ul.lng.toString() + " " + ul.lat.toString() + " " + lr.lng.toString() + " " + lr.lat.toString() +
" -a_srs EPSG:4326 " + target + " " + target;
return output;
},*/
/**
* Convert
*/
convert: function () {
global.document = require("jsdom").jsdom("<html><body></body></html>");
global.window = global.document.createWindow();
var fs = require('fs'),
ofp = require('openfloorplan'),
source = __dirname + SVGPath + this.filename + '.svg',
backgroundTarget = __dirname + PNGPath + this.filename + '-bg.png',
tileTarget = __dirname + PNGPath + this.filename,
overlayTarget = __dirname + SVGPath + this.filename + '-ol.svg',
resolutionMultiplier = 15,
convertSVG = this; //need a local scope reference;
console.log("File: " + backgroundTarget);
/////////////
//Overlay
/////////////
fs.readFile(source, function (err, data) {
if (err) {
throw err;
}
var fp, viewBox;
console.log("loaded SVG file");
//set body as the SVG data
global.document.body.innerHTML = data;
//get the floorplan
fp = new ofp.FloorPlan(global.document.body);
//flag everything for removal
fp.svg.selectAll('g [type=level]').classed('tmp-removed', true);
//un-flag the stuff we want to keep
fp.spaces.layer.classed('tmp-removed', false);
fp.dimensionAnnotations.layer.classed('tmp-removed', false);
//remove everything else
fp.svg.selectAll('.tmp-removed').remove();
convertSVG.writeFile(fp.exportSVG(), overlayTarget);
});
//////////////////
//Background
/////////////////
//read the source SVG file
fs.readFile(source, function (err, data) {
if (err) {
throw err;
}
var fp, viewBox;
console.log("loaded SVG file");
//set body as the SVG data
global.document.body.innerHTML = data;
//get the floorplan
fp = new ofp.FloorPlan(global.document.body);
viewBox = fp.getViewBox();
fp.dimensionAnnotations.remove();
convertSVG.writeFile(fp.exportSVG(), "temp.svg");
convertSVG.createPNG("temp.svg", backgroundTarget, tileTarget, viewBox.width, viewBox.height, resolutionMultiplier);
});
}
};
//run it
new ConvertSVG("fs-hi200-01").convert();