diff --git a/meteoinfo-geo/src/main/java/org/meteoinfo/geo/io/GeoJSONReader.java b/meteoinfo-geo/src/main/java/org/meteoinfo/geo/io/GeoJSONReader.java index 098b897e..099638bd 100644 --- a/meteoinfo-geo/src/main/java/org/meteoinfo/geo/io/GeoJSONReader.java +++ b/meteoinfo-geo/src/main/java/org/meteoinfo/geo/io/GeoJSONReader.java @@ -31,28 +31,18 @@ public class GeoJSONReader { ls.setFieldName(fieldName); for (int i = 0; i < features.getNumFeatures(); i++) { Feature feature = features.getFeature(i); - Map properties = feature.getProperties(); - String titleValue = (String) properties.get("title"); - PolygonBreak cb = new PolygonBreak(); - cb.setStartValue(titleValue); - cb.setCaption(titleValue); - Color color = ColorUtil.parseToColor((String) properties.get("fill")); - float alpha = Float.parseFloat(properties.get("fill-opacity").toString()); - color = ColorUtil.getColor(color, alpha); - cb.setColor(color); - color = ColorUtil.parseToColor((String) properties.get("stroke")); - alpha = Float.parseFloat(properties.get("stroke-opacity").toString()); - color = ColorUtil.getColor(color, alpha); - cb.setOutlineColor(color); - float lineWidth = Float.parseFloat(properties.get("stroke-width").toString()); - cb.setOutlineSize(lineWidth); + if (GeoJSONUtil.getShapeType(feature) != shape.getShapeType()) { + continue; + } + + ColorBreak cb = GeoJSONUtil.getLegendBreak(feature); ls.addLegendBreak(cb); Geometry geometry = feature.getGeometry(); if (geometry != null) { try { int idx = layer.getShapeNum(); layer.editInsertShape(GeoJSONUtil.toShape(geometry), idx); - layer.editCellValue(fieldName, idx, titleValue); + layer.editCellValue(fieldName, idx, cb.getCaption()); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/meteoinfo-geometry/src/main/java/org/meteoinfo/geometry/io/geojson/GeoJSONUtil.java b/meteoinfo-geometry/src/main/java/org/meteoinfo/geometry/io/geojson/GeoJSONUtil.java index cfb4fb00..fd6debe3 100644 --- a/meteoinfo-geometry/src/main/java/org/meteoinfo/geometry/io/geojson/GeoJSONUtil.java +++ b/meteoinfo-geometry/src/main/java/org/meteoinfo/geometry/io/geojson/GeoJSONUtil.java @@ -1,9 +1,18 @@ package org.meteoinfo.geometry.io.geojson; import org.meteoinfo.common.PointD; +import org.meteoinfo.common.colors.ColorUtil; +import org.meteoinfo.geometry.legend.ColorBreak; +import org.meteoinfo.geometry.legend.PointBreak; +import org.meteoinfo.geometry.legend.PolygonBreak; +import org.meteoinfo.geometry.legend.PolylineBreak; import org.meteoinfo.geometry.shape.*; +import org.meteoinfo.geometry.shape.Shape; + +import java.awt.*; import java.util.ArrayList; import java.util.List; +import java.util.Map; public class GeoJSONUtil { @@ -338,4 +347,98 @@ public class GeoJSONUtil { return polygonZShape; } } + + /** + * Get shape type from GeoJSON geometry + * @param geometry GeoJSON geometry + * @return Shape type + */ + public static ShapeTypes getShapeType(Geometry geometry) { + if (geometry instanceof Point) { + return ShapeTypes.POINT; + } else if (geometry instanceof LineString) { + return ShapeTypes.POLYLINE; + } else if (geometry instanceof MultiLineString) { + return ShapeTypes.POLYLINE; + } else if (geometry instanceof Polygon) { + return ShapeTypes.POLYGON; + } else if (geometry instanceof MultiPolygon) { + return ShapeTypes.POLYGON; + } else { + throw new UnsupportedOperationException(); + } + } + + /** + * Get shape type from GeoJSON feature + * @param feature GeoJSON feature + * @return Shape type + */ + public static ShapeTypes getShapeType(Feature feature) { + return getShapeType(feature.getGeometry()); + } + + /** + * Get legend break from GeoJSON feature + * @param feature GeoJSON feature + * @return Legend break + */ + public static ColorBreak getLegendBreak(Feature feature) { + ShapeTypes shapeType = getShapeType(feature); + Map properties = feature.getProperties(); + switch (shapeType) { + case POINT: + PointBreak pointBreak = new PointBreak(); + if (properties.containsKey("marker-color")) { + Color color = ColorUtil.parseToColor((String) properties.get("marker-color")); + pointBreak.setColor(color); + } + return pointBreak; + case POLYLINE: + PolylineBreak polylineBreak = new PolylineBreak(); + if (properties.containsKey("stroke")) { + Color color = ColorUtil.parseToColor((String) properties.get("stroke")); + if (properties.containsKey("stroke-opacity")) { + float alpha = Float.parseFloat(properties.get("stroke-opacity").toString()); + color = ColorUtil.getColor(color, alpha); + } + polylineBreak.setColor(color); + } + if (properties.containsKey("stroke-width")) { + float lineWidth = Float.parseFloat(properties.get("stroke-width").toString()); + polylineBreak.setWidth(lineWidth); + } + return polylineBreak; + case POLYGON: + PolygonBreak cb = new PolygonBreak(); + if (properties.containsKey("title")) { + String titleValue = (String) properties.get("title"); + cb.setStartValue(titleValue); + cb.setCaption(titleValue); + } + if (properties.containsKey("fill")) { + Color color = ColorUtil.parseToColor((String) properties.get("fill")); + if (properties.containsKey("fill-opacity")) { + float alpha = Float.parseFloat(properties.get("fill-opacity").toString()); + color = ColorUtil.getColor(color, alpha); + } + cb.setColor(color); + } + if (properties.containsKey("stroke")) { + Color color = ColorUtil.parseToColor((String) properties.get("stroke")); + if (properties.containsKey("stroke-opacity")) { + float alpha = Float.parseFloat(properties.get("stroke-opacity").toString()); + color = ColorUtil.getColor(color, alpha); + } + cb.setOutlineColor(color); + } + if (properties.containsKey("stroke-width")) { + float lineWidth = Float.parseFloat(properties.get("stroke-width").toString()); + cb.setOutlineSize(lineWidth); + } + return cb; + default: + throw new UnsupportedOperationException(); + } + } }