java使用geotools shp转geojson(wtk转geojson) 精度丢失

java 使用geotools将shp转为geojson时,发现geojson的精度丢失,所以导致geojson展示在地图上如下图:(使用wkt转geojson也存在同样问题)
java使用geotools shp转geojson(wtk转geojson) 精度丢失_第1张图片

查看api发现是GeometryJSON的构造函数有个参数 decimals【控制点的坐标的小数位数】,如果不传会默认保留4位小数。
java使用geotools shp转geojson(wtk转geojson) 精度丢失_第2张图片

使用到的依赖:

<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-geojson</artifactId>
	<version>10.4</version>
</dependency>

代码如下:
wtk转geojson:

	public static String wktToJson(String wkt){
			String json = null;
			try{
				WKTReader reader = new WKTReader();
				Geometry geometry = reader.read(wkt);
				StringWriter writer = new StringWriter();
				//GeometryJSON g = new GeometryJSON();
				GeometryJSON g = new GeometryJSON(20);// 这个参数必须测试,不然精度丢失
				g.write(geometry,writer);
				json = writer.toString();
			}catch(Exception e){
				e.printStackTrace();
			}
			return json;
		}

shp转geojson:


import java.io.*;
import java.util.*;

import java.nio.charset.Charset;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

public class GeojsonUtil {
	
	/**
	 *
	* @Title: shpFolderToGoeJsonList
	* @Description: 生成geojson中的features
	* @Param: shpFile   shp文件夹
	* @returns: java.util.ArrayList  geojson的list
	* @Author: yangzh
	* @Date: 2019/10/24
	*/
     public static ArrayList shpFolderToGoeJsonList(File shpFile) throws Exception {
        // Gson gson = new GsonBuilder().serializeSpecialFloatingPointValues().setDateFormat("yyyy-MM-dd").serializeNulls().create();
        ShapefileDataStore shpDataStore = null;
        shpDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
        shpDataStore.setStringCharset(Charset.forName("GBK"));
        String typeName = shpDataStore.getTypeNames()[0];
        FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
        featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
        FeatureIterator<SimpleFeature> itertor = result.features();
        FeatureJSON fjson = new FeatureJSON(new GeometryJSON(20));// 此处会控制生成geojson的坐标的小数点精度
        ArrayList array = new ArrayList();
        int resutsize=result.size();
        int first = 0;
        try {
            while(itertor.hasNext()){
                first++;
                SimpleFeature feature = itertor.next();
                StringWriter writer = new StringWriter();
                fjson.writeFeature(feature, writer);
                // JSONObject json = new JSONObject(JsonUtil.getMap4Json(writer.toString()));
                if(resutsize==first){
                    array.add(writer.toString());
                }else{
                    array.add(writer.toString());
                }
            }
            itertor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }
}

你可能感兴趣的:(java,java)