*
* 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");
/**//*修改空间库记录*/
}
}