示例#1
0
文件: dbman.py 项目: riskatlas/layman
    def updateVectorFile(self, filePath, dbSchema, table_name):
        """Update existing postgresql table with given file
        """
        logParam = "filePath='%s', dbSchema='%s'" % (filePath, dbSchema)
        logging.debug("[DbMan][updateVectorFile] %s" % logParam)

        os.environ["GDAL_DATA"] = self.config.get("Gdal", "gdal_data")
        devnull = open(os.devnull, "w")
        sys.stdout = sys.__stderr__
        sys.stderr = devnull
        ogr2ogr_params = [
            "", "-lco", "OVERWRITE=YES", "-lco", "SCHEMA=" + str(dbSchema),
            "-lco", "PRECISION=NO", "-nln", table_name, "-f", "PostgreSQL"
        ]

        if self._get_ogr2ogr_version() >= 1100000:
            ogr2ogr_params.extend(["-nlt", "PROMOTE_TO_MULTI"])

        ogr2ogr_params.extend([self.getConnectionString(True), filePath])

        logging.debug(
            "[DbMan][updateVectorFile] Going to call ogr2ogr.main() with the following params: %s"
            % str(ogr2ogr_params))
        success = ogr2ogr.main(ogr2ogr_params)
        logging.debug("[DbMan][updateVectorFile] ogr2ogr.main() returned.")

        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        devnull.close()

        if not success:
            logging.error("[DbMan][updateVectorFile] ogr2ogr failed.")
            raise LaymanError(
                500,
                "Database import (ogr2ogr) failed. (Is the encoding correct?)")
示例#2
0
文件: dbman.py 项目: riskatlas/layman
    def updateVectorFile(self, filePath, dbSchema, table_name):
        """Update existing postgresql table with given file
        """
        logParam = "filePath='%s', dbSchema='%s'" % (filePath, dbSchema)
        logging.debug("[DbMan][updateVectorFile] %s" % logParam)

        os.environ["GDAL_DATA"] = self.config.get("Gdal","gdal_data")
        devnull = open(os.devnull, "w")
        sys.stdout = sys.__stderr__
        sys.stderr = devnull
        ogr2ogr_params = ["", "-lco", "OVERWRITE=YES",
                          "-lco", "SCHEMA=" + str(dbSchema),
                          "-lco", "PRECISION=NO",
                          "-nln", table_name, "-f", "PostgreSQL"]

        if self._get_ogr2ogr_version() >= 1100000:
            ogr2ogr_params.extend(["-nlt", "PROMOTE_TO_MULTI"])

        ogr2ogr_params.extend([self.getConnectionString(True),
                               filePath])

        logging.debug("[DbMan][updateVectorFile] Going to call ogr2ogr.main() with the following params: %s" % str(ogr2ogr_params))
        success = ogr2ogr.main(ogr2ogr_params)
        logging.debug("[DbMan][updateVectorFile] ogr2ogr.main() returned.")

        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        devnull.close()

        if not success:
            logging.error("[DbMan][updateVectorFile] ogr2ogr failed.")
            raise LaymanError(500, "Database import (ogr2ogr) failed. (Is the encoding correct?)")
示例#3
0
文件: dbman.py 项目: riskatlas/layman
    def importVectorFile(self, filePath, dbSchema, srs, tsrs):
        """import given file to database, ogr is used for data READING,
        psycopg2 for data WRITING directly into PostGIS
        If a table of the same name already exists, new name is assigned.
        """
        logParam = "filePath='%s', dbSchema='%s'" % (filePath, dbSchema)
        logging.debug("[DbMan][importVectorFile] %s" % logParam)

        self.createSchemaIfNotExists(dbSchema)
        ds = ogr.Open(filePath)

        layer_in = ds.GetLayerByIndex(0)
        name_out = layer_in.GetName().lower()
        name_out = self._find_new_layername(dbSchema, name_out)

        os.environ["GDAL_DATA"] = self.config.get("Gdal","gdal_data")

        logging.debug("[DbMan][importVectorFile] Going to import layer to db...")
        # hack -> everthing to devnull
        devnull = open(os.devnull, "w")
        sys.stdout = sys.__stderr__
        sys.stderr = devnull
        ogr2ogr_params = ["", "-lco", "SCHEMA=" + str(dbSchema),
                          "-lco", "PRECISION=NO",
                          "-nln", name_out, "-f", "PostgreSQL", "-s_srs", srs, "-t_srs", tsrs]

        if self._get_ogr2ogr_version() >= 1100000:
            ogr2ogr_params.extend(["-nlt", "PROMOTE_TO_MULTI"])

        ogr2ogr_params.extend([self.getConnectionString(True),
                               filePath])
        logging.debug("[DbMan][importVectorFile] Going to call ogr2ogr.main() with the following params: %s" % str(ogr2ogr_params))
        # FIXME: We need to learn the real new name of the table here. 
        # E.g. "some-name" is transferred to "some_name" and we don't have a clue.
        success = ogr2ogr.main(ogr2ogr_params)
        logging.debug("[DbMan][importVectorFile] ogr2ogr.main() returned. Success: %s" % str(success))

        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        devnull.close()

        if not success:
            logging.error("[DbMan][importVectorFile] ogr2ogr failed.")
            raise LaymanError(500, "Database import (ogr2ogr) failed. (Is the encoding correct?)")

        # FIXME: We need to wait until the DB is ready and THEN return. 
        # Otherwise publishing fails and we get 
        # org.postgresql.util.PSQLException: ERROR: LWGEOM_estimated_extent: couldn't locate table within current schema
        # Takes about 12 secs for ArmCommP.shp

        # TODO: Check the result
        # TODO: Exceptions handling
        # TODO: Check if srs and tsrs is valid EPSG code

        logging.debug("[DbMan][importVectorFile] File %s imported as table %s:%s" % (filePath, dbSchema, name_out))

        return name_out
示例#4
0
文件: dbman.py 项目: riskatlas/layman
    def importVectorFile(self, filePath, dbSchema, srs, tsrs):
        """import given file to database, ogr is used for data READING,
        psycopg2 for data WRITING directly into PostGIS
        If a table of the same name already exists, new name is assigned.
        """
        logParam = "filePath='%s', dbSchema='%s'" % (filePath, dbSchema)
        logging.debug("[DbMan][importVectorFile] %s" % logParam)

        self.createSchemaIfNotExists(dbSchema)
        ds = ogr.Open(filePath)

        layer_in = ds.GetLayerByIndex(0)
        name_out = layer_in.GetName().lower()
        name_out = self._find_new_layername(dbSchema, name_out)

        os.environ["GDAL_DATA"] = self.config.get("Gdal", "gdal_data")

        logging.debug(
            "[DbMan][importVectorFile] Going to import layer to db...")
        # hack -> everthing to devnull
        devnull = open(os.devnull, "w")
        sys.stdout = sys.__stderr__
        sys.stderr = devnull
        ogr2ogr_params = [
            "", "-lco", "SCHEMA=" + str(dbSchema), "-lco", "PRECISION=NO",
            "-nln", name_out, "-f", "PostgreSQL", "-s_srs", srs, "-t_srs", tsrs
        ]

        if self._get_ogr2ogr_version() >= 1100000:
            ogr2ogr_params.extend(["-nlt", "PROMOTE_TO_MULTI"])

        ogr2ogr_params.extend([self.getConnectionString(True), filePath])
        logging.debug(
            "[DbMan][importVectorFile] Going to call ogr2ogr.main() with the following params: %s"
            % str(ogr2ogr_params))
        # FIXME: We need to learn the real new name of the table here.
        # E.g. "some-name" is transferred to "some_name" and we don't have a clue.
        success = ogr2ogr.main(ogr2ogr_params)
        logging.debug(
            "[DbMan][importVectorFile] ogr2ogr.main() returned. Success: %s" %
            str(success))

        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        devnull.close()

        if not success:
            logging.error("[DbMan][importVectorFile] ogr2ogr failed.")
            raise LaymanError(
                500,
                "Database import (ogr2ogr) failed. (Is the encoding correct?)")

        # FIXME: We need to wait until the DB is ready and THEN return.
        # Otherwise publishing fails and we get
        # org.postgresql.util.PSQLException: ERROR: LWGEOM_estimated_extent: couldn't locate table within current schema
        # Takes about 12 secs for ArmCommP.shp

        # TODO: Check the result
        # TODO: Exceptions handling
        # TODO: Check if srs and tsrs is valid EPSG code

        logging.debug(
            "[DbMan][importVectorFile] File %s imported as table %s:%s" %
            (filePath, dbSchema, name_out))

        return name_out