private int doExport ( String tableReference , List < String > spatialFieldNames , ResultSet rs , int recordCount , File fileName , ProgressVisitor progress , String encoding ) throws SQLException , IOException { if ( spatialFieldNames . isEmpty ( ) ) { throw new SQLException ( String . format ( "The table or the query %s does not contain a geometry field" , tableReference ) ) ; } int srid = 0 ; ShapeType shapeType = null ; try { ResultSetMetaData resultSetMetaData = rs . getMetaData ( ) ; int geoFieldIndex = JDBCUtilities . getFieldIndex ( resultSetMetaData , spatialFieldNames . get ( 0 ) ) ; ArrayList < Integer > columnIndexes = new ArrayList < Integer > ( ) ; DbaseFileHeader header = DBFDriverFunction . dBaseHeaderFromMetaData ( resultSetMetaData , columnIndexes ) ; columnIndexes . add ( 0 , geoFieldIndex ) ; if ( encoding != null ) { header . setEncoding ( encoding ) ; } header . setNumRecords ( recordCount ) ; SHPDriver shpDriver = null ; Object [ ] row = new Object [ header . getNumFields ( ) + 1 ] ; while ( rs . next ( ) ) { int i = 0 ; for ( Integer index : columnIndexes ) { row [ i ++ ] = rs . getObject ( index ) ; } if ( shpDriver == null ) { // If there is not shape type constraint read the first geometry and use the same type byte [ ] wkb = rs . getBytes ( geoFieldIndex ) ; if ( wkb != null ) { GeometryMetaData gm = GeometryMetaData . getMetaDataFromWKB ( wkb ) ; if ( srid == 0 ) { srid = gm . SRID ; } shapeType = getShapeTypeFromGeometryMetaData ( gm ) ; } if ( shapeType != null ) { shpDriver = new SHPDriver ( ) ; shpDriver . setGeometryFieldIndex ( 0 ) ; shpDriver . initDriver ( fileName , shapeType , header ) ; } else { throw new SQLException ( "Unsupported geometry type." ) ; } } if ( shpDriver != null ) { shpDriver . insertRow ( row ) ; } progress . endStep ( ) ; } if ( shpDriver != null ) { shpDriver . close ( ) ; } } finally { rs . close ( ) ; } return srid ; }
Method to export a resulset into a shapefile