Newer
Older
osmCoverage / src / osm / jp / postgis / PostgisItems.java
@yuu yuu on 24 Dec 2018 5 KB function: UnMapped.
package osm.jp.postgis;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class PostgisItems extends ArrayList<PostgisItem> {
    String geom = "";
    String gmlid = "";
    String idref = "";
    int area = 0;
    int fixed = 0;
    int code = 0;
    double lat = 0.0D;
    double lon = 0.0D;
    
    public static void main() {
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
        PostgisItems items = new PostgisItems();
        items.add(new PostgisItem("gmlid", "gmlid"));
        items.add(new PostgisItem("idref", "idref"));
        items.add(new PostgisItem("area", "area"));
        items.add(new PostgisItem("fixed", "fixed1"));
        items.add(new PostgisItem("code", "code"));
    }
    
    public PostgisItems() {
        super();
    }

    public void setResuit(ResultSet rset) throws SQLException {
        for (PostgisItem item : this) {
            switch (item.postName) {
                case "gmlid":
                    gmlid = rset.getString(item.localName);
                    break;
                case "idref":
                    idref = rset.getString(item.localName);
                    break;
                case "area":
                    area = rset.getInt(item.localName);
                    break;
                case "fixed":
                    fixed = rset.getInt(item.localName);
                    break;
                case "code":
                    code = rset.getInt(item.localName);
                    break;
                default:
                    break;
            }
        }
        lat = rset.getDouble("lat");
        lon = rset.getDouble("lon");
    }
    
    public void setPostResuit(ResultSet rset) throws SQLException {
        for (PostgisItem item : this) {
            switch (item.postName) {
                case "gmlid":
                    gmlid = rset.getString(item.postName);
                    break;
                case "idref":
                    idref = rset.getString(item.postName);
                    break;
                case "area":
                    area = rset.getInt(item.postName);
                    break;
                case "fixed":
                    fixed = rset.getInt(item.postName);
                    break;
                case "code":
                    code = rset.getInt(item.postName);
                    break;
                case "geom":
                    geom = rset.getString(item.postName);
                    break;
                case "lat":
                    lat = rset.getDouble(item.postName);
                    break;
                case "lon":
                    lon = rset.getDouble(item.postName);
                    break;
                default:
                    break;
            }
        }
    }

    public String getSqlStr(String tableName) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        for (PostgisItem item : this) {
            sb.append(item.localName);
            sb.append(",");
        }
        sb.append("lat,lon FROM ");
        sb.append(tableName);
        return sb.toString();
    }

    public String getValue() {
        StringBuilder sb = new StringBuilder();
        boolean top = true;
        for (PostgisItem item : this) {
            if (top) {
                top = false;
            }
            else {
                sb.append(",");
            }
            switch (item.postName) {
                case "gmlid":
                    sb.append(gmlid);
                    break;
                case "idref":
                    sb.append(idref);
                    break;
                case "area":
                    sb.append(area);
                    break;
                case "fixed":
                    sb.append(fixed);
                    break;
                case "code":
                    sb.append(code);
                    break;
                case "geom":
                    sb.append(geom);
                    break;
                case "lat":
                    sb.append(lat);
                    break;
                case "lon":
                    sb.append(lon);
                    break;
                default:
                    break;
            }
        }
        return sb.toString();
    }
    
    public String getPostSqlStr(String tableName) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        boolean top = true;
        for (PostgisItem item : this) {
            if (top) {
                top = false;
            }
            else {
                sb.append(",");
            }
            switch (item.postName) {
                case "lat":
                    sb.append("ST_Y(geom) AS lat");
                    break;
                case "lon":
                    sb.append("ST_X(geom) AS lon");
                    break;
                default:
                    sb.append(item.postName);
                    break;
            }
        }
        sb.append(" FROM t_");
        sb.append(tableName);
        sb.append(" ORDER BY area, gmlid");
        return sb.toString();
    }
    
    public String getCsvHeader() {
        StringBuilder sb = new StringBuilder();
        boolean top = true;
        for (PostgisItem item : this) {
            if (top) {
                top = false;
            }
            else {
                sb.append(",");
            }
            sb.append(item.postName);
        }
        return sb.toString();
    }
    
    public String getGeomStr() {
        return String.format(
            "ST_GeomFromText('POINT(%s %s)', 4612)", 
            BigDecimal.valueOf(lon).toPlainString(), 
            BigDecimal.valueOf(lat).toPlainString()
        );
    }
}