【问题标题】:Matching coordinates with different sign in RR中具有不同符号的匹配坐标
【发布时间】:2017-01-24 16:01:39
【问题描述】:

我正在通过下载其 html 代码来抓取 web 地图,我想通过匹配正则表达式来提取一些点的坐标。我用以下代码实现了大部分坐标的提取:

library(stringr)    
unique(str_extract_all(doc,"\\[[[:digit:]]+[.][[:digit:]]+[,][[:punct:]][[:digit:]]+[.][[:digit:]]+\\]")[[1]])

此代码提取纬度为负但有时为负的情况。我需要指定符号“[”来提取点但没有地图边界。也就是说,我需要获得类似的东西

[10.6302565,-74.9131161]

但我需要包含类似的结果

[-10.6302565,-74.9131161]

感谢您的帮助。谢谢。

附加

我还包括doc。在这种情况下,我的纬度为正:

i<-"https://www.google.com/maps/d/embed?mid=1YhVS6Z--LIc5k9rstJ24tYcb-Nc"
doc<-readLines(url(i))
doc<-doc[7]
doc<-iconv(doc,"latin1","ASCII","")

在这种情况下,我有负纬度:

i<-"https://www.google.com/maps/d/embed?mid=1vq9uMa8L0PxnrhG-m3z3Jt-HDh4"
doc<-readLines(url(i))
doc<-doc[7]
doc<-iconv(doc,"latin1","ASCII","")

【问题讨论】:

标签: r regex web-scraping coordinates stringr


【解决方案1】:

你可以使用

"\\[-?\\d+[.]\\d+,-?\\d+[.]\\d+\\]"

请参阅regex demo。请注意,-?(匹配 1 或 0 减号)添加在\\[ 之后,[[:punct:]] 替换为-?, 之后。我还删除了不必要的转义符号。

> library(stringr)    
> i<-"https://www.google.com/maps/d/embed?mid=1YhVS6Z--LIc5k9rstJ24tYcb-Nc"
> doc<-readLines(url(i))
> doc<-doc[7]
> unique(str_extract_all(doc,"\\[-?\\d+[.]\\d+,-?\\d+[.]\\d+\\]")[[1]])
[1] "[10.6302565,-74.9131161]" "[10.8632551,-74.7769001]"
[3] "[10.9827508,-74.783735]"  "[10.9419975,-74.7826532]"

【讨论】:

猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多