1

我有一个字符串,上面有一堆像这样的纬度和经度

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

我想将坐标从字符串中取出到一个数组中。我知道这可以做到,string splitting但我不明白如何编写表达式以仅从字符串中获取坐标。

有人可以帮我解决这个问题吗

4

5 回答 5

4
  1. (第一个和最后一个之间的子字符串)
  2. 拆分", ",您将获得 String[] 数组,其中包含类似的对"-79.0578544444577 43.0929133770364","-79.0559554404751 43.0929995585932",...
  3. 现在您可以在每一对上用空格分割" "以获得另一个 String[] 数组,这次包含"-79.0578544444577", "43.0929133770364"

您还可以使用正则表达式以 [可选-][一位或两位数][点][多于一位数] 形式查找数字。这种模式看起来像"-?\\d{1,2}[.]\\d+"

于 2013-03-06T20:12:27.307 回答
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 );
      }
   }
}

输出:

[-79.0578544444577, 43.0929133770364, -79.0559554404751, 43.0929995585932, -79.0540564364926, 43.09308574015, -79.0504086322323, 43.0931797561892, -79.0503228015438, 43.0911427096913]
于 2013-03-06T20:21:22.437 回答
1

如果您很高兴将它们全部放在一个数组中:

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("\\(|\\)")(表示在或上分割)。所以那将是{"LINESTRING ", "-79...", ""}

[1]因为这是包含 的位置"-79..."

split(",? ")表示拆分,后跟一个空格或只是一个空格。

如果要成对提取坐标:

for (int i = 0; i < arr.length; i += 2)
{
   System.out.println("coordinate 1 = "+arr[i]);
   System.out.println("coordinate 2 = "+arr[i+1]);
}
于 2013-03-06T20:16:05.473 回答
0

尝试这个:

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

这应该立即用空格和逗号分开。此正则表达式查找零个或多个逗号,然后查找一个或多个空白字符作为分隔符。

于 2013-03-06T20:14:30.293 回答
0

如果 LineString 是您的变量的名称,而括号内是它的值(不包括括号),则:

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

应该管用 !

于 2013-03-06T20:11:22.047 回答