-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson-rdf
executable file
·125 lines (119 loc) · 6.22 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
#!/usr/bin/env node
var countries = require('./countries');
var usStates = require('./usStates');
if (process.argv.length < 3 || process.argv.length > 4) {
die('Flickr geojson to RDF\n' +
'converts a list of Flickr boundaries' +
'to RDF TTL Format.\n\n' +
'usage: geojson-rdf [file_or_uri_to_convert]');
}
var source = process.argv[2];
var fs = require ('fs');
var http = require ('http');
var _ = require("lodash");
var JSONStream = require('JSONStream');
var es = require('event-stream');
var getStream = function (source) {
var stream = fs.createReadStream(source, {encoding: 'utf8'}),
parser = JSONStream.parse('features');
return stream.pipe(parser);
};
//continents, countries, regions (states), counties, localities (cities), and neighbourhoods
var flickrLevels = {
'country': 1,
'region': 2,
'county': 3,
'locality': 4,
'neighbourhood': 5,
};
convertInputToISO3 = function(country) {
var inputCountry = country.toLowerCase().trim();
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'};
var out = inputCountry;
if(predefined[inputCountry]){
out = predefined[inputCountry];
return out;
}
if(inputCountry.length == 3){
return out;
}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;
}
/* Process the specified input */
function processor(node) {
var pType = node.geometry.type;
//ignore all the other details
if(pType === 'Polygon' || pType === 'MultiPolygon'){
//console.log(counter + ':');
output = 'geoR:' + node.id + ' a geoV:AdministrativeArea ; ';
output = output + 'geoV:level "'+flickrLevels[node.properties.place_type]+'"^^xsd:integer ; ';
output = output + 'geoV:WOE """' + node.properties.woe_id + '""" ; ';
//extract the country name
var tmpp = node.properties.label.split(',');
var countryName = tmpp [tmpp.length-1];
if(node.properties.place_type=='country'){
output = output + 'dcterms:title """' + node.properties.label + '""" ; ';
}else{
output = output + 'dcterms:title """' + node.properties.label.replace(',' + countryName,'') + '""" ; ';
}
output = output + 'geoV:ISO """' + convertInputToISO3(countryName) + '""" ; ';
output = output + 'geoV:placeID """' + node.properties.place_id + '""" ; ';
output = output + 'dcterms:created "' + node.geometry.created + '" ; ';
output = output + 'schema:downloadUrl <' + node.geometry.link.href+ '> ; ';
output = output + 'ngeo:bbox "BOX2D(' + node.geometry.bbox[0] +' '+ node.geometry.bbox[1] + ',' + node.geometry.bbox[2] +' '+node.geometry.bbox[3] + ')"^^<http://www.openlinksw.com/schemas/virtrdf#Geometry> ; ';
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 ngeo: <http://geovocab.org/geometry#> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix geoV: <http://geo.risis.eu/vocabulary/flickr/> . @prefix geoR: <http://geo.risis.eu/flickr/> .';
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);
}