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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号