Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. * Extract linework from input (if polys) with ST_Boundary.
  2. * Load each linestring from linework in separate transactions (or batches) with TopoGeo_AddLineString.
  3. * If multiple lineworks, load them each to a topo separately and start with the linework most wanted as a reference.
  4. * Load reference linework to a new topo.
  5. * Load first topo edge_data in separate transactions per edge, rollback if area created is smaller than wanted.
  6.  
  7. Create faces with original attributes using two steps.
  8.  
  9. 1. Step 1, create face geometry and point and indexes
  10.  
  11. drop table areas;
  12. create table areas as
  13. select
  14. ST_GetFacegeometry('topo', face_id) geom,
  15. ST_PointOnSurface(ST_GetFacegeometry('topo', face_id)) point
  16. from topo.face
  17. where face_id>0;
  18. CREATE INDEX ON areas USING gist (geom);
  19. CREATE INDEX ON areas USING gist (point);
  20.  
  21. 2. Step 2, left join in attributes
  22.  
  23. drop table areas_attr;
  24. create table areas_attr as
  25. select
  26. kommunkod,
  27. kommunnamn,
  28. distrkod,
  29. distrnamn,
  30. a.geom::geometry(Polygon, 3006)
  31. from areas a
  32. left join test.kommuner k on ST_Intersects(k.geom, a.point)
  33. left join test.distrikt d on ST_Intersects(d.geom, a.point);
  34. CREATE INDEX ON areas_attr USING gist (geom);
  35.  
  36. ### Batch topo loader
  37.  
  38. HikariDataSource ds = new HikariDataSource();
  39.  
  40. ds.setJdbcUrl("jdbc:postgresql://localhost/lm");
  41. ds.setUsername("postgres");
  42. ds.setPassword("postgres");
  43.  
  44. Connection connection = ds.getConnection();
  45.  
  46. Statement statement = connection.createStatement();
  47. statement.setFetchSize(100);
  48. ResultSet resultSet = statement.executeQuery("SELECT gid, geom FROM input");
  49.  
  50. Connection connection2 = null;
  51. PreparedStatement add = null;
  52. PreparedStatement error = null;
  53.  
  54. int count = 0;
  55. while (resultSet.next()) {
  56. logger.info("Processing row " + count);
  57.  
  58. if (count % 100 == 0) {
  59. if (connection2 != null) connection2.close();
  60. connection2 = ds.getConnection();
  61. add = connection2.prepareStatement("SELECT TopoGeo_AddLineString('topo1', ?, 0.5)");
  62. error = connection2.prepareStatement("INSERT INTO test.errors VALUES (?, ?)");
  63. }
  64.  
  65. Object geom = resultSet.getObject("geom");
  66. add.setObject(1, geom);
  67. try {
  68. add.execute();
  69. } catch (PSQLException e) {
  70. error.setInt(1, resultSet.getInt("gid"));
  71. error.setString(2, e.getMessage());
  72. error.execute();
  73. }
  74.  
  75.  
  76. if (count % 100 == 0) {
  77. connection2.createStatement().execute("VACUUM ANALYZE topo1.edge_data;");
  78. connection2.createStatement().execute("VACUUM ANALYZE topo1.face;");
  79. connection2.createStatement().execute("VACUUM ANALYZE topo1.node;");
  80. }
  81.  
  82. count++;
  83. }
  84. resultSet.close();
  85. statement.close();
  86. connection.close();
  87. ds.close();
  88.  
  89. ### Single edge loader with too small area rollback
  90.  
  91. static double getArea(Connection connection, int face_id) throws SQLException {
  92. ResultSet rs = connection.createStatement().executeQuery("select ST_Area(ST_GetFaceGeometry('topo2', " + face_id + "))");
  93. rs.next();
  94. double area = rs.getDouble(1);
  95. rs.close();
  96. return area;
  97. }
  98.  
  99. public static void main(String[] args) throws SQLException {
  100. HikariDataSource ds = new HikariDataSource();
  101.  
  102. ds.setJdbcUrl("jdbc:postgresql://localhost/lm");
  103. ds.setUsername("postgres");
  104. ds.setPassword("postgres");
  105.  
  106. Connection connection = ds.getConnection();
  107.  
  108. Statement statement = connection.createStatement();
  109. statement.setFetchSize(100);
  110. ResultSet resultSet = statement.executeQuery("SELECT edge_id, geom FROM topo1.edge_data");
  111.  
  112.  
  113. Connection connection2 = null;
  114. PreparedStatement add = null;
  115. PreparedStatement error = null;
  116.  
  117. int count = 0;
  118. while (resultSet.next()) {
  119. logger.info("Processing row " + count);
  120.  
  121. if (connection2 != null) connection2.close();
  122. connection2 = ds.getConnection();
  123. connection2.setAutoCommit(false);
  124.  
  125.  
  126. add = connection2.prepareStatement("SELECT TopoGeo_AddLineString('topo2', ?, 0.5)");
  127. error = connection.prepareStatement("INSERT INTO test.errors VALUES (?, ?)");
  128.  
  129. Object geom = resultSet.getObject("geom");
  130. add.setObject(1, geom);
  131. try {
  132. ResultSet trs = add.executeQuery();
  133. boolean tooSmall = false;
  134. while (trs.next()) {
  135. int edge_id = trs.getInt(1);
  136. ResultSet trs2 = connection2.createStatement().executeQuery("select left_face, right_face from topo2.edge_data where edge_id=" + edge_id);
  137. trs2.next();
  138. int left_face_id = trs2.getInt(1);
  139. int right_face_id = trs2.getInt(2);
  140. trs2.close();
  141. if (left_face_id > 0) {
  142. if (getArea(connection2, left_face_id) < 60000)
  143. tooSmall = true;
  144. }
  145. if (right_face_id > 0) {
  146. if (getArea(connection2, right_face_id) < 60000)
  147. tooSmall = true;
  148. }
  149. }
  150.  
  151. trs.close();
  152.  
  153. if (tooSmall) {
  154. logger.info("Too small area created (will roll back) ");
  155. connection2.rollback();
  156. } else {
  157. connection2.commit();
  158. }
  159. } catch (PSQLException e) {
  160. error.setInt(1, resultSet.getInt("edge_id"));
  161. error.setString(2, e.getMessage());
  162. error.execute();
  163. connection2.rollback();
  164. }
  165.  
  166. count++;
  167. }
  168. resultSet.close();
  169. statement.close();
  170. connection.close();
  171. ds.close();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement