Commit c308b061 authored by Eric Duminil's avatar Eric Duminil
Browse files

RegionChooser: Try to import dynamic kml.

parent ba84ea11
......@@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -59,15 +60,19 @@ public JavaScriptFXBridge() {
public String refreshHulls() throws IOException {
System.out.println("Look for hulls in " + repo);
Path url = Paths.get("just_a_test.kml");
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kml>\n" + " <Document>\n";
String footer = " </Document>\n" + "</kml>";
try (BufferedWriter bw = Files.newBufferedWriter(url)) {
bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kml>\n" + " <Document>\n");
bw.write(header);
Files.walk(repo)
.filter(Files::isRegularFile)
.filter(p -> p.toString().contains(".cache") && p.toString().endsWith(".xyz"))
.forEach(p -> {
System.out.println("Parsing " + p);
jsApp.call("addCityGmlHull", p.toString());
try {
String hullKML = new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
System.out.println(hullKML);
jsApp.call("addCityGmlHull", header + hullKML + footer);
List<String> lines = Files.readAllLines(p);
for (String line : lines) {
bw.write(line + "\n");
......@@ -76,7 +81,7 @@ public String refreshHulls() throws IOException {
ex.printStackTrace();
}
});
bw.write(" </Document>\n" + "</kml>");
bw.write(footer);
} catch (Exception ex) {
ex.printStackTrace();
}
......
......@@ -19,6 +19,7 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
source: new ol.source.OSM()
});
//NOTE: Use GeoJSON instead?
function read_kml(url){
return new ol.source.KML({
projection : ol.proj.get('EPSG:3857'),
......@@ -63,8 +64,13 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
features : []
});
publicScope.addCityGmlHull = function(p) {
dataPanel.append(p + "<br/>\n")
publicScope.addCityGmlHull = function(kmlString) {
kml_source.addFeature(kmlFormat.readFeature(kmlString,
{
featureProjection: ol.proj.get('EPSG:3857')
}
));
updateGMLPolygons();
};
publicScope.addNovaFactoryProduct = function(xmin, ymin, xmax, ymax, name, epsgId) {
......@@ -75,14 +81,15 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
geometry : box,
name : name,
});
feature["geoJSON"] = geoJSONformat.writeFeatureObject(feature);
feature["geoJSON"] = geoJsonFormat.writeFeatureObject(feature);
feature["area"] = feature.getGeometry().getArea();
feature["description"] = "novaFACTORY>" + name;
feature["available"] = true;
feature["source"] = "NovaFACTORY";
novafactory_vectors.addFeature(feature);
};
var novafactory_layer = new ol.layer.Vector({
source : novafactory_vectors,
style : polygon_style('#ff7700', 0.1)
......@@ -96,7 +103,8 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
})
});
var geoJSONformat = new ol.format.GeoJSON();
var geoJsonFormat = new ol.format.GeoJSON();
var kmlFormat = new ol.format.KML();
kml_layer.addEventListener("change", function() {
map.getView().fitExtent(kml_source.getExtent(), (map.getSize()));
......@@ -104,7 +112,7 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
function updateGMLPolygons() {
kml_source.forEachFeature(function(feature) {
feature["geoJSON"] = geoJSONformat.writeFeatureObject(feature);
feature["geoJSON"] = geoJsonFormat.writeFeatureObject(feature);
feature["area"] = feature.getGeometry().getArea();
var project = feature.get("project");
var name = feature.get("name");
......@@ -178,7 +186,7 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
function findIntersections() {
var sketch_area = sketch.getGeometry().getArea();
var poly1 = geoJSONformat.writeFeatureObject(sketch);
var poly1 = geoJsonFormat.writeFeatureObject(sketch);
var intersection_found = false;
intersections.clear();
function findIntersection(feature) {
......@@ -189,7 +197,7 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
dataPanel.append("Intersection found with :<br/>\n");
intersection_found = true;
}
var intersection = geoJSONformat.readFeature(jsonIntersection);
var intersection = geoJsonFormat.readFeature(jsonIntersection);
var intersectionArea = intersection.getGeometry().getArea();
var citygml_percentage = Math.round(intersectionArea / feature["area"] * 100);
var sketch_percentage = Math.round(intersectionArea / sketch_area * 100);
......@@ -301,10 +309,11 @@ proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.3876388
});
$('#refresh_hulls').click(function() {
kml_source.clear();
url = fxapp.refreshHulls();
console.log('Loading ' + url);
kml_source = read_kml(url);
kml_layer.setSource(kml_source);
//kml_source = read_kml(url);
//kml_layer.setSource(kml_source);
});
novafactory_layer.downloadFinished = function() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment