Exemple #1
0
 def test_nested_funcs(self):
     stmt = select([func.ST_AsGeoJSON(func.ST_MakeValid(func.ST_MakePoint(1, 2)))])
     self._assert_stmt(
         stmt,
         'SELECT '
         'ST_AsGeoJSON(ST_MakeValid('
         'ST_MakePoint(:ST_MakePoint_1, :ST_MakePoint_2)'
         ')) AS "ST_AsGeoJSON_1"',
     )
 def test_unknown_func(self):
     stmt = select([
         func.ST_AsGeoJSON(func.ST_UnknownFunction(func.ST_MakePoint(1, 2)))
     ])
     self._assert_stmt(
         stmt,
         'SELECT '
         'ST_AsGeoJSON(ST_UnknownFunction('
         'ST_MakePoint(:ST_MakePoint_1, :ST_MakePoint_2)'
         ')) AS "ST_AsGeoJSON_1"',
     )
Exemple #3
0
def save_place_data():
    data = request.get_json()
    try:
        coordinates = data["location"]['coordinates']
        # create geometric point (lng, lat) order
        geo_point = func.ST_SetSRID(
            func.ST_MakePoint(coordinates['lng'], coordinates['lat']), 4326)
        data = {
            'name': data["name"],
            'aqi': data["pollution"]["aqi"],
            'type': data["location"]["type"],
            'location': coordinates,  # JSON
            'geometric_point': geo_point
            # ToDo: update `updated_at` field
        }

        place_exists = None
        # update or save new entry
        place = get_place_by_spatial_data(geo_point)
        if not place:
            place_exists = False
            place = Place()
        else:
            place_exists = True

        place.from_dict(data)
        if not place_exists:
            db.session.add(place)
        db.session.commit()

        return {
            'response': {
                'statusCode': 201 if not place_exists else 200,
                'statusText': 'OK'
            },
            'data': {
                'status': True,
                'message': 'Air quality data for place saved.',
            }
        }
    except SQLAlchemyError as e:
        db.session.rollback()
        print(e)
        return badrequest('Saving to database failed')
    except Exception as e:
        print(e)
        return badrequest('Check body content')
Exemple #4
0
# Check to see if any features have been returned
if not features:
    my_logger("Nothing found in IRWIN")
    exit(0)
# Open up a connection to the DB
db = FireMap()
# initialize an empty list that will hold all of the objects so we can bulk insert with session.add_all(list)
information_objects_list = []

# Loop through each feature and add to DB
for feature in features:
    irwin_details = Fires()
    irwin_details.__setattr__('source', 'irwin')
    irwin_details.__setattr__('lat', feature['geometry']['y'])
    irwin_details.__setattr__('lon', feature['geometry']['x'])
    irwin_details.__setattr__('geom', func.ST_SetSRID(func.ST_MakePoint(feature['geometry']['x'], feature['geometry']['y']), 4326))
    irwin_details.__setattr__('scrape_date', func.now())
    attributes = feature['attributes']
    for attr, val in attributes.items():
        formatted_key = rss_to_db(attr)
        # print "%s - %s - %s" % (formatted_key, attr, val)
        if not formatted_key:
            continue
        elif formatted_key == 'state':
            val = val.split('-')[1]
        elif formatted_key == 'start_date' or formatted_key == 'estimated_contained_date' or formatted_key == 'published_date':
            seconds = val / 1000
            val = datetime.datetime.utcfromtimestamp(seconds).isoformat()
        irwin_details.__setattr__(formatted_key, val)
    if db.session.query(exists().where(Fires.irwin_id == irwin_details.irwin_id)).scalar():
        # UPDATE query