# CrossCompute
target_folder = '/tmp'
url = 'https://data.cityofnewyork.us/download/q68s-8qxv/application%2Fzip'
color_map_select = """
viridis
viridis
plasma
inferno
magma
cividis
"""
color_map = color_map_select.strip().splitlines()[0]
color_map
import geotable
import numpy as np
import pandas as pd
import gzip
from invisibleroads_macros.disk import uncompress
from os.path import exists
from urllib.request import urlretrieve
def download(target_path, source_url):
if not exists(target_path):
urlretrieve(source_url, target_path)
return target_path
def download_zip(target_folder, source_url):
archive_path = download(target_folder + '.zip', source_url)
return uncompress(archive_path, target_folder)
def download_gz(target_path, source_url):
archive_path = download(target_path + '.gz', source_url)
with gzip.open(archive_path, 'rb') as f:
open(target_path, 'wb').write(f.read())
return target_path
# Load air pollution raster
source_url = (
'https://data.cityofnewyork.us/download/'
'q68s-8qxv/application%2Fzip')
source_folder = '/tmp/pollution'
download_zip(source_folder, source_url)
# Show nitrogen dioxide images
from glob import glob
sorted(glob('/tmp/pollution/*no2*.ovr'))
# Install package
import subprocess
subprocess.call('pip install -U rasterio'.split())
# Load image
import rasterio
pollution_raster = rasterio.open('/tmp/pollution/aa2_no2300m/prj.adf')
# Load image array
x = pollution_raster.read()
array = x[0]
array.shape
# Enable inline plots
%matplotlib inline
import matplotlib.pyplot as plt
plt.imshow(array, vmin=0);
img = plt.imshow(array, vmin=0)
plt.axis('off')
plt.savefig('/tmp/test.png', bbox_inches='tight')
from os.path import join
image_paths = []
for index, folder in enumerate(sorted(glob('/tmp/pollution/*_no2300m'))):
with rasterio.open(join(folder, 'prj.adf')) as pollution_raster:
x = pollution_raster.read()
array = x[0]
img = plt.imshow(array, vmin=0);
img.set_cmap(color_map)
plt.axis('off')
target_path = join(target_folder, 'image%s.png' % index)
plt.savefig(target_path, bbox_inches='tight')
image_paths.append(target_path)
image_paths
import subprocess
subprocess.call('pip install imageio'.split())
import imageio
images = []
for path in image_paths:
images.append(imageio.imread(path))
target_path = join(target_folder, 'movie.gif')
imageio.mimsave(target_path, images)
print('pollution_image_path = %s' % target_path)