fons fons - 1 year ago 142
Java Question

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

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


  • 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).

Answer Source

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: (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:


# Example list of regions (full list can be obtained from )

for REGION in $REGIONS; do 
LATLON=$(curl -s "$REGION" | jq -r '.results[0],@text ",",.results[0].geometry.location.lng')
echo $REGION , $LATLON |  tr -d ' '

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