一、配置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();
}
}
}