-
Notifications
You must be signed in to change notification settings - Fork 0
/
CityGMLChangeDetection.java
474 lines (391 loc) · 17.4 KB
/
CityGMLChangeDetection.java
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import logger.LogUtil;
import org.apache.http.client.ClientProtocolException;
import org.citygml4j.CityGMLContext;
import org.citygml4j.builder.CityGMLBuilder;
import org.citygml4j.model.citygml.CityGMLClass;
import org.citygml4j.model.citygml.building.AbstractOpening;
import org.citygml4j.model.citygml.core.Address;
import org.citygml4j.xml.io.CityGMLInputFactory;
import org.citygml4j.xml.io.reader.CityGMLReadException;
import org.citygml4j.xml.io.reader.CityGMLReader;
import org.citygml4j.xml.io.reader.FeatureReadMode;
import org.citygml4j.xml.io.reader.MissingADESchemaException;
import org.citygml4j.xml.io.reader.UnmarshalException;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import editor.Editor;
import exporter.EditOperationExporter;
import mapper.EnumClasses.GMLRelTypes;
import mapper.Mapper;
import matcher.Matcher;
import matcher.Matcher.EditOperators;
import stats.StatBot;
import util.*;
/**
* Suggestions, bug reports, etc. please contact: son.nguyen@tum.de
*/
public class CityGMLChangeDetection {
// Embedded Neo4j Java API
private GraphDatabaseService graphDb;
private Node mapperRootNode;
private Node matcherRootNode;
private Node editorRootNode;
private EditOperationExporter exporter;
private Logger logger;
private String oldFilename;
private String newFilename;
private String wfsServerUrl;
private long mapperRunTime = 0;
private long matcherRunTime = 0;
private StatBot statBot;
public CityGMLChangeDetection(String configFile) {
// Read command line arguments if available
try {
if (configFile == null || !ReadCMDUtil.readCommandLindArguments(configFile)) {
ReadCMDUtil.readCommandLindArguments("config/Default.txt");
}
} catch (IOException e) {
e.printStackTrace();
}
this.init();
}
public CityGMLChangeDetection(String[] args) {
// Read command line arguments if available
try {
if (args == null || !ReadCMDUtil.readCommandLindArguments(args)) {
ReadCMDUtil.readCommandLindArguments("config/Default.txt");
}
} catch (IOException e) {
e.printStackTrace();
}
this.init();
}
private void init() {
// Check if given folder paths exist, if not they shall be created
String[] checkPaths = {
SETTINGS.DB_LOCATION,
SETTINGS.EXPORT_LOCATION,
SETTINGS.RTREE_IMAGE_LOCATION
};
for (int i = 0; i < checkPaths.length; i++) {
FileUtil.createFileOrDirectory(checkPaths[i], true);
}
this.oldFilename = SETTINGS.OLD_CITY_MODEL_LOCATION;
this.newFilename = SETTINGS.NEW_CITY_MODEL_LOCATION;
this.wfsServerUrl = SETTINGS.WFS_SERVER;
if (SETTINGS.CLEAN_PREVIOUS_DB) {
this.cleanNeo4jStorage();
} else {
// Delete all existing databases from Neo4j to begin a new session
this.cleanNeo4jDatabase();
}
// Initialize a new Neo4j graph database
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(SETTINGS.DB_LOCATION));
// Initalize a logger
this.logger = LogUtil.getLoggerWithSimpleDateFormat(this.getClass().toString(), SETTINGS.LOG_LOCATION);
logger.info(SETTINGS.readSettings());
logger.info("\n------------------------------"
+ "\nINITIALIZING TESTING COMPONENT"
+ "\n------------------------------");
}
public void execute() {
try {
// Map CityGML instances into a graph database in Neo4j
this.map();
// Match mapped CityGML instances
this.match();
// Export edit operations to CSV files
this.export(SETTINGS.EXPORT_LOCATION, SETTINGS.CSV_DELIMITER);
// Execute WFS-Transactions
this.update();
// Statistics
this.printStats();
// Close Neo4j session
this.registerShutdownHook();
} catch (JAXBException e) {
e.printStackTrace();
} catch (CityGMLReadException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (UnmarshalException e) {
e.printStackTrace();
} catch (MissingADESchemaException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
private void registerShutdownHook() {
// Always write database operations in transactions
Transaction tx = graphDb.beginTx();
try {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
tx.success();
} finally {
tx.close();
}
}
private void cleanNeo4jDatabase() {
// !!! IMPORTANT: The constructor must have been executed beforehand to
// initialize a new Neo4j and Logger session. !!!
// Always write database operations in transactions
Transaction tx = graphDb.beginTx();
try {
logger.info("... deleting existing Neo4j databases ...");
graphDb.execute("MATCH (n) DETACH DELETE n");
tx.success();
} finally {
tx.close();
}
}
private void deleteRecursive(File fileOrDir) {
if (fileOrDir.isDirectory()) {
for (File file : fileOrDir.listFiles()) {
deleteRecursive(file);
}
}
fileOrDir.delete();
}
private void cleanNeo4jStorage() {
// Remove neo4j database from the hard drive
File neo4jDb = new File(SETTINGS.DB_LOCATION);
this.deleteRecursive(neo4jDb);
}
private long calculateRunTime(long startTime, String step) {
// Stop timestamp
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
logger.info("Step " + step + " finished");
logger.info("Time elapsed: " + totalTime / 1000 + " seconds");
return totalTime / 1000;
}
private void map() throws JAXBException, CityGMLReadException, InterruptedException, UnmarshalException, MissingADESchemaException {
long startTime = System.currentTimeMillis();
logger.info("... setting up citygml4j context and JAXB builder ...");
CityGMLContext ctx = new CityGMLContext();
CityGMLBuilder builder = ctx.createCityGMLBuilder();
logger.info("... reading CityGML files feature by feature ...");
CityGMLInputFactory in = builder.createCityGMLInputFactory();
if (SETTINGS.SPLIT_PER_COLLECTION_MEMBER) {
in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_COLLECTION_MEMBER);
} else {
in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_FEATURE);
}
in.setProperty(CityGMLInputFactory.EXCLUDE_FROM_SPLITTING, new Class[]{AbstractOpening.class, Address.class}); // to avoid unresolved xlinks
in.setProperty(CityGMLInputFactory.KEEP_INLINE_APPEARANCE, true);
// Initialize root node
// Always write database operations in transactions
Transaction tx = graphDb.beginTx();
try {
mapperRootNode = graphDb.createNode(Label.label("ROOT_MAPPER"));
tx.success();
} finally {
tx.close();
}
// map old city model
mapCityModel(in, this.oldFilename, GMLRelTypes.OLD_CITY_MODEL, true);
// map new city model
mapCityModel(in, this.newFilename, GMLRelTypes.NEW_CITY_MODEL, false);
// statistics
this.mapperRunTime = calculateRunTime(startTime, "MAPPING");
}
private void mapCityModel(CityGMLInputFactory in, String filename, RelationshipType relType, boolean isOld) throws CityGMLReadException, InterruptedException {
// Always write database operations in transactions
final Mapper mapper;
Transaction tx = graphDb.beginTx();
try {
mapper = new Mapper(graphDb, logger, isOld);
tx.success();
} finally {
tx.close();
}
if (filename.isEmpty() || !new File(filename).exists()) {
// if the city model does not exist -> create a dummy old/new city model node
Transaction txx = graphDb.beginTx();
try {
Node targetNode = mapper.createNodeWithLabel(CityGMLClass.CITY_MODEL);
mapperRootNode.createRelationshipTo(targetNode, relType);
tx.success();
} finally {
txx.close();
}
return;
}
CityGMLReader reader = in.createCityGMLReader(new File(filename));
mapper.mapperInit(reader, mapperRootNode);
// due to reading large CityGML files in chunks, separate features are marked with hrefs
// the tester simply links ROOT_MAPPER to those feature nodes with the relationship OBJECT
// the post-processing function should then delete these OBJECT relationships
// with the only exception is that CityModel will have an OLD__CITY_MODEL/NEW_CITY_MODEL relationship
mapper.postProcessing(mapperRootNode, relType);
// close reader
reader.close();
logger.info("Finished reading city model");
}
private void match() throws InterruptedException, FileNotFoundException, XMLStreamException {
long startTime = System.currentTimeMillis();
// initialize root node for matcher
// Always write database operations in transactions
Transaction tx = graphDb.beginTx();
try {
matcherRootNode = graphDb.createNode(Label.label("ROOT_MATCHER"));
tx.success();
} finally {
tx.close();
}
tx = graphDb.beginTx();
try {
// matching without mapping again
if (mapperRootNode == null) {
mapperRootNode = graphDb.findNodes(Label.label("ROOT_MAPPER")).next();
}
tx.success();
} finally {
tx.close();
}
Matcher matcher;
tx = graphDb.beginTx();
try {
matcher = new Matcher(graphDb, logger, this.oldFilename, this.newFilename);
tx.success();
} finally {
tx.close();
}
matcher.matcherInit(mapperRootNode, matcherRootNode);
// matcher.matchNode(
// matcher.findFirstDescendant(mapperRootNode, GMLRelTypes.OLD_CITY_MODEL),
// matcher.findFirstDescendant(mapperRootNode, GMLRelTypes.NEW_CITY_MODEL),
// matcherRootNode,
// new BooleanObject(false));
matcher.matcherPostProcessing(matcherRootNode, graphDb);
// statistics
this.matcherRunTime = calculateRunTime(startTime, "MATCHING");
}
private void export(String exportFolder, String delimiter) {
this.exporter = new EditOperationExporter(delimiter);
if ((exportFolder.charAt(exportFolder.length() - 1) != '/')
&& (exportFolder.charAt(exportFolder.length() - 1) != '\\')) {
exportFolder += "/";
}
String tmpLog = "Exporting created edit operations to " + exportFolder + " ...\n";
tmpLog += this.exporter.exportEditOperationsToCSV(graphDb, matcherRootNode, exportFolder);
logger.info(tmpLog);
}
private void update() throws XMLStreamException, InterruptedException, ClientProtocolException, IOException {
// initialize root node for matcher
// Always write database operations in transactions
Transaction tx = graphDb.beginTx();
try {
editorRootNode = graphDb.createNode(Label.label("ROOT_EDITOR"));
// updating without mapping/matching again
if (mapperRootNode == null) {
mapperRootNode = graphDb.findNodes(Label.label("ROOT_MAPPER")).next();
}
if (matcherRootNode == null) {
matcherRootNode = graphDb.findNodes(Label.label("ROOT_MATCHER")).next();
}
tx.success();
} finally {
tx.close();
}
Editor editor = new Editor(graphDb, logger, oldFilename, newFilename, wfsServerUrl);
// for demo purposes only
// editor.executeSimpleUpdate(matcherRootNode);
// editor.executeUpdate(mapperRootNode);
if (SETTINGS.ENABLE_EDITORS) {
editor.executeEditors(mapperRootNode, matcherRootNode);
}
}
// Statistics
private void printStats() {
logger.info("Preparing statistics report ...");
StringBuilder stats = new StringBuilder();
stats.append("\n\t STATISTICS REPORT\n");
// MAPPER
stats.append("\t _______________________________________________________________________ \n"
+ "\t| " + String.format("%-70s", "MAPPER ...") + " |\n");
long totalNrOfNodes = 0;
ArrayList<Label> editorLabels = new ArrayList<>();
for (EditOperators enu : EditOperators.values()) {
editorLabels.add(Label.label(enu + ""));
}
// Iterator<Entry<String, Long>> it = MapUtil.sortByValue(CityGML2Neo4jMapper.getStats()).entrySet().iterator();
Iterator<Entry<String, Long>> it = null;
Transaction tx = graphDb.beginTx();
try {
it = MapUtil.sortByValue(GraphUtil.countAllNodesWithLabels(graphDb, tx, null, editorLabels)).entrySet().iterator();
tx.success();
} finally {
tx.close();
}
while (it.hasNext()) {
Map.Entry<String, Long> pair = (Map.Entry<String, Long>) it.next();
stats.append("\t| " + String.format("%-60s", "Number of " + pair.getKey() + " nodes: ") + String.format("%10d", pair.getValue()) + " |\n");
totalNrOfNodes += (Long) pair.getValue();
}
stats.append("\t| " + String.format("%-70s", "") + " |\n"
+ "\t| " + String.format("%-55s", "TOTAL NUMBER OF CREATED NODES: ") + String.format("%15s", totalNrOfNodes + " nodes") + " |\n"
+ "\t| " + String.format("%-50s", "MAPPER'S ELAPSED TIME: ") + String.format("%20s", this.mapperRunTime + " seconds") + " |\n");
stats.append("\t ________________________________________________________________________/\n");
// MATCHER
if (this.exporter != null) {
stats.append("\n\t _______________________________________________________________________ \n"
+ "\t| " + String.format("%-70s", "MATCHER ...") + " |\n");
totalNrOfNodes = this.exporter.getTotalNrOfEditOperations();
// it = CityGML2Neo4jMatcher.getStats().entrySet().iterator();
// try (Transaction tx = graphDb.beginTx()) {
// it = GraphUtil.countAllNodesWithLabels(graphDb, tx, editorLabels,
// null).entrySet().iterator();
// tx.success();
// }
it = MapUtil.sortByValue(this.exporter.getNrOfEditOperations()).entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Long> pair = (Map.Entry<String, Long>) it.next();
stats.append("\t| " + String.format("%-60s", "Number of " + pair.getKey() + " nodes: ")
+ String.format("%10d", pair.getValue()) + " |\n");
// totalNrOfNodes += (Long) pair.getValue();
}
stats.append("\t| " + String.format("%-70s", "") + " |\n"
+ "\t| " + String.format("%-40s", "TOTAL NUMBER OF CREATED NODES: ") + String.format("%30s", totalNrOfNodes + " nodes") + " |\n"
+ "\t| " + String.format("%-40s", "OF WHICH ARE OPTIONAL: ") + String.format("%30s", this.exporter.getNrOfOptionalTransactions() + " nodes") + " |\n"
+ "\t| " + String.format("%-50s", "MATCHER'S ELAPSED TIME: ") + String.format("%20s", this.matcherRunTime + " seconds") + " |\n");
stats.append("\t ________________________________________________________________________/\n");
}
logger.info(stats.toString());
logger.info("END OF LOGFILE.");
// Statistics
this.statBot = new StatBot(SETTINGS.LOG_LOCATION, SETTINGS.EXPORT_LOCATION, SETTINGS.CSV_DELIMITER);
this.statBot.printAllStats();
}
public static void main(String[] args) {
CityGMLChangeDetection cityGMLChangeDetection = new CityGMLChangeDetection(args);
// CityGMLChangeDetection cityGMLChangeDetection = new CityGMLChangeDetection("config/Default.txt");
cityGMLChangeDetection.execute();
}
}