-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
448 lines (405 loc) · 16.2 KB
/
Copy pathmain.rs
File metadata and controls
448 lines (405 loc) · 16.2 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Portions of this file contributed by NIST are governed by the
// following statement:
//
// This software was developed at the National Institute of Standards
// and Technology by employees of the Federal Government in the course
// of their official duties. Pursuant to Title 17 Section 105 of the
// United States Code, this software is not subject to copyright
// protection within the United States. NIST assumes no responsibility
// whatsoever for its use by other parties, and makes no guarantees,
// expressed or implied, about its quality, reliability, or any other
// characteristic.
//
// We would appreciate acknowledgement if the software is used.
//! This program takes several steps to convert a CASE JSON-LD file to GeoJSON.
//!
//! 1. A JSON-LD file is parsed and expanded. This is tested in `jsonld_expand_adapted_example`.
//! 1. The expanded JSON-LD object is converted into RDF quads, populating an Oxigraph store. Store population is tested with individual hard-coded quads in `store_insert_adapted_example`.
//! 1. A SPARQL query is run. This is tested in `store_query_adapted_example`.
//! 1. GeoJSON objects are constructed and serialized.
use geojson;
use json_ld::{
rdf_types::{Quad, Term},
syntax::{Parse, Value},
JsonLdProcessor, RemoteDocument,
};
use oxigraph::model::{GraphNameRef, LiteralRef, NamedNodeRef, QuadRef};
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use oxigraph::store::Store;
use static_iref::iri;
use std::{env, fs};
fn unquote_string(mut s: String) -> String {
if s.len() > 0 {
if s[0..1] == *"\"" {
s.pop();
if s.len() > 0 {
s.remove(0);
}
}
}
s
}
#[tokio::main]
async fn main() {
// Drawn from:
// https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html
let args: Vec<String> = env::args().collect();
let input_case_json_ld_path = &args[1];
// Drawn from:
// https://doc.rust-lang.org/book/ch12-02-reading-a-file.html
let file_contents: String = fs::read_to_string(input_case_json_ld_path)
.expect("Should have been able to read the file");
// Parse the file.
let value = Value::parse_str(&file_contents)
.expect("unable to parse file")
.0;
// JSON-LD parsing and expansion code adapted from:
// https://docs.rs/json-ld/0.21.1/json_ld/#example
// The runtime selection followd this documentation:
// https://rust-lang.github.io/async-book/part-guide/async-await.html#the-runtime
// Create a "remote" document by parsing a file manually.
let input = RemoteDocument::new(
// We use `IriBuf` as IRI type.
Some(iri!("https://example.com/sample.jsonld").to_owned()),
// Optional content type.
Some("application/ld+json".parse().unwrap()),
value,
);
// Use `NoLoader` as we won't need to load any remote document.
let loader = json_ld::NoLoader;
let mut generator = json_ld::rdf_types::generator::Blank::new();
// Drawn from https://docs.rs/json-ld/latest/json_ld/trait.JsonLdProcessor.html#method.to_rdf
let mut rdf = input
.to_rdf(&mut generator, &loader)
.await
.expect("flattening failed");
// Store-querying code adapted from:
// https://docs.rs/oxigraph/0.4.1/oxigraph/store/struct.Store.html#method.query
// Store-populating code adapted from:
// https://docs.rs/oxigraph/0.4.3/oxigraph/store/struct.Store.html#method.insert
let store = Store::new().unwrap();
for quad in rdf.quads() {
// dbg!(&quad);
let Quad(s, p, o, _g) = quad;
let n_subject = NamedNodeRef::new(s.as_iri().unwrap()).unwrap();
let n_predicate = NamedNodeRef::new(p.as_iri().unwrap()).unwrap();
if let Term::Id(_id) = o.to_owned() {
store
.insert(QuadRef::new(
n_subject,
n_predicate,
NamedNodeRef::new(_id.as_iri().unwrap()).unwrap(),
GraphNameRef::DefaultGraph,
))
.unwrap();
};
if let Term::Literal(_literal) = o.to_owned() {
store
.insert(QuadRef::new(
n_subject,
n_predicate,
LiteralRef::new_simple_literal(&_literal.value),
GraphNameRef::DefaultGraph,
))
.unwrap();
};
}
let mut gj_features = vec![];
let query = r#"
PREFIX uco-core: <https://ontology.unifiedcyberontology.org/uco/core/>
PREFIX uco-location: <https://ontology.unifiedcyberontology.org/uco/location/>
SELECT ?lLatitude ?lLongitude ?lAddressType ?lCountry ?lLocality ?lPostalCode ?lRegion ?lStreet
WHERE
{
?nLocation a uco-location:Location .
OPTIONAL
{
?nLocation uco-core:hasFacet ?nLatLongFacet .
?nLatLongFacet a uco-location:LatLongCoordinatesFacet .
OPTIONAL { ?nLatLongFacet uco-location:latitude ?lLatitude . }
OPTIONAL { ?nLatLongFacet uco-location:longitude ?lLongitude . }
}
OPTIONAL {
?nLocation uco-core:hasFacet ?nSimpleAddressFacet .
?nSimpleAddressFacet a uco-location:SimpleAddressFacet .
OPTIONAL { ?nSimpleAddressFacet uco-location:addressType ?lAddressType . }
OPTIONAL { ?nSimpleAddressFacet uco-location:country ?lCountry . }
OPTIONAL { ?nSimpleAddressFacet uco-location:locality ?lLocality . }
OPTIONAL { ?nSimpleAddressFacet uco-location:postalCode ?lPostalCode . }
OPTIONAL { ?nSimpleAddressFacet uco-location:region ?lRegion . }
OPTIONAL { ?nSimpleAddressFacet uco-location:street ?lStreet . }
}
}
"#;
// SPARQL query
if let QueryResults::Solutions(solutions) = SparqlEvaluator::new()
.parse_query(query)
.expect("SPARQL syntax expected to be OK")
.on_store(&store)
.execute()
.expect("SPARQL expected to execute")
{
for option_solution in solutions {
let solution = option_solution.unwrap();
let l_latitude = &solution.get("lLatitude");
let l_longitude = &solution.get("lLongitude");
let _l_address_type = &solution.get("lAddressType");
let l_country = &solution.get("lCountry");
let l_locality = &solution.get("lLocality");
let l_postal_code = &solution.get("lPostalCode");
let l_region = &solution.get("lRegion");
let l_street = &solution.get("lStreet");
// Note - this property was not demonstrated in the example data.
// dbg!(_l_address_type);
// dbg!(l_latitude);
// dbg!(l_longitude);
let mut gj_point: Option<geojson::Geometry> = None;
if let Some(x) = l_longitude {
if let Some(y) = l_latitude {
// Remove 1st and last chars due to extra quote marks.
// https://stackoverflow.com/a/70598494
let s_latitude: String = unquote_string(y.to_string());
let s_longitude: String = unquote_string(x.to_string());
let f_latitude = (s_latitude).parse::<f64>().unwrap();
let f_longitude = (s_longitude).parse::<f64>().unwrap();
gj_point = Some(geojson::Geometry::new(geojson::Value::Point(vec![
f_longitude,
f_latitude,
])));
};
};
// dbg!(l_street);
let mut gj_properties: geojson::JsonObject = serde_json::Map::new();
if let Some(x) = l_street {
gj_properties.insert(
String::from("street"),
geojson::JsonValue::String(unquote_string(x.to_string())),
);
}
// dbg!(l_locality);
if let Some(x) = l_locality {
gj_properties.insert(
String::from("locality"),
geojson::JsonValue::String(unquote_string(x.to_string())),
);
}
// dbg!(l_region);
if let Some(x) = l_region {
gj_properties.insert(
String::from("region"),
geojson::JsonValue::String(unquote_string(x.to_string())),
);
}
// dbg!(l_postal_code);
if let Some(x) = l_postal_code {
gj_properties.insert(
String::from("postalCode"),
geojson::JsonValue::String(unquote_string(x.to_string())),
);
}
// dbg!(l_country);
if let Some(x) = l_country {
gj_properties.insert(
String::from("country"),
geojson::JsonValue::String(unquote_string(x.to_string())),
);
}
let gj_feature = geojson::Feature {
bbox: None,
geometry: gj_point,
id: None,
properties: Some(gj_properties),
foreign_members: None,
};
// dbg!(gj_feature);
gj_features.push(gj_feature)
}
}
let gj_feature_collection = geojson::FeatureCollection {
bbox: None,
features: gj_features,
foreign_members: None,
};
println!(
"{}",
geojson::GeoJson::from(gj_feature_collection).to_string()
);
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[tokio::test]
/// Adapted from [example source](https://docs.rs/json-ld/0.21.1/json_ld/#example)
async fn jsonld_expand_adapted_example() {
let file_contents = r#"
{
"@context": {
"@vocab": "http://example.org/local#",
"kb": "http://example.org/kb/",
"acme": "http://custompb.acme.org/core#",
"uco-core": "https://ontology.unifiedcyberontology.org/uco/core/",
"uco-location": "https://ontology.unifiedcyberontology.org/uco/location/",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"@graph": [
{
"@id": "kb:location-4511219e-a924-4ba5-aee7-dfad5a2c9c05",
"@type": "uco-location:Location",
"uco-core:hasFacet": [
{
"@id": "kb:simple-address-facet-59334948-00b9-4370-85b0-4dc8e07f5384",
"@type": "uco-location:SimpleAddressFacet",
"uco-location:locality": "Seattle",
"uco-location:region": "WA",
"uco-location:postalCode": "98052",
"uco-location:street": "20341 Whitworth Institute 405 N. Whitworth"
},
{
"@id": "kb:acme-internal-location-facet-41fb3158-bbab-404d-97e4-ac61debb71f3",
"@type": [
"acme:InternalLocationFacet",
"uco-core:Facet"
],
"acme:floor": 3,
"acme:roomNumber": 345
}
]
},
{
"@id": "kb:location-b579264d-6e30-4055-bf9b-72390364f224",
"@type": "uco-location:Location",
"uco-core:hasFacet": [
{
"@id": "kb:simple-address-facet-258f169e-1e9c-4936-ba65-eed0f0c60788",
"@type": "uco-location:SimpleAddressFacet",
"uco-location:locality": "Paris",
"uco-location:country": "France",
"uco-location:postalCode": "F-75002",
"uco-location:street": "38 Bad Guy Headquarters st."
},
{
"@id": "kb:lat-long-coordinates-facet-36126f9c-0273-48fe-ad4d-6a4e2848458f",
"@type": "uco-location:LatLongCoordinatesFacet",
"uco-location:latitude": {
"@type": "xsd:decimal",
"@value": "48.860346"
},
"uco-location:longitude": {
"@type": "xsd:decimal",
"@value": "2.331199"
}
}
]
}
]
}
"#;
// Parse the file.
let value = Value::parse_str(file_contents)
.expect("unable to parse file")
.0;
// Create a "remote" document by parsing a file manually.
let input = RemoteDocument::new(
// We use `IriBuf` as IRI type.
Some(iri!("https://example.com/sample.jsonld").to_owned()),
// Optional content type.
Some("application/ld+json".parse().unwrap()),
value,
);
// Use `NoLoader` as we won't need to load any remote document.
let mut loader = json_ld::NoLoader;
// Expand the "remote" document.
let expanded = input.expand(&mut loader).await.expect("expansion failed");
let mut expected = HashSet::new();
let mut computed = HashSet::new();
expected.insert(
"http://example.org/kb/location-4511219e-a924-4ba5-aee7-dfad5a2c9c05".to_string(),
);
expected.insert(
"http://example.org/kb/location-b579264d-6e30-4055-bf9b-72390364f224".to_string(),
);
for object in expanded {
if let Some(id) = object.id() {
computed.insert(id.as_iri().unwrap().to_string());
}
}
assert_eq!(expected, computed,);
}
#[test]
/// Adapted from [example source](https://docs.rs/oxigraph/0.4.3/oxigraph/store/struct.Store.html#method.insert)
fn store_insert_adapted_example() -> Result<(), Box<dyn std::error::Error>> {
let n_kb_location = NamedNodeRef::new(
"http://example.org/kb/location-4511219e-a924-4ba5-aee7-dfad5a2c9c05",
)?;
let n_rdf_type = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;
let n_uco_location_location =
NamedNodeRef::new("https://ontology.unifiedcyberontology.org/uco/location/Location")?;
// Create three quads from the same triple-parts.
let quad0 = QuadRef::new(
n_kb_location,
n_rdf_type,
n_uco_location_location,
GraphNameRef::DefaultGraph,
);
let quad1 = QuadRef::new(
n_kb_location,
n_rdf_type,
n_uco_location_location,
GraphNameRef::DefaultGraph,
);
let quad2 = QuadRef::new(
n_kb_location,
n_rdf_type,
n_uco_location_location,
GraphNameRef::DefaultGraph,
);
let store = Store::new()?;
store.insert(quad0)?;
store.insert(quad1)?;
assert!(store.contains(quad2)?);
Result::<_, Box<dyn std::error::Error>>::Ok(())
}
#[test]
/// Adapted from [example source](https://docs.rs/oxigraph/0.4.3/oxigraph/store/struct.Store.html#method.query)
fn store_query_adapted_example() -> Result<(), Box<dyn std::error::Error>> {
let store = Store::new()?;
let query = r#"
PREFIX uco-location: <https://ontology.unifiedcyberontology.org/uco/location/>
SELECT ?nLocation
WHERE {
?nLocation
a uco-location:Location ;
.
}
"#;
// insertions
let n_kb_location = NamedNodeRef::new(
"http://example.org/kb/location-4511219e-a924-4ba5-aee7-dfad5a2c9c05",
)?;
let n_rdf_type = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")?;
let n_uco_location_location =
NamedNodeRef::new("https://ontology.unifiedcyberontology.org/uco/location/Location")?;
// NOTE: store.insert only takes a QuadRef, not a TripleRef.
store.insert(QuadRef::new(
n_kb_location,
n_rdf_type,
n_uco_location_location,
GraphNameRef::DefaultGraph,
))?;
// SPARQL query
if let QueryResults::Solutions(mut solutions) = SparqlEvaluator::new()
.parse_query(query)
.expect("SPARQL syntax expected to be OK")
.on_store(&store)
.execute()
.expect("SPARQL expected to execute")
{
assert_eq!(
solutions.next().unwrap()?.get("nLocation"),
Some(&n_kb_location.into_owned().into())
);
}
Result::<_, Box<dyn std::error::Error>>::Ok(())
}
}