终于搞定GeoTools对PostGis的操作
唉,这几天撒事情都没有做,就搞定了GeoTools的一些bug的删除。2.3版本问题比较多,上次在GT上和Richard聊天,他也对GeoTools目前的状况表示很是不满,但是为了PostGis的推广更顺利(毕竟,大多数使用Postgis的兄台还是在geotools平台上),很多时候很多事情都不是我们能决定的。。。所以只能自己动手去将GeoTools在文档中没有说清楚的,或者无法编译的程序根据新的API从新做一次。
这几天搞定了GeoTools的PostgisDataStore操作。包含连接、读取、新建、插入等等。其实很多功能比如新建和插入都可以用SQL语句去完成,不过这样做对postgis即将推出的空间索引还是有很大影响的。所以我们没有使用SQL去完成这些工作。
目前对GeoTools真的有些失望。。。不过作为一个开源中间件,它做到目前这个地位真的除了它推出比较早以外,还得感谢开源社区的帮助,2006年10月份推出的2.2版本其中对WMS中图层的渲染效率的大幅提升就是得益与Richard兄台的绝妙建议。自己也希望能向Richard学习,一个43岁的丹麦程序员,真的让人真的体会到什么叫“Open Mind”。。。过年期间的事情也有了眉目,着力修正shp2pqsql的问题。目前随post安装版推出的这个程序还是有些bug,在中文编码以及容错方面都需要提高。
这几天搞定了GeoTools的PostgisDataStore操作。包含连接、读取、新建、插入等等。其实很多功能比如新建和插入都可以用SQL语句去完成,不过这样做对postgis即将推出的空间索引还是有很大影响的。所以我们没有使用SQL去完成这些工作。
/**/
/*
* POSTGEO
*/
package com.geotools.test;
/** */ /**
*
* CopyRight (C) All rights reserved.
* <p>
*
* WuHan Inpoint Information Technology Development,Inc.
* <p>
*
* Author sinoly
* <p>
* Project Name: PostGeo
*
* @version 1.0 2006-11-13
*
* <p>
* Base on : JDK1.5
* <p>
*
*/
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.log4j.Logger;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureResults;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.data.FeatureWriter;
import org.geotools.data.postgis.PostgisDataStore;
import org.geotools.data.postgis.PostgisDataStoreFactory;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.feature.AttributeType;
import org.geotools.feature.AttributeTypeFactory;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureType;
import org.geotools.feature.FeatureTypeFactory;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.SchemaException;
import org.geotools.geometry.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
public class GetPostgisData {
private static final Logger LOGGER = Logger.getLogger("org.geotools.postgis");
static PostgisDataStore pgDatastore;
static PostgisDataStoreFactory factory=new PostgisDataStoreFactory();
static FeatureSource fsBC;
@SuppressWarnings("unchecked")
private static void ConnPostGis(String dbtype,String URL,int port,String database,
String user,String password){
Map params = new HashMap();
params.put("dbtype", "postgis");
params.put("host", URL);
params.put("port", new Integer(port));
params.put("database", database);
params.put("user", user);
params.put("passwd", password);
try {
pgDatastore=(PostgisDataStore) factory.createDataStore( params );
if(pgDatastore!=null){
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"成功!");
}else{
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"失败!请检查相关参数");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"失败!请检查相关参数");
}
}
//读取指定类型名的地理特征
public static void getFeatureSource(String sourceName){
try {
fsBC = pgDatastore.getFeatureSource(sourceName);
//System.out.println(fsBC.getFeatures().size());
} catch (IOException e) {
e.printStackTrace();
}
}
//取得POSTGIS中所有的地理图层
public static void getAllLayers(){
try {
String[] typeName = pgDatastore.getTypeNames();
for(int i=0;i<typeName.length;i++){
System.out.println(typeName[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取图层地理元素属性(Feature Attribute)
public static void getAttribute(){
FeatureType ftBC=fsBC.getSchema();
System.out.println(ftBC.getAttributeCount());
for (int i = 0; i < ftBC.getAttributeCount(); i++) {
AttributeType at = ftBC.getAttributeType( i );
//判断属性类型是否为可分配的几何对象
if (!Geometry.class.isAssignableFrom(at.getType()))
System.out.print(at.getType() + "\t");
}
System.out.println();
for (int i = 0; i < ftBC.getAttributeCount(); i++) {
AttributeType at = ftBC.getAttributeType( i );
if (!Geometry.class.isAssignableFrom(at.getType()))
System.out.print(at.getName() + "\t");
}
}
//从数据容器中读取所有的特征属性
@SuppressWarnings("deprecation")
public static void PostGisReading(){
try {
FeatureResults fsRU = fsBC.getFeatures();
FeatureReader reader = fsRU.reader();
while (reader.hasNext()) {
Feature feature;
try {
feature = reader.next();
System.out.print(feature.getID() + "\t");
for (int i = 0; i < feature.getNumberOfAttributes(); i++) {
Object attribute = feature.getAttribute( i );
if (!(attribute instanceof Geometry))
System.out.print(attribute + "\t");
}
System.out.println();
} catch (NoSuchElementException e) {
e.printStackTrace();
} catch (IllegalAttributeException e) {
e.printStackTrace();
}
}
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//添加特征值到新的特征对象中。等同于新建一个postgis数据表并向其中插入数据
@SuppressWarnings("deprecation")
public static void createFeatures(){
try {
AttributeType geom = AttributeTypeFactory.newAttributeType("the_geom",LineString.class);
AttributeType name = AttributeTypeFactory.newAttributeType("name",String.class);
FeatureType ftRoad = FeatureTypeFactory.newFeatureType
(new AttributeType[] {geom,name}, "tem_road");
WKTReader wktReader = new WKTReader();
try {
LineString geometry = (LineString) wktReader.read("LINESTRING (0 0, 10 10)");
String roadName="武络路";
pgDatastore.createSchema(ftRoad);
FeatureWriter aWriter = pgDatastore.getFeatureWriter("tem_road",
((FeatureStore) pgDatastore.getFeatureSource("tem_road")).getTransaction());
/** *//**如有批量导入数据要求,可使用 org.geotools.data.FeatureStore */
Feature aNewFeature = aWriter.next();
aNewFeature.setAttribute("the_geom",geometry);
aNewFeature.setAttribute("name", roadName);
aWriter.write();
aWriter.close();
} catch (ParseException e) {
e.printStackTrace();
} catch (IllegalAttributeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FactoryRegistryException e) {
e.printStackTrace();
} catch (SchemaException e) {
e.printStackTrace();
}
}
//添加Feature到已知的图层之中
public static void insertFeatures(String featurename){
WKTReader wktReader = new WKTReader();
try {
LineString geometry = (LineString) wktReader.read("LINESTRING (10 10, 20 20)");
String roadName="珞瑜路";
FeatureSource source = pgDatastore.getFeatureSource(featurename);
FeatureWriter aWriter = pgDatastore.getFeatureWriterAppend(featurename,((FeatureStore) source).getTransaction());
/** *//**如有批量导入数据要求,可使用 org.geotools.data.FeatureStore */
Feature feature = aWriter.next();
try {
feature.setAttribute("the_geom",geometry);
feature.setAttribute("name", roadName);
} catch (IllegalAttributeException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
aWriter.write();
aWriter.close();
} catch (ParseException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
ConnPostGis("","localhost",5432,"navigation","root","to0124@c");
/**//*读取空间库中所有图层*/
getAllLayers();
/**//*读取roads图层的空间库,取得FeatureSource对象,
* getAttribute()方法用于读取此图层所定义的所有的属性
* 并通过PostGisReading()方法读取此图层中所有信息*/
getFeatureSource("roads");
getAttribute();
PostGisReading();
/**//*在空间库中新建一个schema并向表中插入数据*/
createFeatures();
/**//*向tem_road图层的空间库中插入一条新的记录*/
insertFeatures("tem_road");
/**//*修改空间库记录*/
}
}
* POSTGEO
*/
package com.geotools.test;
/** */ /**
*
* CopyRight (C) All rights reserved.
* <p>
*
* WuHan Inpoint Information Technology Development,Inc.
* <p>
*
* Author sinoly
* <p>
* Project Name: PostGeo
*
* @version 1.0 2006-11-13
*
* <p>
* Base on : JDK1.5
* <p>
*
*/
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.log4j.Logger;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureResults;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.data.FeatureWriter;
import org.geotools.data.postgis.PostgisDataStore;
import org.geotools.data.postgis.PostgisDataStoreFactory;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.feature.AttributeType;
import org.geotools.feature.AttributeTypeFactory;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureType;
import org.geotools.feature.FeatureTypeFactory;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.SchemaException;
import org.geotools.geometry.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
public class GetPostgisData {
private static final Logger LOGGER = Logger.getLogger("org.geotools.postgis");
static PostgisDataStore pgDatastore;
static PostgisDataStoreFactory factory=new PostgisDataStoreFactory();
static FeatureSource fsBC;
@SuppressWarnings("unchecked")
private static void ConnPostGis(String dbtype,String URL,int port,String database,
String user,String password){
Map params = new HashMap();
params.put("dbtype", "postgis");
params.put("host", URL);
params.put("port", new Integer(port));
params.put("database", database);
params.put("user", user);
params.put("passwd", password);
try {
pgDatastore=(PostgisDataStore) factory.createDataStore( params );
if(pgDatastore!=null){
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"成功!");
}else{
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"失败!请检查相关参数");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统连接到位于:"+URL+"的空间数据库"+database+"失败!请检查相关参数");
}
}
//读取指定类型名的地理特征
public static void getFeatureSource(String sourceName){
try {
fsBC = pgDatastore.getFeatureSource(sourceName);
//System.out.println(fsBC.getFeatures().size());
} catch (IOException e) {
e.printStackTrace();
}
}
//取得POSTGIS中所有的地理图层
public static void getAllLayers(){
try {
String[] typeName = pgDatastore.getTypeNames();
for(int i=0;i<typeName.length;i++){
System.out.println(typeName[i]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//获取图层地理元素属性(Feature Attribute)
public static void getAttribute(){
FeatureType ftBC=fsBC.getSchema();
System.out.println(ftBC.getAttributeCount());
for (int i = 0; i < ftBC.getAttributeCount(); i++) {
AttributeType at = ftBC.getAttributeType( i );
//判断属性类型是否为可分配的几何对象
if (!Geometry.class.isAssignableFrom(at.getType()))
System.out.print(at.getType() + "\t");
}
System.out.println();
for (int i = 0; i < ftBC.getAttributeCount(); i++) {
AttributeType at = ftBC.getAttributeType( i );
if (!Geometry.class.isAssignableFrom(at.getType()))
System.out.print(at.getName() + "\t");
}
}
//从数据容器中读取所有的特征属性
@SuppressWarnings("deprecation")
public static void PostGisReading(){
try {
FeatureResults fsRU = fsBC.getFeatures();
FeatureReader reader = fsRU.reader();
while (reader.hasNext()) {
Feature feature;
try {
feature = reader.next();
System.out.print(feature.getID() + "\t");
for (int i = 0; i < feature.getNumberOfAttributes(); i++) {
Object attribute = feature.getAttribute( i );
if (!(attribute instanceof Geometry))
System.out.print(attribute + "\t");
}
System.out.println();
} catch (NoSuchElementException e) {
e.printStackTrace();
} catch (IllegalAttributeException e) {
e.printStackTrace();
}
}
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
//添加特征值到新的特征对象中。等同于新建一个postgis数据表并向其中插入数据
@SuppressWarnings("deprecation")
public static void createFeatures(){
try {
AttributeType geom = AttributeTypeFactory.newAttributeType("the_geom",LineString.class);
AttributeType name = AttributeTypeFactory.newAttributeType("name",String.class);
FeatureType ftRoad = FeatureTypeFactory.newFeatureType
(new AttributeType[] {geom,name}, "tem_road");
WKTReader wktReader = new WKTReader();
try {
LineString geometry = (LineString) wktReader.read("LINESTRING (0 0, 10 10)");
String roadName="武络路";
pgDatastore.createSchema(ftRoad);
FeatureWriter aWriter = pgDatastore.getFeatureWriter("tem_road",
((FeatureStore) pgDatastore.getFeatureSource("tem_road")).getTransaction());
/** *//**如有批量导入数据要求,可使用 org.geotools.data.FeatureStore */
Feature aNewFeature = aWriter.next();
aNewFeature.setAttribute("the_geom",geometry);
aNewFeature.setAttribute("name", roadName);
aWriter.write();
aWriter.close();
} catch (ParseException e) {
e.printStackTrace();
} catch (IllegalAttributeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FactoryRegistryException e) {
e.printStackTrace();
} catch (SchemaException e) {
e.printStackTrace();
}
}
//添加Feature到已知的图层之中
public static void insertFeatures(String featurename){
WKTReader wktReader = new WKTReader();
try {
LineString geometry = (LineString) wktReader.read("LINESTRING (10 10, 20 20)");
String roadName="珞瑜路";
FeatureSource source = pgDatastore.getFeatureSource(featurename);
FeatureWriter aWriter = pgDatastore.getFeatureWriterAppend(featurename,((FeatureStore) source).getTransaction());
/** *//**如有批量导入数据要求,可使用 org.geotools.data.FeatureStore */
Feature feature = aWriter.next();
try {
feature.setAttribute("the_geom",geometry);
feature.setAttribute("name", roadName);
} catch (IllegalAttributeException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
aWriter.write();
aWriter.close();
} catch (ParseException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
ConnPostGis("","localhost",5432,"navigation","root","to0124@c");
/**//*读取空间库中所有图层*/
getAllLayers();
/**//*读取roads图层的空间库,取得FeatureSource对象,
* getAttribute()方法用于读取此图层所定义的所有的属性
* 并通过PostGisReading()方法读取此图层中所有信息*/
getFeatureSource("roads");
getAttribute();
PostGisReading();
/**//*在空间库中新建一个schema并向表中插入数据*/
createFeatures();
/**//*向tem_road图层的空间库中插入一条新的记录*/
insertFeatures("tem_road");
/**//*修改空间库记录*/
}
}
目前对GeoTools真的有些失望。。。不过作为一个开源中间件,它做到目前这个地位真的除了它推出比较早以外,还得感谢开源社区的帮助,2006年10月份推出的2.2版本其中对WMS中图层的渲染效率的大幅提升就是得益与Richard兄台的绝妙建议。自己也希望能向Richard学习,一个43岁的丹麦程序员,真的让人真的体会到什么叫“Open Mind”。。。过年期间的事情也有了眉目,着力修正shp2pqsql的问题。目前随post安装版推出的这个程序还是有些bug,在中文编码以及容错方面都需要提高。
|
|