Skip to content

Instantly share code, notes, and snippets.

@blues-man
Created December 7, 2020 10:32
Show Gist options
  • Select an option

  • Save blues-man/d60eefd5e7c24b85c0d2a69ce11997a0 to your computer and use it in GitHub Desktop.

Select an option

Save blues-man/d60eefd5e7c24b85c0d2a69ce11997a0 to your computer and use it in GitHub Desktop.
protectedplanet.net CSV GIS data conversion to Coordinates (lat,lng) with Google Geodecode API
#!/usr/bin/python
import csv
import country_converter
import json
import requests
file = []
API_KEY = ''
with open('WDPA_WDOECM_search_aff0dc657770c05d4e57bc0ac953a107d8d078964474c09358387de2ee1c59a4_csv.csv, newline='') as csvfile:
reader = csv.reader(csvfile, dialect=csv.excel)
i = 0
for row in reader:
if i > 0 and row:
#print(row[4] + " " + row[28])
ccTLD = country_converter.convert(names=row[28], to='ISO2')
country_name = country_converter.convert(names=row[28], to='short_name')
name = row[4]
r = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + requests.utils.quote(name + " " + country_name) + '&region=' + ccTLD.lower() + '&sensor=false&key='+ API_KEY)
json_obj = r.json()
if json_obj["status"] == "OK":
latitude = json_obj["results"][0]["geometry"]["location"]["lat"]
longitude = json_obj["results"][0]["geometry"]["location"]["lng"]
data = {"countryCode": ccTLD, "countryName": country_name, "coordinates": [ latitude , longitude ], "name": name, "toponymName" : name }
#print(data)
print(json.dumps(data))
file.append(data)
i += 1
f = open("nationalparks.json", "w")
f.write(json.dumps(file, sort_keys=True, indent=4))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment