private boolean parseMetadata ( ) throws SQLException , IOException { FileInputStream fis = null ; try { fis = new FileInputStream ( fileName ) ; this . fc = fis . getChannel ( ) ; this . fileSize = fc . size ( ) ; // Given the file size and an average node file size. // Skip how many nodes in order to update progression at a step of 1% readFileSizeEachNode = Math . max ( 1 , ( this . fileSize / AVERAGE_NODE_SIZE ) / 100 ) ; nodeCountProgress = 0 ; cachedColumnNames = new LinkedHashMap < String , String > ( ) ; finalGeometryTypes = new HashSet < String > ( ) ; try ( JsonParser jp = jsFactory . createParser ( fis ) ) { jp . nextToken ( ) ; //START_OBJECT jp . nextToken ( ) ; // field_name (type) jp . nextToken ( ) ; // value_string (FeatureCollection) String geomType = jp . getText ( ) ; if ( geomType . equalsIgnoreCase ( GeoJsonField . FEATURECOLLECTION ) ) { parseFeaturesMetadata ( jp ) ; } else { throw new SQLException ( "Malformed GeoJSON file. Expected 'FeatureCollection', found '" + geomType + "'" ) ; } } //START_OBJECT } catch ( FileNotFoundException ex ) { throw new SQLException ( ex ) ; } finally { try { if ( fis != null ) { fis . close ( ) ; } } catch ( IOException ex ) { throw new IOException ( ex ) ; } } // Now we create the table if there is at least one geometry field. if ( hasGeometryField ) { StringBuilder createTable = new StringBuilder ( ) ; createTable . append ( "CREATE TABLE " ) ; createTable . append ( tableLocation ) ; createTable . append ( " (" ) ; //Add the geometry column if ( isH2 ) { createTable . append ( "THE_GEOM GEOMETRY" ) ; } else { createTable . append ( "THE_GEOM GEOMETRY(geometry," ) . append ( parsedSRID ) . append ( ")" ) ; } cachedColumnIndex = new HashMap < String , Integer > ( ) ; StringBuilder insertTable = new StringBuilder ( "INSERT INTO " ) ; insertTable . append ( tableLocation ) . append ( " VALUES(?" ) ; int i = 1 ; for ( Map . Entry < String , String > columns : cachedColumnNames . entrySet ( ) ) { String columnName = columns . getKey ( ) ; cachedColumnIndex . put ( columnName , i ++ ) ; createTable . append ( "," ) . append ( columns . getKey ( ) ) . append ( " " ) . append ( columns . getValue ( ) ) ; insertTable . append ( "," ) . append ( "?" ) ; } createTable . append ( ")" ) ; insertTable . append ( ")" ) ; try ( Statement stmt = connection . createStatement ( ) ) { stmt . execute ( createTable . toString ( ) ) ; } preparedStatement = connection . prepareStatement ( insertTable . toString ( ) ) ; return true ; } else { throw new SQLException ( "The geojson file does not contain any geometry." ) ; } }
Parses the all GeoJSON feature to create the PreparedStatement .