Country/region codes (iso-3166-1/iso-3166-2) to longitude and latitude
Asked Answered
B

2

1

I need to convert from iso-3166-1/iso-3166-2 codes to longitude/latitude

Examples:

  • Input: "US", Output: (37.09024, -95.71289100000001).
  • Input "VE-O", Output: (10.9970723, -63.91132959999999).

I have been searching around but failed to find a complete listing or, ideally, a Java library doing it.

This Github project is promising but is missing a lot of geolocation information for a lot of regions.

Note that, unlike the question Need a list of all countries in the world, with a longitude and latitude coordinate, this one refers to regional subdivisions (iso-3166-2).

Blackout answered 1/3, 2015 at 13:48 Comment(4)
possible duplicate of Need a list of all countries in the world, with a longitude and latitude coordinateMetempsychosis
@davidstrachan That question doesn't cover regionsBlackout
How do you determine the lat/long for the country? Centroid of population? Capital? Centroid of land area?Cineaste
Centroid of land area.Blackout
B
4

Since I didn't get any answers, I will explain how I solved it.

I found this csv listing for the iso-3166-1 country code centroids: http://dev.maxmind.com/geoip/legacy/codes/country_latlon/ (I had to make a few manual tweaks)

As for the iso-3166-2 region centroids, I ended up creating a shell script which uses the Google Maps API to print the region centroids in csv format (note that I didn't verify the full output but the cases I checked are correct). Here's a simplified version of the script using curl and jq to process the API's output:

#!/bin/bash

# Example list of regions (full list can be obtained from https://www.ip2location.com/free/iso3166-2 )
REGIONS="VE-O GB-BKM GB-CAM GB-CMA"

for REGION in $REGIONS; do 
LATLON=$(curl -s "maps.googleapis.com/maps/api/geocode/json?sensor=false&address=$REGION" | jq -r '.results[0].geometry.location.lat,@text ",",.results[0].geometry.location.lng')
echo $REGION , $LATLON |  tr -d ' '
done

Then I imported the csv listings in my java code using Apache Commons CSV

Blackout answered 2/6, 2015 at 12:1 Comment(2)
Just a point. This might violate the Google Maps/Google Earth APIs Terms of Service developers.google.com/maps/terms Section 10.5.d "No caching or storage. You will not pre-fetch, cache, index, or store any Content to be used outside the Service..."Ellga
Apart from the ToS violation, this doesn't work any more. A Google Maps geocoding lookup for an ISO-3166-2 code returns zero results. Even the ISO-3166-1 country codes alone don't work.Eckhart
E
1

These are the best resources I found when I had to tackle this problem:

Country coordinates: http://dev.maxmind.com/geoip/legacy/codes/country_latlon/

ISO 3166-2 coordinates: https://github.com/oodavid/iso-3166-2/blob/master/iso_3166_2.js

Ellga answered 19/7, 2016 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.