Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * h2spatial is a library that brings spatial support to the H2 Java database.
   *
   * h2spatial is distributed under GPL 3 license. It is produced by the "Atelier SIG"
   * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
   *
   * Copyright (C) 2007-2014 IRSTV (FR CNRS 2488)
   *
   * h2patial is free software: you can redistribute it and/or modify it under the
  * terms of the GNU General Public License as published by the Free Software
  * Foundation, either version 3 of the License, or (at your option) any later
  * version.
  *
  * h2spatial is distributed in the hope that it will be useful, but WITHOUT ANY
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along with
  * h2spatial. If not, see <http://www.gnu.org/licenses/>.
  *
  * For more information, please consult: <http://www.orbisgis.org/>
  * or contact directly:
  * info_at_ orbisgis.org
  */
 package org.h2gis.drivers.osm;
 
 
 import java.io.File;
 import java.sql.Types;
 import java.util.Map;
 import java.util.Set;
Parse an OSM file and store the elements into a database. The database model contains 10 tables.

Author(s):
Erwan Bocher
 
 public class OSMParser extends DefaultHandler {
 
     //Suffix table names
     private static final String TAG = "_tag";
     private static final String NODE = "_node";
     private static final String WAY = "_way";
     private static final String NODE_TAG = "_node_tag";
     private static final String WAY_TAG = "_way_tag";
     private static final String WAY_NODE = "_way_node";
     private static final String RELATION = "_relation";
     private static final String RELATION_TAG = "_relation_tag";
     private static final String NODE_MEMBER = "_node_member";
     private static final String WAY_MEMBER = "_way_member";
     private static final String RELATION_MEMBER = "_relation_member";
     // Set the same batch size as OSMOSIS
     // 1000 lines is 73 kb for node insert
     private static final int BATCH_SIZE = 1000;
     private int nodePreparedStmtBatchSize = 0;
     private int nodeTagPreparedStmtBatchSize = 0;
     private int wayPreparedStmtBatchSize = 0;
     private int wayTagPreparedStmtBatchSize = 0;
     private int relationPreparedStmtBatchSize = 0;
     private int relationTagPreparedStmtBatchSize = 0;
     private int nodeMemberPreparedStmtBatchSize = 0;
     private int wayMemberPreparedStmtBatchSize = 0;
     private int relationMemberPreparedStmtBatchSize = 0;
    private int wayNodePreparedStmtBatchSize = 0;
    private Set<StringinsertedTagsKeys = new HashSet<String>();
    private int idMemberOrder = 1;
    private TAG_LOCATION tagLocation;
    private final GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 4326);
    private WayOSMElement wayOSMElement;
    private ProgressVisitor progress = new EmptyProgressVisitor();
    private FileChannel fc;
    private long fileSize = 0;
    private long readFileSizeEachNode = 1;
    private long nodeCountProgress = 0;
    // For progression information return
    private static final int AVERAGE_NODE_SIZE = 500;
    private static String TAG_DUPLICATE_EXCEPTION = String.valueOf(.);
    public OSMParser() {
    }

    
Read the OSM file and create its corresponding tables.

Parameters:
inputFile
tableName
connection
progress
Returns:
Throws:
java.sql.SQLException
    public boolean read(Connection connectionString tableNameFile inputFileProgressVisitor progressthrows SQLException {
        this. = progress.subProcess(100);
        // Initialisation
        final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData());
        boolean success = false;
        TableLocation requestedTable = TableLocation.parse(tableNameisH2);
        String osmTableName = requestedTable.getTable();
        checkOSMTables(connectionisH2requestedTableosmTableName);
        createOSMDatabaseModel(connectionisH2requestedTableosmTableName);
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(inputFile);
            this. = fs.getChannel();
            this. = .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%
             = Math.max(1, (this. / ) / 100);
             = 0;
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.setErrorHandler(this);
            parser.setContentHandler(this);
            if(inputFile.getName().endsWith(".osm")) {
                parser.parse(new InputSource(fs));
            } else if(inputFile.getName().endsWith(".osm.gz")) {
                parser.parse(new InputSource(new GZIPInputStream(fs)));
            } else if(inputFile.getName().endsWith(".osm.bz2")) {
                parser.parse(new InputSource(new BZip2CompressorInputStream(fs)));
            }
            success = true;
        } catch (SAXException ex) {
            throw new SQLException(ex);
        } catch (IOException ex) {
            throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex);
        } finally {
            try {
                if (fs != null) {
                    fs.close();
                }
            } catch (IOException ex) {
                throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex);
            }
            // When the reading ends, close() method has to be called
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if ( != null) {
                .close();
            }
            if(!=null){
                .close();
            }
        }
        return success;
    }

    
Check if one table already exists

Parameters:
connection
isH2
requestedTable
osmTableName
Throws:
java.sql.SQLException
    private void checkOSMTables(Connection connectionboolean isH2TableLocation requestedTableString osmTableNamethrows SQLException {
        String[] omsTables = new String[]{};
        for (String omsTableSuffix : omsTables) {
            String osmTable = caseIdentifier(requestedTableosmTableName + omsTableSuffixisH2);
            if (JDBCUtilities.tableExists(connectionosmTable)) {
                throw new SQLException("The table " + osmTable + " already exists.");
            }
        }
    }

    
Return the table identifier in the best fit depending on database type

Parameters:
requestedTable Catalog and schema used
tableName Table without quotes
isH2 True if H2, false if PostGRES
Returns:
Find table identifier
    private static String caseIdentifier(TableLocation requestedTableString tableNameboolean isH2) {
        return new TableLocation(requestedTable.getCatalog(), requestedTable.getSchema(),
                TableLocation.parse(tableNameisH2).getTable()).toString();
    }

    
Create the OMS data model to store the content of the file

Parameters:
connection
isH2
requestedTable
osmTableName
Throws:
java.sql.SQLException
    private void createOSMDatabaseModel(Connection connectionboolean isH2TableLocation requestedTableString osmTableNamethrows SQLException {
        String tagTableName = caseIdentifier(requestedTableosmTableName + isH2);
         =  OSMTablesFactory.createTagTable(connectiontagTableName);
        String nodeTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createNodeTable(connectionnodeTableNameisH2);
        String nodeTagTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createNodeTagTable(connectionnodeTagTableNametagTableName);
        String wayTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createWayTable(connectionwayTableNameisH2);
        String wayTagTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createWayTagTable(connectionwayTagTableNametagTableName);
        String wayNodeTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createWayNodeTable(connectionwayNodeTableName);
        String relationTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createRelationTable(connectionrelationTableName);
        String relationTagTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createRelationTagTable(connectionrelationTagTableNametagTableName);
        String nodeMemberTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createNodeMemberTable(connectionnodeMemberTableName);
        String wayMemberTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createWayMemberTable(connectionwayMemberTableName);
        String relationMemberTableName = caseIdentifier(requestedTableosmTableName + isH2);
         = OSMTablesFactory.createRelationMemberTable(connectionrelationMemberTableName);
    }
    @Override
    public void startElement(String uriString localNameString qNameAttributes attributesthrows SAXException {
        String type = attributes.getValue("type");
        if(.isCanceled()) {
            throw new SAXException("Canceled by user");
        }
        if (localName.compareToIgnoreCase("node") == 0) {
             = new NodeOSMElement(Double.valueOf(attributes.getValue("lat")),Double.valueOf(attributes.getValue("lon")));
            setCommonsAttributes(attributes);
             = .;
        } else if (localName.compareToIgnoreCase("way") == 0) {
             = new WayOSMElement();
            setCommonsAttributes(attributes);
             = .;
        } else if (localName.compareToIgnoreCase("tag") == 0) {
            String key = attributes.getValue("k");
            String value = attributes.getValue("v");
            boolean insertTag = true;
            switch () {
                case :
                    insertTag = .addTag(keyvalue);
                    break;
                case :
                    insertTag = .addTag(keyvalue);
                    break;
                case :
                    insertTag = .addTag(keyvalue);
                    break;
            }
            try{
                if(insertTag && !.contains(key)) {
                    .setObject(1, key);
                    .execute();
                    .add(key);
                }
            } catch (SQLException ex) {
                    if(ex.getErrorCode() != . && !.equals(ex.getSQLState())) {
                        throw new SAXException("Cannot insert the tag :  {" + key + " , " + value + "}"ex);
                    }
            }
        } else if (localName.compareToIgnoreCase("nd") == 0) {
            .addRef(attributes.getValue("ref"));
        } else if (localName.compareToIgnoreCase("relation") == 0) {
             = new OSMElement();
            setCommonsAttributes(attributes);
             = .;
        } else if (localName.compareToIgnoreCase("member") == 0) {
            if (type.equalsIgnoreCase("node")) {
                try {
                    .setObject(1, .getID());
                    .setObject(2, Long.valueOf(attributes.getValue("ref")));
                    .setObject(3, attributes.getValue("role"));
                    .setObject(4, );
                    .addBatch();
                    ++;
                } catch (SQLException ex) {
                    throw new SAXException("Cannot insert the node member for the relation :  " + .getID(), ex);
                }
            } else if (type.equalsIgnoreCase("way")) {
                try {
                    .setObject(1, .getID());
                    .setObject(2, Long.valueOf(attributes.getValue("ref")));
                    .setObject(3, attributes.getValue("role"));
                    .setObject(4, );
                    .addBatch();
                    ++;
                } catch (SQLException ex) {
                    throw new SAXException("Cannot insert the way member for the relation :  " + .getID(), ex);
                }
            } else if (type.equalsIgnoreCase("relation")) {
                try {
                    .setObject(1, .getID());
                    .setObject(2, Long.valueOf(attributes.getValue("ref")));
                    .setObject(3, attributes.getValue("role"));
                    .setObject(4, );
                    .addBatch();
                    ++;
                } catch (SQLException ex) {
                    throw new SAXException("Cannot insert the relation member for the relation :  " + .getID(), ex);
                }
            }
        }
    }
    @Override
    public void endDocument() throws SAXException {
        // Execute remaining batch
        try {
        } catch (SQLException ex) {
            throw new SAXException("Could not insert sql batch"ex);
        }
    }
    @Override
    public void endElement(String uriString localNameString qNamethrows SAXException {
        if (localName.compareToIgnoreCase("node") == 0) {
             = .;
            try {
                .setObject(1, .getID());
                .setObject(2, .getPoint());
                .setObject(3, .getElevation());
                .setObject(4, .getUser());
                .setObject(5, .getUID());
                .setObject(6, .getVisible());
                .setObject(7, .getVersion());
                .setObject(8, .getChangeSet());
                .setObject(9, .getTimeStamp(), .);
                .setString(10, .getName());
                .addBatch();
                ++;
                HashMap<StringStringtags = .getTags();
                for (Map.Entry<StringStringentry : tags.entrySet()) {
                    .setObject(1, .getID());
                    .setObject(2, entry.getKey());
                    .setObject(3, entry.getValue());
                    .addBatch();
                    ++;
                }
            } catch (SQLException ex) {
                throw new SAXException("Cannot insert the node  :  " + .getID(), ex);
            }
        } else if (localName.compareToIgnoreCase("way") == 0) {
             = .;
            try {
                .setObject(1, .getID());
                .setObject(2, .getUser());
                .setObject(3, .getUID());
                .setObject(4, .getVisible());
                .setObject(5, .getVersion());
                .setObject(6, .getChangeSet());
                .setTimestamp(7, .getTimeStamp());
                .setString(8, .getName());
                .addBatch();
                ++;
                HashMap<StringStringtags = .getTags();
                for (Map.Entry<StringStringentry : tags.entrySet()) {
                    .setObject(1, .getID());
                    .setObject(2, entry.getKey());
                    .setObject(3, entry.getValue());
                    .addBatch();
                    ++;
                }
                int order = 1;
                for (long ref :  .getNodesRef()) {
                    .setObject(1, .getID());
                    .setObject(2, ref);
                    .setObject(3, order++);
                    .addBatch();
                    ++;
                }
            } catch (SQLException ex) {
                throw new SAXException("Cannot insert the way  :  " + .getID(), ex);
            }
        } else if (localName.compareToIgnoreCase("relation") == 0) {
             = .;
            try {
                .setObject(1, .getID());
                .setObject(2, .getUser());
                .setObject(3, .getUID());
                .setObject(4, .getVisible());
                .setObject(5, .getVersion());
                .setObject(6, .getChangeSet());
                .addBatch();
                ++;
                HashMap<StringStringtags = .getTags();
                for (Map.Entry<StringStringentry : tags.entrySet()) {
                    .setObject(1, .getID());
                    .setObject(2, entry.getKey());
                    .setObject(3, entry.getValue());
                    .addBatch();
                    ++;
                }
                 = 0;
            } catch (SQLException ex) {
                throw new SAXException("Cannot insert the relation  :  " + .getID(), ex);
            }
        } else if (localName.compareToIgnoreCase("member") == 0) {
            ++;
        }
        try {
            insertBatch();
        } catch (SQLException ex) {
            throw new SAXException("Could not insert sql batch"ex);
        }
        if(++ %  == 0) {
            // Update Progress
            try {
                .setStep((int) (((double.position() / ) * 100));
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
    private void insertBatch() throws SQLException {
    }
    private int insertBatch(PreparedStatement stint batchSizeint maxBatchSizethrows SQLException {
        if(batchSize >= maxBatchSize) {
            st.executeBatch();
            return 0;
        } else {
            return batchSize;
        }
    }
    private int insertBatch(PreparedStatement stint batchSizethrows SQLException {
        return insertBatch(stbatchSize);
    }

    

Parameters:
osmElement
attributes
Throws:
java.text.ParseException
    private void setCommonsAttributes(OSMElement osmElementAttributes attributesthrows SAXException {
        osmElement.setId(attributes.getValue("id"));
        osmElement.setUser(attributes.getValue("user"));
        osmElement.setUid(attributes.getValue("uid"));
        osmElement.setVisible(attributes.getValue("visible"));
        osmElement.setVersion(attributes.getValue("version"));
        osmElement.setChangeset(attributes.getValue("changeset"));
        osmElement.setTimestamp(attributes.getValue("timestamp"));
    }
New to GrepCode? Check out our FAQ X