<s> revision = '<STR_LIT>' <EOL> down_revision = '<STR_LIT>' <EOL> from alembic import op <EOL> import sqlalchemy as sa <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . orm import sessionmaker <EOL> from geopy . geocoders import Nominatim <EOL> from geopy . distance import vincenty <EOL> import numpy as np <EOL> import math <EOL> import json <EOL> def radian_to_degree ( value ) : <EOL> return <NUM_LIT> / math . pi * value <EOL> def upgrade ( ) : <EOL> op . add_column ( '<STR_LIT>' , sa . Column ( '<STR_LIT>' , sa . String ( ) , nullable = True ) ) <EOL> op . add_column ( '<STR_LIT>' , sa . Column ( '<STR_LIT>' , sa . String ( ) , nullable = True ) ) <EOL> op . add_column ( '<STR_LIT>' , sa . Column ( '<STR_LIT>' , sa . Float ( ) , nullable = True ) ) <EOL> op . add_column ( '<STR_LIT>' , sa . Column ( '<STR_LIT>' , sa . Float ( ) , nullable = True ) ) <EOL> op . add_column ( '<STR_LIT>' , sa . Column ( '<STR_LIT>' , sa . Float ( ) , nullable = True ) ) <EOL> postprocess_moves ( ) <EOL> def downgrade ( ) : <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> op . drop_column ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def calculate_gps_center ( gps_samples ) : <EOL> coordinates = np . ndarray ( shape = ( len ( gps_samples ) , <NUM_LIT:2> ) , dtype = float ) <EOL> for idx , sample in enumerate ( gps_samples ) : <EOL> coordinates [ idx ] [ <NUM_LIT:0> ] = sample . latitude <EOL> coordinates [ idx ] [ <NUM_LIT:1> ] = sample . longitude <EOL> cos_lat = np . cos ( coordinates [ : , <NUM_LIT:0> ] ) <EOL> sin_lat = np . sin ( coordinates [ : , <NUM_LIT:0> ] ) <EOL> cos_lon = np . cos ( coordinates [ : , <NUM_LIT:1> ] ) <EOL> sin_lon = np . sin ( coordinates [ : , <NUM_LIT:1> ] ) <EOL> x = np . average ( cos_lat * cos_lon ) <EOL> y = np . average ( cos_lat * sin_lon ) <EOL> z = np . average ( sin_lat ) <EOL> lon = math . atan2 ( y , x ) <EOL> hyp = np . sqrt ( x * x + y * y ) <EOL> lat = math . atan2 ( z , hyp ) <EOL> center = ( lat , lon ) <EOL> return center <EOL> def postprocess_moves ( ) : <EOL> Base = declarative_base ( ) <EOL> Session = sessionmaker ( bind = op . get_bind ( ) ) <EOL> class Move ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = sa . Column ( sa . Integer , name = "<STR_LIT:id>" , primary_key = True ) <EOL> activity = sa . Column ( sa . String , name = '<STR_LIT>' ) <EOL> location_address = sa . Column ( sa . String , name = "<STR_LIT>" ) <EOL> location_raw = sa . Column ( sa . String , name = "<STR_LIT>" ) <EOL> gps_center_max_distance = sa . Column ( sa . Float , name = "<STR_LIT>" ) <EOL> gps_center_latitude = sa . Column ( sa . Float , name = "<STR_LIT>" ) <EOL> gps_center_longitude = sa . Column ( sa . Float , name = "<STR_LIT>" ) <EOL> class Sample ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = sa . Column ( sa . Integer , name = "<STR_LIT:id>" , primary_key = True ) <EOL> moveId = sa . Column ( sa . Integer , sa . ForeignKey ( Move . id ) , name = "<STR_LIT>" , nullable = False ) <EOL> move = sa . orm . relationship ( Move , backref = sa . orm . backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) ) <EOL> sample_type = sa . Column ( sa . String , name = '<STR_LIT>' ) <EOL> longitude = sa . Column ( sa . Float , name = '<STR_LIT>' ) <EOL> latitude = sa . Column ( sa . Float , name = '<STR_LIT>' ) <EOL> session = Session ( ) <EOL> moves_count = session . query ( Move ) . count ( ) <EOL> for idx , move in enumerate ( session . query ( Move ) ) : <EOL> print ( u"<STR_LIT>" % ( idx + <NUM_LIT:1> , moves_count ) ) <EOL> gps_samples = [ sample for sample in move . samples if sample . sample_type and sample . sample_type . startswith ( '<STR_LIT>' ) ] <EOL> if len ( gps_samples ) > <NUM_LIT:0> : <EOL> print ( u"<STR_LIT>" % ( len ( gps_samples ) , move . id , move . activity ) ) <EOL> gps_center = calculate_gps_center ( gps_samples ) <EOL> move . gps_center_latitude = gps_center [ <NUM_LIT:0> ] <EOL> move . gps_center_longitude = gps_center [ <NUM_LIT:1> ] <EOL> gps_center_degrees = [ radian_to_degree ( x ) for x in gps_center ] <EOL> gps_center_max_distance = <NUM_LIT:0> <EOL> for sample in gps_samples : <EOL> point = ( sample . latitude , sample . longitude ) <EOL> point_degrees = [ radian_to_degree ( x ) for x in point ] <EOL> distance = vincenty ( gps_center_degrees , point_degrees ) . meters <EOL> gps_center_max_distance = max ( gps_center_max_distance , distance ) <EOL> move . gps_center_max_distance = gps_center_max_distance <EOL> first_sample = gps_samples [ <NUM_LIT:0> ] <EOL> latitude = first_sample . latitude <EOL> longitude = first_sample . longitude <EOL> geolocator = Nominatim ( ) <EOL> location = geolocator . reverse ( "<STR_LIT>" % ( radian_to_degree ( latitude ) , radian_to_degree ( longitude ) ) ) <EOL> move . location_address = location . address <EOL> move . location_raw = json . dumps ( location . raw ) <EOL> else : <EOL> print ( u"<STR_LIT>" % ( move . id , move . activity ) ) </s>
