读取postgis中数据写入shapefile-2

package org.geotools;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.json.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.ubiloc.mj.Door;
import com.ubiloc.mj.Postgre;
import com.ubiloc.mj.State;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.GeometryFactory;

public class PostGISTest {
	   private static Postgre pg=new Postgre();
	   static GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
	/**
	 * @param args
	 * @throws Throwable 
	 */
	public static void main(String[] args) throws Throwable {
		// TODO Auto-generated method stub
          
             pg.init();
             getTransition();
             pg.destroy();
         	}
	
     private static void getTransition() throws SQLException, JSONException, IOException{
         File file = new File("data/TransitionClosedDoor.shp");  
         Map<String, Serializable> params = new HashMap<String, Serializable>();  
         params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );  
         ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);  
         //定义图形信息和属性信息  
         SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();  
         tb.setCRS(DefaultGeographicCRS.WGS84);
/*            tb.setCRS(null);*/
         tb.setName("shapefile");  
         tb.add("ID", Integer.class); 
         tb.add("the_geom", LineString.class);  
         ds.createSchema(tb.buildFeatureType());  
         ds.setCharset(Charset.forName("GBK"));  
         //设置Writer  
         FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT); 
    	 
    	 ArrayList<Door> doorArr=pg.getDoor();
         for(int i=0;i<doorArr.size();i++){
         	 int leftSpaceID=doorArr.get(i).getLeftSpaceID();
         	 int rightSpaceID=doorArr.get(i).getRightSpaceID();
         	Point pcenter=doorArr.get(i).getGeo().getCentroid();
         	 
         	ArrayList<State> LeftStateArr=pg.getStateByID(leftSpaceID);
         	ArrayList<State> RightStateArr=pg.getStateByID(rightSpaceID);
         	
         	Point leftcenter=LeftStateArr.get(0).getPgeo();
         	Point rightcenter=RightStateArr.get(0).getPgeo();
         	
         	Coordinate[] transitionCoo  =
    			new Coordinate[] {new Coordinate(leftcenter.getX(),leftcenter.getY()), 
         			new Coordinate(pcenter.getX(),pcenter.getY()),new Coordinate(rightcenter.getX(),rightcenter.getY()) };
         	LineString transition = null;
           if(doorArr.get(i).isCurrentState()==true){
            	transition = geometryFactory.createLineString(transitionCoo);
    		/*System.out.println(transition)*/;
         	/*System.out.println(doorArr.get(i).isCurrentState());*/
/*         	transition = geometryFactory.createLineString(transitionCoo);*/
            try{                
                //写下一条             
                SimpleFeature feature = writer.next();         
                feature.setAttribute("ID", i);    
                feature.setAttribute("the_geom",transition); 
                writer.write();
                 
                
            } catch (Exception e) {  
                e.printStackTrace();  
            } 
           }
         	 
          }
         writer.close();  
         ds.dispose();

     }
}

将得到三个中心点数据组成一条LineString,依次写入Shapefile文件中

转载自:https://blog.csdn.net/cehui115081/article/details/18730885

You may also like...