-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-tgn.js
140 lines (125 loc) · 4.12 KB
/
import-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
var fs = require('fs'),
util = require('util'),
xml2js = require('xml2js'),
pg = require('pg'),
elasticsearch = require('elasticsearch');
var esHostname = 'erfgoedenlocatie.cloud.tilaa.com:9200';
//var esHostname = 'localhost:9200';
// Expects BAG to be available in database BAG, with username and password 'postgres'
var conString = "postgres://postgres:postgres@localhost/bag",
bagQuery = "SELECT identificatie::int AS id, ST_AsGeoJSON(ST_ForceRHR(ST_Force2D(ST_Transform(geovlak, 4326)))) AS geom FROM woonplaatsactueelbestaand WHERE woonplaatsnaam = $1";
var pgClient = new pg.Client(conString);
pgClient.connect(function(err) {
if (err) {
return console.error('could not connect to postgres', err);
}
parseXML(closeConnection);
});
var esClient = new elasticsearch.Client({
host: esHostname
});
function closeConnection() {
pgClient.end();
}
function parseXML(done) {
var parser = new xml2js.Parser();
fs.readFile(__dirname + '/tgn.xml', function(err, data) {
parser.parseString(data, function (err, result) {
var count = result['rdf:RDF']['rdf:Description'].length;
6
result['rdf:RDF']['rdf:Description'].forEach(function(element, index) {
createDocument(element, function() {
count--;
if (count <= 0) {
done();
}
});
});
});
});
}
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 createDocument(element, callback) {
var label = getElementTagValue(element, 'rdfs:label'),
source = getElementTagAttribute(element, 'dc-term:source', 'rdf:resource'),
term = getElementTagValue(element, 'gvp:term'),
long = getElementTagValue(element, 'geo-pos:long'),
lat = getElementTagValue(element, 'geo-pos:lat'),
startDate = getElementTagValue(element, 'schema:startDate'),
endDate = getElementTagValue(element, 'schema:endDate');
var geometry = getBAGGeometryByName(label, function(id, geometry) {
if (geometry) {
console.log("Found " + term + " = " + label + " in BAG, adding to Elasticsearch...");
var now = new Date().toISOString();
var doc = {
"uri": "http://data.erfgeo.nl/grs/Place/" + term + "/1",
"date_created": now,
"source": {
"name": term,
"uri": source,
"dataset": "tgn"
},
"relationship": {
"created": now,
"author": "[email protected]",
"type": "grs:approximation",
"uri": "http://data.erfgeo.nl/grs/Relationship/" + term + "/1"
},
"target": {
"name": label,
"uri": "http://lod.geodan.nl/basisreg/bag/woonplaats/id_" + id,
"startDate": 2014,
"endDate": null,
"dataset": "bag",
"geometry": geometry
}
};
if (startDate) {
doc.source.startDate = startDate;
}
if (endDate) {
doc.source.endDate = endDate;
}
esClient.create({
index: 'pelias',
type: 'pit',
body: doc
}, function (error, response) {
callback();
});
} else {
// Not found in BAG, don't do anything!
callback();
}
});
}
// TODO: find way to round coordinates without corrupting polygons
//function round_coordinates(str) {
// return str.replace(/(\d+)\.(\d{6})\d+/g, '$1.$2');
//}
function getBAGGeometryByName(name, callback) {
// TODO: use pg-query? (https://github.com/brianc/node-pg-query)
pgClient.query(bagQuery.replace("$1", "'" + name.replace("'", "''") + "'"), function(err, result) {
if (err) {
callback(null);
}
if (result.rows.length > 0) {
callback(result.rows[0].id, JSON.parse(result.rows[0].geom));
} else {
callback(null);
}
});
}