如果数据库中的某条数据是经纬度的时候处理方法
来源:互联网 发布:命令行安装node 编辑:程序博客网 时间:2024/05/19 03:44
写一个工具类
1 连上PipeGisInfo库
2 查出所有表名
写一个函数 参数为 (point p1,point p2)-------------point是属性为经纬度的点
3 遍历每个表 判断每个表的是否有the_geom字段;如果有:遍历条数据 判断该the_geom的第一个点 是否在p1 p2之间;如果在 继续下次循环;如果不在,删除此条数据
这是我的工作任务,由于自己掌握的java技术不太强硬,所以难免有错误,请大家帮忙提问修改。
1/直接上代码,不解释。实体类有经纬度,因为数据库存储的就是the_geom xx(22.22 33.22)格式的
package com.entity;import java.io.Serializable;public class Point implements Serializable {/** * */private static final long serialVersionUID = -1602091081457600614L;private double lon; // 经度private double lat; // 维度public Point() {}public Point(double lon, double lat) {super();this.lon = lon;this.lat = lat;}public double getLon() {return lon;}public void setLon(double lon) {this.lon = lon;}public double getLat() {return lat;}public void setLat(double lat) {this.lat = lat;}}2.工具类 PropertiesUtil
package com.util;import java.io.IOException;import java.util.Properties;public class PropertiesUtil {private static Properties pro = null;public static Properties getProperties(){if(null == pro){pro = new Properties();}try {pro.load(PropertiesUtil.class.getResourceAsStream("/jdbc.properties"));} catch (IOException e) {e.printStackTrace();}return pro;}}工具类 SqlHelper
package com.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;public class SqlHelper {private static Properties pro = PropertiesUtil.getProperties();static{try {Class.forName(pro.getProperty("driverClassName"));} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection(){java.sql.Connection con = null;try {con = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("userName"),pro.getProperty("password"));} catch (SQLException e) { e.printStackTrace(); }return con;}}3.service层数据处理
package com.service;import java.sql.Connection;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import com.entity.Point;import com.util.SqlHelper;public class CheckService {public boolean checkPoint(Point p1, Point p2) {Connection con = SqlHelper.getConnection();Statement stmt = null;try {stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);// 获得所有的表信息ResultSet rsTable = con.getMetaData().getTables(null, null, null, new String[] { "TABLE" });while (rsTable.next()) { // 循环所有的表// 得到表名String tableName = rsTable.getString(3);boolean hasGeom = validateColumn(tableName, stmt);if (hasGeom) {String sql = "select *,ST_asText(the_geom) from \"" + tableName + "\"";ResultSet rs = stmt.executeQuery(sql);// 删除该表System.out.println("======================点的集合Start=======================");while (rs.next()) {String the_geom = rs.getString(1);boolean flag = validatePoint(p1, p2, the_geom);if (!flag) {// 如果第一点在两点之外则删除数据库中的数据rs.deleteRow();String sqldelete = "delete from \"" + tableName + "\" where ST_asText(the_geom) = " + the_geom;System.out.println(sqldelete);}}System.out.println("======================点的集合End=======================");}System.out.println("---------------------------------");}} catch (SQLException e) {e.printStackTrace();}return true;}/** * 判断点the_geom的第一个点是否在给定的两点之间 * * @param p1 * @param p2 * @param theGeom * @return */private boolean validatePoint(Point p1, Point p2, String theGeom) {Point p = getPointFromGeom(theGeom);if (p1.getLon() < p.getLon() && p2.getLon() > p.getLon() && p1.getLat() < p.getLat() && p2.getLat() > p.getLat()) {return true;}return false;}/** * 根据the_geom字符串的第一个点构造Point * * @param theGeom * @return */private Point getPointFromGeom(String theGeom) {// 首先判断theGeom的开头是(点线面)Point p = new Point();String firstPoint = null;if (null != theGeom) {/*数据格式 * POINT(1 1) MULTIPOINT(1 1, 3 4, -1 3) LINESTRING(1 1, 2 2, 3 4) * POLYGON((0 0, 0 1, 1 1, 1 0, 0 0)) MULTIPOLYGON((0 0, 0 1, 1 1, 1 * 0, 0 0), (5 5, 5 6, 6 6, 6 5, 5 5)) MULTILINESTRING((1 1, 2 2, 3 * 4),(2 2, 3 3, 4 5)) */if (theGeom.startsWith("MULTILINESTRING((")) {firstPoint = theGeom.substring("MULTILINESTRING((".length(), theGeom.indexOf(","));} else if (theGeom.startsWith("MULTIPOLYGON(((")) {firstPoint = theGeom.substring("MULTIPOLYGON(((".length(), theGeom.indexOf(","));} else if (theGeom.startsWith("POINT(")) {firstPoint = theGeom.substring("POINT(".length(), theGeom.indexOf(")"));} else if (theGeom.startsWith("MULTIPOINT(")) {firstPoint = theGeom.substring("MULTIPOINT(".length(), theGeom.indexOf(","));} else if (theGeom.startsWith("LINESTRING(")) {firstPoint = theGeom.substring("LINESTRING(".length(), theGeom.indexOf(","));} else if (theGeom.startsWith("POLYGON((")) {firstPoint = theGeom.substring("POLYGON((".length(), theGeom.indexOf(","));}String[] firstPointArr = firstPoint.split(" ");p.setLon(Double.parseDouble(firstPointArr[0]));p.setLat(Double.parseDouble(firstPointArr[1]));}return p;}private boolean validateColumn(String tableName, Statement stmt) {boolean hasGeom = false;try {String sql = "select * from \"" + tableName + "\"";ResultSet rs;rs = stmt.executeQuery(sql);ResultSetMetaData meta = rs.getMetaData();int count = meta.getColumnCount();// 表是否有the_geom字段for (int i = 0; i < count; i++) {String colName = meta.getColumnName(i + 1);if ("the_geom".equalsIgnoreCase(colName)) {hasGeom = true;break;}}} catch (SQLException e) {e.printStackTrace();}return hasGeom;}}4.进行测试
package com.test;import com.entity.Point;import com.service.CheckService;public class Test {public static void main(String args[]) {CheckService cs = new CheckService();Point p1 = new Point(111.112804,36.7943374);Point p2 = new Point(121.102804,39.7243374);cs.checkPoint(p1, p2);}}ps:个人觉得还不错,自己的技术得到了很大的提升,因为里面的知识点很多,任何一个部分都基本上是一个知识点,希望大家能认真阅读。
大神求别笑。
0 0
- 如果数据库中的某条数据是经纬度的时候处理方法
- Spark中的DataFrame的getAs方法如果取到的值是null的处理
- SQLite数据库多条数据的处理
- AS导入文件时候如果是乱码怎么处理
- 后台传过来的JSON数据里的date传到前台的时候显示的是【Object object】 处理方法
- 按批次处理list数据的两种方法(list每1000条存入数据库)
- 怎么判断数据库中的某条日期的数据为某月的第一条信息
- 场景:第一张 有3条数据 第二张表有21条数据 如果得到两张表的总数据呢 总数据是 24条
- 删除数据库表中的重复冗余记录,以及随机取N条数据的方法
- mdf数据库附加的时候被拒绝的处理方法
- 在数据库中有相同数据时候的清理方法
- 一条查询某一条数据是整个表中的第几条数据的SQL
- 网络请求的时候如果是中文需要转码
- 做mvc+EF的项目中如果你用的是code first,当更改某实体类时,你会发现数据库中的数据没了,该怎么做?
- mybatis操作数据库是如果mapper文件中的id和java代码中的参数名字不同是报的错
- C#一次性向数据库插入上万条数据的方法
- C#一次性向数据库插入上万条数据的方法
- 枚举类型在数据库中的处理(查询的时候如何处理)
- SIP协议&开源SIP服务器搭建和客户端安装
- hdu1162
- android项目打开之后出现大红色感叹号的问题
- java split的一些用法
- Oracle索引学习
- 如果数据库中的某条数据是经纬度的时候处理方法
- ProgressBar 样式自定义
- Android 最火的快速开发框架XUtils
- POJ 2586 Y2K Accounting Bug
- 浅析ControlTemplate类
- 服务器结构
- 判断当前窗口是否是全屏的山寨版和官方版
- Oracle创建用户并给用户授权查询指定表或视图的权限
- 2014Esri全球用户大会之位置分析