-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgn.js
189 lines (155 loc) · 4.83 KB
/
tgn.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
var fs = require('fs');
var path = require('path');
var SparqlClient = require('sparql-client');
var async = require('async');
var xml2js = require('xml2js');
// TGN configuration
var sparqlFiles = [
'tgn-parents',
'tgn-places',
'tgn-terms'
];
var sparqlEndpoint = 'http://vocab.getty.edu/sparql.rdf';
function download(config, dir, writer, callback) {
async.eachSeries(sparqlFiles, function(sparqlFile, callback) {
async.eachSeries(config.parents, function(parent, callback) {
var client = new SparqlClient(sparqlEndpoint);
var sparqlQuery = fs.readFileSync(path.join(__dirname, 'sparql', sparqlFile + '.sparql'), 'utf8');
sparqlQuery = sparqlQuery.replace(new RegExp('{{ parent }}', 'g'), parent);
client.query(sparqlQuery)
.execute(function(error, results) {
fs.writeFileSync(path.join(dir, sparqlFile + '.' + parent.replace('tgn:', '') + '.xml'), results);
callback();
});
},
function(err) {
callback(err);
});
},
function(err) {
callback(err);
});
}
function convert(config, dir, writer, callback) {
var parser = new xml2js.Parser();
async.eachSeries(sparqlFiles, function(sparqlFile, callback) {
async.eachSeries(config.parents, function(parent, callback) {
fs.readFile(path.join(dir, sparqlFile + '.' + parent.replace('tgn:', '') + '.xml'), function(err, data) {
parser.parseString(data, function(err, result) {
async.eachSeries(result['rdf:RDF']['rdf:Description'], function(element, callback) {
parseElement(config, element, function(err) {
callback(err);
});
},
function(err) {
callback(err);
});
});
});
},
function(err) {
callback(err);
});
},
function(err) {
callback(err);
});
function getElementTagValue(element, tag) {
if (element[tag] && element[tag].length > 0 && element[tag][0]._) {
return element[tag][0]._;
} else if (element[tag] && element[tag].length > 0) {
return element[tag][0];
}
return null;
}
function getElementTagAttribute(element, tag, attribute) {
if (element[tag] && element[tag].length > 0 && element[tag][0].$ && element[tag][0].$[attribute]) {
return element[tag][0].$[attribute];
}
return null;
}
function parseElement(config, element, callback) {
var elementType = getElementTagValue(element, 'tgn:typeTerm');
var type = config.types[elementType];
// Only process elements with valid type
if (type) {
var data = [];
var name = getElementTagValue(element, 'gvp:term');
var uri = getElementTagAttribute(element, 'dct:source', 'rdf:resource');
var pit = {
uri: uri,
name: name,
type: type,
data: {
type: elementType
}
};
var long = getElementTagValue(element, 'wgs:long');
var lat = getElementTagValue(element, 'wgs:lat');
if (long && lat) {
pit.geometry = {
type: 'Point',
coordinates: [
parseFloat(long),
parseFloat(lat)
]
};
}
var comment = getElementTagValue(element, 'rdfs:comment');
if (comment) {
pit.data.comment = comment;
}
// TODO: use just years, and specify fuzziness!
var estStart = getElementTagValue(element, 'gvp:estStart');
var estEnd = getElementTagValue(element, 'gvp:estEnd');
if (estStart) {
pit.validSince = estStart;
}
if (estEnd) {
pit.validUntil = estEnd;
}
data.push({
type: 'pit',
obj: pit
});
var broaderPreferred = getElementTagAttribute(element, 'gvp:broaderPreferred', 'rdf:resource');
if (broaderPreferred) {
// This implies that current PIT lies in broaderPreferred
// Add liesIn relation
data.push({
type: 'relation',
obj: {
from: uri,
to: broaderPreferred,
type: config.relations.liesIn
}
});
}
var subject = getElementTagAttribute(element, 'rdf:subject', 'rdf:resource');
if (subject) {
// This implies that subject is an alternative name for current PIT
// Add sameHgConcept relation
data.push({
type: 'relation',
obj: {
from: uri,
to: subject,
type: config.relations.equivalence
}
});
}
writer.writeObjects(data, function(err) {
callback(err);
});
} else {
setImmediate(callback);
}
}
}
// ==================================== API ====================================
module.exports.title = 'Getty Thesaurus of Geographic Names';
module.exports.url = 'http://www.getty.edu/research/tools/vocabularies/tgn/';
module.exports.steps = [
download,
convert
];