<s> from pymongo import MongoClient , GEO2D , DESCENDING , errors <EOL> from flask import Flask , make_response , request , Blueprint , render_template , redirect , url_for , send_from_directory <EOL> from bson . json_util import dumps <EOL> from werkzeug . utils import secure_filename <EOL> from ast import literal_eval <EOL> from bson . son import SON <EOL> import json <EOL> import os <EOL> from loxoutils import * <EOL> from loxostats import * <EOL> from loxoerrors import * <EOL> DB_DOWN = False <EOL> UPLOAD_FOLDER = '<STR_LIT>' <EOL> ALLOWED_EXTENSIONS = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ] ) <EOL> app = Flask ( __name__ ) <EOL> app . register_blueprint ( stats_api , url_prefix = '<STR_LIT>' ) <EOL> app . config [ '<STR_LIT>' ] = UPLOAD_FOLDER <EOL> try : <EOL> mdb_port = <NUM_LIT> <EOL> host = os . environ . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) <EOL> client = MongoClient ( host , mdb_port , serverSelectionTimeoutMS = <NUM_LIT:1> ) <EOL> client . server_info ( ) <EOL> except errors . ServerSelectionTimeoutError as err : <EOL> DB_DOWN = True <EOL> print "<STR_LIT>" , err <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def loxo ( ) : <EOL> """<STR_LIT>""" <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def viewer ( ) : <EOL> """<STR_LIT>""" <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def upload_file ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> file = request . files [ '<STR_LIT:file>' ] <EOL> if file and allowed_file ( file . filename , ALLOWED_EXTENSIONS ) : <EOL> filename = secure_filename ( file . filename ) <EOL> file_location = os . path . join ( app . config [ '<STR_LIT>' ] , filename ) <EOL> print "<STR_LIT>" , file_location <EOL> file . save ( file_location ) <EOL> saved_file = url_for ( '<STR_LIT>' , filename = filename ) <EOL> endpoint_name = os . path . splitext ( filename ) [ <NUM_LIT:0> ] <EOL> database = request . form . get ( "<STR_LIT>" ) <EOL> if not database : <EOL> print request . form [ "<STR_LIT>" ] <EOL> raise InvalidUsage ( "<STR_LIT>" , <NUM_LIT> , '<STR_LIT>' ) <EOL> else : <EOL> handle_file ( database , file_location , endpoint_name , host ) <EOL> return redirect ( '<STR_LIT>' + database + '<STR_LIT>' + endpoint_name ) <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def uploaded_file ( filename ) : <EOL> return send_from_directory ( app . config [ '<STR_LIT>' ] , filename ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def index_collections ( database ) : <EOL> """<STR_LIT>""" <EOL> db = client [ database ] <EOL> for collection in db . collection_names ( include_system_collections = False ) : <EOL> print "<STR_LIT>" , collection , "<STR_LIT>" , GEO2D , "<STR_LIT>" <EOL> db [ collection ] . create_index ( [ ( "<STR_LIT>" , GEO2D ) ] , background = True ) <EOL> return make_response ( json . dumps ( { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def get_data_by_value ( database , dataset ) : <EOL> """<STR_LIT>""" <EOL> db = client [ database ] <EOL> collection = db [ dataset ] <EOL> if len ( request . args ) == <NUM_LIT:0> : <EOL> feature_collection = find_features ( collection , { } ) <EOL> return make_response ( create_feature_collection ( feature_collection ) ) <EOL> else : <EOL> property = request . args . get ( "<STR_LIT>" ) <EOL> within_proximity = request . args . get ( "<STR_LIT>" ) <EOL> within_donut = request . args . get ( "<STR_LIT>" ) <EOL> within_polygon = request . args . get ( "<STR_LIT>" ) <EOL> k_nearest = request . args . get ( "<STR_LIT>" ) <EOL> if property : <EOL> get_property = "<STR_LIT>" + property <EOL> return_features = find_features ( collection , { get_property : request . args . get ( "<STR_LIT:value>" ) } ) <EOL> feature_collection = create_feature_collection ( return_features ) <EOL> return make_response ( feature_collection ) <EOL> if within_proximity and not property : <EOL> args = within_proximity . split ( "<STR_LIT:U+002C>" ) <EOL> lng = float ( args [ <NUM_LIT:0> ] ) <EOL> lat = float ( args [ <NUM_LIT:1> ] ) <EOL> proximity_radius = abs ( float ( args [ <NUM_LIT:2> ] ) ) <EOL> print proximity_radius <EOL> proximity_radius_query = [ [ lng , lat ] , meters_to_radians ( proximity_radius ) ] <EOL> find = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : proximity_radius_query } } } <EOL> return_features = find_features ( collection , find ) <EOL> feature_collection = create_feature_collection ( return_features ) <EOL> return make_response ( feature_collection ) <EOL> if within_donut and not property : <EOL> args = within_donut . split ( "<STR_LIT:U+002C>" ) <EOL> lng = float ( args [ <NUM_LIT:0> ] ) <EOL> lat = float ( args [ <NUM_LIT:1> ] ) <EOL> donut_min = meters_to_radians ( abs ( float ( args [ <NUM_LIT:2> ] ) ) ) <EOL> donut_max = meters_to_radians ( abs ( float ( args [ <NUM_LIT:3> ] ) ) ) <EOL> donut_query = { "<STR_LIT>" : [ lng , lat ] , "<STR_LIT>" : donut_min , "<STR_LIT>" : donut_max } <EOL> find = { "<STR_LIT>" : donut_query } <EOL> return_features = find_features ( collection , find ) <EOL> feature_collection = create_feature_collection ( return_features ) <EOL> return make_response ( feature_collection ) <EOL> if within_polygon and not property : <EOL> polygon = literal_eval ( within_polygon ) <EOL> find = { "<STR_LIT>" : { "<STR_LIT>" : { "<STR_LIT>" : polygon } } } <EOL> return_features = find_features ( collection , find ) <EOL> feature_collection = create_feature_collection ( return_features ) <EOL> return make_response ( feature_collection ) <EOL> if k_nearest and not property : <EOL> args = k_nearest . split ( "<STR_LIT:U+002C>" ) <EOL> lng = float ( args [ <NUM_LIT:0> ] ) <EOL> lat = float ( args [ <NUM_LIT:1> ] ) <EOL> distance_radius = abs ( float ( args [ <NUM_LIT:2> ] ) ) <EOL> result = db . command ( SON ( [ ( '<STR_LIT>' , dataset ) , ( '<STR_LIT>' , [ lng , lat ] ) , ( '<STR_LIT>' , distance_radius ) ] ) ) [ "<STR_LIT>" ] <EOL> feature_type = "<STR_LIT>" <EOL> results = [ ] <EOL> for parent_obj in result : <EOL> print parent_obj <EOL> print parent_obj [ "<STR_LIT>" ] <EOL> print type ( parent_obj ) <EOL> obj = parent_obj [ "<STR_LIT>" ] <EOL> feature = { } <EOL> feature [ "<STR_LIT>" ] = obj [ "<STR_LIT>" ] <EOL> feature [ "<STR_LIT:type>" ] = feature_type <EOL> feature [ "<STR_LIT>" ] = obj [ "<STR_LIT>" ] <EOL> feature [ "<STR_LIT>" ] [ "<STR_LIT>" ] = parent_obj [ "<STR_LIT>" ] <EOL> results . append ( feature ) <EOL> return create_feature_collection ( results ) <EOL> if within_proximity and property or within_proximity and property : <EOL> return "<STR_LIT>" <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def get_data_by_id ( database , dataset , id ) : <EOL> db = client [ database ] <EOL> collection = db [ dataset ] <EOL> get_property = "<STR_LIT>" <EOL> return_feature = find_features ( collection , { get_property : id } ) <EOL> return make_response ( return_feature ) <EOL> apply_error_handling ( app ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if DB_DOWN == False : <EOL> if host == '<STR_LIT:localhost>' : <EOL> app . run ( host = '<STR_LIT:localhost>' ) <EOL> else : <EOL> app . run ( host = '<STR_LIT>' ) </s>
