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.
