Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
require 'great_schools'
GreatSchools::API.key = KEY
schools = GreatSchools::School.nearby('NJ', city: ARGV[0], radius: 5, school_types: ['public'], level: 'high-schools')
# schools = GreatSchools::School.nearby('NJ', city: 'princeton', school_types: ['public'], level: 'high-schools')
# schools.each do |s|
# puts
# end
ratings ={|r| r.to_i}
puts ratings.reduce(:+).to_f / ratings.size
import constants
import googlemaps
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import simplekml
import numpy as np
import csv
import subprocess
import os
import random
from scipy import interpolate
see docs at:
Browse Schools - Returns a list of schools in a city.
Nearby Schools - Returns a list of schools closest to the center of a city, ZIP Code or address.
School Profile - Returns a profile data for a specific school.
School Search - Returns a list of schools based on a search string.
School Reviews - Returns a list of the most recent reviews for a school or for any schools in a city.
School Test Scores - Returns test and rank data for a specific school.
School Census Data - Returns census and profile data for a school.
City Overview - Returns information about a city.
Nearby Cities - Returns a list of cities near another city.
Browse Districts - Returns a list of school districts in a city.
gmaps = googlemaps.Client(key=constants.GOOGLE_KEY)
extent = constants.EXTENTS
def parse_city(geocode_data):
if (not geocode_data is None) and ('address_components' in geocode_data):
for component in geocode_data['address_components']:
if 'locality' in component['types']:
return component['long_name']
elif 'postal_town' in component['types']:
return component['long_name']
elif 'administrative_area_level_2' in component['types']:
return component['long_name']
elif 'administrative_area_level_1' in component['types']:
return component['long_name']
return None
def save_polygon(cs, m, levels):
kml = simplekml.Kml()
for i in range(len(cs.collections)):
if cs.collections[i].get_paths():
poly_name = str(levels[i])
pol = kml.newlinestring(name=poly_name)
p = cs.collections[i].get_paths()[0]
v = p.vertices
x = v[:, 0]
y = v[:, 1]
lonpt, latpt = m(x, y, inverse=True)
pol.coords = list(zip(lonpt, latpt))
pol.extrude = 1
pol.altitudemode = simplekml.AltitudeMode.relativetoground
# = simplekml.Color.hex(constants.COLORS[i]) = 2'schools.kml')
def get_rating(city):
# return random.randint(0, 10)
if city:
str = 'ruby /Users/tim/Hacking/greatschools/get_school_info.rb ' + city
output = subprocess.check_output(str, shell=True)
return float(output.strip())
except subprocess.CalledProcessError as e:
print(city, e.output)
return np.nan
return np.nan
def build_c(m, grid_size, csv_file):
c = np.zeros((grid_size, grid_size))
lons, lats = m.makegrid(grid_size, grid_size)
x, y = m(lons, lats)
for i in range(0, grid_size):
for j in range(0, grid_size):
reverse_geocode_result = gmaps.reverse_geocode((lats[i, j], lons[i, j]))
city = parse_city(reverse_geocode_result[0])
rating = get_rating(city)
print(lats[i, j], lons[i, j], city, rating)
c[i][j] = rating
csv_file.writerow([i, j, c[i][j], lons[i][j], lats[i][j]])'x_data_schools', x)'y_data_schools', y)'c_data_backup_schools', c)
return x, y, c
def fill_array(my_array):
x = np.arange(0, my_array.shape[1])
y = np.arange(0, my_array.shape[0])
# mask invalid values
array =
xx, yy = np.meshgrid(x, y)
# get only the valid values
x1 = xx[~array.mask]
y1 = yy[~array.mask]
newarr = array[~array.mask]
return interpolate.griddata((x1, y1), newarr.ravel(),
(xx, yy),
def build_map(m, cont, extent):
name = 'schools'
im = plt.imread('../nj_modified.tif')
m.imshow(im, origin='upper', extent=extent)
# plt.clabel(cont, inline=1, fontsize=10)
m = Basemap(llcrnrlon=extent[0], llcrnrlat=extent[1], urcrnrlon=extent[2], urcrnrlat=extent[3],
projection='lcc', lat_1=40.880841, lat_2=40.197260, lon_0=-74.851512,
resolution='l', area_thresh=1000.)
levels = [i for i in range(2,11)]
grid_size = 20
f = open('great_schools.csv', 'w')
csv_file = csv.writer(f)
# x, y, c = build_c(m, grid_size, csv_file)
c = fill_array(np.load('c_data_backup_schools.npy'))
x = np.load('x_data_schools.npy')
y = np.load('y_data_schools.npy')
cs = m.contourf(x, y, c, levels, alpha=0.5)
save_polygon(cs, m, levels)
build_map(m, cs, extent)
# print(get_rating('princeton'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.