public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding) throws SQLException, IOException {
        if (FileUtil.isExtensionWellFormated(fileName, "geojson")) {
            FileOutputStream fos = null;
            JsonEncoding jsonEncoding = JsonEncoding.UTF8;
            if (encoding != null) {
                try {
                    jsonEncoding = JsonEncoding.valueOf(encoding);
                } catch (IllegalArgumentException ex) {
                    throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported");
                }
            }
            try {                
                fos = new FileOutputStream(fileName);
                int rowCount = 0;
                int type = rs.getType();
                if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) {
                    rs.last();
                    rowCount = rs.getRow();
                    rs.beforeFirst();
                }
                ProgressVisitor copyProgress = progress.subProcess(rowCount);                
                // Read Geometry Index and type
                List<String> spatialFieldNames = SFSUtilities.getGeometryFields(rs);
                if (spatialFieldNames.isEmpty()) {
                    throw new SQLException("The resulset %s does not contain a geometry field");
                }

                JsonFactory jsonFactory = new JsonFactory();
                JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding);

                // header of the GeoJSON file
                jsonGenerator.writeStartObject();
                jsonGenerator.writeStringField("type", "FeatureCollection");
                jsonGenerator.writeArrayFieldStart("features");
                try {
                    ResultSetMetaData resultSetMetaData = rs.getMetaData();
                    int geoFieldIndex = JDBCUtilities.getFieldIndex(resultSetMetaData, spatialFieldNames.get(0));
                    cacheMetadata(resultSetMetaData);
                    while (rs.next()) {
                        writeFeature(jsonGenerator, rs, geoFieldIndex);
                        copyProgress.endStep();
                    }
                    copyProgress.endOfProgress();
                    // footer
                    jsonGenerator.writeEndArray();
                    jsonGenerator.writeEndObject();
                    jsonGenerator.flush();
                    jsonGenerator.close();
                } finally {
                    rs.close();
                }

            } catch (FileNotFoundException ex) {
                throw new SQLException(ex);

            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException ex) {
                    throw new SQLException(ex);
                }
            }
        } else {
            throw new SQLException("Only .geojson extension is supported");
        }
    }
public void write ( ProgressVisitor progress , ResultSet rs , File fileName , String encoding ) throws SQLException , IOException { if ( FileUtil . isExtensionWellFormated ( fileName , "geojson" ) ) { FileOutputStream fos = null ; JsonEncoding jsonEncoding = JsonEncoding . UTF8 ; if ( encoding != null ) { try { jsonEncoding = JsonEncoding . valueOf ( encoding ) ; } catch ( IllegalArgumentException ex ) { throw new SQLException ( "Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported" ) ; } } try { fos = new FileOutputStream ( fileName ) ; int rowCount = 0 ; int type = rs . getType ( ) ; if ( type == ResultSet . TYPE_SCROLL_INSENSITIVE || type == ResultSet . TYPE_SCROLL_SENSITIVE ) { rs . last ( ) ; rowCount = rs . getRow ( ) ; rs . beforeFirst ( ) ; } ProgressVisitor copyProgress = progress . subProcess ( rowCount ) ; List < String > spatialFieldNames = SFSUtilities . getGeometryFields ( rs ) ; if ( spatialFieldNames . isEmpty ( ) ) { throw new SQLException ( "The resulset %s does not contain a geometry field" ) ; } JsonFactory jsonFactory = new JsonFactory ( ) ; JsonGenerator jsonGenerator = jsonFactory . createGenerator ( new BufferedOutputStream ( fos ) , jsonEncoding ) ; jsonGenerator . writeStartObject ( ) ; jsonGenerator . writeStringField ( "type" , "FeatureCollection" ) ; jsonGenerator . writeArrayFieldStart ( "features" ) ; try { ResultSetMetaData resultSetMetaData = rs . getMetaData ( ) ; int geoFieldIndex = JDBCUtilities . getFieldIndex ( resultSetMetaData , spatialFieldNames . get ( 0 ) ) ; cacheMetadata ( resultSetMetaData ) ; while ( rs . next ( ) ) { writeFeature ( jsonGenerator , rs , geoFieldIndex ) ; copyProgress . endStep ( ) ; } copyProgress . endOfProgress ( ) ; jsonGenerator . writeEndArray ( ) ; jsonGenerator . writeEndObject ( ) ; jsonGenerator . flush ( ) ; jsonGenerator . close ( ) ; } finally { rs . close ( ) ; } } catch ( FileNotFoundException ex ) { throw new SQLException ( ex ) ; } finally { try { if ( fos != null ) { fos . close ( ) ; } } catch ( IOException ex ) { throw new SQLException ( ex ) ; } } } else { throw new SQLException ( "Only .geojson extension is supported" ) ; } }
Write a resulset to a geojson file

@param progress
@param rs input resulset
@param fileName the output file
@param encoding
@throws SQLException
@throws java.io.IOException