[英]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: 笔记:
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. 您可以使用
jsawk
或python
进行此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.