Beispiel #1
0
def sql_ready_geom(validated_geom, buff):
    """
    :param validated_geom: geoJSON fragment as extracted from geom_validator
    :param buff: int representing lenth of buffer around geometry if geometry is a linestring
    :return: Geometry string suitable for postgres query
    """
    return make_fragment_str(validated_geom, buff)
Beispiel #2
0
def sql_ready_geom(validated_geom, buff):
    """
    :param validated_geom: geoJSON fragment as extracted from geom_validator
    :param buff: int representing lenth of buffer around geometry if geometry is a linestring
    :return: Geometry string suitable for postgres query
    """
    return make_fragment_str(validated_geom, buff)
Beispiel #3
0
def find_intersecting_shapes(geojson):
    """
    Respond with all shape datasets that intersect with the geojson provided.
    Also include how many geom rows of the dataset intersect.
    :param geojson: URL encoded geojson.
    """
    fragment = make_fragment_str(extract_first_geometry_fragment(geojson))

    try:
        # First, do a bounding box check on all shape datasets.
        query = '''
        SELECT m.dataset_name
        FROM meta_shape as m
        WHERE m.bbox &&
            (SELECT ST_GeomFromGeoJSON('{geojson_fragment}'));
        '''.format(geojson_fragment=fragment)

        intersecting_datasets = engine.execute(query)
        bounding_box_intersection_names = [
            dataset.dataset_name for dataset in intersecting_datasets
        ]
    except Exception as e:
        print 'Error finding candidates'
        raise e

    try:
        # Then, for the candidates, get a count of the rows that intersect.
        response_objects = []
        for dataset_name in bounding_box_intersection_names:
            num_intersections_query = '''
            SELECT count(g.geom) as num_geoms
            FROM {dataset_name} as g
            WHERE ST_Intersects(g.geom, ST_GeomFromGeoJSON('{geojson_fragment}'))
            '''.format(dataset_name=dataset_name, geojson_fragment=fragment)

            num_intersections = engine.execute(num_intersections_query)\
                                      .first().num_geoms

            if num_intersections > 0:
                response_objects.append({
                    'dataset_name': dataset_name,
                    'num_geoms': num_intersections
                })

    except Exception as e:
        print 'Error narrowing candidates'
        raise e

    response_skeleton = {
        'meta': {
            'status': 'ok',
            'message': '',
        },
        'objects': response_objects
    }

    resp = make_response(json.dumps(response_skeleton), 200)
    return resp
Beispiel #4
0
def find_intersecting_shapes(geojson):
    """
    Respond with all shape datasets that intersect with the geojson provided.
    Also include how many geom rows of the dataset intersect.
    :param geojson: URL encoded geojson.
    """
    fragment = make_fragment_str(extract_first_geometry_fragment(geojson))

    try:
        # First, do a bounding box check on all shape datasets.
        query = '''
        SELECT m.dataset_name
        FROM meta_shape as m
        WHERE m.bbox &&
            (SELECT ST_GeomFromGeoJSON('{geojson_fragment}'));
        '''.format(geojson_fragment=fragment)

        intersecting_datasets = engine.execute(query)
        bounding_box_intersection_names = [dataset.dataset_name for dataset in intersecting_datasets]
    except Exception as e:
        print 'Error finding candidates'
        raise e

    try:
        # Then, for the candidates, get a count of the rows that intersect.
        response_objects = []
        for dataset_name in bounding_box_intersection_names:
            num_intersections_query = '''
            SELECT count(g.geom) as num_geoms
            FROM {dataset_name} as g
            WHERE ST_Intersects(g.geom, ST_GeomFromGeoJSON('{geojson_fragment}'))
            '''.format(dataset_name=dataset_name, geojson_fragment=fragment)

            num_intersections = engine.execute(num_intersections_query)\
                                      .first().num_geoms

            if num_intersections > 0:
                response_objects.append({
                    'dataset_name': dataset_name,
                    'num_geoms': num_intersections
                })

    except Exception as e:
        print 'Error narrowing candidates'
        raise e

    response_skeleton = {
                'meta': {
                    'status': 'ok',
                    'message': '',
                },
                'objects': response_objects
            }

    resp = make_response(json.dumps(response_skeleton), 200)
    return resp
Beispiel #5
0
def export_shape(dataset_name):
    """
    :param dataset_name: Name of shape dataset. Expected to be found in meta_shape table.
    Expected query parameter: `data_type`. We expect it to be one of 'json', 'kml', or 'shapefile'.
                                If none of these (or unspecified), return JSON.
    """

    params = request.args.copy()

    where_string = ""

    if params.get('location_geom__within'):
        fragment = make_fragment_str(extract_first_geometry_fragment(params['location_geom__within']))
        where_string = "WHERE ST_Intersects(g.geom, ST_GeomFromGeoJSON('{}'))".format(fragment)

    query = '''SELECT * FROM {} AS g {}'''.format(dataset_name, where_string)
    return export_dataset_to_response(dataset_name, query)
Beispiel #6
0
def filter_shape(dataset_name, geojson):
    """
    Given a shape dataset and user-provided geojson,
    return all shapes from the dataset that intersect the geojson.

    :param dataset_name: Name of shape dataset
    :param geojson: URL encoded goejson
    :return:
    """
    fragment = make_fragment_str(extract_first_geometry_fragment(geojson))

    intersect_query = '''
    SELECT *
    FROM {dataset_name} AS g
    WHERE ST_Intersects(g.geom, ST_GeomFromGeoJSON('{geojson_fragment}'))
    '''.format(dataset_name=dataset_name, geojson_fragment=fragment)

    return export_dataset_to_json_response(dataset_name, intersect_query)
Beispiel #7
0
def export_shape(dataset_name):
    """
    :param dataset_name: Name of shape dataset. Expected to be found in meta_shape table.
    Expected query parameter: `data_type`. We expect it to be one of 'json', 'kml', or 'shapefile'.
                                If none of these (or unspecified), return JSON.
    """

    params = request.args.copy()

    where_string = ""

    if params.get('location_geom__within'):
        fragment = make_fragment_str(
            extract_first_geometry_fragment(params['location_geom__within']))
        where_string = "WHERE ST_Intersects(g.geom, ST_GeomFromGeoJSON('{}'))".format(
            fragment)

    query = '''SELECT * FROM {} AS g {}'''.format(dataset_name, where_string)
    return export_dataset_to_response(dataset_name, query)
Beispiel #8
0
def get_all_shape_datasets():
    """Fetches metadata for every shape dataset in meta_shape.
    """
    try:
        response_skeleton = {
            'meta': {
                'status': 'ok',
                'message': '',
            },
            'objects': []
        }

        geom = request.args.get('location_geom__within')
        simple_bbox = request.args.get('simple_bbox')

        if geom:
            geom = make_fragment_str(
                extract_first_geometry_fragment(geom)
            )

        if simple_bbox:
            public_listing = ShapeMetadata.simple_index(geom)
        else:
            public_listing = ShapeMetadata.index(geom)
        response_skeleton['objects'] = public_listing
        status_code = 200

    except Exception as e:
        response_skeleton = {
            'meta': {
                'status': 'error',
                'message': str(e),
            }
        }
        status_code = 500

    resp = make_response(json.dumps(response_skeleton, default=str), status_code)
    resp.headers['Content-Type'] = 'application/json'
    return resp
Beispiel #9
0
def get_all_shape_datasets():
    """Fetches metadata for every shape dataset in meta_shape.
    """
    try:
        response_skeleton = {
            'meta': {
                'status': 'ok',
                'message': '',
            },
            'objects': []
        }

        geom = request.args.get('location_geom__within')
        simple_bbox = request.args.get('simple_bbox')

        if geom:
            geom = make_fragment_str(extract_first_geometry_fragment(geom))

        if simple_bbox:
            public_listing = ShapeMetadata.simple_index(geom)
        else:
            public_listing = ShapeMetadata.index(geom)
        response_skeleton['objects'] = public_listing
        status_code = 200

    except Exception as e:
        response_skeleton = {
            'meta': {
                'status': 'error',
                'message': str(e),
            }
        }
        status_code = 500

    resp = make_response(json.dumps(response_skeleton, default=str),
                         status_code)
    resp.headers['Content-Type'] = 'application/json'
    return resp
Beispiel #10
0
 def get_geom(self):
     validated_geom = self.vals['location_geom__within']
     if validated_geom is not None:
         buff = self.vals.get('buffer', 100)
         return make_fragment_str(validated_geom, buff)
Beispiel #11
0
def make_sql_ready_geom(geojson_str):
    return make_fragment_str(extract_first_geometry_fragment(geojson_str))    
Beispiel #12
0
    'agg': str,
    'buffer': int,
    'dataset': lambda x: MetaTable.get_by_dataset_name(x).point_table,
    'shapeset': lambda x: ShapeMetadata.get_by_dataset_name(x).shape_table,
    'data_type': str,
    'shape': lambda x: ShapeMetadata.get_by_dataset_name(x).shape_table,
    'dataset_name__in': lambda x: x.split(','),
    'date__time_of_day_ge': int,
    'date__time_of_day_le': int,
    'obs_date__ge': lambda x: parser.parse(x).date(),
    'obs_date__le': lambda x: parser.parse(x).date(),
    'date': lambda x: parser.parse(x).date(),
    'point_date': lambda x: parser.parse(x),
    'offset': int,
    'resolution': int,
    'geom': lambda x: make_fragment_str(extract_first_geometry_fragment(x)),
}


def convert(request_args):
    """Convert a dictionary of arguments from strings to their types. How the
    values are converted are specified by the converters dictionary defined
    above.

    :param request_args: dictionary of request arguments

    :returns: converted dictionary"""

    for key, value in request_args.items():
        try:
            request_args[key] = converters[key](value)
Beispiel #13
0
def make_sql_ready_geom(geojson_str):
    return make_fragment_str(extract_first_geometry_fragment(geojson_str))
Beispiel #14
0
 def _deserialize(self, value, attr, data):
     try:
         return make_fragment_str(extract_first_geometry_fragment(value))
     except (ValueError, AttributeError):
         raise ValidationError('Invalid geom: {}'.format(value))
Beispiel #15
0
 def get_geom(self):
     validated_geom = self.vals['location_geom__within']
     if validated_geom is not None:
         buff = self.vals.get('buffer', 100)
         return make_fragment_str(validated_geom, buff)
Beispiel #16
0
 def _deserialize(self, value, attr, data):
     try:
         return make_fragment_str(extract_first_geometry_fragment(value))
     except (ValueError, AttributeError):
         raise ValidationError('Invalid geom: {}'.format(value))