shp文件转json

一、配置maven settings.xml 文件

mirrorOf 不能设置为*

<mirrors>
        <!-- 阿里云镜像 -->
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
</mirrors>

二、设置GeoTools仓库来源

在pom文件中加入:

<repositories>
		<repository>
			<id>osgeo</id>
			<name>OSGeo Release Repository</name>
			<url>https://repo.osgeo.org/repository/release/</url>
			<snapshots><enabled>false</enabled></snapshots>
			<releases><enabled>true</enabled></releases>
		</repository>
		<repository>
			<id>osgeo-snapshot</id>
			<name>OSGeo Snapshot Repository</name>
			<url>https://repo.osgeo.org/repository/snapshot/</url>
			<snapshots><enabled>true</enabled></snapshots>
			<releases><enabled>false</enabled></releases>
		</repository>		
		<repository>
			<id>opengeo</id>
			<name>OpenGeo Maven Repository</name>
			<url>http://repo.opengeo.org</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
</repositories>

三、在pom中引入GeoTools相关jar包

    <properties>
      <geotools.version>19.0</geotools.version>
	</properties>
    
     <!--以下内容 放在dependencys 标签内部-->
    <dependencies>
        <!--geotools GIS开发包-->
		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-shapefile</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-opengis</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-data</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-api</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-main</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-metadata</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-referencing</artifactId>
			<version>${geotools.version}</version>
		</dependency>

		<dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-geojson</artifactId>
			<version>${geotools.version}</version>
		</dependency>
    </dependencies>

四、java中创建一个转换类

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.geotools.data.shapefile.ShapefileDataStore;
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 java.io.*;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

public class ShpToJson{
    /**
	 * 方法一
	 *
	 * @author molei
	 * @date 2021/11/26 14:53
	 */
	public static void shp2json() throws MalformedURLException {
		StringBuffer sb = new StringBuffer();
		sb.append("{\"type\":\"GeometryCollection\", \"geometries\": ");
		FeatureJSON fJson = new FeatureJSON();
		String shpPath = "E:\\gzsx14.shp";
		File file = new File(shpPath);
		JSONArray array = new JSONArray();
		JSONObject json = new JSONObject();
		try {
			ShapefileDataStore store = new ShapefileDataStore(file.toURI().toURL());
			//设置编码
			store.setCharset(Charset.forName("GBK"));
			//文件名称
			String typeName = store.getTypeNames()[0];
			SimpleFeatureSource featureSource = store.getFeatureSource(typeName);
			SimpleFeatureIterator iterator = featureSource.getFeatures().features();
			while (iterator.hasNext()) {
				SimpleFeature feature = iterator.next();
				StringWriter writer = new StringWriter();
				fJson.writeFeature(feature, writer);
				json = JSONObject.parseObject(writer.toString());
				//使用jsonArray可以把所有数据转成一条;不使用,
				array.add(json);
			}
			iterator.close();
			sb.append(array.toJSONString());
			sb.append("}");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		WriteStringToFile(sb.toString());
		System.out.println(sb.toString());
	}

	/**
	 * 保存数据
	 *
	 * @author molei
	 * @date 2021/11/26 14:32
	 */
	public static void WriteStringToFile(String string) {
		String filePath = "E:\\gzsx14.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();
		}
	}


}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值