Newer
Older
osmCoverage / src / osm / jp / postgis / ToGeoJSON.java
@hayashi hayashi on 28 Jan 2018 3 KB GML_POSTOFFICE
  1. package osm.jp.postgis;
  2.  
  3. import osm.jp.coverage.postoffice.*;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11.  
  12. import jp.co.areaweb.tools.database.DatabaseTool;
  13.  
  14. /**
  15. * PostGISデータをGeoJSONファイルに出力する。
  16. * 出力ファイル名: "fuel.json"
  17. * テーブル名: t_fuel
  18. * fixed OSMの周辺に存在するかどうか、存在しない場合は0,存在する場合は1。ブランド有りは50。
  19. * geom PostGIS形式の位置情報(4612:)
  20. * SELECT row_to_json(feature) FROM (
  21. select 'Feature' As type, ST_AsGeoJSON(ST_Transform(t_fuel.geom,4326))::json As geometry, row_to_json((
  22. SELECT p FROM (SELECT t_fuel.idref, t_fuel.fixed) AS p)) AS properties From t_fuel) As feature
  23. *
  24. * @author yuu
  25. *
  26. */
  27. public class ToGeoJSON {
  28. public static void main (String[] argv) throws Exception {
  29. boolean slim = false;
  30. for (String arg : argv) {
  31. if (arg.equals("-slim")) {
  32. slim = true;
  33. }
  34. }
  35. ToGeoJSON obj = new ToGeoJSON(DbPostoffice.TABLE_NAME);
  36. Connection con = DatabaseTool.openDb("postgis");
  37. try {
  38. obj.outputDb(con, "", new File("postoffice.json"), false);
  39. obj.outputDb(con, "WHERE fixed=0", new File("postoffice0.json"), slim);
  40. obj.outputDb(con, "WHERE fixed>0 and fixed<50", new File("postoffice1.json"), slim);
  41. obj.outputDb(con, "WHERE fixed>49", new File("postoffice2.json"), slim);
  42. }
  43. finally {
  44. DatabaseTool.closeDb(con);
  45. }
  46. }
  47. public String tableName;
  48. public ToGeoJSON(String tableName) {
  49. this.tableName = tableName;
  50. }
  51.  
  52. public void outputDb(Connection con, String whereStr, File outputFile, boolean slim) throws Exception {
  53. try (BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"))) {
  54. String sql = "SELECT row_to_json(feature) FROM ("
  55. + "select 'Feature' As type, "
  56. + "ST_AsGeoJSON(ST_Transform(t_"+ this.tableName +".geom,4326))::json As geometry, row_to_json(("
  57. + "SELECT p FROM (SELECT t_"+ this.tableName +".idref, t_"+ this.tableName +".fixed) AS p"
  58. + ")) AS properties From t_"+ this.tableName +" "+ whereStr +" order by area,idref) As feature";
  59. if (slim) {
  60. sql = "SELECT row_to_json(feature) FROM ("
  61. + "select 'Feature' As type, ST_AsGeoJSON(ST_Transform(t_"+ this.tableName +".geom,4326))::json As geometry From t_"+ this.tableName +" "+ whereStr +" order by area,idref) As feature";
  62. }
  63. System.out.println(sql);
  64.  
  65. String line = "{" +
  66. "\"type\": \"FeatureCollection\"," +
  67. "\"features\": [";
  68. System.out.println(line);
  69. ow.write(line);
  70. ow.newLine();
  71.  
  72. PreparedStatement ps8 = con.prepareStatement(sql);
  73. try (ResultSet rset8 = ps8.executeQuery()) {
  74. boolean top = true;
  75. while (rset8.next()) {
  76. if (top) {
  77. top = false;
  78. }
  79. else {
  80. line = ",";
  81. System.out.println(line);
  82. ow.write(line);
  83. ow.newLine();
  84. }
  85. line = rset8.getString(1);
  86. System.out.print(line);
  87. ow.write(line);
  88. }
  89. }
  90. System.out.println();
  91. ow.newLine();
  92. line = "]}";
  93. System.out.println(line);
  94. ow.write(line);
  95. ow.newLine();
  96. ow.flush();
  97. }
  98. }
  99. }