def add_airspace(self, country_code, airspace_class, name, base, top, geom_str): try: geom = loads(geom_str) except ReadingError: print name + "(" + airspace_class + ") is not a polygon (maybe not enough points?)" return False # orient polygon clockwise geom = polygon.orient(geom, sign=-1) if not airspace_class: print name + " has no airspace class" return False base = self.normalise_height(base, name) top = self.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 = from_shape(geom, srid=4326) db.session.add(airspace) return True
def add_airspace(country_code, airspace_class, name, base, top, geom_str): try: geom = loads(geom_str) except ReadingError: print name + "(" + airspace_class + ") is not a polygon (maybe not enough points?)" return False # orient polygon clockwise geom = polygon.orient(geom, sign=-1) 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 = from_shape(geom, srid=4326) db.session.add(airspace) return True
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 add_airspace(self, country_code, airspace_class, name, base, top, geom_str): try: geom = loads(geom_str) except ReadingError: print name + "(" + airspace_class + ") is not a polygon (maybe not enough points?)" return False # orient polygon clockwise geom = polygon.orient(geom, sign=-1) if not airspace_class: print name + " has no airspace class" return False base = self.normalise_height(base, name) top = self.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 # Check geometry type, disregard everything except POLYGON if geom.geom_type != 'Polygon': print name + " is not a polygon (it's a " + geom.geom_type + ")" return False wkb = from_shape(geom, srid=4326) # Try to fix invalid (self-intersecting) geometries valid_dump = (func.ST_Dump(func.ST_MakeValid(wkb))).geom valid_query = db.session.query(func.ST_SetSRID(valid_dump, 4326)).order_by(func.ST_Area(valid_dump).desc()).first() if not valid_query: print 'Error importing ' + name print 'Could not validate geometry' return False else: wkb = valid_query[0] geom_type = db.session.query(func.ST_GeometryType(wkb)).first()[0] if geom_type != 'ST_Polygon': print name + " got some errors makeing it valid..." return False tolerance = 0.0000001 simplify = lambda x: func.ST_SimplifyPreserveTopology(x, tolerance) airspace.the_geom = case( [ (func.ST_IsValid(wkb), wkb), (func.ST_IsValid(simplify(wkb)), simplify(wkb)), ], else_=None) db.session.add(airspace) return True
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