Skip to content

Instantly share code, notes, and snippets.

@valentina-s
Created May 9, 2019 22:04
Show Gist options
  • Save valentina-s/bd756a50f024ef4f09577741bc74e0f0 to your computer and use it in GitHub Desktop.
Save valentina-s/bd756a50f024ef4f09577741bc74e0f0 to your computer and use it in GitHub Desktop.
A sript for extracting satellite images of city capitals using the HERE api.
import math
def calculate_xy(lat,lon,z):
latRad = lat * math.pi / 180;
n = math.pow(2, z);
xTile = n * ((lon + 180) / 360);
yTile = n * (1-(math.log(math.tan(latRad) + 1/math.cos(latRad))/math.pi)) / 2;
return(xTile,yTile)
def get_api_keys(keys_file):
keys = []
try:
with open(keys_file) as f:
keys.append(f.readline())
keys.append(f.readline())
return(keys)
except FileNotFoundError:
print("'%s' file not found" % keys_file)
def get_city_image(lat,lon,z,size,filename,keys):
xTile,yTile = calculate_xy(lat,lon,z)
xTile = int(xTile)
yTile = int(yTile)
url = "https://2.aerial.maps.api.here.com/maptile/2.1/maptile/newest/satellite.day/{}/{}/{}/{}/png8?app_id={}&app_code={}"\
.format(z,xTile,yTile,size,keys[0][:-1],keys[1][:-1])
import requests
r = requests.get(url)
import io
import os
import skimage
img_stringIO = io.BytesIO(r.content)
img = skimage.io.imread(img_stringIO)
from skimage.io import imsave
imsave(os.path.join('cities',filename),img[:,:,:3])
# Reading the API keys:
keys_file = "keys.txt"
keys = get_api_keys(keys_file)
# Testing with Seattle
Seattle_lat = 47
Seattle_lon = 120
z = 15
size = 256
xTile,yTile = calculate_xy(Seattle_lat,Seattle_lon,z)
xTile = int(xTile)
yTile = int(yTile)
url = "https://2.aerial.maps.api.here.com/maptile/2.1/maptile/newest/satellite.day/{}/{}/{}/{}/png8?app_id={}&app_code={}".format(z,xTile,yTile,size,keys[0][:-1],keys[1][:-1])
print(url)
# load the coordinates from the country capitals
# http://techslides.com/list-of-countries-and-capitals
import pandas as pd
df = pd.read_csv("country-capitals.csv",error_bad_lines=False)
for index, row in df.iterrows():
get_city_image(row["CapitalLatitude"],row["CapitalLongitude"],z,size,row["CapitalName"]+'.jpg',keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment