From a7601109c4bf8de875bb10efd3f596c9861fcdc4 Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Wed, 20 Sep 2023 10:05:25 +0200 Subject: [PATCH] Add getGeoJSONFeatureFromGeometry --- src/main/java/no/nibio/vips/gis/GISUtil.java | 30 ++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/no/nibio/vips/gis/GISUtil.java b/src/main/java/no/nibio/vips/gis/GISUtil.java index 41e901c..201af6d 100644 --- a/src/main/java/no/nibio/vips/gis/GISUtil.java +++ b/src/main/java/no/nibio/vips/gis/GISUtil.java @@ -42,7 +42,7 @@ import org.wololo.jts2geojson.GeoJSONWriter; /** * Handy tools for GIS operations - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> + * @copyright 2023 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class GISUtil { @@ -142,7 +142,7 @@ public class GISUtil { } /** - * Convert one JTS Geometry object into a GeoJSON feature + * Convert one JTS Geometry object into a complete GeoJSON String * @param geometry * @param properties * @return @@ -154,14 +154,28 @@ public class GISUtil { return ""; } List<Feature> features = new ArrayList<>(); + features.add(this.getGeoJSONFeatureFromGeometry(geometry, properties)); GeoJSONWriter writer = new GeoJSONWriter(); - - features.add(new Feature(writer.write(geometry), properties)); - FeatureCollection json = writer.write(features); return json.toString(); } + /** + * Convert one JTS Geometry object into a GeoJSON Feature + * @param geometry + * @param properties + * @return + */ + public Feature getGeoJSONFeatureFromGeometry(Geometry geometry,Map<String, Object> properties) + { + if(geometry == null) + { + return null; + } + GeoJSONWriter writer = new GeoJSONWriter(); + return new Feature(writer.write(geometry), properties); + } + /** * If you have WGS84 coordinates, th * @param coordinate in WGS84 system @@ -172,6 +186,12 @@ public class GISUtil { GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326); return gf.createPoint(coordinate); } + + public Point createPointWGS84(Double longitude, Double latitude) + { + Coordinate coordinate = new Coordinate(longitude, latitude); + return this.createPointWGS84(coordinate); + } /** * Calculate distance between two points, courtesy of <a href="http://www.movable-type.co.uk/scripts/latlong.html">this page</a>. Explanation: -- GitLab