def add_airspace(country_code, airspace_class, name, base, top, geom_str): # 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" return False if not airspace_class: print name + " has no airspace class" return False base = normalise_height(base, name) top = normalise_height(top, name) flightlevel_re = re.compile(r'^FL (\d+)$') match = flightlevel_re.match(base) if match and int(match.group(1)) >= 200: print name + " has it's base above FL 200 and is therefore disregarded" return False airspace = Airspace() airspace.country_code = country_code airspace.airspace_class = airspace_class airspace.name = name airspace.base = base airspace.top = top airspace.the_geom = WKTElement(geom_str, srid=4326) DBSession.add(airspace) return True
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 create_pilot(self, email_address, display_name, **kw): if not self.club.is_writable(): raise HTTPForbidden pilot = User(display_name=display_name, email_address=email_address, club=self.club) DBSession.add(pilot) pilots = DBSession.query(Group).filter(Group.group_name == 'pilots').first() if pilots: pilots.users.append(pilot) redirect('pilots')
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 new_post(self, display_name, club, email_address, password, **kw): if not club: club = None user = User(display_name=display_name, club_id=club, email_address=email_address, password=password) user.created_ip = request.remote_addr user.generate_tracking_key() DBSession.add(user) pilots = DBSession.query(Group).filter(Group.group_name == 'pilots').first() if pilots: pilots.users.append(user) 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 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 follow(cls, source, destination): f = cls.query(source, destination).first() if not f: f = Follower(source=source, destination=destination) DBSession.add(f)
def fixReceived(self, host, key, payload): if len(payload) != 32: return data = struct.unpack('!IIiiIHHHhhH', payload) pilot = User.by_tracking_key(key) if not pilot: log.err("No such pilot: %d" % key) return flags = data[0] fix = TrackingFix() fix.ip = host fix.pilot = pilot # import the time stamp from the packet if it's within a # certain range time_of_day_ms = data[1] % (24 * 3600 * 1000) time_of_day_s = time_of_day_ms / 1000 time_of_day = datetime.time(time_of_day_s / 3600, (time_of_day_s / 60) % 60, time_of_day_s % 60, (time_of_day_ms % 1000) * 1000) now = datetime.datetime.utcnow() now_s = ((now.hour * 60) + now.minute) * 60 + now.second if now_s - 1800 < time_of_day_s < now_s + 180: fix.time = datetime.datetime.combine(now.date(), time_of_day) elif now_s < 1800 and time_of_day_s > 23 * 3600: # midnight rollover occurred fix.time = datetime.datetime.combine(now.date(), time_of_day) \ - datetime.timedelta(days=1) else: log.msg("ignoring time stamp from FIX packet: " + str(time_of_day)) if flags & FLAG_LOCATION: fix.location = Location(latitude=data[2] / 1000000., longitude=data[3] / 1000000.) if flags & FLAG_TRACK: fix.track = data[5] if flags & FLAG_GROUND_SPEED: fix.ground_speed = data[6] / 16. if flags & FLAG_AIRSPEED: fix.airspeed = data[7] / 16. if flags & FLAG_ALTITUDE: fix.altitude = data[8] if flags & FLAG_VARIO: fix.vario = data[9] / 256. if flags & FLAG_ENL: fix.engine_noise_level = data[10] log.msg(u"%s %s %s %s" % (fix.time and fix.time.time(), host, pilot, fix.location)) DBSession.add(fix) try: transaction.commit() except SQLAlchemyError, e: log.err(e, 'database error') transaction.abort()
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 DBSession.add(airport) DBSession.flush() transaction.commit()
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
if len(sys.argv) > 2: conf_path = sys.argv[1] del sys.argv[1] if len(sys.argv) != 2: print >>sys.stderr, "Usage: %s [config.ini] PATH" % sys.argv[0] sys.exit(1) conf = appconfig('config:' + os.path.abspath(conf_path)) load_environment(conf.global_conf, conf.local_conf) path = sys.argv[1] r = re.compile(r'^(.*?)\s*\.+[\.\s]*(\d+)\s*$') for line in file(path): m = r.match(line) if m: names, index = m.group(1), int(m.group(2)) for name in names.split(';'): name = name.strip().decode('utf-8') model = AircraftModel.by_name(name) if model is None: model = AircraftModel(name=name) model.kind = 1 DBSession.add(model) model.dmst_index = index DBSession.flush() transaction.commit()
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"