-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-map-generator.js
228 lines (197 loc) · 7.22 KB
/
svg-map-generator.js
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
function platTownship(patents){
var mapMatrix = patentConverter.getElasticMatrix(patents);
var map = document.getElementById("map");
map.setAttribute("width", mapMatrix[0].length * scale);
map.setAttribute("height", mapMatrix.length * scale);
var mapBorder = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
mapBorder.setAttribute("width", mapMatrix[0].length * scale);
mapBorder.setAttribute("height", mapMatrix.length * scale);
mapBorder.classList.add("mapBorder");
map.appendChild(mapBorder);
drawPlatBorders(map, mapMatrix);
colorPlats(map, mapMatrix, patents);
labelPlats(map, mapMatrix, patents);
}
function labelPlats(map, mapMatrix, patents){
var plats = getUniquePlats(mapMatrix);
plats.forEach(function(plat){
var width = getPlatWidth(plat.x, plat.y, mapMatrix, 1);
var height = getPlatHeight(plat.x, plat.y, mapMatrix, 1);
var sq = mapMatrix[plat.y][plat.x];
var name = patents.find(function(pat){
return pat._id == sq.patentID;
}).Names.split(",")[0];
if(width >= height){
//Write label horizontally
var label = document.createElementNS("http://www.w3.org/2000/svg", 'text');
label.setAttribute("x", (plat.x * scale) + 2);
label.setAttribute("y", (plat.y * scale) + (height * scale / 2));
label.setAttribute("textLength", (width * scale) - 4);
label.setAttribute("lengthAdjust", "spacingAndGlyphs");
label.setAttribute("font-size", scale / 2);
label.innerHTML = name;
map.appendChild(label);
}
else {
//Write vertically
var label = document.createElementNS("http://www.w3.org/2000/svg", 'text');
label.setAttribute("x", (plat.x * scale) + (scale / 2));
label.setAttribute("y", (plat.y * scale) + 4);
label.style["writing-mode"] = "tb";
label.setAttribute("textLength", (height * scale) - 4);
label.setAttribute("lengthAdjust", "spacingAndGlyphs");
label.setAttribute("font-size", scale / 1.5);
label.innerHTML = name;
map.appendChild(label);
}
});
}
function getPlatWidth(sqX, sqY, mapMatrix, width){
var sq = mapMatrix[sqY] ? mapMatrix[sqY][sqX] : null;
var nextSq = mapMatrix[sqY] ? mapMatrix[sqY][sqX + 1] : null;
if(nextSq != null && nextSq != undefined && nextSq.platID == sq.platID){
width++;
return getPlatWidth(sqX + 1, sqY, mapMatrix, width);
}
else {
return width;
}
}
function getPlatHeight(sqX, sqY, mapMatrix, height){
var sq = mapMatrix[sqY] ? mapMatrix[sqY][sqX] : null;
var nextSq = mapMatrix[sqY + 1] ? mapMatrix[sqY + 1][sqX] : null;
if(nextSq != null && nextSq != undefined && nextSq.platID == sq.platID){
height++;
return getPlatHeight(sqX, sqY + 1, mapMatrix, height);
}
else {
return height;
}
}
function colorPlats(map, mapMatrix, patents){
var names = getUniquePatentNames(patents);
var colors = [];
for (var i = 0; i < names.length; i++) {
var color = selectColor(i, names.length);
colors.push(color);
}
for(var y=0;y < mapMatrix.length;y++){
for(var x=0; x < mapMatrix[y].length; x++){
var patID = mapMatrix[y][x].patentID;
if(patID != null){
var patent = getPatByID(patID, patents);
var color = colors[names.indexOf(patent.Names)];
var rec = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
rec.setAttribute("x", x * scale);
rec.setAttribute("y", y * scale);
rec.setAttribute("width", scale);
rec.setAttribute("height", scale);
rec.classList.add("platColor");
rec.setAttribute("fill", color);
rec.dataset.patID = patID;
rec.dataset.Accession = patent.Accession;
var title = document.createElementNS("http://www.w3.org/2000/svg", 'title');
title.textContent = patent.Names + " - " + patent.Date + "\n " + patent.Aliquots + " Sec " + patent.Sec + " " + patent.Twp_Rng + "\n Accession: " + patent.Accession;
rec.appendChild(title);
rec.onclick = function(){
window.open("https://glorecords.blm.gov/details/patent/default.aspx?accession=" + this.dataset.Accession + "&docClass=STA");
};
map.appendChild(rec);
}
}
}
}
function selectColor(colorNum, colors) {
if (colors < 1) colors = 1; // defaults to one color - avoid divide by zero
return "hsla(" + (colorNum * (360 / colors)) % 360 + ",70%,50%,0.5)";
}
function drawPlatBorders(map, mapMatrix){
for(var y=0;y<mapMatrix.length;y++){
for(var x=0;x<mapMatrix[y].length;x++){
var sq = mapMatrix[y][x];
if(sq.borders.n){
var line = document.createElementNS("http://www.w3.org/2000/svg", 'line');
line.setAttribute("x1", x * scale);
line.setAttribute("y1", y * scale);
line.setAttribute("x2", (x + 1) * scale);
line.setAttribute("y2", y * scale);
line.classList.add("platBorder");
map.appendChild(line);
}
if(sq.borders.s){
var line = document.createElementNS("http://www.w3.org/2000/svg", 'line');
line.setAttribute("x1", x * scale);
line.setAttribute("y1", (y + 1) * scale);
line.setAttribute("x2", (x + 1) * scale);
line.setAttribute("y2", (y + 1) * scale);
line.classList.add("platBorder");
map.appendChild(line);
}
if(sq.borders.w){
var line = document.createElementNS("http://www.w3.org/2000/svg", 'line');
line.setAttribute("x1", x * scale);
line.setAttribute("y1", y * scale);
line.setAttribute("x2", x * scale);
line.setAttribute("y2", (y + 1) * scale);
line.classList.add("platBorder");
map.appendChild(line);
}
if(sq.borders.e){
var line = document.createElementNS("http://www.w3.org/2000/svg", 'line');
line.setAttribute("x1", (x + 1) * scale);
line.setAttribute("y1", y * scale);
line.setAttribute("x2", (x + 1) * scale);
line.setAttribute("y2", (y + 1) * scale);
line.classList.add("platBorder");
map.appendChild(line);
}
}
}
}
function getUniquePlats(map){
var plats = [];
var platObjs = [];
for(var y=0;y < map.length; y++){
for(var x=0; x < map[y].length; x++){
var plat = map[y][x].platID;
if(plat != null && plats.indexOf(plat) == -1){
plats.push(plat);
platObjs.push({
platID: plat,
x: x,
y: y
});
}
}
}
return platObjs;
}
function getUniquePatentNames(patents) {
var names = patents.map(function(pt) {
return pt["Names"];
});
var uniqueNames = [];
names.forEach(function(name){
if(uniqueNames.indexOf(name) == -1)
uniqueNames.push(name);
});
return uniqueNames;
}
function getPatByID(id, patents){
return patents.find(function(p){
return p._id == id;
});
}
function saveSVG(svgEl, name) {
svgEl.setAttribute("xmlns", "http://www.w3.org/2000/svg");
var svgData = svgEl.outerHTML;
var preface = '<?xml version="1.0" standalone="no"?>\r\n';
var svgBlob = new Blob([preface, svgData], {type:"image/svg+xml;charset=utf-8"});
var svgUrl = URL.createObjectURL(svgBlob);
var downloadLink = document.createElement("a");
downloadLink.href = svgUrl;
downloadLink.download = name;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}