2

I've seen previous discussions about finding a country based on longitude and latitude coordinates which have led to a couple useful links.

http://www.worldatlas.com/aatlas/latitude_and_longitude_finder.htm

https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

are two examples.

My problem is that I have a long list of coordinate pairs (26,400 pairs to be exact) and obviously don't have time to search each one individually. Is there a way I can automatically search all of my coordinate pairs, which are stored in a .csv file?

Community
  • 1
  • 1
jls
  • 21
  • 2

1 Answers1

0

Assuming your CSV looks like this:

51.23,2.34
52.5,-1.8
54.88,-3.2

you can read it line by line and have Google geocode it for you with this

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

There are limits to how many lookups you can do in a day (please check and observe) so that is why I have added a sleep in there. Alternatively, you can change the word xml in the curl request to json and you will get the result in JSON format for parsing.

If you want to test this out, just paste the parameter to curl into your browser address bar without the double quotes, and you will see what you get returned:

<?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>

There are other geocoding services, with different capabilities and restrictions, available - Geonames springs to mind.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432