Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

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


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

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

  <properties>  
        <java-version>1.7</java-version>  
        <geotools.version>15.1</geotools.version>
        <log4j-version>2.7</log4j-version> 
        <jdk.version>1.7</jdk.version>
		<tomcat.version>2.2</tomcat.version>
		<jetty.version>7.6.14.v20131031</jetty.version>
	    
    </properties>  
	<!-- geotools -->
			<dependency>  
	            <groupId>org.geotools</groupId>  
	            <artifactId>gt-geojson</artifactId>  
	            <version>${geotools.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.geotools</groupId>  
	            <artifactId>gt-geometry</artifactId>  
	            <version>${geotools.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.geotools</groupId>  
	            <artifactId>gt-epsg-hsql</artifactId>  
	            <version>${geotools.version}</version>  
	        </dependency>  
	        <dependency>  
	            <groupId>org.geotools</groupId>  
	            <artifactId>gt-jts-wrapper</artifactId>  
	            <version>${geotools.version}</version>  
	        </dependency> 
	       <!--  <dependency> 
				<groupId>net.sf.jacob-project</groupId> 
				<artifactId>jacob</artifactId> 
				<version>1.18</version> 
			</dependency> -->
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-shapefile</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-swing</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-epsg-oracle</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-jdbc-oracle</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <!-- <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-oracle-spatial</artifactId>
	            <version>${geotools.version}</version>
	        </dependency> -->
	        
	        <!-- <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-epsg-mysql</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-jdbc-mysql</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-epsg-postgis</artifactId>
	            <version>${geotools.version}</version>
	        </dependency>
	        
	        <dependency>
	            <groupId>org.geotools</groupId>
	            <artifactId>gt-jdbc-postgis</artifactId>
	            <version>${geotools.version}</version>
	        </dependency> -->

代码部分:

package com.gangtong.util.geotools;

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

转载自:https://blog.csdn.net/lduzxh/article/details/83508451