-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson-rdf
executable file
·216 lines (210 loc) · 10.3 KB
/
geojson-rdf
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
#!/usr/bin/env node
var countries = require('./countries');
var usStates = require('./usStates');
if (process.argv.length < 4 || process.argv.length > 5) {
die('OSM geojson to RDF\n' +
'converts a list of OSM administrative areas' +
'to RDF TTL Format.\n\n' +
'usage: geojson-rdf [country] [file_or_uri_to_convert]');
}
var country = process.argv[2], source = process.argv[3];
var fs = require ('fs');
var http = require ('http');
var _ = require("lodash");
var JSONStream = require('JSONStream');
var es = require('event-stream');
checkUSState = function(str){
var out=0;
usStates.listOfStates.forEach((row)=>{
if(row['name'].toLowerCase() == str){
out = row['abbreviation'];
return out;
}
});
return out;
}
convertInputToISO3 = function() {
var inputCountry = country.toLowerCase();
//in this case we have to automatically detect country name from the file name
if(inputCountry == 'auto'){
inputCountry = source.toLowerCase().replace('-latest.osm.pbf.geojson','');
var tmp = inputCountry.split('\/');
if(tmp.length){
inputCountry = tmp[tmp.length-1];
}
inputCountry = inputCountry.replace('-',' ');
}
var out = inputCountry;
var predefined = {'aland islands': 'ALA', 'macau': 'MAC', 'the bahamas': 'BHS', 'bolivia': 'BOL', 'brunei': 'BRN', 'democratic republic of congo': 'COD', 'cape verde': 'CPV', 'falkland islands': 'FLK', 'federated states of micronesia': 'FSM', 'the gambia': 'GMB', 'ivory coast': 'CIV', 'north korea': 'PRK', 'south korea': 'KOR', 'macedonia': 'MKD', 'netherlands antilles': 'ANT', 'pitcairn islands': 'PCN', 'spratly islands': 'Spratly Islands', 'russia': 'RUS', 'saint helena': 'SHN', 'st. lucia': 'LCA', 'east timor': 'TLS', 'taiwan': 'TWN', 'tanzania': 'TZA', 'united kingdom': 'GBR', 'united states': 'USA', 'venezuela': 'VEN', 'british virgin islands': 'VGB', 'us virgin islands': 'VIR', 'vatican city': 'VAT', 'palestinian occupied territories': 'PSE', 'saint-barthélémy': 'BLM', 'saint-martin': 'MAF', 'iran': 'IRN'};
var out = inputCountry;
if(predefined[inputCountry]){
out = predefined[inputCountry];
return out;
}
if(inputCountry.length == 3){
return out.toUpperCase();
}else if(inputCountry.length == 2){
countries.listOfCountries.forEach((row)=>{
if(row['alpha-2'].toLowerCase() == inputCountry){
out = row['alpha-3'];
return out;
}
});
}else{
countries.listOfCountries.forEach((row)=>{
if(row['name'].toLowerCase() == inputCountry){
out = row['alpha-3'];
return out;
}
});
}
return out;
}
var getStream = function (source) {
var stream = fs.createReadStream(source, {encoding: 'utf8'}),
parser = JSONStream.parse('features');
return stream.pipe(parser);
};
/* Process the specified input */
function processor(node) {
var pType = node.geometry.type;
//ignore all the other details
if((pType === 'Polygon' || pType === 'MultiPolygon') && (node.id || node.properties.id)){
//console.log(counter + ':');
if(node.id){
output = 'geoR:relation_' + node.id + ' a geoV:AdministrativeArea ; ';
}else if(node.properties.id) {
output = 'geoR:' + node.properties.id.replace('/','_') + ' a geoV:AdministrativeArea ; ';
}
//if we already have the country in data, we use that one instead of the one inserted by user
if(node.properties['ISO3166-1:alpha3']){
output = output + 'geoV:ISO "' + node.properties['ISO3166-1:alpha3']+ '" ; ';
}else{
var tmpCountry = convertInputToISO3();
var usState = checkUSState(tmpCountry);
//for some specific country groups, we have to add multiple countries as well!
if(tmpCountry == 'gcc states'){
output = output + 'geoV:ISO "BHR", "KWT", "OMN", "QAT", "SAU", "ARE" ; ';
}else if (tmpCountry == 'israel and palestine'){
output = output + 'geoV:ISO "ISR", "PSE" ; ';
}else if (tmpCountry == 'ireland and northern ireland'){
output = output + 'geoV:ISO "IRL", "GBR" ; ';
}else if (tmpCountry == 'district of columbia'){
output = output + 'geoV:ISO "COL" ; ';
}else if (tmpCountry == 'malaysia singapore brunei'){
output = output + 'geoV:ISO "MYS", "SGP", "BRN" ; ';
}else if (tmpCountry == 'north korea'){
output = output + 'geoV:ISO "PRK"; ';
}else if (tmpCountry == 'south korea'){
output = output + 'geoV:ISO "KOR"; ';
}else if (usState){
output = output + 'geoV:ISO "USA"; ';
output = output + 'schema:State "'+usState+'"; ';
}else if (tmpCountry == 'south africa and lesotho'){
output = output + 'geoV:ISO "ZAF"; ';
}else{
output = output + 'geoV:ISO "' + tmpCountry + '" ; ';
}
}
output = output + 'dcterms:title """' + node.properties.name + '""" ; ';
if(node.properties.admin_level){
output = output + 'geoV:level "'+node.properties.admin_level+'"^^xsd:integer ; ';
}
if(node.properties.source){
output = output + 'geoV:source """'+node.properties.source+'""" ; ';
}
if(node.properties.wikipedia){
output = output + 'geoV:dbpedia <' + wikiToDB(node.properties.wikipedia) + '> ; ';
}
if(node.properties.wikidata){
output = output + 'geoV:wikidata <http://www.wikidata.org/entity/' + node.properties.wikidata + '> ; ';
}
if(node.properties.timestamp){
output = output + 'geoV:timestamp "' + node.properties.timestamp+ '" ; ';
}
if(node.properties.start_date){
output = output + 'geoV:startDate "' + node.properties.start_date+ '" ; ';
}
if(node.properties.authoritative){
output = output + 'geoV:isAuthoritative "' + node.properties.authoritative+ '" ; ';
}
if(node.properties['addr:postcode']){
output = output + 'geoV:postCode "' + node.properties['addr:postcode']+ '" ; ';
}
if(node.properties['ref:INS']){
output = output + 'geoV:refINS "' + node.properties['ref:INS']+ '" ; ';
}
if(node.properties['ref:NUTS']){
output = output + 'geoV:refNUTS "' + node.properties['ref:NUTS']+ '" ; ';
}
if(node.properties['loc_name']){
output = output + 'geoV:locName """' + node.properties['loc_name']+ '""" ; ';
}
if(node.properties['old_name']){
output = output + 'geoV:oldName """' + node.properties['old_name']+ '""" ; ';
}
if(node.properties['note']){
output = output + 'geoV:note """' + node.properties['note']+ '""" ; ';
}
if(node.properties['population']){
output = output + 'geoV:population "' + node.properties['population']+ '"^^xsd:integer ; ';
}
if(node.properties['website']){
output = output + 'foaf:page <' + node.properties['website'].trim()+ '> ; ';
}
//console.log(node.properties.CODE_of_fu);
//console.log(node.properties.CLASS);
//console.log(pType);
output = output + 'geoV:shapeType "' + pType+ '" ; ';
if(pType === 'Polygon'){
var tmpp = [];
_.forEach(node.geometry.coordinates[0], function(coordinate, ii) {
tmpp.push(coordinate.join(' '));
});
output = output + 'geo:geometry "POLYGON((' + tmpp.join(',') + '))"^^<http://www.openlinksw.com/schemas/virtrdf#Geometry> .';
//console.log('POLYGON((' + tmpp.join(',') + '))');
} else if(pType === 'MultiPolygon'){
var tmppTop = [];
_.forEach(node.geometry.coordinates, function(coordinateset, ii) {
var tmpp = [];
_.forEach(coordinateset[0], function(coordinate, iii) {
tmpp.push(coordinate.join(' '));
});
tmppTop.push('((' + tmpp.join(',') + '))');
});
output = output + 'geo:geometry "MULTIPOLYGON(' + tmppTop.join(',') + ')"^^<http://www.openlinksw.com/schemas/virtrdf#Geometry> .';
//console.log('MULTIPOLYGON(' + tmppTop.join(',') + ')');
}
console.log(output);
}
}
// Open HTTP stream if the source is a URL
if (source.match(/^https?:\/\//))
http.request(source, processor).end();
// Open a file stream otherwise
else {
var prefixes = '@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> . @prefix edm: <http://www.europeana.eu/schemas/edm/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix schema: <http://schema.org/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix geoV: <http://geo.risis.eu/vocabulary/osm/> . @prefix geoR: <http://geo.risis.eu/osm/> .';
console.log(prefixes);
getStream(source)
.pipe(es.mapSync(function (data) {
_.forEach(data, function(node, i) {
processor(node);
});
}));
}
/* Halt execution with an error message. */
function die(message) {
console.log(message);
process.exit(1);
}
function wikiToDB(str) {
var tmp = str.split(':');
var tmp2 = tmp[1].split(' ').join('_');
var out = '';
if(tmp.length < 2 || tmp[0]== 'en'){
out = 'http://dbpedia.org/resource/' + encodeURIComponent(tmp2);
}else{
out = 'http://'+tmp[0]+'.dbpedia.org/resource/' + encodeURIComponent(tmp2);
}
return out;
}