我之前已经看过有关根据经度和纬度坐标查找国家的讨论,这些讨论已经形成了一些有用的链接。
http://www.worldatlas.com/aatlas/latitude_and_longitude_finder.htm
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
是两个例子。
我的问题是我有很长的坐标对(确切地说是26,400对),显然没有时间单独搜索每一对。有没有办法可以自动搜索我的所有坐标对,这些坐标对都存储在.csv文件中?
答案 0 :(得分:0)
假设您的CSV如下所示:
51.23,2.34
52.5,-1.8
54.88,-3.2
您可以逐行阅读并让Google使用此地理编码
while IFS="," read lat lon
do
country=$(curl "http://maps.googleapis.com/maps/api/geocode/xml?latlng=$lat,$lon&sensor=false" | xmlstarlet sel -t -v '/GeocodeResponse/result/address_component[type="country"]/long_name' | head -1)
echo $lat,$lon,$country
sleep 10
done < file.csv
您可以在一天内完成多少次查找(请检查并观察),因此我在其中添加了sleep
。或者,您可以将xml
请求中的单词curl
更改为json
,然后您将获得JSON格式的结果进行解析。
如果您想对此进行测试,只需将参数粘贴到您的浏览器地址栏中curl
,而不使用双引号,您将看到返回的内容:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>route</type>
<formatted_address>A303, Salisbury, Wiltshire SP4 7DE, UK</formatted_address>
<address_component>
<long_name>A303</long_name>
<short_name>A303</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Salisbury</long_name>
<short_name>Salisbury</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>Wiltshire</long_name>
<short_name>Wiltshire</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name> <----- Here it is!
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>SP4 7DE</long_name>
<short_name>SP4 7DE</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>51.1771403</lat>
<lng>-1.8236094</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>51.1757619</lat>
<lng>-1.8272403</lng>
</southwest>
<northeast>
<lat>51.1784599</lat>
<lng>-1.8199612</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>51.1766085</lat>
<lng>-1.8272403</lng>
</southwest>
<northeast>
<lat>51.1776133</lat>
<lng>-1.8199612</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJcWNIljzmc0gROJ_DtvMZDew</place_id>
</result>
<result>
<type>postal_code</type>
<formatted_address>Salisbury, Wiltshire SP4 7DE, UK</formatted_address>
<address_component>
<long_name>SP4 7DE</long_name>
<short_name>SP4 7DE</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>Salisbury</long_name>
<short_name>Salisbury</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>Wiltshire</long_name>
<short_name>Wiltshire</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>51.1802192</lat>
<lng>-1.8270873</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>51.1658495</lat>
<lng>-1.8606635</lng>
</southwest>
<northeast>
<lat>51.1865446</lat>
<lng>-1.8147963</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>51.1658495</lat>
<lng>-1.8606635</lng>
</southwest>
<northeast>
<lat>51.1865446</lat>
<lng>-1.8147963</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJ58eYzzvmc0gR-x4igRCCDgI</place_id>
</result>
<result>
<type>administrative_area_level_4</type>
<type>political</type>
<formatted_address>Amesbury, Wiltshire, UK</formatted_address>
<address_component>
<long_name>Amesbury</long_name>
<short_name>Amesbury</short_name>
<type>administrative_area_level_4</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Wiltshire</long_name>
<short_name>Wiltshire</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>51.1749580</lat>
<lng>-1.7790983</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>51.1494527</lat>
<lng>-1.8535583</lng>
</southwest>
<northeast>
<lat>51.1919313</lat>
<lng>-1.7117001</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>51.1494527</lat>
<lng>-1.8535583</lng>
</southwest>
<northeast>
<lat>51.1919313</lat>
<lng>-1.7117001</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJMxey-_nlc0gRfdhQ9YfIO7A</place_id>
</result>
<result>
<type>postal_code_prefix</type>
<type>postal_code</type>
<formatted_address>SP4, United Kingdom</formatted_address>
<address_component>
<long_name>SP4</long_name>
<short_name>SP4</short_name>
<type>postal_code_prefix</type>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>51.1870058</lat>
<lng>-1.7711005</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>51.0708387</lat>
<lng>-1.8606635</lng>
</southwest>
<northeast>
<lat>51.2759513</lat>
<lng>-1.6287743</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>51.0708387</lat>
<lng>-1.8606635</lng>
</southwest>
<northeast>
<lat>51.2759513</lat>
<lng>-1.6287743</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJ4dSKGIjjc0gRQoURkIFH8Ww</place_id>
</result>
<result>
<type>postal_town</type>
<formatted_address>Salisbury, UK</formatted_address>
<address_component>
<long_name>Salisbury</long_name>
<short_name>Salisbury</short_name>
<type>postal_town</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>51.1232079</lat>
<lng>-1.8390956</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>50.9187178</lat>
<lng>-2.2108535</lng>
</southwest>
<northeast>
<lat>51.2759513</lat>
<lng>-1.5617645</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>50.9187178</lat>
<lng>-2.2108535</lng>
</southwest>
<northeast>
<lat>51.2759513</lat>
<lng>-1.5617645</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJy0okEgrCc0gRCVJTyWezH-k</place_id>
</result>
<result>
<type>administrative_area_level_2</type>
<type>political</type>
<formatted_address>Wiltshire, UK</formatted_address>
<address_component>
<long_name>Wiltshire</long_name>
<short_name>Wiltshire</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>51.2462714</lat>
<lng>-1.9922127</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>50.9449920</lat>
<lng>-2.3655985</lng>
</southwest>
<northeast>
<lat>51.7031417</lat>
<lng>-1.4857261</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>50.9449920</lat>
<lng>-2.3655985</lng>
</southwest>
<northeast>
<lat>51.7031417</lat>
<lng>-1.4857261</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJKcgiicI3cUgRNJe9kfg0iGQ</place_id>
</result>
<result>
<type>administrative_area_level_1</type>
<type>political</type>
<formatted_address>England, UK</formatted_address>
<address_component>
<long_name>England</long_name>
<short_name>England</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>52.3555177</lat>
<lng>-1.1743197</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>49.9588004</lat>
<lng>-5.7169817</lng>
</southwest>
<northeast>
<lat>55.8111127</lat>
<lng>1.7629159</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>49.8647517</lat>
<lng>-6.4177822</lng>
</southwest>
<northeast>
<lat>55.8111127</lat>
<lng>1.7629159</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJ39UebIqp0EcRqI4tMyWV4fQ</place_id>
</result>
<result>
<type>country</type>
<type>political</type>
<formatted_address>United Kingdom</formatted_address>
<address_component>
<long_name>United Kingdom</long_name>
<short_name>GB</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>54.9713902</lat>
<lng>-2.7427767</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>49.8669688</lat>
<lng>-8.6493572</lng>
</southwest>
<northeast>
<lat>60.8565530</lat>
<lng>1.7627096</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>34.5625145</lat>
<lng>-8.6493572</lng>
</southwest>
<northeast>
<lat>60.9023960</lat>
<lng>33.9165550</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJqZHHQhE7WgIReiWIMkOg-MQ</place_id>
</result>
</GeocodeResponse>
还有其他具有不同功能和限制的地理编码服务 - 想到Geonames。