duancai7002 2017-04-15 18:53
浏览 148
已采纳

如何在golang中解析ISO6709坐标?

Some examples from Wikipedia on ISO 6709:

Atlantic Ocean +00-025/
France +46+002/
Paris +48.52+002.20/
Eiffel Tower +48.8577+002.295/
Mount Everest +27.5916+086.5640+8850CRSWGS_84/
North Pole +90+000/
Pacific Ocean +00-160/
South Pole -90+000+2800CRSWGS_84/
United States +38-097/
New York City +40.75-074.00/
Statue of Liberty +40.6894-074.0447/

What's the way to parse this since there's no consistent delimiting character? Regex? Read and parse it byte by byte?

To clarify: the desired output is a pair of float32 latitude and longitudes. So for e.g:

input: +40.6894-074.0447/
output: 40.6894 and -074.0447

  • 写回答

1条回答 默认 最新

  • duanpin2034 2017-04-15 19:52
    关注

    I'm not sure which pieces you would want to extract but the following regex works within your examples to select them all.

    (\+|-)\d+\.?\d+(\+|-)\d+\.?[\d]+(\+|-)?[^/]*
    

    It does work it out in pieces, and depends on the last / as being a terminator, though if it isn't there would be other ways around it.

    (\+|-)\d+\.?\d+(\+|-)\d+\.?\d+(\+|-)?[A-Z_\d]*
    

    Does not rely on the / terminator.

    To provide a perfect answer, the context of the coordinates would be required.

    here is the code to implement, given a string as input:

    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        toSearch := "Atlantic Ocean +00-025/
    France +46+002/
    Paris +48.52+002.20/
    Eiffel Tower +48.8577+002.295/
    Mount Everest +27.5916+086.5640+8850CRSWGS_84/
    North Pole +90+000/
    Pacific Ocean +00-160/
    South Pole -90+000+2800CRSWGS_84/
    United States +38-097/
    New York City +40.75-074.00/
    Statue of Liberty +40.6894-074.0447/"
        ISOCoord := regexp.MustCompile(`(\+|-)\d+\.?\d+(\+|-)\d+\.?\d+(\+|-)?[A-Z_\d]*`)
        result := ISOCoord.FindAll([]byte(toSearch), 11)
        for _, v := range result {
            fmt.Printf("%s
    ", v)
        }
    }
    

    returns:

    +00-025
    +46+002
    +48.52+002.20
    +48.8577+002.295
    +27.5916+086.5640+8850CRSWGS_84
    +90+000
    +00-160
    -90+000+2800CRSWGS_84
    +38-097
    +40.75-074.00
    +40.6894-074.0447
    

    Given the new idea that you want the 2 sepearate coords, this approch works:

    import (
        "fmt"
        "regexp"
        "strconv"
    )
    
    type coord struct {
        lat, long float64
    }
    
    func main() {
        toSearch := "Atlantic Ocean +00-025/
    France +46+002/
    Paris +48.52+002.20/
    Eiffel Tower +48.8577+002.295/
    Mount Everest +27.5916+086.5640+8850CRSWGS_84/
    North Pole +90+000/
    Pacific Ocean +00-160/
    South Pole -90+000+2800CRSWGS_84/
    United States +38-097/
    New York City +40.75-074.00/
    Statue of Liberty +40.6894-074.0447/"
        ISOCoord := regexp.MustCompile(`((\+|-)\d+\.?\d*){2}`)
        result := ISOCoord.FindAllString(toSearch, -1)
        INDCoord := regexp.MustCompile(`(\+|-)\d+\.?\d*`)
        answer := make([]coord, 11)
    
        for i, v := range result {
            temp := INDCoord.FindAllString(v, 2)
            lat, _ := strconv.ParseFloat(temp[0], 64)
            lon, _ := strconv.ParseFloat(temp[1], 64)
            answer[i] = coord{lat, lon}
        }
        fmt.Println(answer)
    }
    

    The regex is doubled so that it is a little more robust, but it would be faster to only do it once, if it were possible given the input.

    The code also should have error checking on the conversions, but you can add that.

    Also worth noting that it trims 0's. If you want to maintain those as stated, ie. if 012.1 is not the same as 12.1, you could just leave out the conversion to float and work with the strings.

    Code produces as float:

    [{0 -25} {46 2} {48.52 2.2} {48.8577 2.295} {27.5916 86.564} {90 0} {0 -160} {-90 0} {38 -97} {40.75 -74} {40.6894 -74.0447}]
    

    or

    [{+00 -025} {+46 +002} {+48.52 +002.20} {+48.8577 +002.295} {+27.5916 +086.5640} {+90 +000} {+00 -160} {-90 +000} {+38 -097} {+40.75 -074.00} {+40.6894 -074.0447}]
    

    as string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名