Class GeometryTableUtilities


  • public class GeometryTableUtilities
    extends Object
    Utilities to get geometry metadata from a table that contains at least one geometry column
    Author:
    Erwan Bocher, CNRS (2020)
    • Constructor Detail

      • GeometryTableUtilities

        public GeometryTableUtilities()
    • Method Detail

      • getFirstColumnMetaData

        public static Tuple<String,​GeometryMetaData> getFirstColumnMetaData​(ResultSet resultSet)
                                                                           throws SQLException
        The geometry metadata of a resulset is getting from the first row Because a resulset can mixed geometry types and SRID we are not able to return all geometry metadatas Use this method only to instantiate a GeometryMetaData object
        Parameters:
        resultSet -
        Returns:
        Partial geometry metaData
        Throws:
        SQLException
      • getMetaData

        public static GeometryMetaData getMetaData​(Connection connection,
                                                   String geometryTable,
                                                   String geometryColumnName)
                                            throws SQLException
        Read the geometry metadata from a column name
        Parameters:
        connection -
        geometryTable -
        geometryColumnName -
        Returns:
        Geometry MetaData
        Throws:
        SQLException
      • prepareInformationSchemaStatement

        public static PreparedStatement prepareInformationSchemaStatement​(Connection connection,
                                                                          String catalog,
                                                                          String schema,
                                                                          String table,
                                                                          String informationSchemaTable,
                                                                          String endQuery)
                                                                   throws SQLException
        For table containing catalog, schema and table name, this function create a prepared statement with a filter on this combination. Use "f_table_catalog","f_table_schema","f_table_name" as field names.
        Parameters:
        connection - Active connection
        catalog - Table catalog, may be empty
        schema - Table schema, may be empty
        table - Table name
        informationSchemaTable - Information table location
        endQuery - Additional where statement
        Returns:
        Prepared statement
        Throws:
        SQLException
      • prepareInformationSchemaStatement

        public static PreparedStatement prepareInformationSchemaStatement​(Connection connection,
                                                                          String catalog,
                                                                          String schema,
                                                                          String table,
                                                                          String informationSchemaTable,
                                                                          String endQuery,
                                                                          String catalog_field,
                                                                          String schema_field,
                                                                          String table_field)
                                                                   throws SQLException
        For table containing catalog, schema and table name, this function create a prepared statement with a filter on this combination.
        Parameters:
        connection - Active connection
        catalog - Table catalog, may be empty
        schema - Table schema, may be empty
        table - Table name
        informationSchemaTable - Information table location
        endQuery - Additional where statement
        catalog_field - Catalog field name
        schema_field - Schema field name
        table_field - Table field name
        Returns:
        Prepared statement
        Throws:
        SQLException
      • getFirstGeometryColumnNameAndIndex

        public static Tuple<String,​Integer> getFirstGeometryColumnNameAndIndex​(ResultSet resultSet)
                                                                              throws SQLException
        Find the first geometry column name and its index of a resultSet.
        Parameters:
        resultSet - ResultSet to analyse
        Returns:
        The name and index of first geometry field
        Throws:
        SQLException
      • hasGeometryColumn

        public static boolean hasGeometryColumn​(ResultSet resultSet)
                                         throws SQLException
        Check if the ResultSet contains a geometry column
        Parameters:
        resultSet - ResultSet to analyse
        Returns:
        True if the ResultSet contains one geometry field
        Throws:
        SQLException
      • hasGeometryColumn

        public static boolean hasGeometryColumn​(Connection connection,
                                                String tableLocation)
                                         throws SQLException
        Check if the table contains a geometry column
        Parameters:
        connection -
        tableLocation -
        Returns:
        True if the ResultSet contains one geometry field
        Throws:
        SQLException
      • hasGeometryColumn

        public static boolean hasGeometryColumn​(Connection connection,
                                                TableLocation tableLocation)
                                         throws SQLException
        Check if the table contains a geometry column
        Parameters:
        connection -
        tableLocation -
        Returns:
        True if the ResultSet contains one geometry field
        Throws:
        SQLException
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(ResultSet resultSet)
                                                              throws SQLException
        Compute the full extend of a ResultSet using the first geometry field. If the ResultSet does not contain any geometry field throw an exception
        Parameters:
        resultSet - ResultSet to analyse
        Returns:
        A geometry that represents the full extend of the first geometry column in the ResultSet
        Throws:
        SQLException
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(ResultSet resultSet,
                                                                     String geometryColumnName)
                                                              throws SQLException
        Compute the full extend of a ResultSet using a specified geometry column. If the ResultSet does not contain this geometry field throw an exception If the geometries don't have the same SRID throw an exception
        Parameters:
        resultSet - ResultSet to analyse
        geometryColumnName - Field to analyse
        Returns:
        The full extend of the geometry column name in the ResultSet
        Throws:
        SQLException
      • getEstimatedExtent

        public static org.locationtech.jts.geom.Geometry getEstimatedExtent​(Connection connection,
                                                                            String tableName)
                                                                     throws SQLException
        Compute the 'estimated' extent of the given spatial table. Use the first geometry field In case of POSTGIS : the estimated is taken from the geometry column's statistics. In case of H2GIS : the estimated is taken from the spatial index of the geometry column. If the estimated extend is null the extent is computed.
        Parameters:
        connection -
        tableName -
        Returns:
        Throws:
        SQLException
      • getEstimatedExtent

        public static org.locationtech.jts.geom.Geometry getEstimatedExtent​(Connection connection,
                                                                            TableLocation tableLocation)
                                                                     throws SQLException
        Compute the 'estimated' extent of the given spatial table. Use the first geometry field In case of POSTGIS : the estimated is taken from the geometry column's statistics. In case of H2GIS : the estimated is taken from the spatial index of the geometry column. If the estimated extend is null the extent is computed.
        Parameters:
        connection -
        tableLocation -
        Returns:
        an estimated extend of the table as geometry
        Throws:
        SQLException
      • getEstimatedExtent

        public static org.locationtech.jts.geom.Geometry getEstimatedExtent​(Connection connection,
                                                                            String tableName,
                                                                            String geometryColumnName)
                                                                     throws SQLException
        Compute the 'estimated' extent of the given spatial table. In case of POSTGIS : the estimated is taken from the geometry column's statistics. In case of H2GIS : the estimated is taken from the spatial index of the geometry column. If the estimated extend is null the extent is computed.
        Parameters:
        connection -
        tableName -
        geometryColumnName -
        Returns:
        an estimated extend of the table as geometry
        Throws:
        SQLException
      • getEstimatedExtent

        public static org.locationtech.jts.geom.Geometry getEstimatedExtent​(Connection connection,
                                                                            TableLocation tableLocation,
                                                                            String geometryColumnName)
                                                                     throws SQLException
        Compute the 'estimated' extent of the given spatial table. In case of POSTGIS : the estimated is taken from the geometry column's statistics. In case of H2GIS : the estimated is taken from the spatial index of the geometry column. If the estimated extend is null the extent is computed.
        Parameters:
        connection -
        tableLocation -
        geometryColumnName -
        Returns:
        an estimated extend of the table as geometry
        Throws:
        SQLException
      • getSRID

        public static int getSRID​(Connection connection,
                                  String tableName,
                                  String geometryColumnName)
                           throws SQLException
        Return the SRID of the first geometry column of the input table
        Parameters:
        connection - Active connection
        tableName - Table name
        geometryColumnName - The geometryf field column
        Returns:
        The SRID of the first geometry column
        Throws:
        SQLException
      • getSRID

        public static int getSRID​(Connection connection,
                                  TableLocation tableLocation,
                                  String geometryColumnName)
                           throws SQLException
        Return the SRID of the first geometry column of the input table
        Parameters:
        connection - Active connection
        tableLocation - Table name
        geometryColumnName - The geometryf field column
        Returns:
        The SRID of the first geometry column
        Throws:
        SQLException
      • getSRID

        public static int getSRID​(Connection connection,
                                  String tableName)
                           throws SQLException
        Return the SRID of the first geometry column of the input table
        Parameters:
        connection - Active connection
        tableName - Table name
        Returns:
        The SRID of the first geometry column
        Throws:
        SQLException
      • getSRID

        public static int getSRID​(Connection connection,
                                  TableLocation tableLocation)
                           throws SQLException
        Return the SRID of the first geometry column of the input table
        Parameters:
        connection - Active connection
        tableLocation - Table name
        Returns:
        The SRID of the first geometry column
        Throws:
        SQLException
      • getGeometryColumnNamesAndIndexes

        public static LinkedHashMap<String,​Integer> getGeometryColumnNamesAndIndexes​(Connection connection,
                                                                                           TableLocation tableLocation)
                                                                                    throws SQLException
        Find geometry column names and indexes of a table
        Parameters:
        connection - Active connection
        tableLocation - Table location
        Returns:
        A list of Geometry column names and indexes
        Throws:
        SQLException
      • getGeometryColumnNamesAndIndexes

        public static LinkedHashMap<String,​Integer> getGeometryColumnNamesAndIndexes​(ResultSetMetaData metadata)
                                                                                    throws SQLException
        Find geometry column names and indexes from a resulset
        Parameters:
        metadata - metadata of a resulset
        Returns:
        A list of Geometry column names and indexes
        Throws:
        SQLException
      • getGeometryColumnNames

        public static List<String> getGeometryColumnNames​(Connection connection,
                                                          String tableName)
                                                   throws SQLException
        Find geometry column names
        Parameters:
        connection - Active connection
        tableName - Table location
        Returns:
        A list of Geometry column names and indexes
        Throws:
        SQLException
      • getGeometryColumnNames

        public static List<String> getGeometryColumnNames​(Connection connection,
                                                          TableLocation tableLocation)
                                                   throws SQLException
        Find geometry column names
        Parameters:
        connection - Active connection
        tableLocation - Table location
        Returns:
        A list of Geometry column names and indexes
        Throws:
        SQLException
      • getGeometryColumnNames

        public static List<String> getGeometryColumnNames​(ResultSetMetaData metadata)
                                                   throws SQLException
        Find geometry column names from a resulset
        Parameters:
        metadata - metadata of a resulset
        Returns:
        A list of Geometry column names
        Throws:
        SQLException
      • getFirstGeometryColumnNameAndIndex

        public static Tuple<String,​Integer> getFirstGeometryColumnNameAndIndex​(Connection connection,
                                                                                     String tableName)
                                                                              throws SQLException
        Find the first geometry column name of a table with its index
        Parameters:
        connection - Active connection
        tableName - Table location
        Returns:
        The first geometry column name and its index
        Throws:
        SQLException
      • getFirstGeometryColumnNameAndIndex

        public static Tuple<String,​Integer> getFirstGeometryColumnNameAndIndex​(Connection connection,
                                                                                     TableLocation tableLocation)
                                                                              throws SQLException
        Find the first geometry column name of a table with its index
        Parameters:
        connection - Active connection
        tableLocation - Table location
        Returns:
        The first geometry column name and its index
        Throws:
        SQLException
      • getFirstGeometryColumnNameAndIndex

        public static Tuple<String,​Integer> getFirstGeometryColumnNameAndIndex​(ResultSetMetaData metadata)
                                                                              throws SQLException
        Find the first geometry column name of a table with its index
        Parameters:
        metadata - metadata of a resulset
        Returns:
        The first geometry column name and its index
        Throws:
        SQLException
      • getGeometryColumnsView

        public static ResultSet getGeometryColumnsView​(Connection connection,
                                                       String catalog,
                                                       String schema,
                                                       String table,
                                                       String geometryField)
                                                throws SQLException
        Return a resulset of the geometry column view properties from
        Parameters:
        connection -
        catalog -
        schema -
        table -
        geometryField -
        Returns:
        Throws:
        SQLException
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     String table,
                                                                     String geometryColumn)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table.
        Parameters:
        connection - Active connection (not closed by this function)
        table - Location of the table
        geometryColumn - Geometry field or empty string (take the first geometry field)
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     TableLocation location,
                                                                     String geometryColumn)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table.
        Parameters:
        connection - Active connection (not closed by this function)
        location - Location of the table
        geometryColumn - Geometry field or empty string (take the first geometry field)
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     TableLocation location,
                                                                     String... geometryColumns)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table and geometry columns Note that the geometry column can be an expression. Supported syntaxes the_geom -> Column name st_buffer(the_geom, 20) -> Geometry function
        Parameters:
        connection - Active connection (not closed by this function)
        location - Location of the table
        geometryColumns - List of geometry columns or geometry functions
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     TableLocation location,
                                                                     String[] geometryColumns,
                                                                     String filter)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table, geometry columns and filter condition Note that the geometry column can be an expression. Supported syntaxes the_geom -> Column name st_buffer(the_geom, 20) -> Geometry function
        Parameters:
        connection - Active connection (not closed by this function)
        location - Location of the table
        geometryColumns - List of geometry columns or geometry functions
        filter - filter condition after the from
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     String subQuery,
                                                                     String[] geometryColumns)
                                                              throws SQLException
        Merge the bounding box of all geometries inside a provided subquery, geometry columns Note that the geometry column can be an expression. Supported syntaxes the_geom -> Column name st_buffer(the_geom, 20) -> Geometry function
        Parameters:
        connection - Active connection (not closed by this function)
        subQuery - a subquery to filter the data
        geometryColumns - List of geometry columns or geometry functions
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     String subQuery,
                                                                     String[] geometryColumns,
                                                                     String filter)
                                                              throws SQLException
        Merge the bounding box of all geometries inside a provided subquery, geometry columns and filter condition Note that the geometry column can be an expression. Supported syntaxes the_geom -> Column name st_buffer(the_geom, 20) -> Geometry function
        Parameters:
        connection - Active connection (not closed by this function)
        subQuery - a subquery to filter the data
        geometryColumns - List of geometry columns or geometry functions
        filter - filter condition after the from
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or geometry field empty.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     String table)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table. Use the first geometry column
        Parameters:
        connection - Active connection (not closed by this function)
        table - table name
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or does not contain a geometry field.
      • getEnvelope

        public static org.locationtech.jts.geom.Geometry getEnvelope​(Connection connection,
                                                                     TableLocation location)
                                                              throws SQLException
        Merge the bounding box of all geometries inside the provided table. Use the first geometry column
        Parameters:
        connection - Active connection (not closed by this function)
        location - Location of the table
        Returns:
        Envelope of the table as Geometry
        Throws:
        SQLException - If the table not exists, empty or does not contain a geometry field.
      • getAuthorityAndSRID

        public static String[] getAuthorityAndSRID​(Connection connection,
                                                   int srid)
                                            throws SQLException
        Return an array of two string if the input SRID exists in the spatial ref table. The array contains the authority name and its SRID code. If the SRID does not exist return null
        Parameters:
        connection - Active connection
        srid -
        Returns:
        Array of two string that correspond to the authority name and its SRID code
        Throws:
        SQLException
      • getAuthorityAndSRID

        public static String[] getAuthorityAndSRID​(Connection connection,
                                                   String table,
                                                   String geometryColumnName)
                                            throws SQLException
        Return an array of two string that correspond to the authority name and its SRID code.If the SRID does not exist return the array {null, null}
        Parameters:
        connection - Active connection
        table - Table name
        geometryColumnName - Field to analyse
        Returns:
        Array of two string that correspond to the authority name and its SRID code
        Throws:
        SQLException
      • getAuthorityAndSRID

        public static String[] getAuthorityAndSRID​(Connection connection,
                                                   TableLocation table,
                                                   String geometryColumnName)
                                            throws SQLException
        Return an array of two string that correspond to the authority name and its SRID code. If the SRID does not exist return the array {null, null}
        Parameters:
        connection - Active connection
        table - Table name
        geometryColumnName - Field to analyse
        Returns:
        Array of two string that correspond to the authority name and its SRID code
        Throws:
        SQLException
      • alterSRID

        public static boolean alterSRID​(Connection connection,
                                        String table,
                                        String geometryColumnName,
                                        int srid)
                                 throws SQLException
        Change the SRID of the table
        Parameters:
        connection - Active connection
        table - Table name
        geometryColumnName - geometry column name
        srid - to force
        Returns:
        true if query is well executed
        Throws:
        SQLException
      • alterSRID

        public static boolean alterSRID​(Connection connection,
                                        TableLocation tableLocation,
                                        String geometryColumnName,
                                        int srid)
                                 throws SQLException
        Change the SRID of the table
        Parameters:
        connection - Active connection
        tableLocation - Table name
        geometryColumnName - geometry column name
        srid - to force
        Returns:
        true if query is well executed
        Throws:
        SQLException
      • isSpatialIndexed

        public static boolean isSpatialIndexed​(Connection connection,
                                               TableLocation tableLocation,
                                               String geometryColumnName)
                                        throws SQLException
        Check if the geometry column has a spatial index
        Parameters:
        connection - Active connection
        tableLocation - Table name
        geometryColumnName - geometry column name
        Returns:
        true if query is well executed
        Throws:
        SQLException