/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
* 2010-05-05 Jed Young Creation
*/
package chunmap.data.provider.postgis;
import java.sql.ResultSet;
import chunmap.data.ChunMapDataException;
import chunmap.data.feature.FeatureSchama;
import chunmap.data.feature.LayerMetadata;
import chunmap.data.provider.FeatureDataReader;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryType;
import chunmap.model.geom.WktReader;
/**
* @author yangjiandong
*
*/
public class PostgisDataSource implements FeatureDataReader {
private String tableName;
private String geomColumn;
private String IdColumn;
private PostgisConnection pgSql;
private WktReader wkt = new WktReader();
public PostgisDataSource(String tableName, String geomColumn,
String idColumn, PostgisConnection pgSql) {
super();
this.tableName = tableName;
this.geomColumn = geomColumn;
IdColumn = idColumn;
this.pgSql = pgSql;
}
//--------------------------------------------------------------------
@Override
public Envelope getEnvelop() {
String sql = "SELECT EXTENT(" + geomColumn + ") FROM " + tableName;
String str = pgSql.executeScalar(sql).toString();
Envelope ep = wkt.readEnvelop(str);
return ep;
}
public Geometry getGeometry(long id) {
String sql = "SELECT Astext(" + geomColumn + ") FROM " + tableName
+ " WHERE " + IdColumn + "=" + id;
String str = pgSql.executeScalar(sql).toString();
return wkt.read(str);
}
private GeometryType GetShapeType()
{
String sql = "SELECT GeometryType(" + geomColumn + ") FROM " + tableName;
String str = pgSql.executeScalar(sql).toString();
if (str.equals("POINT"))
return GeometryType.Point;
else if (str.equals("LINESTRING"))
return GeometryType.LineString;
else if (str.equals("POLYGON"))
return GeometryType.Polygon;
else if (str.equals("MULTIPOINT"))
return GeometryType.MultiPoint;
else if (str.equals("MULTILINESTRING"))
return GeometryType.MultiLineString;
else if (str.equals("MULTIPOLYGON"))
return GeometryType.MultiPolygon;
else
throw new ChunMapDataException("不能识别的类型");
}
@Override
public Envelope getShapeEnvelop(long id) {
String sql = "SELECT EXTENT(" + geomColumn + ") FROM " + tableName
+ " WHERE " + IdColumn + "=" + id;
String str = pgSql.executeScalar(sql).toString();
Envelope ep = wkt.readEnvelop(str);
return ep;
}
@Override
public long size() {
String sql = "Select count(*) from " + tableName;
return Long.valueOf(pgSql.executeScalar(sql).toString());
}
@Override
public void close() {
pgSql.close();
}
@Override
public Object[] data(long index) {
//String sql = "Select * from " + tableName + " " + where;
//return pgSql.executeScalar(sql);
return null;
}
@Override
public FeatureSchama getFeatureSchama() {
// TODO Auto-generated method stub
return null;
}
@Override
public LayerMetadata getMetadata() {
LayerMetadata meta = new LayerMetadata(tableName,GetShapeType());
return meta;
}
@Override
public ResultSet query(String where) {
String sql = "Select * from " + tableName + " " + where;
return pgSql.executeScalar(sql);
}
}