Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.40 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ohdmrenderfilterpolygons;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author michael
  20.  */
  21. public class OhdmRenderFilterPolygons {
  22.    private static final int AMOUNT_THREADS = 16;
  23.    
  24.     public static Connection connectDB( String DB) throws SQLException {
  25.        
  26.         try {
  27.             Class.forName("org.postgresql.Driver");
  28.         } catch (ClassNotFoundException e) {
  29.             System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
  30.             e.printStackTrace();
  31.             return null;
  32.         }
  33.  
  34.     //System.out.println("PostgreSQL JDBC Driver Registered!");
  35.  
  36.     Connection conn = null;
  37.        
  38.         try{
  39.             conn = DriverManager.getConnection(DB, "*****", "********" );
  40.             return conn;
  41.         }catch(SQLException e){
  42.             System.out.println("Connection Failed! Check output console");
  43.             e.printStackTrace();
  44.             return conn;
  45.         }    
  46.     }
  47.    
  48.  
  49.     /**
  50.      * @param args the command line arguments
  51.      * @throws java.sql.SQLException
  52.      * @throws java.lang.InterruptedException
  53.      */
  54.     public static void main(String[] args) throws SQLException, InterruptedException {
  55.        
  56.         final Counter counterNumber = new Counter();
  57.         long timeStart;
  58.         long timeEnd;
  59.         //final ProgressBar progressbar = new ProgressBar();
  60.        
  61.         timeStart = System.currentTimeMillis();
  62.  
  63.  
  64.  
  65.         System.out.println("Rendering Filter start...");
  66.        
  67.        
  68.         Runnable runnable;
  69.         runnable = new Runnable() {
  70.            @Override
  71.            public void run() {
  72.                while(counterNumber.getCheck()){
  73.                    String db_ohdm = "jdbc:postgresql://ohm.f4.htw-berlin.de:5432/ohdm";
  74.                    String db_rendering = "jdbc:postgresql://ohm.f4.htw-berlin.de:5432/ohdm_rendering";
  75.                    String key = "";
  76.                    Statement st = null;
  77.                    int counterFrom  = counterNumber.getCounterFrom();
  78.                    int counterTo    = counterNumber.getCounterTo();
  79.                    //System.out.println("Connect to DB: " + db);
  80.                    
  81.                    Connection conn = null;
  82.                    try {
  83.                        conn = connectDB(db_ohdm);
  84.                    } catch (SQLException ex) {
  85.                        Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  86.                    }
  87.                    
  88.                    if (conn != null) {
  89.                        //System.out.println("Database connection successful");
  90.                    } else {
  91.                        System.out.println("Failed to make connection!");
  92.                    }
  93.                    
  94.                    Statement stmt = null;
  95.                    try {
  96.                        stmt = conn.createStatement();
  97.                    } catch (SQLException ex) {
  98.                        Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  99.                    }
  100.                    
  101.                    
  102.                    String sql =    "SELECT ST_AsBinary(mp.multipolygon_geom) as geometrie, gt.key as key, gt.value as value, gt.valid_until as until, gt.valid_since as since "                              
  103.                            +   "FROM ohdm.multipolygon_geom mp "
  104.                            +   "JOIN ohdm.geographic_geom_dates gd ON gd.id_multipolygon_geom = mp.id "
  105.                            +   "JOIN ohdm.geographic_objects gobjects  ON gobjects.id = gd.id_geographic_object_source "
  106.                            +   "JOIN ohdm.geographic_tag gt ON gt.id_geographic_objects = gobjects.id "
  107.                            +   "WHERE   mp.id > " + counterFrom + " and mp.id < " + counterTo + " "
  108.                            +   "AND gt.key in ('admin_level', 'boundary', 'highway', 'landuse', 'natural')" ;
  109.                    
  110.                    
  111.                    ResultSet rs = null;
  112.                    try {
  113.                        rs = stmt.executeQuery(sql);
  114.                    } catch (SQLException ex) {
  115.                        Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  116.                    }
  117.                    try {
  118.                        conn.close();
  119.                    } catch (SQLException ex) {
  120.                        Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  121.                    }
  122.                    
  123.                    try {
  124.                    conn = connectDB(db_rendering);
  125.                    } catch (SQLException ex) {
  126.                    Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  127.                    }
  128.                    
  129.                    if (conn != null) {
  130.                    //System.out.println("Database connection successful");
  131.                    } else {
  132.                    System.out.println("Failed to make connection!");
  133.                    }
  134.                    try {
  135.                    stmt = conn.createStatement();
  136.                    } catch (SQLException ex) {
  137.                    Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  138.                    }
  139.                    
  140.                    
  141.                    
  142.                    try {
  143.                     while(rs.next()){
  144.                          sql = "SELECT id FROM public.polygons WHERE way = ST_GeomFromWKB('" + rs.getString("geometrie") + "') LIMIT 1";
  145.  
  146.                          ResultSet rs_select = null;
  147.                          try {
  148.                              rs_select = stmt.executeQuery(sql);
  149.                          } catch (SQLException ex) {
  150.                              Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  151.                          }
  152.                          if(rs.getString("key").matches("natural")){
  153.                                   key = "natural_";
  154.                               }else {
  155.                                   key = rs.getString("key");
  156.                               }
  157.                          if(rs_select.isBeforeFirst()){
  158.                               //System.out.println("Update");
  159.                              
  160.                               sql =    "UPDATE public.polygons SET " + key +""
  161.                                     + " = '" + rs.getString("value") + "' WHERE way = ST_GeomFromWKB('" + rs.getString("geometrie") + "')";
  162.                               //System.out.println(sql);
  163.                               st = conn.createStatement();
  164.                               conn.setAutoCommit(false);
  165.                               st.executeUpdate(sql);
  166.                               conn.commit();
  167.                          }else{
  168.                               //System.out.println("INSERT");
  169.                               conn.setAutoCommit(true);
  170.                               sql =    "INSERT INTO public.polygons (" + key + ", way, since, until)"
  171.                                     + " VALUES (?, ST_GeomFromWKB('" + rs.getString("geometrie") + "'), ?, ?)";    
  172.  
  173.                              
  174.                               PreparedStatement pst = conn.prepareStatement(sql);
  175.                               pst.setString(1, rs.getString("value"));
  176.                               pst.setDate(2, rs.getDate("since"));
  177.                               pst.setDate(3, rs.getDate("until"));
  178.                               //System.out.println(pst.toString());
  179.                               pst.executeUpdate();  
  180.                          }
  181.                          rs_select.close();
  182.                     }  } catch (SQLException ex) {
  183.                     Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  184.                     }
  185.                  
  186.                    try {
  187.                    rs.close();
  188.                    } catch (SQLException ex) {
  189.                    Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  190.                    }
  191.                    
  192.                    try {
  193.                    conn.close();
  194.                    } catch (SQLException ex) {
  195.                    Logger.getLogger(OhdmRenderFilterPolygons.class.getName()).log(Level.SEVERE, null, ex);
  196.                    }
  197.                    
  198.                    //System.out.println("connection closed");
  199.                    //System.out.println("connection closed");
  200.                }
  201.            }
  202.        };
  203.        
  204.         for (int i = 0; i < AMOUNT_THREADS; i++) {
  205.             new Thread(runnable).start();
  206.         }
  207.        
  208.         while (counterNumber.getCheck()) {
  209.            //progressbar.update(counterNumber.getCounter(), 198623546);
  210.            System.out.printf(counterNumber.getCounter() + " von 198623546 erledigt");
  211.         }
  212.         timeEnd = System.currentTimeMillis();
  213.         System.out.println("\nZeit benötigt: " + (((timeEnd - timeStart)/1000))/60 + " min");
  214.         System.out.println("Rendering Filter Stop...");
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement