SHCJ2BDJW.java 2.84 KB
package com.bsth.util.Geo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.bsth.util.JWDUtil;
import com.bsth.util.TransGPS;
import com.bsth.util.TransGPS.Location;
import com.bsth.util.db.DBUtils_MS;

public class SHCJ2BDJW {
	
	public static void main(String[] args) {

		Connection conn = null;
		
		PreparedStatement ps = null;
		
		ResultSet rs = null;
		
		String sqlSelect = "SELECT b.SHAPESTRING as SHAPE , b.id as ID FROM jjwgps_t_gjldb b where  SHAPESTRING is not null order by b.ldbh asc  ";
		
		String sqlUpdate = "UPDATE jjwgps_t_gjldb SET bdjw = GeomFromText(?),SHAPE = GeomFromText(?) where id = ?";
		
		List<Map<String, Object>> list = new ArrayList<>();
		
		try {
			conn = DBUtils_MS.getConnection();
			
			ps = conn.prepareStatement(sqlSelect);
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				
				Map<String, Object> map = new HashMap<String, Object>();
				
				map.put("shape", rs.getString("SHAPE"));
				
				map.put("id", rs.getInt("ID"));
				
				list.add(map);
			}
			
			for(int i =0;i<list.size();i++) {
				
				Map<String, Object> temp = list.get(i);
				
				String lineString = temp.get("shape").toString();
				/*String lineString = "LINESTRING  (13532.5305161702 -3677.63275264995, 13433.22401617 -3881.74765264988)";*/
				
				
				String arrayP[] = lineString.substring(lineString.indexOf("(")+1, lineString.length()-1).split(", ");
				
				int id = Integer.parseInt(temp.get("id").toString());
				String geometry = "";
				
				for(int k =0;k<arrayP.length;k++) {
					
					String arrayXY[] = arrayP[k].split(" ");
					
					Double lng = Double.parseDouble(arrayXY[0]);
					
					Double lat = Double.parseDouble(arrayXY[1]);
					
					Map map_2 = JWDUtil.ConvertSHToJW(lng,lat);
					
					Location location = TransGPS.LocationMake(Float.parseFloat(map_2.get("x").toString()), Float.parseFloat(map_2.get("y").toString()));
					
					location = TransGPS.bd_encrypt(TransGPS.transformFromWGSToGCJ(location));
					
					if(k==arrayP.length-1)
						geometry = geometry + (location.getLng()+0.000727) + " " + (location.getLat() - 0.0000624);
					else 
						geometry = geometry + (location.getLng()+0.000727) + " " + (location.getLat() - 0.0000624) + ",";
						
					
				}
				
				ps = null;
				
				geometry = "LINESTRING(" + geometry +")";
				
				ps = conn.prepareStatement(sqlUpdate);
				
				ps.setString(1, geometry);
				
				ps.setString(2, lineString);
				
				ps.setInt(3, id);
				
				int stauts = ps.executeUpdate();
				
			}
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}