Extract Lat and Long values from GeoJSON coordinates separately to a required format using Java

Viewed 754

I'm trying to split a GeoJSON coordinates into separate latitude and longitude values to a required format (as shown in required output).

I am having a GeoJSON file through which I extract just the coordinates values. These coordinate values are stored as a String variable as shown : GeoJSON : ":[[[7.365046,46.948655],[7.365046,46.949254],[7.367558,46.949254],[7.367558,46.948655],[7.365046,46.948655]]]}}. How do I extract specific values of latitude and longitude separately from the given String. The code snippet for coordinates extraction from GeoJSON file is as shown below:


String GeoJSON = GeoJSONFromFile().split("coordinates")[1];
System.out.println("GeoJSON : " + GeoJSON );

//Splitting within the brackets
String delims = "\\[(.*?)\\]";
String[] tokens = GeoJSON.split(delims);

GeoJSON = GeoJSON.split("}")[0];

The expected output what I would like to achieve is as follows:

\"points\": [\n" +
"            {\n" +
"              \"@type\": \"Point\",\n" +
"              \"lat\": 46.948655,\n" +
"              \"lon\": 7.365046\n" +
"            },\n" +
"            {\n" +
"              \"@type\": \"Point\",\n" +
"              \"lat\": 46.949254,\n" +
"              \"lon\": 7.365046\n" +
"            },\n" +
"            {\n" +
"              \"@type\": \"Point\",\n" +
"              \"lat\": 46.949254,\n" +
"              \"lon\": 7.367558\n" +
"            },\n" +
"            {\n" +
"              \"@type\": \"Point\",\n" +
"              \"lat\": 46.948655,\n" +
"              \"lon\": 7.367558\n" +
"            },\n" +
"            {\n" +
"              \"@type\": \"Point\",\n" +
"              \"lat\": 46.948655,\n" +
"              \"lon\": 7.365046\n" +
"            }\n" 
"          ]

The end result should be containing the coordinates separately as shown in the above format.

1 Answers

I guess an expression similar to:

(?::\[\[)?\[([-+]?\d+\.\d+)\s*,\s*([-+]?\d+\.\d+)\]\s*,?(?:\]\]}})?

with a proper replacement that'd use $1 and $2 as lat and lon, such as:

{\n"@type": "point",\n"lat":"$1",\n"lon":"$2"\n},\n

might work to some extent, not fully though.

Demo

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex = "(?::\\[\\[)?\\[([-+]?\\d+\\.\\d+)\\s*,\\s*([-+]?\\d+\\.\\d+)\\]\\s*,?(?:\\]\\]\\}})?";
        final String string = ":[[[7.365046,46.948655],[7.365046,46.949254],[7.367558,46.949254],[7.367558,46.948655],[7.365046,46.948655]]]}}";
        final String subst = "{\"@type\": \"point\",\"lat\":\"$1\",\"lon\":\"$2\"},";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

        final String result = matcher.replaceAll(subst);

        System.out.println(result);

    }
}

Output

{"@type": "point","lat":"7.365046","lon":"46.948655"},{"@type": "point","lat":"7.365046","lon":"46.949254"},{"@type": "point","lat":"7.367558","lon":"46.949254"},{"@type": "point","lat":"7.367558","lon":"46.948655"},{"@type": "point","lat":"7.365046","lon":"46.948655"},

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Related