This commit is contained in:
2025-10-30 15:30:14 +08:00
parent fbf1804998
commit 0f69b41c1d
21 changed files with 1046 additions and 62 deletions

View File

@ -34,7 +34,7 @@ public class CodeUtil {
}
// 传入需要生成代码的表名
Generation("poi_info");
Generation("matter");
}
public static void Generation(String... tableName) {

View File

@ -410,7 +410,7 @@ public class GdalJsonConverter {
}
/**
* 创建空的MultiPolygon几何
* 创建空的 MultiPolygon 几何
*/
private static JSONObject createEmptyMultiPolygon() throws JSONException {
JSONObject geometry = new JSONObject();

View File

@ -0,0 +1,172 @@
package com.yj.earth.common.util;
import java.sql.*;
public class PoiDataUtil {
// 源数据库路径
private static final String SOURCE_DB_PATH = "jdbc:sqlite:E:\\Users\\MSI\\Downloads\\poi.db3";
// 目标数据库路径
private static final String TARGET_DB_PATH = "jdbc:sqlite:F:\\poi.db";
public static void main(String[] args) {
Connection sourceConn = null;
Connection targetConn = null;
try {
// 加载SQLite JDBC驱动
Class.forName("org.sqlite.JDBC");
// 建立数据库连接
sourceConn = DriverManager.getConnection(SOURCE_DB_PATH);
targetConn = DriverManager.getConnection(TARGET_DB_PATH);
System.out.println("数据库连接成功");
// 禁用自动提交、以便事务管理
targetConn.setAutoCommit(false);
// 创建目标表结构
createTargetTables(targetConn);
// 迁移地区表数据
migrateAreaTable(sourceConn, targetConn);
System.out.println("地区表数据迁移完成");
// 迁移全国数据(包括香港和澳门)
migratePoiData(sourceConn, targetConn, "poi_gcj");
migratePoiData(sourceConn, targetConn, "poi_bd_hk");
migratePoiData(sourceConn, targetConn, "poi_bd_mo");
System.out.println("POI数据迁移完成");
// 为name字段创建索引
createNameIndex(targetConn);
System.out.println("索引创建完成");
// 提交事务
targetConn.commit();
System.out.println("所有操作已提交");
} catch (ClassNotFoundException e) {
System.err.println("找不到SQLite JDBC驱动: " + e.getMessage());
} catch (SQLException e) {
System.err.println("数据库操作错误: " + e.getMessage());
try {
if (targetConn != null) {
targetConn.rollback();
System.out.println("事务已回滚");
}
} catch (SQLException ex) {
System.err.println("回滚错误: " + ex.getMessage());
}
} finally {
// 关闭数据库连接
try {
if (sourceConn != null) sourceConn.close();
if (targetConn != null) targetConn.close();
System.out.println("数据库连接已关闭");
} catch (SQLException e) {
System.err.println("关闭连接错误: " + e.getMessage());
}
}
}
/**
* 创建目标数据库的表结构
*/
private static void createTargetTables(Connection conn) throws SQLException {
// 创建地区表
String createAreaTable = "CREATE TABLE IF NOT EXISTS \"poi_area\" (" +
"\"adcode\" INTEGER," +
"\"parentid\" INTEGER," +
"\"citycode\" TEXT," +
"\"level\" TEXT," +
"\"name\" TEXT," +
"\"center\" TEXT," +
"\"islast\" TEXT" +
");";
// 创建数据主表
String createDataTable = "CREATE TABLE IF NOT EXISTS \"data\" (" +
"\"id\" INTEGER," +
"\"lng\" REAL," +
"\"lat\" REAL," +
"\"name\" TEXT," +
"\"areaid\" INTEGER" +
");";
try (Statement stmt = conn.createStatement()) {
stmt.execute(createAreaTable);
stmt.execute(createDataTable);
}
}
/**
* 迁移地区表数据
*/
private static void migrateAreaTable(Connection sourceConn, Connection targetConn) throws SQLException {
String selectSql = "SELECT adcode, parentid, citycode, level, name, center, islast FROM poi_area";
String insertSql = "INSERT INTO poi_area (adcode, parentid, citycode, level, name, center, islast) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement selectStmt = sourceConn.prepareStatement(selectSql);
PreparedStatement insertStmt = targetConn.prepareStatement(insertSql);
ResultSet rs = selectStmt.executeQuery()) {
int count = 0;
while (rs.next()) {
insertStmt.setInt(1, rs.getInt("adcode"));
insertStmt.setInt(2, rs.getInt("parentid"));
insertStmt.setString(3, rs.getString("citycode"));
insertStmt.setString(4, rs.getString("level"));
insertStmt.setString(5, rs.getString("name"));
insertStmt.setString(6, rs.getString("center"));
insertStmt.setString(7, rs.getString("islast"));
insertStmt.executeUpdate();
count++;
// 每1000条记录打印一次进度
if (count % 1000 == 0) {
System.out.println("已迁移地区数据: " + count + "");
}
}
System.out.println("地区表共迁移: " + count + " 条记录");
}
}
/**
* 迁移 POI 数据
*/
private static void migratePoiData(Connection sourceConn, Connection targetConn, String sourceTable) throws SQLException {
// 查询源表中需要的字段
String selectSql = "SELECT id, wgslng, wgslat, name, areaid FROM " + sourceTable;
// 插入到目标表
String insertSql = "INSERT INTO data (id, lng, lat, name, areaid) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement selectStmt = sourceConn.prepareStatement(selectSql);
PreparedStatement insertStmt = targetConn.prepareStatement(insertSql);
ResultSet rs = selectStmt.executeQuery()) {
int count = 0;
while (rs.next()) {
insertStmt.setInt(1, rs.getInt("id"));
insertStmt.setDouble(2, rs.getDouble("wgslng"));
insertStmt.setDouble(3, rs.getDouble("wgslat"));
insertStmt.setString(4, rs.getString("name"));
if (rs.wasNull()) {
insertStmt.setNull(5, Types.INTEGER);
} else {
insertStmt.setInt(5, rs.getInt("areaid"));
}
insertStmt.executeUpdate();
count++;
// 每10000条记录打印一次进度
if (count % 10000 == 0) {
System.out.println("已迁移" + sourceTable + "数据: " + count + "");
}
}
System.out.println(sourceTable + "共迁移: " + count + " 条记录");
}
}
private static void createNameIndex(Connection conn) throws SQLException {
String createIndexSql = "CREATE INDEX IF NOT EXISTS idx_data_name ON data(name)";
try (Statement stmt = conn.createStatement()) {
stmt.execute(createIndexSql);
}
}
}

View File

@ -0,0 +1,184 @@
package com.yj.earth.common.util;
import java.sql.*;
public class PoiExportDataUtil {
// 源数据库路径
private static final String SOURCE_DB_PATH = "jdbc:sqlite:E:\\Users\\MSI\\Downloads\\poi.db3";
// 目标数据库路径
private static final String TARGET_DB_PATH = "jdbc:sqlite:F:\\export.db";
public static void main(String[] args) {
Connection sourceConn = null;
Connection targetConn = null;
try {
// 加载SQLite JDBC驱动
Class.forName("org.sqlite.JDBC");
// 建立数据库连接
sourceConn = DriverManager.getConnection(SOURCE_DB_PATH);
targetConn = DriverManager.getConnection(TARGET_DB_PATH);
System.out.println("数据库连接成功");
// 禁用自动提交、以便事务管理
targetConn.setAutoCommit(false);
// 创建目标表结构
createTargetTables(targetConn);
// 迁移地区表数据
migrateAreaTable(sourceConn, targetConn);
System.out.println("地区表数据迁移完成");
// 迁移全国数据(包括香港和澳门)
migratePoiData(sourceConn, targetConn, "poi_gcj");
migratePoiData(sourceConn, targetConn, "poi_bd_hk");
migratePoiData(sourceConn, targetConn, "poi_bd_mo");
System.out.println("POI数据迁移完成");
// 为areaid字段创建索引此处为修改点
createAreaidIndex(targetConn);
System.out.println("索引创建完成");
// 提交事务
targetConn.commit();
System.out.println("所有操作已提交");
} catch (ClassNotFoundException e) {
System.err.println("找不到SQLite JDBC驱动: " + e.getMessage());
} catch (SQLException e) {
System.err.println("数据库操作错误: " + e.getMessage());
try {
if (targetConn != null) {
targetConn.rollback();
System.out.println("事务已回滚");
}
} catch (SQLException ex) {
System.err.println("回滚错误: " + ex.getMessage());
}
} finally {
// 关闭数据库连接
try {
if (sourceConn != null) sourceConn.close();
if (targetConn != null) targetConn.close();
System.out.println("数据库连接已关闭");
} catch (SQLException e) {
System.err.println("关闭连接错误: " + e.getMessage());
}
}
}
/**
* 创建目标数据库的表结构
*/
private static void createTargetTables(Connection conn) throws SQLException {
// 创建地区表
String createAreaTable = "CREATE TABLE IF NOT EXISTS \"poi_area\" (" +
"\"adcode\" INTEGER," +
"\"parentid\" INTEGER," +
"\"citycode\" TEXT," +
"\"level\" TEXT," +
"\"name\" TEXT," +
"\"center\" TEXT," +
"\"islast\" TEXT" +
");";
// 创建数据主表
String createDataTable = "CREATE TABLE IF NOT EXISTS \"data\" (" +
"\"id\" INTEGER," +
"\"lng\" REAL," +
"\"lat\" REAL," +
"\"name\" TEXT," +
"\"areaid\" INTEGER" +
");";
try (Statement stmt = conn.createStatement()) {
stmt.execute(createAreaTable);
stmt.execute(createDataTable);
}
}
/**
* 迁移地区表数据
*/
private static void migrateAreaTable(Connection sourceConn, Connection targetConn) throws SQLException {
String selectSql = "SELECT adcode, parentid, citycode, level, name, center, islast FROM poi_area";
String insertSql = "INSERT INTO poi_area (adcode, parentid, citycode, level, name, center, islast) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement selectStmt = sourceConn.prepareStatement(selectSql);
PreparedStatement insertStmt = targetConn.prepareStatement(insertSql);
ResultSet rs = selectStmt.executeQuery()) {
int count = 0;
while (rs.next()) {
insertStmt.setInt(1, rs.getInt("adcode"));
insertStmt.setInt(2, rs.getInt("parentid"));
insertStmt.setString(3, rs.getString("citycode"));
insertStmt.setString(4, rs.getString("level"));
insertStmt.setString(5, rs.getString("name"));
insertStmt.setString(6, rs.getString("center"));
insertStmt.setString(7, rs.getString("islast"));
insertStmt.executeUpdate();
count++;
// 每1000条记录打印一次进度
if (count % 1000 == 0) {
System.out.println("已迁移地区数据: " + count + "");
}
}
System.out.println("地区表共迁移: " + count + " 条记录");
}
}
/**
* 迁移POI数据包括全国、香港、澳门数据
*/
private static void migratePoiData(Connection sourceConn, Connection targetConn, String sourceTable) throws SQLException {
// 查询源表中需要的字段
String selectSql = "SELECT id, wgslng, wgslat, name, areaid FROM " + sourceTable;
// 插入到目标表
String insertSql = "INSERT INTO data (id, lng, lat, name, areaid) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement selectStmt = sourceConn.prepareStatement(selectSql);
PreparedStatement insertStmt = targetConn.prepareStatement(insertSql);
ResultSet rs = selectStmt.executeQuery()) {
int count = 0;
while (rs.next()) {
insertStmt.setInt(1, rs.getInt("id"));
insertStmt.setDouble(2, rs.getDouble("wgslng"));
insertStmt.setDouble(3, rs.getDouble("wgslat"));
insertStmt.setString(4, rs.getString("name"));
// 处理可能为NULL的areaid
if (rs.wasNull()) {
insertStmt.setNull(5, Types.INTEGER);
} else {
insertStmt.setInt(5, rs.getInt("areaid"));
}
insertStmt.executeUpdate();
count++;
// 每10000条记录打印一次进度
if (count % 10000 == 0) {
System.out.println("已迁移" + sourceTable + "数据: " + count + "");
}
}
System.out.println(sourceTable + "共迁移: " + count + " 条记录");
}
}
/**
* 为 areaid 字段创建索引
*/
private static void createAreaidIndex(Connection conn) throws SQLException {
String createIndexSql = "CREATE INDEX IF NOT EXISTS idx_data_areaid ON data(areaid)";
try (Statement stmt = conn.createStatement()) {
stmt.execute(createIndexSql);
}
}
}

View File

@ -0,0 +1,173 @@
package com.yj.earth.common.util;
import lombok.extern.slf4j.Slf4j;
import org.sqlite.JDBC;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* POI数据按地区导出工具类
*/
@Slf4j
public class PoiExporter {
/**
* 根据地区名称导出对应区域及子区域的POI数据到新的SQLite文件
*/
public void exportPoiByArea(String areaName) throws SQLException {
log.info("=== 开始导出[{}]的POI数据 ===", areaName);
// 注册SQLite驱动
DriverManager.registerDriver(new JDBC());
String originalDbPath = "jdbc:sqlite:F:\\export.db";
String projectDir = System.getProperty("user.dir");
String newDbUrl = "jdbc:sqlite:" + projectDir + File.separator + areaName + ".poi";
String newDbPath = newDbUrl.replace("jdbc:sqlite:", "");
try {
log.info("正在查询[{}]及其子区域的地区码", areaName);
List<Integer> adcodeList = getAdcodeList(originalDbPath, areaName);
if (adcodeList.isEmpty()) {
log.error("未找到地区[{}]及其子区域的地区码数据、导出终止", areaName);
throw new IllegalArgumentException("未找到地区[" + areaName + "]及其子区域的地区编码数据");
}
log.info("查询完成、共获取到[{}]个地区码(含子区域)", adcodeList.size());
log.info("开始导出数据到新文件:{}", newDbPath);
exportDataToNewDb(originalDbPath, newDbUrl, adcodeList);
log.info("=== [{}]的POI数据导出成功文件路径{} ===", areaName, newDbPath);
} catch (SQLException e) {
log.error("=== [{}]的POI数据导出失败原因{} ===", areaName, e.getMessage(), e);
throw e;
}
}
/**
* 获取指定地区及其所有子区域的地区码列表
*/
private List<Integer> getAdcodeList(String dbUrl, String areaName) throws SQLException {
List<Integer> adcodeList = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(dbUrl)) {
// 查询根地区码
Integer rootAdcode = getRootAdcode(conn, areaName);
if (rootAdcode == null) {
log.warn("未查询到[{}]对应的根地区码", areaName);
return adcodeList;
}
log.info("查询到[{}]的根地区码:{}", areaName, rootAdcode);
// 递归查询所有子adcode
String recursiveSql = "WITH RECURSIVE sub_areas(adcode) AS (" +
" SELECT adcode FROM poi_area WHERE adcode = ?" +
" UNION ALL" +
" SELECT a.adcode FROM poi_area a " +
" JOIN sub_areas s ON a.parentid = s.adcode" +
") SELECT adcode FROM sub_areas";
try (PreparedStatement ps = conn.prepareStatement(recursiveSql)) {
ps.setInt(1, rootAdcode);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
adcodeList.add(rs.getInt("adcode"));
}
}
}
} catch (SQLException e) {
log.error("查询地区码列表失败!原因:{}", e.getMessage(), e);
throw e;
}
return adcodeList;
}
/**
* 获取地区名称对应的根地区码
*/
private Integer getRootAdcode(Connection conn, String areaName) throws SQLException {
String sql = "SELECT adcode FROM poi_area WHERE name = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, areaName);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return rs.getInt("adcode");
}
}
} catch (SQLException e) {
log.error("查询[{}]的根地区码失败!原因:{}", areaName, e.getMessage(), e);
throw e;
}
return null;
}
/**
* 将指定地区码列表的数据导出到新数据库(带数据处理进度日志)
*/
private void exportDataToNewDb(String originalDbUrl, String newDbUrl, List<Integer> adcodeList) throws SQLException {
try (Connection newConn = DriverManager.getConnection(newDbUrl)) {
log.info("正在创建新表[data](若已存在则删除)...");
newConn.createStatement().execute("DROP TABLE IF EXISTS \"data\"");
String createTableSql = "CREATE TABLE \"data\" (" +
"\"id\" INTEGER, " +
"\"lng\" REAL, " +
"\"lat\" REAL, " +
"\"name\" TEXT, " +
"\"areaid\" INTEGER" +
")";
newConn.createStatement().execute(createTableSql);
log.info("新表[data]创建成功");
try (Connection originalConn = DriverManager.getConnection(originalDbUrl)) {
String placeholders = String.join(",", Collections.nCopies(adcodeList.size(), "?"));
String querySql = "SELECT id, lng, lat, name, areaid FROM data WHERE areaid IN (" + placeholders + ")";
try (PreparedStatement queryPs = originalConn.prepareStatement(querySql);
PreparedStatement insertPs = newConn.prepareStatement(
"INSERT INTO data (id, lng, lat, name, areaid) VALUES (?, ?, ?, ?, ?)")) {
// 设置查询参数
for (int i = 0; i < adcodeList.size(); i++) {
queryPs.setInt(i + 1, adcodeList.get(i));
}
// 批量插入(带进度日志)
int batchSize = 5000;
int totalCount = 0; // 总处理数据量
try (ResultSet rs = queryPs.executeQuery()) {
while (rs.next()) {
insertPs.setInt(1, rs.getInt("id"));
insertPs.setDouble(2, rs.getDouble("lng"));
insertPs.setDouble(3, rs.getDouble("lat"));
insertPs.setString(4, rs.getString("name"));
insertPs.setInt(5, rs.getInt("areaid"));
insertPs.addBatch();
totalCount++;
// 每10000条打印一次进度
if (totalCount % batchSize == 0) {
insertPs.executeBatch();
log.info("已处理数据:{}条", totalCount);
}
}
// 处理剩余数据
if (totalCount % batchSize != 0) {
insertPs.executeBatch();
}
log.info("数据插入完成、共处理[{}]条POI数据", totalCount);
}
}
}
} catch (SQLException e) {
log.error("新数据库操作失败!原因:{}", e.getMessage(), e);
throw e;
}
}
public static void main(String[] args) {
try {
new PoiExporter().exportPoiByArea("兰州市");
} catch (SQLException e) {
e.printStackTrace();
}
}
}