2010-07-19 21:28:29 +02:00
|
|
|
package com.osmand.swing;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
|
|
|
import com.osmand.LogUtil;
|
2010-07-27 20:58:42 +02:00
|
|
|
import com.osmand.map.ITileSource;
|
|
|
|
import com.osmand.map.TileSourceManager.TileSourceTemplate;
|
2010-07-19 21:28:29 +02:00
|
|
|
|
|
|
|
public class SQLiteBigPlanetIndex {
|
|
|
|
private static final Log log = LogUtil.getLog(SQLiteBigPlanetIndex.class);
|
|
|
|
|
|
|
|
private static final int BATCH_SIZE = 50;
|
|
|
|
|
2010-07-27 20:58:42 +02:00
|
|
|
public static void createSQLiteDatabase(File dirWithTiles, String regionName, ITileSource template) throws SQLException, IOException {
|
2010-07-19 21:28:29 +02:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
try {
|
|
|
|
Class.forName("org.sqlite.JDBC"); //$NON-NLS-1$
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
log.error("Illegal configuration", e); //$NON-NLS-1$
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
2010-07-27 20:58:42 +02:00
|
|
|
File fileToWrite = new File(dirWithTiles, regionName + "." + template.getName() + ".sqlitedb");
|
2010-07-19 21:28:29 +02:00
|
|
|
fileToWrite.delete();
|
|
|
|
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileToWrite.getAbsolutePath()); //$NON-NLS-1$
|
|
|
|
Statement statement = conn.createStatement();
|
|
|
|
statement.execute("CREATE TABLE tiles (x int, y int, z int, s int, image blob, PRIMARY KEY (x,y,z,s))");
|
|
|
|
statement.execute("CREATE INDEX IND on tiles (x,y,z,s)");
|
2010-07-27 20:58:42 +02:00
|
|
|
statement.execute("CREATE TABLE info(minzoom,maxzoom,url)");
|
2010-07-19 21:28:29 +02:00
|
|
|
statement.execute("CREATE TABLE android_metadata (locale TEXT)");
|
|
|
|
statement.close();
|
2010-07-27 20:58:42 +02:00
|
|
|
|
2010-07-19 21:28:29 +02:00
|
|
|
|
2010-07-27 20:58:42 +02:00
|
|
|
PreparedStatement pStatement = conn.prepareStatement("INSERT INTO INFO VALUES(?,?,?)");
|
|
|
|
if (template instanceof TileSourceTemplate) {
|
|
|
|
pStatement.setInt(1, template.getMinimumZoomSupported());
|
|
|
|
pStatement.setInt(2, template.getMaximumZoomSupported());
|
|
|
|
pStatement.setString(3, ((TileSourceTemplate) template).getUrlTemplate());
|
|
|
|
pStatement.execute();
|
|
|
|
}
|
|
|
|
pStatement.close();
|
2010-07-19 21:28:29 +02:00
|
|
|
|
2010-07-27 20:58:42 +02:00
|
|
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
pStatement = conn.prepareStatement("INSERT INTO tiles VALUES (?, ?, ?, ?, ?)");
|
2010-07-19 21:28:29 +02:00
|
|
|
int ch = 0;
|
|
|
|
int bufSize = 32 * 1024;
|
|
|
|
byte[] buf = new byte[bufSize];
|
|
|
|
int maxZoom = 17;
|
|
|
|
int minZoom = 1;
|
|
|
|
|
2010-07-27 20:58:42 +02:00
|
|
|
File rootDir = new File(dirWithTiles, template.getName());
|
2010-07-19 21:28:29 +02:00
|
|
|
for(File z : rootDir.listFiles()){
|
|
|
|
try {
|
|
|
|
int zoom = Integer.parseInt(z.getName());
|
|
|
|
for(File xDir : z.listFiles()){
|
|
|
|
try {
|
|
|
|
int x = Integer.parseInt(xDir.getName());
|
|
|
|
for(File f : xDir.listFiles()){
|
|
|
|
if(!f.isFile()){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
int i = f.getName().indexOf('.');
|
|
|
|
int y = Integer.parseInt(f.getName().substring(0, i));
|
|
|
|
buf = new byte[(int) f.length()];
|
|
|
|
if(zoom > maxZoom){
|
|
|
|
maxZoom = zoom;
|
|
|
|
}
|
|
|
|
if(zoom < minZoom){
|
|
|
|
minZoom = zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInputStream is = new FileInputStream(f);
|
|
|
|
int l = is.read(buf);
|
|
|
|
if (l > 0) {
|
|
|
|
pStatement.setInt(1, x);
|
|
|
|
pStatement.setInt(2, y);
|
2010-07-19 22:38:40 +02:00
|
|
|
pStatement.setInt(3, 17 - zoom);
|
2010-07-19 21:28:29 +02:00
|
|
|
pStatement.setInt(4, 0);
|
|
|
|
pStatement.setBytes(5, buf);
|
|
|
|
pStatement.addBatch();
|
|
|
|
ch++;
|
|
|
|
if (ch >= BATCH_SIZE) {
|
|
|
|
pStatement.executeBatch();
|
|
|
|
ch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch > 0) {
|
|
|
|
pStatement.executeBatch();
|
|
|
|
ch = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pStatement.close();
|
|
|
|
conn.commit();
|
|
|
|
conn.close();
|
|
|
|
log.info("Index created " + fileToWrite.getName() + " " + (System.currentTimeMillis() - now) + " ms");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|