<commit_before>import requests
import json


def run():
    wa_state = [45.1510532655634, -125.41992187499999, 49.15296965617042, -116.630859375]

    airports = get_airport_points(bbox=wa_state)
    print '{} airport points obtained.'.format(len(airports))
    save_to_file(airports, 'airports.json')


def get_airport_points(
        url='http://overpass-api.de/api/interpreter',
        bbox=None):

    airport_node = 'node[\"aeroway\"=\"aerodrome\"]'

    if bbox is None:
        bbox_str=''
    else:
        bbox_str = '({})'.format(', '.join([repr(x) for x in bbox]))

    query_pc = [
        '?data=',
        '[out:json];',
        '{}{};'.format(airport_node, bbox_str),
        'out;'
        ]
    query = ''.join(query_pc)

    r = requests.get(url + query)
    l = json.loads(r.text)
    return l['elements']


def save_to_file(elements_json, filename):
    with open(filename, 'w') as f:
        f.write(json.dumps(elements_json))


if __name__ == "__main__":
    run()<commit_msg>Save/load or return airport points as GeoJSON<commit_after>import requests
import json
from geojson import Point


def run():
    wa_state = [45.1510532655634, -125.41992187499999, 49.15296965617042, -116.630859375]

    airports = get_airport_points(bbox=wa_state)
    print '{} airport points obtained.'.format(len(airports))

    filename = 'airports.json'
    save_to_file(airports[:5], filename)
    elements = load_elements_from_file(filename)
    print '{} airport points read.'.format(len(elements))


def get_airport_points(
        bbox=None):
    
    airport_query = '\"aeroway\"=\"aerodrome\"'
    r = osm_node_query(airport_query, bbox)
    elements = r['elements']
    points = [Point((x['lat'], x['lon'])) for x in elements]
    return points


def osm_query(
        query, url='http://overpass-api.de/api/interpreter'):
    # Example query (airport points): 'node[\"aeroway\"=\"aerodrome\"]''
    query_pc = [
        '?data=',
        '[out:json];',
        '{};'.format(query),
        'out;'
        ]
    query = ''.join(query_pc)

    r = requests.get(url + query)
    return json.loads(r.text)


def osm_node_query(node_query, bbox):
    if bbox is None:
        bbox_str=''
    else:
        bbox_str = '({})'.format(', '.join([repr(x) for x in bbox]))

    ret = osm_query('node[{}]{}'.format(node_query, bbox_str))
    return ret


def save_to_file(elements_json, filename):
    with open(filename, 'w') as f:
        f.write(json.dumps(elements_json))


def load_elements_from_file(filename):
    with open(filename, 'r') as f:
        elements = json.loads(f.read())
    return elements    
    

if __name__ == "__main__":
    run()
{'fraction_non_alphanumeric': 0.15745960165351372, 'fraction_numerical': 0.04547162720781661, 'mean_word_length': 3.06412213740458, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6848067', 'n_tokens_mistral': 1047, 'n_tokens_neox': 957, 'n_words': 187}