diff --git a/colorMappings.csv b/colorMappings.csv new file mode 100644 index 0000000000000000000000000000000000000000..44d604eed2ff2aa7753c126fc5f48b68f1847dc0 --- /dev/null +++ b/colorMappings.csv @@ -0,0 +1 @@ +# polygonName, red (0 - 255), green (0 - 255), blue (0 - 255) diff --git a/src/main/java/de/hft/stuttgart/citygml/viewer/CityGMLViewer.java b/src/main/java/de/hft/stuttgart/citygml/viewer/CityGMLViewer.java index 0dd1f295bcd62c247f3c0178c88138d41414ceab..6777879fc0d2df4a32a7c63709e19073f0732279 100644 --- a/src/main/java/de/hft/stuttgart/citygml/viewer/CityGMLViewer.java +++ b/src/main/java/de/hft/stuttgart/citygml/viewer/CityGMLViewer.java @@ -34,6 +34,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; +import javax.swing.JOptionPane; import javax.xml.namespace.QName; import javax.xml.parsers.ParserConfigurationException; @@ -117,6 +118,12 @@ public class CityGMLViewer { } try { setupWindow(f); + } catch (Exception e) { + String message = e.getMessage(); + if (e.getCause() != null) { + message += "\n" + e.getCause().getMessage(); + } + JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); } finally { for (PolygonViewInformation view : viewing) { view.destroy(); @@ -372,9 +379,9 @@ public class CityGMLViewer { try (ObservedInputStream ois = new ObservedInputStream(file); CityGMLReader reader = in.createCityGMLReader(file.getAbsolutePath(), ois)) { ois.addListener(p -> bar.drawProgress((int) (p * 82))); - - int threads = Runtime.getRuntime().availableProcessors(); - ExecutorService service = Executors.newFixedThreadPool(threads); + + int threads = Runtime.getRuntime().availableProcessors(); + ExecutorService service = Executors.newFixedThreadPool(threads); List<Future<FeatureMapper>> mappers = new ArrayList<>(); while (reader.hasNext()) { CityGMLChunk nextChunk = reader.nextChunk(); @@ -414,8 +421,8 @@ public class CityGMLViewer { Vector3d center = bbox.getCenter(); fMappers.forEach(m -> m.movePolygonsBy(center)); bar.drawProgress(82); - long nrOfPolygons = (long) lod1Polygons.size() + lod2Polygons.size() - + lod3Polygons.size() + lod4Polygons.size(); + long nrOfPolygons = (long) lod1Polygons.size() + lod2Polygons.size() + lod3Polygons.size() + + lod4Polygons.size(); long[] count = new long[1]; log.info(() -> "Found " + nrOfPolygons + " polygons"); PolygonListener l = () -> { diff --git a/src/main/java/de/hft/stuttgart/citygml/viewer/datastructure/Polygon.java b/src/main/java/de/hft/stuttgart/citygml/viewer/datastructure/Polygon.java index b3ccf15ec9ca401f12a3cc9b3eb241754cd31388..b831f7a9fbbcf231186b30c7d7f9808c7e3f3577 100644 --- a/src/main/java/de/hft/stuttgart/citygml/viewer/datastructure/Polygon.java +++ b/src/main/java/de/hft/stuttgart/citygml/viewer/datastructure/Polygon.java @@ -35,7 +35,7 @@ public class Polygon { public Color getColor() { return color; } - + public Ring getExteriorRing() { return exteriorRing; } diff --git a/src/main/java/de/hft/stuttgart/citygml/viewer/parser/FeatureMapper.java b/src/main/java/de/hft/stuttgart/citygml/viewer/parser/FeatureMapper.java index 9bb358e2d790ab3aeae4c39f5036ab5b19d34836..b350a2cc0fa72b04f3480430d0116c123d71831e 100644 --- a/src/main/java/de/hft/stuttgart/citygml/viewer/parser/FeatureMapper.java +++ b/src/main/java/de/hft/stuttgart/citygml/viewer/parser/FeatureMapper.java @@ -93,7 +93,14 @@ public class FeatureMapper extends ObjectWalker { if (currentPolygons == null) { return; } - Polygon viewerPoly = new Polygon(currentColor); + Color setColor = currentColor; + if (gmlPoly.getId() != null) { + Color mappedColor = PolygonColorMapper.getColorForPolygon(gmlPoly.getId()); + if (mappedColor != null) { + setColor = mappedColor; + } + } + Polygon viewerPoly = new Polygon(setColor); // parse rings Ring extRing = new Ring(); viewerPoly.setExteriorRing(extRing); diff --git a/src/main/java/de/hft/stuttgart/citygml/viewer/parser/PolygonColorMapper.java b/src/main/java/de/hft/stuttgart/citygml/viewer/parser/PolygonColorMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..e0ee135abc4679a7574278d148b3447938246ef0 --- /dev/null +++ b/src/main/java/de/hft/stuttgart/citygml/viewer/parser/PolygonColorMapper.java @@ -0,0 +1,83 @@ +package de.hft.stuttgart.citygml.viewer.parser; + +import java.awt.Color; +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import javax.swing.JOptionPane; + +public class PolygonColorMapper { + + private static final String IGNORING_ALL_POLYGON_COLOR_MAPPINGS = "\nIgnoring all polygon color mappings"; + private static Function<String, Color> producer; + private static Map<String, Color> colorMap; + + static { + loadColorMap(); + } + + + private static void loadColorMap() { + Path colorMappingPath = Path.of("colorMappings.csv"); + if (!Files.exists(colorMappingPath)) { + producer = s -> null; + return; + } + try (BufferedReader reader = Files.newBufferedReader(colorMappingPath)) { + colorMap = new HashMap<>(); + String line = null; + while ((line = reader.readLine()) != null) { + if (line.startsWith("#")) { + // skip comments + continue; + } + String[] mappingStrings = line.split(","); + String polygonId = mappingStrings[0]; + int r = Integer.parseInt(mappingStrings[1]); + if (r < 0 || r > 255) { + JOptionPane.showMessageDialog(null, "Wrong red value " + r + " for polygon: " + polygonId + IGNORING_ALL_POLYGON_COLOR_MAPPINGS, "Warning", JOptionPane.WARNING_MESSAGE); + colorMap = null; + producer = s -> null; + return; + } + int g = Integer.parseInt(mappingStrings[2]); + if (g < 0 || g > 255) { + JOptionPane.showMessageDialog(null, "Wrong green value " + g + " for polygon: " + polygonId + IGNORING_ALL_POLYGON_COLOR_MAPPINGS, "Warning", JOptionPane.WARNING_MESSAGE); + colorMap = null; + producer = s -> null; + return; + } + int b = Integer.parseInt(mappingStrings[3]); + if (b < 0 || b > 255) { + JOptionPane.showMessageDialog(null, "Wrong green value " + b + " for polygon: " + polygonId + IGNORING_ALL_POLYGON_COLOR_MAPPINGS, "Warning", JOptionPane.WARNING_MESSAGE); + colorMap = null; + producer = s -> null; + return; + } + Color color = new Color(r, g, b); + colorMap.put(polygonId, color); + } + if (colorMap.isEmpty()) { + producer = s -> null; + } else { + producer = s -> colorMap.get(s); + } + } catch (IOException e) { + JOptionPane.showMessageDialog(null, "Could not read mapping csv file, ignoring: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE); + } + } + + + public static Color getColorForPolygon(String polygonId) { + return producer.apply(polygonId); + } + + private PolygonColorMapper() { + } + +}