def get_data(session, country=None, geoframe=None, begin=None, end=None): """Get data in GeoJSON format. :param session: SQLAlchemy session :type session: sqlalchemy.orm.session.Session :param country: 'country' url parameter :type country: string :param geoframe: 'geoframe' url parameter :type geoframe: string :return: Feature Collection with requested Points :rtype: geojson.FeatureCollection """ rectangle = None # Parse parameter geoframe if geoframe is not None: try: rectangle = bounding_box_to_wkt( *[float(x) for x in geoframe.split(',')]) except ValueError: return 'Invalid geoparam', 400 # parse parameter country elif country is not None: if country not in country_bounding_boxes: return 'Unknown country code.', 400 rectangle = bounding_box_to_wkt(*country_bounding_boxes[country][1]) # Parse parameter begin try: if begin is not None: begin = datetime.datetime.fromisoformat(begin) except ValueError: return 'Invalid begin', 400 # Parse parameter end try: if end is not None: end = datetime.datetime.fromisoformat(end) except ValueError: return 'Invalid end', 400 # Init feature list features = [] # Iterate through database query query = emissionsapi.db.get_points(session, polygon=rectangle, begin=begin, end=end) for feature in query: # Create and append single features. features.append( geojson.Feature(geometry=geojson.Point( (feature.longitude, feature.latitude)), properties={ "carbonmonixide": feature.value, "timestamp": feature.timestamp })) # Create feature collection from gathered points feature_collection = geojson.FeatureCollection(features) return feature_collection
def download(): """Download data files from ESA and store them in the configured storage directory. """ # Load download configuration storage = config('storage') or 'data' date_begin = config('download', 'date', 'begin')\ or '2019-09-10T00:00:00.000Z' date_end = config('download', 'date', 'end') or '2019-09-11T00:00:00.000Z' countries = config('download', 'country') or ['DE'] # create storage folder if not existing os.makedirs(storage, exist_ok=True) for country in countries: wkt = bounding_box_to_wkt(*country_bounding_boxes[country][1]) # Search for data matching the parameter logger.info(f'Looking for products in country {country}') result = sentinel5dl.search(wkt, begin_ts=date_begin, end_ts=date_end, product=product, processing_level=processing_level, logger_fn=logger.info) logger.info('Found {0} products'.format(len(result.get('products')))) # Download data sentinel5dl.download(result.get('products'), output_dir=storage, logger_fn=logger.info)
def decorated(*args, **kwargs): geoframe = kwargs.pop('geoframe', None) country = kwargs.pop('country', None) polygon = kwargs.pop('polygon', None) point = kwargs.pop('point', None) number_of_parameter = sum(x is not None for x in (geoframe, country, polygon, point)) if number_of_parameter > 1: return ("'geoframe', 'country', 'polygon' and " "'point' are mutually exclusive", 400) # Parse parameter geoframe if geoframe is not None: try: logger.debug('Try parsing geoframe') kwargs['wkt'] = bounding_box_to_wkt(*geoframe) except ValueError: return 'Invalid geoparam', 400 # parse parameter country elif country is not None: logger.debug('Try parsing country') try: kwargs['wkt'] = get_country_wkt(country.upper()) except CountryNotFound: return 'Unknown country code.', 400 # parse parameter polygon elif polygon is not None: try: logger.debug('Try parsing polygon') kwargs['wkt'] = polygon_to_wkt(polygon) except RESTParamError as err: return str(err), 400 # parse parameter point elif point is not None: try: logger.debug('Try to parse point') point = h3.h3_to_geo( h3.geo_to_h3(point[1], point[0], emissionsapi.db.resolution)) kwargs['wkt'] = f'POINT({point[1]} {point[0]})' # Take a radius from 0.01 decimal degree which are approx. # 1113 meter kwargs['distance'] = 0.01 except KeyError: return 'Invalid point', 400 return f(*args, **kwargs)