Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import GeometryClasses.Feature;
  2. import GeometryClasses.FeatureCollection;
  3. import GeometryClasses.Geometry;
  4. import GeometryClasses.Utils;
  5. import oracle.spatial.geometry.JGeometry;
  6. import oracle.sql.STRUCT;
  7.  
  8. import java.io.IOException;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14. import java.util.Map;
  15.  
  16.  
  17. public class DBFeatureCollectionInserter {
  18.  
  19. private static String url = "jdbc:oracle:thin:@//10.100.100.144:1521/develdb.napr.local";
  20. private static String username = "navigation";
  21. private static String password = "test";
  22.  
  23. static {
  24. try {
  25.  
  26. Class.forName("oracle.jdbc.driver.OracleDriver");
  27.  
  28. } catch (ClassNotFoundException e) {
  29.  
  30. System.out.println("Where is your Oracle JDBC Driver?");
  31. e.printStackTrace();
  32.  
  33. }
  34. }
  35.  
  36. private static Connection getConnection(){
  37. try {
  38. Connection connection = DriverManager.getConnection(url, username, password);
  39. return connection;
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. return null;
  44. }
  45.  
  46. public static void insertRoadsInDB(FeatureCollection roadFeatures){
  47.  
  48. Connection connection = null;
  49. List<Feature> featureList = roadFeatures.getFeatures();
  50. int k = 0;
  51. while (k < featureList.size()) {
  52. connection = getConnection();
  53.  
  54. if (connection != null) {
  55.  
  56. Feature curFeature = featureList.get(k);
  57. insertRoadGeometry(curFeature.getGeometry(), connection, k);
  58.  
  59. System.out.println("Inserted Road: " + k);
  60. k++;
  61.  
  62. try {connection.close();} catch (SQLException e) {e.printStackTrace();}
  63. }
  64. }
  65. k = 0;
  66.  
  67. while (k < featureList.size()) {
  68.  
  69.  
  70. connection = getConnection();
  71.  
  72. if (connection != null) {
  73.  
  74. Feature curFeature = featureList.get(k);
  75. insertRoadProperties(curFeature.getProperties(), connection, k);
  76.  
  77. System.out.println("Inserted Propertie: " + k);
  78. k++;
  79.  
  80. try {connection.close();} catch (SQLException e) {e.printStackTrace();}
  81. }
  82. }
  83. }
  84.  
  85. private static void insertRoadProperties(Map<String, Object> properties, Connection connection, int id) {
  86. try (PreparedStatement ps = connection.prepareStatement("INSERT INTO ROAD_PROPERTIES VALUES (?, ?, ?, 0, 3, NULL, NULL, ?, ?)")) {
  87. ps.setInt(1, id);
  88. ps.setString(2, (String) properties.get("RoadObID"));
  89. ps.setString(3, (String) properties.get("RoadGrID"));
  90. ps.setString(4, (String) properties.get("RoadsName"));
  91. ps.setInt(5, ((Double) properties.get("RoadCount")).intValue());
  92. ps.executeUpdate();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96.  
  97. }
  98.  
  99. private static void insertRoadGeometry(Geometry g, Connection connection, int id) {
  100. JGeometry geometry = Utils.geometryToJGeometry(g);
  101. String updateQuery = "INSERT INTO road_geometries VALUES (" + id + ",?) ";
  102. try (PreparedStatement ps = connection.prepareStatement(updateQuery)) {
  103. STRUCT obj = null;
  104. try {
  105. obj = JGeometry.store(connection, geometry);
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. ps.setObject(1, obj);
  110. ps.executeUpdate();
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115.  
  116. public static void insertGridsInDB(FeatureCollection gridFeatures){
  117.  
  118. Connection connection = null;
  119.  
  120. List<Feature> featureList = gridFeatures.getFeatures();
  121. int k = 0;
  122. while (k < featureList.size()) {
  123.  
  124.  
  125. connection = getConnection();
  126.  
  127. if (connection != null) {
  128.  
  129. Feature curFeature = featureList.get(k);
  130. insertGridGeometry(curFeature.getGeometry(), connection, k);
  131.  
  132. System.out.println("Inserted Geometry: " + k);
  133. k++;
  134.  
  135. try {connection.close();} catch (SQLException e) {e.printStackTrace();}
  136. }
  137. }
  138. k = 0;
  139. while (k < featureList.size()) {
  140.  
  141.  
  142. connection = getConnection();
  143.  
  144. if (connection != null) {
  145.  
  146. Feature curFeature = featureList.get(k);
  147. insertGridProperties(curFeature.getProperties(), connection, k);
  148.  
  149. System.out.println("Inserted Propertie: " + k);
  150. k++;
  151.  
  152. try {connection.close();} catch (SQLException e) {e.printStackTrace();}
  153. }
  154. }
  155. }
  156.  
  157.  
  158. public static void insertGridGeometry(Geometry g, Connection connection, int id){
  159.  
  160. JGeometry geometry = Utils.geometryToJGeometry(g);
  161. String updateQuery = "INSERT INTO grid_geometries VALUES (" + id + ",?) ";
  162. try (PreparedStatement ps = connection.prepareStatement(updateQuery)) {
  163. STRUCT obj = null;
  164. try {
  165. obj = JGeometry.store(connection, geometry);
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. }
  169. ps.setObject(1, obj);
  170. ps.executeUpdate();
  171. } catch (SQLException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175.  
  176. public static void insertGridProperties(Map<String, Object> properties, Connection connection, int id){
  177.  
  178. String updateQuery = "INSERT INTO GRID_PROPERTIES VALUES (?, ?, 0, 3, NULL, NULL, ?, ?)";
  179. //TODO insert parameters
  180. try (PreparedStatement ps = connection.prepareStatement(updateQuery)) {
  181. ps.executeUpdate();
  182. } catch (SQLException e) {
  183. e.printStackTrace();
  184. }
  185. }
  186.  
  187. public static void main(String[] args) throws IOException {
  188. // insertGridsInDB(GeoJsonReader.readGeoJson("/home/kokadva/IdeaProjects/GeoJsonUploader/src/main/resources/grid.json"));
  189. insertRoadsInDB(GeoJsonReader.readGeoJson("/home/kokadva/IdeaProjects/GeoJsonUploader/src/main/resources/roads.geojson"));
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement