把经纬度坐标转成shp文件

import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import com.xxx.config.CommonConfig;
import com.xxx.tool.ConnectionManager;
import com.xxx.tool.LogTool;
import com.xxx.tool.PropertiesUtil;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class writeShp {
    
    public static void main(String[] args) throws Exception {
        write("H:\\TEST\\shp1\\test.shp");
    }
    
    public static void queryData(String filepath) {  
        PropertiesUtil.loadSysCofing();
        Connection conn = null;
        try {
            conn = getConn();
            conn.setAutoCommit(false);
            PreparedStatement pstmt = conn.prepareStatement("");
            
        } catch (SQLException ex) {
            // LogTool.logger.error("批量更新出错,错误信息为"+ex.getMessage());
            LogTool.logger.error("更新出错,错误信息为" + ex.getMessage());
            try {
                conn.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    
    }
    public static void write(String filepath) {  
        try {  
            //创建shape文件对象  
            File file = new File(filepath);  
            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.setName("shapefile");  
            tb.add("the_geom", Point.class);  
            tb.add("POIID", Long.class);  
            tb.add("NAMEC", String.class);  
            ds.createSchema(tb.buildFeatureType());  
            ds.setCharset(Charset.forName("GBK"));  
            //设置Writer  
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);  
            //写下一条  
            //SimpleFeature feature = writer.next();  
            //feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.123, 39.345)));  
            //feature.setAttribute("POIID", 1234567890l);  
            //feature.setAttribute("NAMEC", "某兴趣点1");  
            //feature = writer.next();  
            //feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.456, 39.678)));  
            //feature.setAttribute("POIID", 1234567891l);  
            //feature.setAttribute("NAMEC", "某兴趣点2");  
            writer.write();  
            writer.close();  
            ds.dispose();  
              
            //读取刚写完shape文件的图形信息  
            ShpFiles shpFiles = new ShpFiles(filepath);  
            ShapefileReader reader = new ShapefileReader(shpFiles, false, true, new GeometryFactory(), false);  
            try {  
                while (reader.hasNext()) {  
                    System.out.println(reader.nextRecord().shape());      
                }  
            } finally {  
                reader.close();  
            }  
        } catch (Exception e) { }  
    }  
     

    //由源shape文件创建新的shape文件
    //Java代码  收藏代码
    public void transShape(String srcfilepath, String destfilepath) {  
        try {  
            //源shape文件  
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());  
            //创建目标shape文件对象  
            Map<String, Serializable> params = new HashMap<String, Serializable>();  
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();  
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());  
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);  
            // 设置属性  
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);  
            //下面这行还有其他写法,根据源shape文件的simpleFeatureType可以不用retype,而直接用fs.getSchema设置  
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));  
              
            //设置writer  
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);  
              
            //写记录  
            SimpleFeatureIterator it = fs.getFeatures().features();  
            try {  
                while (it.hasNext()) {  
                    SimpleFeature f = it.next();  
                    SimpleFeature fNew = writer.next();  
                    fNew.setAttributes(f.getAttributes());  
                    writer.write();  
                }  
            } finally {  
                it.close();  
            }  
            writer.close();  
            ds.dispose();  
            shapeDS.dispose();  
        } catch (Exception e) { e.printStackTrace();    }  
    } 
    protected static Connection getConn() {
        return getConn(CommonConfig.DBConn.DB_SPMD);
    }
    // 获取数据库连接
    protected static Connection getConn(String connName) {
        return ConnectionManager.getInstance().getConnection(connName);
    }
}
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-shapefile</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-geojson</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-csv</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.58</version>
</dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值