简体   繁体   English

bash中的反向地理编码

[英]reverse geocoding in bash

I have a gps unit which extracts longitude and latitude and outputs as a google maps link 我有一个gps单位,它提取经度和纬度并输出为Google Maps链接

http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false

From this i'd like to call it via curl and display the "short name" in line 20 由此,我想通过curl调用它,并在第20行中显示“短名称”

"short_name" : "Northwood",

so i'd just like to be left with 所以我只想留下

Northwood

so something like 所以像

curl -s http://maps.googleapis.com/maps/api/geocode/xml?latlng=latlng=51.601154,-0.404765&sensor=false sed sort_name

Mmmm, this is kind of quick and dirty: 嗯,这有点麻烦和肮脏:

curl -s "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false" | grep -B 1 "route" | awk -F'"' '/short_name/ {print $4}'
Bedford Avenue

It looks for the line before the line with "route" in it, then the word "short_name" and then prints the 4th field as detected by using " as the field separator. Really you should use a JSON parser though! 它先查找其中带有“ route”的行,然后搜索单词“ short_name”,然后打印使用“作为字段分隔符”检测到的第四个字段。尽管如此,您确实应该使用JSON解析器!

Notes: 笔记:

  1. This doesn't require you to install anything. 这不需要您安装任何东西。
  2. I look for the word "route" in the JSON because you seem to want the road name - you could equally look for anything else you choose. 我在JSON中寻找“ route”一词,因为您似乎想要道路名称-您同样可以寻找其他选择。
  3. This isn't a very robust solution as Google may not always give you a route, but I guess other programs/solutions won't work then either! 这不是一个非常强大的解决方案,因为Google可能不会总是给您提供路线,但是我想其他程序/解决方案也将无法使用!
  4. You can play with my solution by successively removing parts from the right hand end of the pipeline to see what each phase produces. 您可以通过依次从管道的右端移开零件来查看每个阶段产生了什么,从而使用我的解决方案。

EDITED 已编辑

Mmm, you have changed from JSON to XML, I see... well, this parses out what you want, but I note you are now looking for a locality whereas before you were looking for a route or road name? 嗯,您已经从JSON更改为XML,我明白了……嗯,这解析出了您想要的东西,但是我注意到您现在正在寻找地点,而在寻找路线或道路名称之前? Which do you want? 你要哪个?

curl -s "http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false" | grep -B1 locality | grep short_name| head -1|sed -e 's/<\/.*//' -e 's/.*>//'

The "grep -B1" looks for the line before the line containing "locality". “ grep -B1”在包含“ locality”的行之前寻找行。 The "grep short_name" then gets the locality's short name. 然后,“ grep short_name”将获取位置的简称。 The "head -1" discards all but the first locality if there are more than one. 如果有多个头,则“ head -1”将丢弃除第一个位置以外的所有位置。 The "sed" stuff removes the <> XML delimiters. “ sed”东西删除了<> XML分隔符。

This isn't text, it's structured JSON. 这不是文本,而是结构化的JSON。 You don't want the value after the colon on line 12, you want the value of short name in the address_component with type 'route' from the result. 您不希望在第12行的冒号后面有该值,而是要从结果中在address_component中键入“ route”类型的短名称值。

You could do this with jsawk or python , but it's easier to get it from XML output with xmlstarlet , which is lighter than python and more available than jsawk. 您可以使用jsawkpython进行此jsawk ,但是使用xmlstarlet从XML输出中获取它要容易xmlstarlet ,它比python轻巧,比jsawk可用。 Install xmlstarlet and try: 安装xmlstarlet并尝试:

curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false' \
| xmlstarlet sel -t -v '/GeocodeResponse/result/address_component[type="route"]/short_name'

This is much more robust than trying to parse JSON as plaintext. 这比尝试将JSON解析为纯文本要强大得多。

The following seems to work assuming you always like the short_name at line 12: 假设您总是喜欢第12行的short_name ,那么以下内容似乎可以工作:

curl -s 'http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false' | sed -n -e '12s/^.*: "\([a-zA-Z ]*\)",/\1/p'

or if you are using the xml api and wan't to trap the short_name on line 20: 或者,如果您使用的是xml API,并且short_name在第20行捕获short_name

curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=51.601154,-0.404765&sensor=false' | sed -n -e '19s/<short_name>\([a-zA-Z ]*\)<\/short_name>/\1/p'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM