zoukankan      html  css  js  c++  java
  • geotools实现追加数据到数据库

    一、在这之前先写一点关于事务的小知识点如下:
    引用:import org.geotools.data.DefaultTransaction;
    import org.geotools.data.Transaction;
    //追加数据的事务
    1、Transaction session = new DefaultTransaction(“Adding”);
    //事务的设置
    featureStore.setTransaction( session );
    //创建文件存储事务
    2、Transaction transaction = new DefaultTransaction(“create”)
    //自动提交事务
    3、Transaction.AUTO_COMMIT
    二、将数据添加到数据库demo

    package org.geotools.WPS;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.geotools.data.DataStore;
    import org.geotools.data.DataStoreFinder;
    import org.geotools.data.DefaultTransaction;
    import org.geotools.data.Transaction;
    import org.geotools.data.collection.ListFeatureCollection;
    import org.geotools.data.postgis.PostgisNGDataStoreFactory;
    import org.geotools.data.simple.SimpleFeatureCollection;
    import org.geotools.data.simple.SimpleFeatureSource;
    import org.geotools.data.simple.SimpleFeatureStore;
    
    import org.geotools.feature.simple.SimpleFeatureBuilder;
    
    import org.opengis.feature.simple.SimpleFeature;
    import org.opengis.feature.simple.SimpleFeatureType;
    import org.opengis.filter.identity.FeatureId;
    
    import org.locationtech.jts.geom.Coordinate;
    import org.locationtech.jts.geom.GeometryFactory;
    import org.locationtech.jts.geom.Point;
    public class FeatureAddToDataBase {
        /**
         * @param dbtype: 数据库类型,postgis or mysql
         * @param host: ip地址
         * @param port: 端口号
         * @param database: 需要连接的数据库
         * @param userName: 用户名
         * @param password: 密码
         * @param tableName: 需要连接的表名
         * @return: 返回为SimpleFeatureSource类型
         */
        public static SimpleFeatureSource connAndgetCollection(String dbtype, String host, String port,  
                String database, String userName, String password,String tableName) {  
            Map<String, Object> params = new HashMap<String, Object>();   
            DataStore pgDatastore=null;
            params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbtype); //需要连接何种数据库,postgis or mysql
            params.put(PostgisNGDataStoreFactory.HOST.key, host);//ip地址
            params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));//端口号
            params.put(PostgisNGDataStoreFactory.DATABASE.key, database);//需要连接的数据库  
            params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");//架构
            params.put(PostgisNGDataStoreFactory.USER.key, userName);//需要连接数据库的名称 
            params.put(PostgisNGDataStoreFactory.PASSWD.key, password);//数据库的密码
            SimpleFeatureSource fSource=null;
            try { 
            	//获取存储空间
                pgDatastore = DataStoreFinder.getDataStore(params); 
                //根据表名获取source
                 fSource=pgDatastore.getFeatureSource(tableName);                     
                if (pgDatastore != null) {  
                    System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                            + "成功!"); 
                     
                } else {  
                    System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                            + "失败!请检查相关参数"); 
                   
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
                System.out.println("系统连接到位于:" + host + "的空间数据库" + database  
                        + "失败!请检查相关参数");                       
            }  
            return fSource;
        }  
        
        /**
         * @param featureSource:要素源
         * @return SimpleFeatureCollection类型
         */
        public static SimpleFeatureCollection GetFeatureCollection (SimpleFeatureSource featureSource) {
        	GeometryFactory geometryFactory = new GeometryFactory();
        	SimpleFeatureCollection collection =null;
        	//获取类型
        	SimpleFeatureType TYPE = featureSource.getSchema();
        	System.out.println(TYPE);
            //创建要素集合
            List<SimpleFeature> features = new ArrayList<>();
            //创建要素模板
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
            //创建要素并添加道集合
            double latitude = Double.parseDouble("39.9");
            double longitude = Double.parseDouble("116.3");
            String name ="beijing";
            int number = Integer.parseInt("16");
            //创建一个点geometry
            Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
            //添加的数据一定按照SimpleFeatureType给的字段顺序进行赋值
            //添加name属性
            featureBuilder.add(name);
            //添加number属性
            featureBuilder.add(number);
            //添加geometry属性
            featureBuilder.add(point);
            //构建要素
            SimpleFeature feature = featureBuilder.buildFeature(null);
            System.out.println(feature);
            //将要素添加到要素几何中
            features.add(feature);      
        	 collection = new ListFeatureCollection(TYPE, features);
        	return collection;    	
        }
        public static void main(String[] args)  {
        	//调用方法
        	SimpleFeatureSource featureSource=FeatureAddToDataBase.connAndgetCollection("postgis", "localhost", "5432", "sqlView", "postgres", "postgres","7");
        	if( featureSource instanceof SimpleFeatureStore ){
        		   SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
        		   SimpleFeatureCollection featureCollection=FeatureAddToDataBase.GetFeatureCollection(featureSource);   		   
        		   //创建事务
        		   Transaction session = new DefaultTransaction("Adding");
        		   featureStore.setTransaction( session );
        		   try {
        		     List<FeatureId> added = featureStore.addFeatures( featureCollection );
        		     System.out.println( "Added "+added );
        		     //提交事务
        		     session.commit();
        		   }
        		   catch (Throwable t){
        		     System.out.println( "Failed to add features: "+t );
        		     try {
        		    	 //事务回归
    					session.rollback();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
        		   }
        	}     
    }
    }
    
    
    
  • 相关阅读:
    把C语言的指针按在地上摩擦!
    组合索引相关介绍
    ConcurrentModificationException异常
    chat和varchar的区别?
    二进制部署K8S集群(二十三)addons之安装部署dashboard
    二进制部署K8S集群(二十二)addons之安装部署ingress
    二进制部署K8S集群(二十一)addons之flanneld优化SNAT规则
    二进制部署K8S集群(二十)K8s服务暴露之NodePort型Service
    二进制部署K8S集群(十九)addons之安装部署coredns
    二进制部署K8S集群(十八)addons之flannel三种模型安装部署详解
  • 原文地址:https://www.cnblogs.com/tuboshu/p/10752282.html
Copyright © 2011-2022 走看看