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