improve GeoJSON read function

This commit is contained in:
wyq 2025-01-08 16:47:06 +08:00
parent 3fe5d1330a
commit 5b154cc6e1
2 changed files with 109 additions and 16 deletions

View File

@ -31,28 +31,18 @@ public class GeoJSONReader {
ls.setFieldName(fieldName); ls.setFieldName(fieldName);
for (int i = 0; i < features.getNumFeatures(); i++) { for (int i = 0; i < features.getNumFeatures(); i++) {
Feature feature = features.getFeature(i); Feature feature = features.getFeature(i);
Map<String, Object> properties = feature.getProperties(); if (GeoJSONUtil.getShapeType(feature) != shape.getShapeType()) {
String titleValue = (String) properties.get("title"); continue;
PolygonBreak cb = new PolygonBreak(); }
cb.setStartValue(titleValue);
cb.setCaption(titleValue); ColorBreak cb = GeoJSONUtil.getLegendBreak(feature);
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);
ls.addLegendBreak(cb); ls.addLegendBreak(cb);
Geometry geometry = feature.getGeometry(); Geometry geometry = feature.getGeometry();
if (geometry != null) { if (geometry != null) {
try { try {
int idx = layer.getShapeNum(); int idx = layer.getShapeNum();
layer.editInsertShape(GeoJSONUtil.toShape(geometry), idx); layer.editInsertShape(GeoJSONUtil.toShape(geometry), idx);
layer.editCellValue(fieldName, idx, titleValue); layer.editCellValue(fieldName, idx, cb.getCaption());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -1,9 +1,18 @@
package org.meteoinfo.geometry.io.geojson; package org.meteoinfo.geometry.io.geojson;
import org.meteoinfo.common.PointD; 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.*;
import org.meteoinfo.geometry.shape.Shape;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class GeoJSONUtil { public class GeoJSONUtil {
@ -338,4 +347,98 @@ public class GeoJSONUtil {
return polygonZShape; 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<String, Object> 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();
}
}
} }