Commit 6cc5a16b authored by Matthias Betz's avatar Matthias Betz
Browse files

added configuration file to define colors by polygonID

parent 19a9b8c6
No related merge requests found
Showing with 105 additions and 7 deletions
+105 -7
# polygonName, red (0 - 255), green (0 - 255), blue (0 - 255)
......@@ -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 = () -> {
......
......@@ -35,7 +35,7 @@ public class Polygon {
public Color getColor() {
return color;
}
public Ring getExteriorRing() {
return exteriorRing;
}
......
......@@ -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);
......
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() {
}
}
Supports Markdown
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