GeoJSON
1 parent 9470dbf commit 483a5e5cfe7b3a6e4425548554a20871a4e715c9
@hayashi hayashi authored on 3 Nov 2017
Showing 1 changed file
View
73
src/osm/jp/coverage/busstop/ToGeoJSON.java 0 → 100644
package osm.jp.coverage.busstop;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
import jp.co.areaweb.tools.database.DatabaseTool;
 
/**
* PostGISデータをGeoJSONファイルに出力する。
* 出力ファイル名: "busstop.json"
* テーブル名: t_busstop
* fixed OSMの周辺に存在するかどうか、存在しない場合は0,存在する場合は1。ブランド有りは50。
* geom PostGIS形式の位置情報(4612:)
* SELECT row_to_json(feature) FROM (
select 'Feature' As type, ST_AsGeoJSON(ST_Transform(t_busstop.geom,4326))::json As geometry, row_to_json((
SELECT p FROM (SELECT t_busstop.idref, t_busstop.fixed) AS p)) AS properties From t_busstop) As feature
*
* @author yuu
*
*/
public class ToGeoJSON {
public static void main (String[] argv) throws Exception {
Connection con = DatabaseTool.openDb("postgis");
outputDb(con, "WHERE fixed=0", new File("busstop0.json"));
outputDb(con, "WHERE fixed=1", new File("busstop1.json"));
outputDb(con, "WHERE fixed>1", new File("busstop2.json"));
}
 
public static void outputDb(Connection con, String whereStr, File outputFile) throws Exception {
try (BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"))) {
String line = "{" +
"\"type\": \"FeatureCollection\"," +
"\"features\": [";
System.out.println(line);
ow.write(line);
ow.newLine();
 
PreparedStatement ps8 = con.prepareStatement("SELECT row_to_json(feature) FROM ("
+ "select 'Feature' As type, ST_AsGeoJSON(ST_Transform(t_busstop.geom,4326))::json As geometry From t_busstop "+ whereStr +" order by area,gmlid) As feature");
try (ResultSet rset8 = ps8.executeQuery()) {
boolean top = true;
while (rset8.next()) {
if (top) {
top = false;
}
else {
line = ",";
System.out.println(line);
ow.write(line);
ow.newLine();
}
line = rset8.getString(1);
System.out.print(line);
ow.write(line);
}
}
System.out.println();
ow.newLine();
line = "]}";
System.out.println(line);
ow.write(line);
ow.newLine();
ow.flush();
}
}
}