Esempio n. 1
0
    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
Esempio n. 2
0
def _list():
    location = parse_location(request.args)

    airspaces = airspace_list_schema.dump(Airspace.by_location(location).all(), many=True).data
    waves = wave_list_schema.dump(MountainWaveProject.by_location(location), many=True).data

    return jsonify(airspaces=airspaces, waves=waves)
Esempio n. 3
0
def get_airspaces_by_location(location):
    if not isinstance(location, Location):
        raise TypeError('Invalid `location` parameter.')

    data, errors = airspace_list_schema.dump(
        Airspace.by_location(location).all(), many=True)

    return data
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
def list():
    location = parse_location(request.args)

    airspaces = airspace_list_schema.dump(Airspace.by_location(location).all(),
                                          many=True).data
    waves = wave_list_schema.dump(MountainWaveProject.by_location(location),
                                  many=True).data

    return jsonify(airspaces=airspaces, waves=waves)
Esempio n. 7
0
def test_airspace(**kwargs):
    shape = Polygon(((30, 10), (40, 40), (20, 40), (10, 20), (30, 10)))

    return Airspace(
        name='TestAirspace',
        airspace_class='WAVE',
        top='FL100',
        base='4500ft',
        country_code='de',
        the_geom=from_shape(shape, srid=4326),
    ).apply_kwargs(kwargs)
Esempio n. 8
0
def test_airspace(**kwargs):
    shape = Polygon(((30, 10), (40, 40), (20, 40), (10, 20), (30, 10)))

    return Airspace(
        name="TestAirspace",
        airspace_class="WAVE",
        top="FL100",
        base="4500ft",
        country_code="de",
        the_geom=from_shape(shape, srid=4326),
    ).apply_kwargs(kwargs)
Esempio n. 9
0
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
Esempio n. 10
0
File: api.py Progetto: dkm/skylines
    def airspace(self, **kwargs):
        try:
            latitude = float(kwargs['lat'])
            longitude = float(kwargs['lon'])

        except (KeyError, ValueError):
            raise HTTPBadRequest

        location = Location(latitude=latitude,
                            longitude=longitude)

        airspaces = Airspace.get_info(location)
        info = []

        for airspace in airspaces:
            info.append(dict(name=airspace.name,
                             base=airspace.base,
                             top=airspace.top,
                             airspace_class=airspace.airspace_class,
                             country=airspace.country_code))

        return dict(airspaces=info)
Esempio n. 11
0
def get_airspaces_by_location(location):
    return airspace_list_schema.dump(Airspace.by_location(location).all(),
                                     many=True).data
Esempio n. 12
0
    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
Esempio n. 13
0
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
Esempio n. 14
0
def get_airspaces_by_location(location):
    if not isinstance(location, Location):
        raise TypeError('Invalid `location` parameter.')

    airspaces = Airspace.by_location(location)
    return map(airspace_to_dict, airspaces)
Esempio n. 15
0
def _query_airspace(location):
    airspaces = Airspace.by_location(location)
    return map(airspace_to_json, airspaces)