通过GeoTools把shp文件数据转换成json格式

由于项目中需要把大量的shp文件的数据转换成json格式存储到数据库。简单记录一下,以备以后的遗忘之需。

首先引入所需的依赖:由于项目依赖过多和保密,这里只写了所需的geotools依赖:

    
        1.7  
        15.1
        2.7 
        1.7
		2.2
		7.6.14.v20131031
	    
      
	
			  
	            org.geotools  
	            gt-geojson  
	            ${geotools.version}  
	          
	          
	            org.geotools  
	            gt-geometry  
	            ${geotools.version}  
	          
	          
	            org.geotools  
	            gt-epsg-hsql  
	            ${geotools.version}  
	          
	          
	            org.geotools  
	            gt-jts-wrapper  
	            ${geotools.version}  
	         
	       
	        
	            org.geotools
	            gt-shapefile
	            ${geotools.version}
	        
	        
	            org.geotools
	            gt-swing
	            ${geotools.version}
	        
	        
	        
	            org.geotools
	            gt-epsg-oracle
	            ${geotools.version}
	        
	        
	        
	            org.geotools
	            gt-jdbc-oracle
	            ${geotools.version}
	        
	        
	        
	        
	        

代码部分:



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @author Jzxh
 * @description 读取shp格式文件,转成json格式数据
 * @data 2018年10月29日 上午9:37:05
 */
public class FormatShp {
	public static void shp2json() throws MalformedURLException {
		StringBuffer sb=new StringBuffer();
		FeatureJSON fJson=new FeatureJSON();
		String shpPath="E:\\shp\\gongnengqu.shp";
		File file = new File(shpPath);
		ShapefileDataStore store=null;
		JSONArray array=new JSONArray();
		JSONObject json=new JSONObject();
		try {
			store=new ShapefileDataStore(file.toURL());
			Charset charset=Charset.forName("GBK");
			store.setCharset(charset);
			String typeName =store.getTypeNames()[0];
			SimpleFeatureSource featureSource=null;
			featureSource=store.getFeatureSource(typeName);
			SimpleFeatureCollection collection=featureSource.getFeatures();
			SimpleFeatureIterator iterator=collection.features();
			while (iterator.hasNext()) {
				SimpleFeature feature=iterator.next();
				StringWriter writer=new StringWriter();
				fJson.writeFeature(feature, writer);
				 json=JSONObject.parseObject(writer.toString());
				//array.add(json);//使用jsonArray可以把所有数据转成一条;不使用,
                //下方输出只会输出一条JSON数据,如需存入数据库,改写此方法,在实现类里迭代。
				
			}
			iterator.close();
			sb.append(json);
			//sb.append("}");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		WriteStringToFile(sb.toString());
		System.out.println(sb.toString());
	}

public static void WriteStringToFile(String string) {
	String filePath="E:\\shp\\point.geojson";
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.append(string);// 在已有的基础上添加字符串
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	public static void main(String[] args) {
		try {
			shp2json();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

有需要的伙伴可以根据自己的需要改写代码。鄙人才疏学浅,错误之处请多多指正。

附:如果pom文件无法加载依赖,可以手动引入jar包,自己下了更多jar包。各位可根据需要自行导入,下载地址:https://download.csdn.net/download/lduzxh/10751305

你可能感兴趣的:(Java)