-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfinna.js
304 lines (272 loc) · 13.1 KB
/
finna.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const axios = require('axios');
const turf = require('@turf/turf');
const BASE_URL = "https://api.finna.fi";
module.exports = {
async getImagesFromFinnaWithTitle(topic, lat, lon, maxradius) {
let query = '"' + JSON.parse(topic).join('" OR "') + '"';
//construct query
const requestConfig = {
baseURL: BASE_URL+"/",
url: "/v1/search",
method: "get",
params: {
lookfor: query,
type: 'AllFields',
limit: 100,
//"filter[0]": '~format:"0/Image/"',
//"filter[1]": '~format:"0/WorkOfArt/"',
//"filter[2]": 'usage_rights_str_mv:"usage_E"',
//"filter[0]": 'free_online_boolean:"1"',
'filter[0]': '~format_ext_str_mv:"0/Image/"',
'filter[1]': '~format_ext_str_mv:"0/Map/"',
'filter[2]': '~format_ext_str_mv:"0/WorkOfArt/"',
'filter[3]': 'free_online_boolean:1',
"field[0]": 'id',
"field[1]": 'title',
"field[2]": 'geoLocations',
"field[3]": 'images',
"field[4]": 'year',
"field[5]": 'publisher',
"field[6]": 'authors',
"field[7]": 'institutions',
"field[8]": 'imageRights',
"field[9]": 'summary',
"field[10]": 'onlineUrls',
"field[11]": 'nonPresenterAuthors',
"field[12]": 'subjectActors',
"field[13]": 'subjectDetails',
"field[14]": 'subjectPlaces',
"field[15]": 'buildings',
"field[16]": 'subjects',
"field[17]": 'formats',
"field[18]": 'identifierString',
"field[19]": 'measurements',
"field[20]": 'inscriptions',
"field[21]": 'imagesExtended',
"field[22]": 'events',
// field[]=collections&
// field[]=thumbnail&
}
}
// Remove images too faraway from the provided coordinates if they and maxdistance given
// if (lat != undefined &&
// lon != undefined &&
// maxradius != undefined) {
// var filter =
// '{!geofilt sfield=location_geo pt=' +
// lat +
// ',' +
// lon +
// ' d=' +
// maxradius / 1000 +
// '}';
// requestConfig.params['filter[2]'] = filter;
// }
const response = await axios.request(requestConfig);
let images = [];
if (!response.data.records) {
return [];
}
//format response
for (var i = 0; i < response.data.records.length; i++) {
var record = response.data.records[i];
if (record.images != undefined && record.images.length > 0) {
// Remove images too faraway from the provided coordinates if they and maxdistance given
if (lat != undefined &&
lon != undefined &&
maxradius != undefined &&
record.geoLocations != undefined) {
var location = getFirstGeoLocationAsPoint(record);
if (location != null) {
var distance =
turf.distance([lon, lat], [location[0], location[1]]);
if (distance > maxradius / 1000) {
continue;
}
}
}
// var authors = "";
// if (record.authors != undefined) {
// for (var author in record.authors) {
// if (record.authors.hasOwnProperty(author)) {
// for (var key in record.authors[author]) {
// if (record.authors[author].hasOwnProperty(key)) {
// authors += key + ", ";
// }
// }
// }
// }
// authors = authors.slice(0, -2);
// }
let collection = "";
if ((record.buildings!=undefined) && (record.buildings.length>1) && (record.buildings[1].value!=undefined)) {
let collectionElements = record.buildings[1].value.split("/");
if (collectionElements.length > 2) collection = collectionElements[2];
}
// let thumbURL = BASE_URL + record.images[0];
let thumbURL = '/static/pngs/imageplaceholder.png';
if (!!record.imagesExtended) {
let imagesExtendedUrls = record.imagesExtended[0].urls;
//if (!!imagesExtendedUrls.small) {
thumbURL = BASE_URL + imagesExtendedUrls.small + '&h=300';
//}
// else if (imagesExtendedUrls.medium != undefined) thumbURL = BASE_URL + imagesExtendedUrls.medium;
}
var subjects = record.subjects && [].concat(...record.subjects) || [];
var datecreated = [];
var materials = [];
if (!!record.events && !!record.events.valmistus) {
datecreated = record.events.valmistus.filter(x => (x.type==='valmistus' && x.date)).map(x => x.date);
materials = record.events.valmistus.filter(x => (x.type==='valmistus' && x.materials)).map(x => x.materials);
}
//assign data to metadata properties
var image = {
actors: record.subjectActors,
collection: collection,
creators: record.nonPresenterAuthors,
creditline: record.imagesExtended[0].rights.creditline,
datecreated: datecreated,
description: record.summary,
details: record.subjectDetails,
downloadURL: '',
formats: [],
geoLocations: (record.geoLocations != undefined ? record.geoLocations : []),
id: record.id,
imageURL: '',
infoURL: "https://www.finna.fi/Record/" + encodeURIComponent(record.id),
inscriptions: record.inscriptions,
institutions: [],
inventoryNumber: record.identifierString,
license: '',
license_link: '',
materials: materials,
measurements: record.measurements,
places: record.subjectPlaces,
publisher: (record.publisher != undefined ? record.publisher : null),
rightsstatement: '',
source: "Finna",
subjects: subjects,
thumbURL: thumbURL,
title: [],
year: ''
}
if (record.title) {
image.title.push(record.title);
}
if ((record.formats != undefined) && (record.formats.length > 1)) {
image.formats = record.formats[record.formats.length - 1].translated;
}
if (record.imagesExtended) {
let displayImage = record.imagesExtended[record.imagesExtended.length - 1].urls.large ? record.imagesExtended[record.imagesExtended.length - 1].urls.large : record.imagesExtended[record.imagesExtended.length - 1].urls.medium;
image.imageURL = BASE_URL + displayImage;
}
if (record.imageRights != undefined) {
image.license_link = record.imageRights.link;
image.license = record.imageRights.copyright;
}
if (record.institutions) {
for (let institution of record.institutions) {
image.institutions.push(institution.translated);
}
}
if (record.year != undefined) {
image.year = parseInt(record.year, 10);
}
images.push(image);
}
}
if (images.length > 30) { // Good practice
images = images.slice(0, 30);
}
return images;
}
};
function getFirstGeoLocation(image) {
var geoLocation = null;
if (image.geoLocations.length > 0) {
var wkt = image.geoLocations[0];
if (wkt.indexOf("POINT") != -1) {
// "POINT(24.9600002 60.1796223)"
var coordPart = wkt.split('(')[1].split(')')[0];
geoLocation = coordPart.split(' ').map(Number);
}
else if (wkt.indexOf("LINESTRING") != -1) {
// "LINESTRING(24.9697848 60.1877939,24.9695072 60.1876021)"
var coordPart = wkt.split('(')[1].split(')')[0];
var pointParts = coordPart.split(',');
geoLocation = [];
for (var i = 0; i < pointParts.length; i++) {
geoLocation.push(pointParts[i].split(' ').map(Number));
}
}
else if (wkt.indexOf("POLYGON") != -1) {
// "POLYGON((24.7828131 60.0999549, 24.8356577 60.130414, 24.8513844 60.2249765, 24.8419098 60.2212043, 24.8347825 60.2585099, 24.8677628 60.2523073, 24.9473908 60.2784652, 24.9731653 60.2643801, 25.0209862 60.2893227, 25.0882105 60.2713417, 25.0823359 60.2496391, 25.1358461 60.2372286, 25.1598757 60.2488133, 25.1425242 60.2697779, 25.2545116 60.2952274, 25.2509121 60.2734979, 25.2273451 60.2611057, 25.240926 60.246305, 25.2014099 60.2181613, 25.2204176 60.1997262, 25.1800446 60.0987408, 25.1693516 59.9434386, 24.9423061 59.922486, 24.7828131 60.0999549))"
geoLocation = [];
var parenthesisPart = wkt.substring(wkt.indexOf('('));
var parenthesisPartInner = parenthesisPart.substr(1, parenthesisPart.length - 2);
var polygonPartCount = parenthesisPartInner.match(/\(/g).length;
var parts = parenthesisPartInner.split('(').slice(1);
var partsWithoutParenthesis = [];
for (var i = 0; i < parts.length; i++) {
var part = null;
var trimmed = parts[i].trim();
if (trimmed.substr(trimmed.length - 1, 1) == ',') {
part = trimmed.substr(0, trimmed.length - 1);
}
else {
part = parts[i];
}
partsWithoutParenthesis.push(part.slice(0, -1));
}
for (var i = 0; i < partsWithoutParenthesis.length; i++) {
var pointParts = partsWithoutParenthesis[i].split(',');
var polygonPart = [];
for (var j = 0; j < pointParts.length; j++) {
polygonPart.push(pointParts[j].trim().split(' ').map(Number));
}
geoLocation.push(polygonPart);
}
}
else if (wkt.indexOf("ENVELOPE") != -1) {
// "ENVELOPE(24.9320989, 24.9512479, 60.1799755, 60.1677043)"
var coordPart = wkt.split('(')[1].split(')')[0];
var pointParts = coordPart.split(',').map(Number);
var envelopePolygon = [[pointParts[0], pointParts[3]], [pointParts[0], pointParts[2]], [pointParts[1], pointParts[2]], [pointParts[1], pointParts[3]], [pointParts[0], pointParts[3]]];
geoLocation = [envelopePolygon];
}
}
return geoLocation;
}
function getFirstGeoLocationAsPoint(image) {
var geoLocation = getFirstGeoLocation(image)
if (image.geoLocations.length > 0) {
var wkt = image.geoLocations[0];
if (wkt.indexOf("POINT") != -1) {
// "POINT(24.9600002 60.1796223)"
var coordPart = wkt.split('(')[1].split(')')[0];
geoLocation = coordPart.split(' ').map(Number);
}
else if (wkt.indexOf("LINESTRING") != -1) {
geoLocation = getCentroid(geoLocation);
}
else if (wkt.indexOf("POLYGON") != -1) {
geoLocation = getCentroid(geoLocation[0]); // We do not care of the possible holes in the polygon
}
else if (wkt.indexOf("ENVELOPE") != -1) {
// "ENVELOPE(24.9320989, 24.9512479, 60.1799755, 60.1677043)"
var coordPart = wkt.split('(')[1].split(')')[0];
var pointParts = coordPart.split(',').map(Number);
var lng = (pointParts[0] + pointParts[1]) / 2;
var lat = (pointParts[2] + pointParts[3]) / 2;
//var envelopePolygon = [[pointParts[0], pointParts[3]], [pointParts[0], pointParts[2]], [pointParts[1], pointParts[2]], [pointParts[1], pointParts[3]], [pointParts[0], pointParts[3]]];
geoLocation = [lng, lat];
}
}
return geoLocation;
}
function getCentroid(coords) {
var center = coords.reduce(function (x,y) {
return [x[0] + y[0]/coords.length, x[1] + y[1]/coords.length];
}, [0,0])
return center;
}