RegionChooserBrowser.java 8.33 KB
Newer Older
eric.duminil's avatar
eric.duminil committed
1
2
3
4
5
6
7
8
9
10
11
12
package eu.simstadt.regionchooser;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
Eric Duminil's avatar
Eric Duminil committed
13
import java.util.logging.Logger;
eric.duminil's avatar
eric.duminil committed
14
15
16
17
18
19
20
21
22
23
24
import java.util.prefs.Preferences;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.xml.stream.XMLStreamException;
import org.xml.sax.SAXParseException;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.ximpleware.NavException;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;
25
import eu.simstadt.geo.fast_xml_parser.ConvexHullCalculator;
eric.duminil's avatar
eric.duminil committed
26
import eu.simstadt.nf4j.ExportJobFromJavaFXRegionChooser;
27
import javafx.application.Platform;
eric.duminil's avatar
eric.duminil committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import netscape.javascript.JSObject;


public class RegionChooserBrowser extends Region
{
Eric Duminil's avatar
Eric Duminil committed
43
44
	private static final Logger LOGGER = Logger.getLogger(RegionChooserBrowser.class.getName());

eric.duminil's avatar
eric.duminil committed
45
46
47
48
49
50
51
52
53
54
55
56
	/**
	 * JavaFX Backend for RegionChooser. Inside simstadt_openlayers.js frontend, this class is available as `fxapp`.
	 */
	public class JavaScriptFXBridge
	{
		private Path repo;
		private WKTReader wktReader = new WKTReader();

		public JavaScriptFXBridge() {
			Preferences userPrefs = Preferences.userRoot().node("/eu/simstadt/desktop");
			String repoString = userPrefs.get("RECENT_REPOSITORY", null);
			if (repoString == null) {
57
				//TODO: Add a FileDialog? Possibly next to RegionChooserTitle
eric.duminil's avatar
eric.duminil committed
58
59
60
61
62
63
				repo = Paths.get("../TestRepository");
			} else {
				repo = Paths.get(repoString);
			}
		}

64
65
66
67
		/**
		 * Launches a background thread in which the hull gets extracted for every CityGML file. The hull gets sent back
		 * to the JS app in order to be displayed.
		 * 
Eric Duminil's avatar
Eric Duminil committed
68
69
		 * NOTE: To be very honest, I don't really understand concurrency in JavaFX. Eric
		 * 
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
		 */
		public void refreshHulls() {
			//NOTE: Could add progress bar?
			Task<Void> task = new Task<Void>() {
				@Override
				public Void call() throws IOException {
					ConvexHullCalculator.extractHullsForEveryCityGML(repo,
							(hullKML -> {
								Platform.runLater(new Runnable() {
									@Override
									public void run() {
										jsApp.call("addCityGmlHull", hullKML);
									}
								});
							}));
					return null;
				}
			};

			task.setOnRunning(e -> {
				jsApp.call("display", "Importing citgyml. Please wait.");
			});

			task.setOnSucceeded(e -> {
				jsApp.call("ready");
			});

			new Thread(task).start();
98
99
		}

eric.duminil's avatar
eric.duminil committed
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
		public void downloadRegion(String wktPolygon, String productName, JSObject novaFactoryLayer)
				throws InterruptedException {
			//TODO: Ask nf Server about available regions
			Task<Integer> task = new Task<Integer>() {
				@Override
				protected Integer call() throws Exception {
					ExportJobFromJavaFXRegionChooser nfJob = new ExportJobFromJavaFXRegionChooser();
					Geometry poly = wktReader.read(wktPolygon);
					nfJob.processJob(poly, productName, novaFactoryLayer);
					return 0;
				}
			};
			new Thread(task).start();
		}

		public void extractZIPtoGML(String zipFilename) throws IOException {
			ZipFile zipFile = new ZipFile(zipFilename);
			Enumeration<? extends ZipEntry> entries = zipFile.entries();
			String userName = System.getProperty("user.name");
			while (entries.hasMoreElements()) {
				ZipEntry ze = entries.nextElement();
				String zeName = ze.getName();
				if (zeName.toLowerCase().contains("gml")) {
					File extractedCityGML = selectSaveFileWithDialog(null,
							zeName.replace("_GML.", ".").replace(userName, "novaFACTORY"), "");
					if (extractedCityGML != null) {
						InputStream cityGMLInputStream = zipFile.getInputStream(ze);
						BufferedReader cityGMLZipReader = new BufferedReader(new InputStreamReader(cityGMLInputStream));
						BufferedWriter cityGMLOutput = Files.newBufferedWriter(extractedCityGML.toPath());
						String buf = null;
						while ((buf = cityGMLZipReader.readLine()) != null) {
							cityGMLOutput.write(buf.replace("srsName=\"\"", "srsName=\"EPSG:31467\"")); //TODO: Get EPSG:id from NovaFactory Server?
						}
						cityGMLZipReader.close();
						cityGMLInputStream.close();
						cityGMLOutput.close();
Eric Duminil's avatar
Eric Duminil committed
136
						LOGGER.info("Extracted");
eric.duminil's avatar
eric.duminil committed
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
					}
				}
			}
			zipFile.close();
		}

		public void downloadRegionFromCityGML(String wktPolygon, String project, String citygml, String srsName)
				throws IOException, ParseException, SAXParseException, XMLStreamException, NumberFormatException,
				XPathParseException, NavException, XPathEvalException {
			StringBuffer sb = RegionExtractor.selectRegionDirectlyFromCityGML(citygmlPath(project, citygml), wktPolygon,
					srsName);

			File buildingIdsFile = selectSaveFileWithDialog(project, citygml, "selected_region");
			if (buildingIdsFile != null) {
				BufferedWriter writer = Files.newBufferedWriter(buildingIdsFile.toPath());
				writer.write(sb.toString());
				writer.close();
			}
		}

		private File selectSaveFileWithDialog(String project, String citygml, String suffix) {
			Stage mainStage = (Stage) RegionChooserBrowser.this.getScene().getWindow();
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle("Save CITYGML ids");
			if (project != null) {
				fileChooser.setInitialDirectory(repo.resolve(project + ".proj").toFile());
			} else {
				fileChooser.setInitialDirectory(repo.toFile());
			}
			if (suffix.isEmpty()) {
				fileChooser.setInitialFileName(citygml);
			} else {
				fileChooser.setInitialFileName(citygml.replace(".", "_" + suffix + "."));
			}
			FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("GML files (*.gml)", "*.gml");
			fileChooser.getExtensionFilters().add(extFilter);
			return fileChooser.showSaveDialog(mainStage);
		}

		public void log(String text) {
Eric Duminil's avatar
Eric Duminil committed
177
			LOGGER.info(text);
eric.duminil's avatar
eric.duminil committed
178
179
		}

180
181
182
183
		public void warning(String text) {
			LOGGER.warning(text);
		}

eric.duminil's avatar
eric.duminil committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
		private Path citygmlPath(String project, String citygml) {
			return repo.resolve(project + ".proj").resolve(citygml);
		}

		public void importNovaFactoryBoundingBoxes() throws IOException {
			BufferedReader nf_csv = new BufferedReader(new InputStreamReader(
					RegionChooserFX.class.getResourceAsStream("website/data/novafactory_products.csv")));
			nf_csv.readLine();
			String sCurrentLine;
			while ((sCurrentLine = nf_csv.readLine()) != null) {
				String[] values = sCurrentLine.trim().split(",");
				String product = values[1];
				//				String description = values[2];
				String[] srs = values[3].split(" ");
				String epsgId = srs[srs.length - 1];
				//				System.out.println(product);
200
				jsApp.call("addNovaFactoryProduct", values[8], values[9], values[10], values[11], product,
eric.duminil's avatar
eric.duminil committed
201
202
203
204
205
206
207
208
						epsgId);
			}
			nf_csv.close();
		}
	}

	final WebView browser = new WebView();
	final WebEngine webEngine = browser.getEngine();
209
	final JavaScriptFXBridge fxapp = new JavaScriptFXBridge();
210
	private JSObject jsApp;
eric.duminil's avatar
eric.duminil committed
211
212
213
214
215
216
217
218
219
220

	public RegionChooserBrowser() {
		//apply the styles
		getStyleClass().add("browser");
		String url = RegionChooserFX.class.getResource("website/index.html").toExternalForm();
		webEngine.load(url); // load the web page
		// process page loading
		webEngine.getLoadWorker().stateProperty().addListener(
				(ObservableValue<? extends State> ov, State oldState, State newState) -> {
					if (newState == State.SUCCEEDED) {
221
222
						jsApp = (JSObject) webEngine.executeScript("regionChooser");
						jsApp.call("setFxApp", fxapp);
223
						fxapp.refreshHulls();
eric.duminil's avatar
eric.duminil committed
224
225
226
						try {
							fxapp.importNovaFactoryBoundingBoxes();
						} catch (Exception ex) {
Eric Duminil's avatar
Eric Duminil committed
227
							LOGGER.warning("NovaFactory CSV not found or corrupt");
eric.duminil's avatar
eric.duminil committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
							ex.printStackTrace();
						}
					}
				});
		//add the web view to the scene
		getChildren().add(browser);
	}

	@Override
	protected void layoutChildren() {
		double w = getWidth();
		double h = getHeight();
		layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
	}

	@Override
	protected double computePrefWidth(double height) {
245
		return 1024;
eric.duminil's avatar
eric.duminil committed
246
247
248
249
	}

	@Override
	protected double computePrefHeight(double width) {
250
		return 720;
eric.duminil's avatar
eric.duminil committed
251
252
	}
}