1

I have a string with a bunch of latitude and longitudes like this

LINESTRING (-79.0578544444577 43.0929133770364, -79.0559554404751 43.0929995585932, -79.0540564364926 43.09308574015, -79.0504086322323 43.0931797561892, -79.0503228015438 43.0911427096913)

and I want to get the coordinates out of the string into an array. I know this can be done with string splitting but I dont understand how to write the expression to get just the coordinates from the string.

can someone help me outwith this

5 Answers 5

4
  1. substring between first ( and last )
  2. split on ", " and you will get String[] array that will contain pairs like "-79.0578544444577 43.0929133770364","-79.0559554404751 43.0929995585932",...
  3. now you can split with space " " on each of that pair to get another String[] array, this time containing "-79.0578544444577", "43.0929133770364"

You could also use regex to find numbers in form [optional -][one or two digits][dot][more than one digits]. Such pattern could look like "-?\\d{1,2}[.]\\d+"

Sign up to request clarification or add additional context in comments.

Comments

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

public class Coordinates {
   static class LatLon {
      double lat;
      double lon;
      public LatLon( double lat, double lon ) {
         this.lat = lat;
         this.lon = lon;
      }
      @Override public String toString(){ return lat + ", " + lon; }
   }
   public static void main( String[] args ){
      String info =
         "LINESTRING (" +
            "-79.0578544444577 43.0929133770364, " +
            "-79.0559554404751 43.0929995585932, " +
            "-79.0540564364926 43.09308574015, " +
            "-79.0504086322323 43.0931797561892, " +
            "-79.0503228015438 43.0911427096913)";
      Pattern p = Pattern.compile( "[^\\(]+\\(([^\\)]+).*" );
      Matcher m = p.matcher( info );
      if( m.matches()) {
         List< LatLon > coordinates = new java.util.LinkedList<>();
         String[] coords = m.group( 1 ).split( "," );
         for( int i = 0; i < coords.length; ++i ) {
            String[] latLon = coords[i].trim().split( " " );
            coordinates.add(
               new LatLon(
                  Double.parseDouble( latLon[0] ),
                  Double.parseDouble( latLon[1] )));
         }
         System.out.println( coordinates );
      }
   }
}

Outputs:

[-79.0578544444577, 43.0929133770364, -79.0559554404751, 43.0929995585932, -79.0540564364926, 43.09308574015, -79.0504086322323, 43.0931797561892, -79.0503228015438, 43.0911427096913]

Comments

1

If you're happy having them all in one array:

String str = "LINESTRING (-79.0578544444577 43.0929133770364, -79.0559554404751 43.0929995585932, -79.0540564364926 43.09308574015, -79.0504086322323 43.0931797561892, -79.0503228015438 43.0911427096913)";
String[] arr = str.split("\\(|\\)")[1].split(",? ");
for (String s: arr)
   System.out.println(a);

split("\\(|\\)") means split on ( or ). So that would be {"LINESTRING ", "-79...", ""}.

[1] because this is the position containing "-79...".

split(",? ") means split on , followed by a space or just a space.

If you want to extract the coordinates in pairs:

for (int i = 0; i < arr.length; i += 2)
{
   System.out.println("coordinate 1 = "+arr[i]);
   System.out.println("coordinate 2 = "+arr[i+1]);
}

Comments

0

If LineString is the name of your variable and what is inside the parenthesis its value (excluding parenthesis) then :

String[] coords = LINESTRING.split(" ");

should work !

Comments

0

Try this:

String[] coordinates = LINESTRING.split(",*\\s+");

This should immediately split both by space and by comma. This regular expression looks for zero or more commas and then one or more whitespace characters as the delimiter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.