/** * Return the first geometry field of tableName or null if it contains none. * * @param connection Connection * @param tableName TableLocation * @return The first geometry field of tableName or null if it contains none * @throws SQLException */ protected static String getFirstGeometryField(Connection connection, TableLocation tableName) throws SQLException { final List<String> geometryFields = SFSUtilities.getGeometryFields(connection, tableName); if (geometryFields.isEmpty()) { return null; } return geometryFields.get(0); }
@Override protected Object doInBackground() throws Exception { try (Connection connection = dataSource.getConnection()) { fetchingTableIcons.set(false); for (String tableName : tableGeomType.keySet()) { // Fetch icons where type is -1 int currentType = tableGeomType.get(tableName); if(currentType < 0) { int newType = SFSUtilities.getGeometryType(connection, TableLocation.parse(tableName), ""); if(newType != currentType) { tableGeomType.put(tableName, newType); fetchedTableIcons.add(tableName); } } } } return null; } }
/** * 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. * @param connection Active connection * @param catalog Table catalog, may be empty * @param schema Table schema, may be empty * @param table Table name * @param informationSchemaTable Information table location * @param endQuery Additional where statement * @return Prepared statement * @throws SQLException */ public static PreparedStatement prepareInformationSchemaStatement(Connection connection,String catalog, String schema, String table, String informationSchemaTable, String endQuery) throws SQLException { return prepareInformationSchemaStatement(connection,catalog, schema, table, informationSchemaTable, endQuery,"f_table_catalog","f_table_schema","f_table_name"); }
/** * Compute the full extend of a resultset using the first geometry field. If * the resulset does not contain any geometry field throw an exception * * @param resultSet * @return the full envelope of the resulSet * @throws SQLException */ public static Envelope getResultSetEnvelope(ResultSet resultSet) throws SQLException { List<String> geometryFields = getGeometryFields(resultSet); if (geometryFields.isEmpty()) { throw new SQLException("This resultset doesn't contain any geometry field."); } else { return getResultSetEnvelope(resultSet, geometryFields.get(0)); } }
private static void checkGeometryType(Connection connection, TableLocation tableName, int spatialFieldIndex) throws SQLException { final String fieldName = JDBCUtilities.getFieldName(connection.getMetaData(), tableName.getTable(), spatialFieldIndex); int geomType = SFSUtilities.getGeometryType(connection, tableName, fieldName); if (geomType != GeometryTypeCodes.LINESTRING) { throw new IllegalArgumentException(TYPE_ERROR + SFSUtilities.getGeometryTypeNameFromCode(geomType)); } }
/** * Builds the WizardPanel. The objects it will create will be linked to {@code l}. * @param l The parent layer. * @param m Needed for LegendContext implementation. */ public ThematicMapWizard(ILayer l, MapTransform m){ layer = l; try(Connection connection = l.getDataManager().getDataSource().getConnection()) { TableLocation tableLocation = TableLocation.parse(l.getTableReference()); int type = SFSUtilities.getGeometryType(connection, tableLocation, SFSUtilities.getGeometryFields(connection, tableLocation).get(0)); this.geometryType = SimpleGeometryType.getSimpleType(type); mt = m; } catch (SQLException e) { LOGGER.error("Error while reading the data source"); } }
List<String> spatialFieldNames = SFSUtilities.getGeometryFields(connection,parse); if (spatialFieldNames.isEmpty()) { throw new SQLException(String.format("The table %s does not contain a geometry field", tableName)); writeCRS(jsonGenerator,SFSUtilities.getAuthorityAndSRID(connection, parse, spatialFieldNames.get(0))); jsonGenerator.writeArrayFieldStart("features");
try(Connection connection = layer.getDataManager().getDataSource().getConnection()) { try(PreparedStatement pst = SFSUtilities.prepareInformationSchemaStatement(connection, tableLocation.getCatalog(), tableLocation.getSchema(), tableLocation.getTable(), "PUBLIC.GEOMETRY_COLUMNS", ""); List<String> geomFields = SFSUtilities.getGeometryFields(connection, tableLocation); if(geomFields.isEmpty()) { return;
/** * @param connection Active connection * @param location Catalog, schema and table name * @param fieldName Geometry field name or empty (take the first one) * @return The sfs geometry type identifier * @see GeometryTypeCodes * @throws SQLException */ public static int getGeometryType(Connection connection,TableLocation location, String fieldName) throws SQLException { if(fieldName==null || fieldName.isEmpty()) { List<String> geometryFields = getGeometryFields(connection, location); if(geometryFields.isEmpty()) { throw new SQLException("The table "+location+" does not contain a Geometry field, then geometry type cannot be computed"); } fieldName = geometryFields.get(0); } ResultSet geomResultSet = getGeometryColumnsView(connection,location.getCatalog(),location.getSchema(),location.getTable()); boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); while(geomResultSet.next()) { if(fieldName.isEmpty() || geomResultSet.getString("F_GEOMETRY_COLUMN").equalsIgnoreCase(fieldName)) { if(isH2) { return geomResultSet.getInt("GEOMETRY_TYPE"); } else { return GEOM_TYPE_TO_SFS_CODE.get(geomResultSet.getString("type").toLowerCase()); } } } throw new SQLException("Field not found "+fieldName); }
@Override protected Set<Long> doInBackground() throws Exception { Set<Long> newSelection; // Get all primary value where default geometry intersects a bounding box try (Connection connection = SFSUtilities.wrapConnection(activeLayer.getDataManager().getDataSource().getConnection())) { String geomFieldName = SFSUtilities.getGeometryFields(connection, TableLocation.parse(activeLayer.getTableReference())).get(0); newSelection = ReadTable.getTablePkByEnvelope(mc.getDataManager(), activeLayer.getTableReference(), geomFieldName, selectionRect, !intersects); } catch (SQLException e) { automaton.transition(Code.NO_SELECTION); throw new TransitionException(e); } return newSelection; }
/** * Write a prj file according the SRID code of an input table * @param connection database connection * @param location input table name * @param geomField geometry field name * @param fileName path of the prj file * @throws SQLException * @throws FileNotFoundException */ public static void writePRJ(Connection connection, TableLocation location, String geomField, File fileName) throws SQLException, FileNotFoundException { int srid = SFSUtilities.getSRID(connection, location, geomField); writePRJ(connection, srid, fileName); }
/** * Add the geometry type constraint and the SRID */ private void setGeometryTypeConstraints() throws SQLException { if(isH2){ connection.createStatement().execute(String.format("ALTER TABLE %s ALTER COLUMN the_geom %s", tableLocation.toString(), firstGeometryType)); SFSUtilities.addTableSRIDConstraint(connection, tableLocation, parsedSRID); } else{ connection.createStatement().execute(String.format("ALTER TABLE %s ALTER COLUMN the_geom SET DATA TYPE geometry(%s,%d)", tableLocation.toString(), firstGeometryType, parsedSRID)); } } }
/** * Parse the constraint and return the Geometry type name. * @param constraint Constraint on geometry type * @param numericPrecision of the geometry type * @return Geometry type */ public static String getGeometryTypeNameFromConstraint(String constraint, int numericPrecision) { int geometryTypeCode = GeometryTypeFromConstraint.geometryTypeFromConstraint(constraint, numericPrecision); return SFSUtilities.getGeometryTypeNameFromCode(geometryTypeCode); } }
try(Connection connection = ds.getConnection()) { TableLocation tableLocation = TableLocation.parse(layer.getTableReference()); geomType = SFSUtilities.getGeometryType(connection, tableLocation, ""); geometryColumnsCount = SFSUtilities.getGeometryFields(connection, tableLocation).size(); } catch (SQLException e) { LOGGER.warn("Could not determine the specific geometry type for " +
List<String> spatialFieldNames = SFSUtilities.getGeometryFields(connection,parse); if (spatialFieldNames.isEmpty()) { throw new SQLException(String.format("The table %s does not contain a geometry field", tableName)); writeCRS(jsonGenerator,SFSUtilities.getAuthorityAndSRID(connection, parse, spatialFieldNames.get(0))); jsonGenerator.writeArrayFieldStart("features");
/** * Compute the full extend of a resultset using the first geometry field. If * the resulset does not contain any geometry field throw an exception * * @param resultSet * @return the full envelope of the resulSet * @throws SQLException */ public static Envelope getResultSetEnvelope(ResultSet resultSet) throws SQLException { List<String> geometryFields = getGeometryFields(resultSet); if (geometryFields.isEmpty()) { throw new SQLException("This resultset doesn't contain any geometry field."); } else { return getResultSetEnvelope(resultSet, geometryFields.get(0)); } }
/** * @param connection Active connection * @param location Catalog, schema and table name * @param fieldName Geometry field name or empty (take the first one) * @return The sfs geometry type identifier * @see GeometryTypeCodes * @throws SQLException */ public static int getGeometryType(Connection connection,TableLocation location, String fieldName) throws SQLException { if(fieldName==null || fieldName.isEmpty()) { List<String> geometryFields = getGeometryFields(connection, location); if(geometryFields.isEmpty()) { throw new SQLException("The table "+location+" does not contain a Geometry field, then geometry type cannot be computed"); } fieldName = geometryFields.get(0); } ResultSet geomResultSet = getGeometryColumnsView(connection,location.getCatalog(),location.getSchema(),location.getTable()); boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); while(geomResultSet.next()) { if(fieldName.isEmpty() || geomResultSet.getString("F_GEOMETRY_COLUMN").equalsIgnoreCase(fieldName)) { if(isH2) { return geomResultSet.getInt("GEOMETRY_TYPE"); } else { return GEOM_TYPE_TO_SFS_CODE.get(geomResultSet.getString("type").toLowerCase()); } } } throw new SQLException("Field not found "+fieldName); }
private static void checkGeometryType(Connection connection, TableLocation tableName, int spatialFieldIndex) throws SQLException { final String fieldName = JDBCUtilities.getFieldName(connection.getMetaData(), tableName.getTable(), spatialFieldIndex); int geomType = SFSUtilities.getGeometryType(connection, tableName, fieldName); if (geomType != GeometryTypeCodes.LINESTRING) { throw new IllegalArgumentException(TYPE_ERROR + SFSUtilities.getGeometryTypeNameFromCode(geomType)); } }
/** * Write a prj file according the SRID code of an input table * @param connection database connection * @param location input table name * @param geomField geometry field name * @param fileName path of the prj file * @throws SQLException * @throws FileNotFoundException */ public static void writePRJ(Connection connection, TableLocation location, String geomField, File fileName) throws SQLException, FileNotFoundException { int srid = SFSUtilities.getSRID(connection, location, geomField); writePRJ(connection, srid, fileName); }
SFSUtilities.addTableSRIDConstraint(connection, parse, srid);