def remove_country(country_code): print "removing all entries for country_code " + country_code query = DBSession.query(Airspace) \ .filter(Airspace.country_code == country_code) query.delete(synchronize_session=False) DBSession.flush() transaction.commit()
def analysis(self, **kwargs): """Hidden method that restarts flight analysis.""" analyse_flight(self.flight) DBSession.flush() return redirect('.')
def save(self, email_address, display_name, club, tracking_delay=0, unit_preset=1, distance_unit=1, speed_unit=1, lift_unit=0, altitude_unit=0, eye_candy=False, **kwargs): if not self.user.is_writable(request.identity): raise HTTPForbidden self.user.email_address = email_address self.user.display_name = display_name if not club: club = None self.user.club_id = club self.user.tracking_delay = tracking_delay unit_preset = int(unit_preset) if unit_preset == 0: self.user.distance_unit = distance_unit self.user.speed_unit = speed_unit self.user.lift_unit = lift_unit self.user.altitude_unit = altitude_unit else: self.user.unit_preset = unit_preset self.user.eye_candy = eye_candy DBSession.flush() redirect('.')
def analysis(self): """Hidden method that restarts flight analysis.""" for flight in DBSession.query(Flight): analyse_flight(flight) DBSession.flush() return redirect('/flights/')
def save(self, name, website, **kwargs): if not self.club.is_writable(): raise HTTPForbidden self.club.name = name self.club.website = website DBSession.flush() redirect('.')
def import_openair(filename, country_code): print "reading " + filename country_blacklist = blacklist.get(country_code, []) airspace_file = ogr.Open(filename) if not airspace_file: return layer = airspace_file.GetLayerByIndex(0) feature = layer.GetFeature(0) i = 0 j = 0 while(feature): feature = layer.GetFeature(i) i += 1 if not feature: continue geom_str = "POLYGON" + str(feature.geometry())[8:] name = unicode(feature.GetFieldAsString('name'), 'latin1') airspace_class = feature.GetFieldAsString('class') # this is a real kludge to determine if the polygon has more than 3 points... if (geom_str.count(',') < 3): print name + "(" + airspace_class + ") has not enough points to be a polygon" continue if not airspace_class.strip(): print name + " has no airspace class" continue if name in country_blacklist: print name + " is in blacklist" continue airspace = Airspace() airspace.country_code = country_code airspace.airspace_class = airspace_class airspace.name = name airspace.base = feature.GetFieldAsString('floor') airspace.top = feature.GetFieldAsString('ceiling') airspace.the_geom = WKTSpatialElement(geom_str) if i%100 == 0: print "inserting geometry " + str(i) j += 1 DBSession.add(airspace) airspace_file.Destroy() DBSession.flush() transaction.commit() print "added " + str(j) + " features for country " + country_code
def generate_keys(self): """Hidden method that generates missing tracking keys.""" for user in DBSession.query(User): if user.tracking_key is None: user.generate_tracking_key() DBSession.flush() return redirect('/users/')
def apply_and_commit(func, q): n_success, n_failed = 0, 0 for record in q: if func(record): n_success += 1 else: n_failed += 1 if n_success > 0: print "commit" DBSession.flush() transaction.commit() return n_success, n_failed
def setUp(self): """Prepare model test fixture.""" try: new_attrs = {} new_attrs.update(self.attrs) new_attrs.update(self.do_get_dependencies()) self.obj = self.klass(**new_attrs) DBSession.add(self.obj) DBSession.flush() return self.obj except: DBSession.rollback() raise
def select_pilot(self, pilot, co_pilot, **kwargs): if not self.flight.is_writable(request.identity): raise HTTPForbidden if self.flight.pilot_id != pilot: self.flight.pilot_id = pilot if pilot: self.flight.club_id = DBSession.query(User).get(pilot).club_id self.flight.co_pilot_id = co_pilot self.flight.time_modified = datetime.utcnow() DBSession.flush() redirect('.')
def update(self, **kw): flight_id_list = kw.get('flight_id') model_list = kw.get('model') registration_list = kw.get('registration') if not isinstance(flight_id_list, list): flight_id_list = [flight_id_list] if not isinstance(model_list, list): model_list = [model_list] if not isinstance(registration_list, list): registration_list = [registration_list] if flight_id_list is None \ or len(flight_id_list) != len(model_list) \ or len(flight_id_list) != len(registration_list): flash(_('Sorry, some error happened when updating your flight(s). Please contact a administrator for help.'), 'warning') return redirect('/flights/today') for index, id in enumerate(flight_id_list): try: id = int(id) except ValueError: continue try: model_id = int(model_list[index]) except ValueError: model_id = None registration = registration_list[index] if registration is not None: registration = registration.strip() if not 0 < len(registration) < 32: registration = None flight = DBSession.query(Flight).get(id) if not flight.is_writable(): continue flight.model_id = model_id flight.registration = registration flight.time_modified = datetime.utcnow() DBSession.flush() flash(_('Your flight(s) have been successfully updated.')) return redirect('/flights/today')
def igc_headers(self): """Hidden method that parses all missing IGC headers.""" igc_files = DBSession.query(IGCFile) igc_files = igc_files.filter(or_(IGCFile.logger_manufacturer_id == None, IGCFile.logger_id == None, IGCFile.model == None, IGCFile.registration == None)) for igc_file in igc_files: igc_file.update_igc_headers() DBSession.flush() return redirect('/flights/')
def select_aircraft(self, model, registration, **kwargs): if not self.flight.is_writable(): raise HTTPForbidden if registration is not None: registration = registration.strip() if len(registration) == 0: registration = None self.flight.model_id = model self.flight.registration = registration self.flight.time_modified = datetime.utcnow() DBSession.flush() redirect('.')
def create_club(self, name, **kw): if not self.user.is_writable(request.identity): raise HTTPForbidden current_user = request.identity['user'] club = Club(name=name) club.owner_id = current_user.id DBSession.add(club) self.user.club = club DBSession.flush() redirect('.')
def igc_headers(self, **kwargs): """Hidden method that parses all missing IGC headers.""" igc_files = DBSession.query(IGCFile) igc_files = igc_files.filter(or_(IGCFile.logger_manufacturer_id is None, IGCFile.logger_id is None, IGCFile.model is None, IGCFile.registration is None, IGCFile.competition_id is None, IGCFile.date_utc is None)) for igc_file in igc_files: igc_file.update_igc_headers() DBSession.flush() return redirect('.')
def import_openair(filename, country_code): print "reading " + filename country_blacklist = blacklist.get(country_code, []) airspace_file = ogr.Open(filename) if not airspace_file: return layer = airspace_file.GetLayerByIndex(0) feature = layer.GetFeature(0) i = 0 j = 0 while(feature): feature = layer.GetFeature(i) i += 1 if not feature: continue name = unicode(feature.GetFieldAsString('name'), 'latin1').strip() if name in country_blacklist: print name + " is in blacklist" continue added = add_airspace( country_code, feature.GetFieldAsString('class').strip(), name, feature.GetFieldAsString('floor'), feature.GetFieldAsString('ceiling'), "POLYGON" + str(feature.geometry())[8:]) if not added: continue if i % 100 == 0: print "inserting geometry " + str(i) j += 1 airspace_file.Destroy() DBSession.flush() transaction.commit() print "added " + str(j) + " features for country " + country_code
def select_club(self, club, **kwargs): if not self.user.is_writable(request.identity): raise HTTPForbidden self.user.club_id = club # assign the user's new club to all of his flights that have # no club yet flights = DBSession.query(Flight).outerjoin(IGCFile) flights = flights.filter(and_(Flight.club_id == None, or_(Flight.pilot_id == self.user.id, IGCFile.owner_id == self.user.id))) for flight in flights: flight.club_id = club DBSession.flush() redirect('.')
def do(self, file, pilot): user = request.identity['user'] pilot_id = None club_id = user.club_id if pilot: pilot = DBSession.query(User).get(int(pilot)) if pilot: pilot_id = pilot.id club_id = pilot.club_id flights = [] success = False for name, f in IterateUploadFiles(file): filename = files.sanitise_filename(name) filename = files.add_file(filename, f) # check if the file already exists with files.open_file(filename) as f: md5 = file_md5(f) other = Flight.by_md5(md5) if other: files.delete_file(filename) flights.append((name, other, _('Duplicate file'))) continue igc_file = IGCFile() igc_file.owner = user igc_file.filename = filename igc_file.md5 = md5 igc_file.update_igc_headers() if igc_file.date_utc is None: files.delete_file(filename) flights.append((name, None, _('Date missing in IGC file'))) continue flight = Flight() flight.pilot_id = pilot_id flight.club_id = club_id flight.igc_file = igc_file flight.model_id = igc_file.guess_model() if igc_file.registration: flight.registration = igc_file.registration else: flight.registration = igc_file.guess_registration() flight.competition_id = igc_file.competition_id if not analyse_flight(flight): files.delete_file(filename) flights.append((name, None, _('Failed to parse file'))) continue if not flight.takeoff_time or not flight.landing_time: files.delete_file(filename) flights.append((name, None, _('No flight found in file'))) continue if not flight.update_flight_path(): files.delete_file(filename) flights.append((name, None, _('No flight found in file'))) continue flights.append((name, flight, None)) DBSession.add(igc_file) DBSession.add(flight) create_flight_notifications(flight) success = True DBSession.flush() return dict(flights=flights, success=success, ModelSelectField=aircraft_model.SelectField)
def main(): mwp_center_file = ogr.Open(sys.argv[1]) if not mwp_center_file: return mwp_ext_file = ogr.Open(sys.argv[2]) if not mwp_ext_file: return mwp_center_layer = mwp_center_file.GetLayerByIndex(0) mwp_ext_layer = mwp_ext_file.GetLayerByIndex(0) center_feature = mwp_center_layer.GetFeature(0) ext_feature = mwp_ext_layer.GetFeature(0) i = 0 j = 0 while(center_feature): center_feature = mwp_center_layer.GetFeature(i) ext_feature = mwp_ext_layer.GetFeature(i) i += 1 if not center_feature: continue name = unicode(center_feature.GetFieldAsString('name'), 'utf-8') \ .strip() country = center_feature.GetFieldAsString('country').strip() vertical_value = center_feature.GetFieldAsDouble('verticalve') synoptical = center_feature.GetFieldAsString('synoptical').strip() main_wind_direction = center_feature.GetFieldAsString('mainwinddi') \ .strip() additional = center_feature.GetFieldAsString('additional').strip() source = center_feature.GetFieldAsString('source').strip() remark1 = center_feature.GetFieldAsString('remark1').strip() remark2 = center_feature.GetFieldAsString('remark2').strip() orientation = center_feature.GetFieldAsDouble('orientatio') rotor_height = center_feature.GetFieldAsString('rotorheigh').strip() weather_dir = center_feature.GetFieldAsInteger('weatherdir') location = center_feature.geometry() if ext_feature: axis_length = ext_feature.GetFieldAsDouble('shape_leng') axis = ext_feature.geometry().ExportToWkt() else: axis_length = None axis = None wave = MountainWaveProject() wave.name = name wave.country = country wave.vertical_value = vertical_value wave.synoptical = synoptical wave.main_wind_direction = main_wind_direction wave.additional = additional wave.source = source wave.remark1 = remark1 wave.remark2 = remark2 wave.orientation = orientation wave.rotor_height = rotor_height wave.weather_dir = weather_dir wave.axis = WKTElement(axis, srid=4326) wave.axis_length = axis_length wave.location = WKTElement(location.ExportToWkt(), srid=4326) wave.ellipse = ellipse(axis_length / 2, axis_length / 8, -orientation, location.GetX(), location.GetY()) DBSession.add(wave) if i % 100 == 0: print "inserting geometry " + str(i) j += 1 mwp_center_file.Destroy() mwp_ext_file.Destroy() DBSession.flush() transaction.commit() print "added " + str(j) + " waves"
def import_sua(filename, country_code): print "reading " + filename country_blacklist = blacklist.get(country_code, []) temporary_file = os.path.join(config['skylines.temporary_dir'], os.path.basename(filename) + '.tmp') # try to uncomment a CLASS definition, as many SUA files from soaringweb.org have a CLASS comment with open(filename, 'r') as in_file: with open(temporary_file, 'w') as out_file: for line in in_file.xreadlines(): out_file.write(line.replace('# CLASS', 'CLASS')) airspace_file = ogr.Open(temporary_file) if not airspace_file: return layer = airspace_file.GetLayerByIndex(0) feature = layer.GetFeature(0) i = 0 j = 0 while(feature): feature = layer.GetFeature(i) i += 1 if not feature: continue name = unicode(feature.GetFieldAsString('title'), 'latin1').strip() if name in country_blacklist: print name + " is in blacklist" continue airspace_class = feature.GetFieldAsString('class').strip() airspace_type = parse_airspace_type(feature.GetFieldAsString('type').strip()) if not airspace_class: if airspace_type: airspace_class = airspace_type else: print name + " has neither class nor type" continue added = add_airspace( country_code, airspace_class, name, feature.GetFieldAsString('base'), feature.GetFieldAsString('tops'), "POLYGON" + str(feature.geometry())[8:]) if not added: continue if i % 100 == 0: print "inserting geometry " + str(i) j += 1 airspace_file.Destroy() DBSession.flush() transaction.commit() os.remove(temporary_file) print "added " + str(j) + " features for country " + country_code
def import_sua(filename, country_code): print "reading " + filename country_blacklist = blacklist.get(country_code, []) temporary_file = os.path.join(config['skylines.temporary_dir'], os.path.basename(filename) + '.tmp') # try to uncomment a CLASS definition, as many SUA files from soaringweb.org have a CLASS comment with open(filename, 'r') as in_file: with open(temporary_file, 'w') as out_file: for line in in_file.xreadlines(): out_file.write(line.replace('# CLASS', 'CLASS')) airspace_file = ogr.Open(temporary_file) if not airspace_file: return layer = airspace_file.GetLayerByIndex(0) feature = layer.GetFeature(0) i = 0 j = 0 while(feature): feature = layer.GetFeature(i) i += 1 if not feature: continue geom_str = "POLYGON" + str(feature.geometry())[8:] name = unicode(feature.GetFieldAsString('title'), 'latin1').strip() airspace_class = feature.GetFieldAsString('class').strip() airspace_type = parse_airspace_type(feature.GetFieldAsString('type').strip()) # this is a real kludge to determine if the polygon has more than 3 points... if (geom_str.count(',') < 3): print name + "(" + airspace_class + ") has not enough points to be a polygon" continue if not airspace_class: if airspace_type: airspace_class = airspace_type else: print name + " has neither class nor type" continue if name in country_blacklist: print name + " is in blacklist" continue airspace = Airspace() airspace.country_code = country_code airspace.airspace_class = airspace_class airspace.name = name airspace.base = feature.GetFieldAsString('base') airspace.top = feature.GetFieldAsString('tops') airspace.the_geom = WKTSpatialElement(geom_str) if i%100 == 0: print "inserting geometry " + str(i) j += 1 DBSession.add(airspace) airspace_file.Destroy() DBSession.flush() transaction.commit() os.remove(temporary_file) print "added " + str(j) + " features for country " + country_code
conf = appconfig('config:' + os.path.abspath(conf_path)) load_environment(conf.global_conf, conf.local_conf) welt2000 = get_database() i = 0 for airport_w2k in welt2000: if airport_w2k.type != 'airport' \ and airport_w2k.type != 'glider_site' \ and airport_w2k.type != 'uml': continue i += 1 if i%100 == 0: DBSession.flush() print str(i) + ": " + airport_w2k.country_code + " " + airport_w2k.name airport = Airport() airport.location = airport_w2k airport.altitude = airport_w2k.altitude airport.name = airport_w2k.name airport.short_name = airport_w2k.short_name airport.icao = airport_w2k.icao airport.country_code = airport_w2k.country_code airport.surface = airport_w2k.surface airport.runway_len = airport_w2k.runway_len airport.runway_dir = airport_w2k.runway_dir airport.frequency = airport_w2k.freq airport.type = airport_w2k.type