@@ -0,0 +1,2933 @@
+#!/usr/local/bin python3%0A%0Afrom argparse import ArgumentParser%0Aimport json%0Aimport pymongo%0Afrom bson.son import SON%0Afrom pprint import pprint%0A%0A%0Adef main(data_file, weight, label):%0A    collection = get_db_collection()%0A    if data_file is not None:%0A        geo_data = json.load(open(data_file))%0A        count = add_mongo_data(collection, geo_data, weight, label)%0A        print('total features: %7B%7D'.format(count))%0A    address = %5B-122.3331, 47.6097%5D%0A    nearby = get_points_near(collection, address)%0A    print('nearby features: %7B%7D'.format(len(nearby)))%0A    #pprint(nearby)%0A%0A%0Adef get_points_near(coll, addr):%0A    query = %7B%0A            %22geometry%22: %7B%0A                %22$near%22: %7B%0A                    %22$geometry%22: %7B%0A                        %22type%22: %22Point%22,%0A                        %22coordinates%22: addr,%0A                        %7D,%0A                    %22$maxDistance%22: 1600,%0A                    %7D%0A                %7D%0A            %7D%0A    return %5Bx for x in coll.find(query)%5D%0A%0A%0Adef add_mongo_data(collection, data, weight, label):%0A    count = 0%0A    for f in data%5B'features'%5D:%0A        # create a new one b/c mongo will know%0A        small_data = %7B%0A                      'weight': weight,%0A                      'label': label,%0A                      'geometry': f.get('geometry')%0A                     %7D%0A        # don't care about the id%0A        _ = collection.insert(small_data)%0A        count += 1%0A    print('attempted to insert %7B%7D features'.format(count))%0A    return collection.count()%0A%0A%0Adef get_db_collection(name=None):%0A    db = get_db()%0A    if name is None:%0A        name = 'map_data'%0A    existing = name in db.collection_names()%0A%0A    # get/create the collection%0A    collection = db%5Bname%5D%0A    if not existing:%0A        collection.create_index(%5B('geometry', '2dsphere')%5D)%0A    return collection%0A%0Adef get_db(name=None):%0A    c = pymongo.MongoClient('localhost', 27017)%0A    if name is None:%0A        name = 'map_database'%0A    # get/create the db%0A    db = c%5Bname%5D%0A    return db%0A%0Adef clear_collection():%0A    if ask('Are you sure you want to clear the collection?'):%0A        print('clearing collection')%0A        coll = get_db_collection()%0A        coll.drop()%0A    else:%0A        print('clear aborted')%0A%0Adef clear_db():%0A    if ask('Are you sure you want to drop the database?'):%0A        print('dropping the db')%0A        c = pymongo.MongoClient('localhost', 27017)%0A        # get the db%0A        db = c.map_database%0A        db.drop%0A    else:%0A        print('drop aborted')%0A%0Adef ask(msg):%0A    res = raw_input('%7B%7D (y/n) %3E%3E '.format(msg))%0A    return res.lower() in %5B'y','yes'%5D%0A%0A%0Aif __name__ == '__main__':%0A    parser = ArgumentParser()%0A    parser.add_argument('-f', '--data_file')%0A    parser.add_argument('-w', '--weight', type=int)%0A    parser.add_argument('-l', '--label')%0A    parser.add_argument('-d', '--drop', action='store_true')%0A    args = parser.parse_args()%0A%0A    if args.drop:%0A        clear_collection()%0A    else:%0A        main(args.data_file, args.weight, args.label)%0A

add python script for adding data to mongo locally