javagisgeogeotools

User input geographic point syntax validation


I'm trying to work with geographic points in a java project. I started to look into good practice, started looking into GeoTols which seems to be the reference for java.

What I'm trying to do is validate a user input for geographic points. The user input is JSON file. So I'm parsing the file, and I would like to find something that validates the syntax and that a point at this coordinates is likely.

I couldn't find it (maybe I searched wrong) in GeoTools. Thanks!


Solution

  • validation coordinates should be determined from the reference coordinate system. you can visit epsg.io like wgs84 bounds, use the following code to determine whether the coordinates are within the corresponding interval.

    package org.example;
    
    
    import org.geotools.referencing.CRS;
    import org.opengis.referencing.crs.CoordinateReferenceSystem;
    import org.opengis.referencing.cs.CoordinateSystem;
    import org.opengis.referencing.cs.CoordinateSystemAxis;
    
    /**
     * @author Arjen10
     * @date 2023/9/15 下午4:55
     */
    public class Test2 {
    
        public static void main(String[] args) throws Exception {
            CoordinateReferenceSystem decode = CRS.decode("EPSG:4326", true);
            CoordinateSystem coordinateSystem = decode.getCoordinateSystem();
            CoordinateSystemAxis axis = coordinateSystem.getAxis(0);
            CoordinateSystemAxis axis1 = coordinateSystem.getAxis(1);
            double maxX = axis.getMaximumValue();
            double maxY = axis1.getMaximumValue();
            double minX = axis.getMinimumValue();
            double minY = axis1.getMinimumValue();
            System.out.println("wgs84 maxX " + maxX);
            System.out.println("wgs84 maxY " + maxY);
            System.out.println("wgs84 minX " + minX);
            System.out.println("wgs84 minY " + minY);
    
            double userInputX = 180d;
            double userInputY = 90.1;
    
            if (userInputX > maxX || userInputX < minX) {
                throw new IllegalArgumentException("error x");
            }
    
            if (userInputY > maxY || userInputY < minY) {
                throw new IllegalArgumentException("error y");
            }
        }
    
    }