def rest_query_centroid(datasource, layer_code, column_code, codes):
    try:
        sq = SpatialQuery(config)
        codes = codes.split(",")
        result = sq.query_centroid(datasource, layer_code, column_code, codes)
        return Response(simplejson.dumps(result), content_type='application/json; charset=utf-8')
    except Exception, e:
        log.error(e)
        raise Exception(e)
Beispiel #2
0
def rest_query_bbox(datasource, layer_code, column_code, codes):
    try:
        sq = SpatialQuery(config)
        codes = codes.split(",")
        result = sq.query_bbox(datasource, layer_code, column_code, codes)
        return Response(simplejson.dumps(result),
                        content_type='application/json; charset=utf-8')
    except Exception, e:
        log.error(e)
        raise Exception(e)
def rest_query_db(datasource, query):
    """
    Query the PostGIS with a custom query
    :param datasource: postgis/postgres datasource
    :param query: query to be passed to the db
    :return:
    """
    # TODO it's not used the schema in the query.
    # it should be replaced if the query contains {{SCHEMA}} or something like that
    try:
        sq = SpatialQuery(config)
        geojson_encoding = request.args.get('geojsonEncoding')
        result = sq.query_db(datasource, query, False, geojson_encoding)
        return Response(simplejson.dumps(result), content_type='application/json; charset=utf-8')
    except Exception, e:
        log.error(e)
        raise Exception(e)
Beispiel #4
0
def rest_query_db(datasource, query):
    """
    Query the PostGIS with a custom query
    :param datasource: postgis/postgres datasource
    :param query: query to be passed to the db
    :return:
    """
    # TODO it's not used the schema in the query.
    # it should be replaced if the query contains {{SCHEMA}} or something like that
    try:
        sq = SpatialQuery(config)
        geojson_encoding = request.args.get('geojsonEncoding')
        result = sq.query_db(datasource, query, False, geojson_encoding)
        return Response(simplejson.dumps(result),
                        content_type='application/json; charset=utf-8')
    except Exception, e:
        log.error(e)
        raise Exception(e)
    def export_raster_by_spatial_query(self, user_json, distribution_url=None, distribution_folder=None):
        log.info(user_json)
        log.info(self.config)

        # getting distribution folder
        distribution_folder = self._get_distribution_folder(distribution_folder)

        # TODO remove dependency from here?
        sq = SpatialQuery(self.config)

        vector_filter = user_json["extract_by"]
        db_options = vector_filter["options"]
        db_datasource = db_options["db"]
        layer_code = db_options["layer"]
        column_code = db_options["column"]
        codes = db_options["codes"]
        email_address = None if "email_address" not in user_json else user_json["email_address"]
        rasters = user_json["raster"]

        log.info(rasters)

        # create a random tmp folder
        zip_folder_id = str(uuid.uuid4()).encode("utf-8")
        zip_folder = os.path.join(distribution_folder, zip_folder_id)
        os.mkdir(zip_folder)

        # create a valid folder name to zip it
        output_folder = os.path.join(zip_folder, "layers")
        os.mkdir(output_folder)

        output_files = []
        for raster in rasters:
            log.info(raster)
            raster_path = get_raster_path(raster)
            log.info(raster_path)
            # turning relative to absolute path if required
            # TODO: handle somehow better (it is used just for test)
            if not os.path.isabs(raster_path):
                # this is used to normalize relative path used during test
                raster_path = os.path.normpath(os.path.join(os.path.dirname(__file__), raster_path))
                log.info(raster_path)
                raster_path = os.path.abspath(raster_path)
            log.info(raster_path)
            srid = get_srid_raster(raster_path)
            log.info(srid)

            # retrieving bounding box
            bbox = sq.query_bbox(db_datasource, layer_code, column_code, codes, srid)
            log.info(bbox)

            # create the file on tm folder
            db = sq.get_db_instance()
            db_connection_string = db.get_connection_string(True)
            query = sq.get_query_string_select_all(db_datasource, layer_code, column_code, codes, "*")
            log.info(query)
            filepath = crop_raster_on_vector_bbox_and_postgis_db(raster_path, db_connection_string, query, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1])
            # bounding_box = crop_raster_with_bounding_box(raster_path, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1])

            # move file to distribution tmp folder
            path, filename, name = get_filename(filepath, True)
            dst_file = os.path.join(output_folder, filename)
            move(filepath, dst_file)

            # rename file based on uid layer_name (i.e. fenix:trmm_08_2014 -> trmm_08_2014)
            output_filename = get_filename(raster_path) + ".tif"
            output_file = os.path.join(output_folder, output_filename)
            os.rename(dst_file, output_file)

            # saving the output file to zip
            output_files.append(output_file)

        # zip folder or files
        # TODO: change and use make_archive
        #output_filename = os.path.join(zip_folder, zip_filename)
        #make_archive(folder_to_zip, output_filename)
        zip_path = zip_files(zip_filename, output_files, zip_folder)

        # URL to the resource
        if distribution_url is None:
            return zip_path
        else:
            url = distribution_url + zip_folder_id

            # send email if email address
            self._send_email(url, email_address)

            return '{ "url" : "' + url + '"}'
    def export_vector_by_spatial_query(self, user_json, distribution_url=None, distribution_folder=None):

        vector_filter = user_json["extract_by"]
        db_options = vector_filter["options"]
        db_datasource = db_options["db"]
        layer_code = db_options["layer"]
        column_code = db_options["column"]
        codes = db_options["codes"]
        email_address = None if "email_address" not in user_json else user_json["email_address"]
        vectors = user_json["vector"]


        # getting distribution folder
        distribution_folder = self._get_distribution_folder(distribution_folder)

        # TODO remove dependency from here?
        sq = SpatialQuery(self.config)

        # get file to extract
        output_dirs = []
        for vector in vectors:
            vector_path = get_vector_path(vector)
            srid = get_srid_vector(vector_path)
            #srid = "'merc'"
            log.info(srid)

            # get query
            query = sq.get_query_string_select_all(db_datasource, layer_code, column_code, codes, "ST_Transform(geom, " + srid + ")")
            log.info(query)

            db = sq.get_db_instance()
            db_connection_string = db.get_connection_string(True)
            output_name = get_filename(vector_path)
            output_file_path = crop_vector_on_vector_bbox_and_postgis(vector_path, '"' + db_connection_string + '"', query, output_name)

            # check if filepath exists
            if output_file_path:
                output_dirs.append(os.path.dirname(output_file_path))

        # create a random tmp folder
        zip_folder_id = str(uuid.uuid4()).encode("utf-8")
        zip_folder = os.path.join(distribution_folder, zip_folder_id)
        os.mkdir(zip_folder)

        # create a valid folder name to zip it
        output_folder = os.path.join(zip_folder, "layers")
        os.mkdir(output_folder)

        # move output dirs to distribution folder
        for output_dir in output_dirs:
            for file in glob.glob(os.path.join(output_dir, "*")):
                move(file, output_folder)

        # zip the folder
        tmp_file = create_tmp_filename()
        tmp_zip_path = make_archive(zip_folder, tmp_file)
        # TODO: workaround, strangly it cannot be created a zip file in the folder to zip
        zip_path = os.path.join(zip_folder, "layers.zip")
        os.rename(tmp_zip_path, zip_path)
        log.info(zip_path)

        # URL to the resource
        if distribution_url is None:
            return zip_path
        else:
            url = distribution_url + zip_folder_id

            # send email if email address
            self._send_email(url, email_address)

            return '{ "url" : "' + url + '"}'
Beispiel #7
0
 def test_query(self):
     sq = SpatialQuery(config)
     result = sq.query_db(self.db, "select name from spatial.ne_110m_admin_0_countries where iso_a2 = 'AF'")
     result = simplejson.dumps(result)
     self.assertEqual(result, '[["Afghanistan"]]')
Beispiel #8
0
 def test_centroid(self):
     sq = SpatialQuery(config)
     result = sq.query_centroid(self.db, self.layer_table, self.column_code, self.codes, "4326", self.column_label, "geojson")
     self.assertEqual(result, {'type': 'FeatureCollection', 'features': [{'geometry': {'type': 'Point', 'coordinates': [66.0866902219283, 33.8563992816908]}, 'type': 'Feature', 'properties': {'prop0': 'AF', 'prop1': 'Afghanistan'}}]})
Beispiel #9
0
 def test_countries_4326_in_3857_geojson(self):
     sq = SpatialQuery(config)
     result = sq.query_bbox(self.db, self.layer_table, self.column_code, self.codes, "3857", "geojson")
     self.assertEqual(result, [('{"type":"Polygon","coordinates":[[[6737993.98422105,3416255.99756658],[6737993.98422105,4648350.70054128],[8366553.38206859,4648350.70054128],[8366553.38206859,3416255.99756658],[6737993.98422105,3416255.99756658]]]}',)]                         )
Beispiel #10
0
 def test_srid(self):
     sq = SpatialQuery(config)
     result = sq.query_srid(self.db, self.layer_table, self.column_geom)
     self.assertEqual(result, "4326")
Beispiel #11
0
 def test_countries_4326_in_4326_geojson(self):
     sq = SpatialQuery(config)
     result = sq.query_bbox(self.db, self.layer_table, self.column_code, self.codes, "4326", "geojson")
     self.assertEqual(result, [('{"type":"Polygon","coordinates":[[[60.5284298033116,29.3185724960443],[60.5284298033116,38.4862816432164],[75.1580277851409,38.4862816432164],[75.1580277851409,29.3185724960443],[60.5284298033116,29.3185724960443]]]}',)]                         )
Beispiel #12
0
 def test_countries_4326_in_3857(self):
     sq = SpatialQuery(config)
     result = sq.query_bbox(self.db, self.layer_table, self.column_code, self.codes, "3857")
     self.assertEqual(result, [[4648350.70054128, 6737993.98422105], [3416255.99756658, 8366553.38206859]])
Beispiel #13
0
 def test_countries_4326_in_4326(self):
     sq = SpatialQuery(config)
     result = sq.query_bbox(self.db, self.layer_table, self.column_code, self.codes, "4326")
     self.assertEqual(result, [[38.4862816432164, 60.5284298033116], [29.3185724960443, 75.1580277851409]])
    def export_raster_by_spatial_query(self,
                                       user_json,
                                       distribution_url=None,
                                       distribution_folder=None):
        log.info(user_json)
        log.info(self.config)

        # getting distribution folder
        distribution_folder = self._get_distribution_folder(
            distribution_folder)

        # TODO remove dependency from here?
        sq = SpatialQuery(self.config)

        vector_filter = user_json["extract_by"]
        db_options = vector_filter["options"]
        db_datasource = db_options["db"]
        layer_code = db_options["layer"]
        column_code = db_options["column"]
        codes = db_options["codes"]
        email_address = None if "email_address" not in user_json else user_json[
            "email_address"]
        rasters = user_json["raster"]

        log.info(rasters)

        # create a random tmp folder
        zip_folder_id = str(uuid.uuid4()).encode("utf-8")
        zip_folder = os.path.join(distribution_folder, zip_folder_id)
        os.mkdir(zip_folder)

        # create a valid folder name to zip it
        output_folder = os.path.join(zip_folder, "layers")
        os.mkdir(output_folder)

        output_files = []
        for raster in rasters:
            log.info(raster)
            raster_path = get_raster_path(raster)
            log.info(raster_path)
            # turning relative to absolute path if required
            # TODO: handle somehow better (it is used just for test)
            if not os.path.isabs(raster_path):
                # this is used to normalize relative path used during test
                raster_path = os.path.normpath(
                    os.path.join(os.path.dirname(__file__), raster_path))
                log.info(raster_path)
                raster_path = os.path.abspath(raster_path)
            log.info(raster_path)
            srid = get_srid_raster(raster_path)
            log.info(srid)

            # retrieving bounding box
            bbox = sq.query_bbox(db_datasource, layer_code, column_code, codes,
                                 srid)
            log.info(bbox)

            # create the file on tm folder
            db = sq.get_db_instance()
            db_connection_string = db.get_connection_string(True)
            query = sq.get_query_string_select_all(db_datasource, layer_code,
                                                   column_code, codes, "*")
            log.info(query)
            filepath = crop_raster_on_vector_bbox_and_postgis_db(
                raster_path, db_connection_string, query, bbox[0][0],
                bbox[0][1], bbox[1][0], bbox[1][1])
            # bounding_box = crop_raster_with_bounding_box(raster_path, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1])

            # move file to distribution tmp folder
            path, filename, name = get_filename(filepath, True)
            dst_file = os.path.join(output_folder, filename)
            move(filepath, dst_file)

            # rename file based on uid layer_name (i.e. fenix:trmm_08_2014 -> trmm_08_2014)
            output_filename = get_filename(raster_path) + ".tif"
            output_file = os.path.join(output_folder, output_filename)
            os.rename(dst_file, output_file)

            # saving the output file to zip
            output_files.append(output_file)

        # zip folder or files
        # TODO: change and use make_archive
        #output_filename = os.path.join(zip_folder, zip_filename)
        #make_archive(folder_to_zip, output_filename)
        zip_path = zip_files(zip_filename, output_files, zip_folder)

        # URL to the resource
        if distribution_url is None:
            return zip_path
        else:
            url = distribution_url + zip_folder_id

            # send email if email address
            self._send_email(url, email_address)

            return '{ "url" : "' + url + '"}'
    def export_vector_by_spatial_query(self,
                                       user_json,
                                       distribution_url=None,
                                       distribution_folder=None):

        vector_filter = user_json["extract_by"]
        db_options = vector_filter["options"]
        db_datasource = db_options["db"]
        layer_code = db_options["layer"]
        column_code = db_options["column"]
        codes = db_options["codes"]
        email_address = None if "email_address" not in user_json else user_json[
            "email_address"]
        vectors = user_json["vector"]

        # getting distribution folder
        distribution_folder = self._get_distribution_folder(
            distribution_folder)

        # TODO remove dependency from here?
        sq = SpatialQuery(self.config)

        # get file to extract
        output_dirs = []
        for vector in vectors:
            vector_path = get_vector_path(vector)
            srid = get_srid_vector(vector_path)
            #srid = "'merc'"
            log.info(srid)

            # get query
            query = sq.get_query_string_select_all(
                db_datasource, layer_code, column_code, codes,
                "ST_Transform(geom, " + srid + ")")
            log.info(query)

            db = sq.get_db_instance()
            db_connection_string = db.get_connection_string(True)
            output_name = get_filename(vector_path)
            output_file_path = crop_vector_on_vector_bbox_and_postgis(
                vector_path, '"' + db_connection_string + '"', query,
                output_name)

            # check if filepath exists
            if output_file_path:
                output_dirs.append(os.path.dirname(output_file_path))

        # create a random tmp folder
        zip_folder_id = str(uuid.uuid4()).encode("utf-8")
        zip_folder = os.path.join(distribution_folder, zip_folder_id)
        os.mkdir(zip_folder)

        # create a valid folder name to zip it
        output_folder = os.path.join(zip_folder, "layers")
        os.mkdir(output_folder)

        # move output dirs to distribution folder
        for output_dir in output_dirs:
            for file in glob.glob(os.path.join(output_dir, "*")):
                move(file, output_folder)

        # zip the folder
        tmp_file = create_tmp_filename()
        tmp_zip_path = make_archive(zip_folder, tmp_file)
        # TODO: workaround, strangly it cannot be created a zip file in the folder to zip
        zip_path = os.path.join(zip_folder, "layers.zip")
        os.rename(tmp_zip_path, zip_path)
        log.info(zip_path)

        # URL to the resource
        if distribution_url is None:
            return zip_path
        else:
            url = distribution_url + zip_folder_id

            # send email if email address
            self._send_email(url, email_address)

            return '{ "url" : "' + url + '"}'