From 62448ed03f2038597bbad1424ec322ce770e261a Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Thu, 17 Jan 2019 11:24:33 +0100 Subject: [PATCH] Added lonlat string conversion utility method --- src/main/java/no/nibio/vips/gis/GISUtil.java | 22 +++++++++++++ .../vips/gis/LonLatStringFormatException.java | 31 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/no/nibio/vips/gis/LonLatStringFormatException.java diff --git a/src/main/java/no/nibio/vips/gis/GISUtil.java b/src/main/java/no/nibio/vips/gis/GISUtil.java index a009737..c7c2ca5 100644 --- a/src/main/java/no/nibio/vips/gis/GISUtil.java +++ b/src/main/java/no/nibio/vips/gis/GISUtil.java @@ -267,4 +267,26 @@ public class GISUtil { return null; } } + + public Point getJtsPointFromString(String lonlatCoordinate) throws LonLatStringFormatException + { + String[] xy = lonlatCoordinate.split(","); + if(xy.length != 2) + { + throw new LonLatStringFormatException("Coordinate string must contain [lon,lat], which is not the case here: " + lonlatCoordinate); + } + try + { + Double x = Double.valueOf(xy[0]); + Double y = Double.valueOf(xy[1]); + com.vividsolutions.jts.geom.Coordinate jtsCoordinate = new com.vividsolutions.jts.geom.Coordinate(); + jtsCoordinate.x = x; + jtsCoordinate.y = y; + return createPointWGS84(jtsCoordinate); + } + catch(NumberFormatException ex) + { + throw new LonLatStringFormatException(ex.getMessage()); + } + } } diff --git a/src/main/java/no/nibio/vips/gis/LonLatStringFormatException.java b/src/main/java/no/nibio/vips/gis/LonLatStringFormatException.java new file mode 100644 index 0000000..5a4c078 --- /dev/null +++ b/src/main/java/no/nibio/vips/gis/LonLatStringFormatException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSLogic. + * VIPSLogic is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSLogic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.gis; + +/** + * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class LonLatStringFormatException extends Exception { + public LonLatStringFormatException(String msg) + { + super(msg); + } +} -- GitLab