依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geometry</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-jts-wrapper</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>20.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
如果依赖下载不下来,可以配置maven
<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> <!--GeoServer--> <repository> <id>GeoSolutions</id> <url>http://maven.geo-solutions.it/</url> </repository> </repositories>
工具类
旧版
mport org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.xadc.jmrfb3des.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
public static String transformShpToGeoJsons(String shpPath) throws IOException {
String geojson = null;
ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
SimpleFeatureSource source = store.getFeatureSource();
store.setCharset(Charset.forName("GBK"));
SimpleFeatureCollection featureCollection = source.getFeatures();
FeatureJSON json = new FeatureJSON();
try (StringWriter writer = new StringWriter()) {
json.writeFeatureCollection(featureCollection, writer);
geojson = writer.toString();
}catch (Exception e){
log.info("shp -> 格式转换错误 {} " , e.getMessage());
throw new CustomException("shp -> 格式转换错误");
}
return geojson;
}
public static void main(String[] args) throws IOException {
}
public static String transformShpToGeoJson(String shpPath) throws IOException {
String geojson = null;
ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
store.setCharset(StandardCharsets.UTF_8);
SimpleFeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
FeatureJSON featureJSON = new FeatureJSON();
try (StringWriter writer = new StringWriter()) {
featureJSON.writeFeatureCollection(featureCollection, writer);
geojson = writer.toString();
} catch (IOException e) {
log.info("shp -> 格式转换错误 {} " , e.getMessage());
throw new CustomException("shp -> 格式转换错误");
}
return geojson;
}
看着没啥问题,但是在实际的过程中,转换过程中会报错 Null啥啥啥methodError 好像是这样的一个问题
跟新后
public static String shp2Geojson(String shpPath) {
JSONObject geojsonObject = new JSONObject();
geojsonObject.put("type", "FeatureCollection");
String string = null;
try {
JSONArray array = new JSONArray();
String fileName = readShpContent(shpPath, array);
geojsonObject.put("features", array);
geojsonObject.put("name", fileName);
String crs = getCoordinateSystemWKT(shpPath);
//GEOGCS表示这个是地址坐标系,PROJCS则表示是平面投影坐标系
JSONObject crsJson = new JSONObject();
JSONObject proJson = new JSONObject();
crsJson.put("type", "name");
if (crs.startsWith("PROJCS")) {
proJson.put("name", "urn:ogc:def:crs:EPSG::3857");
crsJson.put("properties", proJson);
} else {
proJson.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
crsJson.put("properties", proJson);
}
geojsonObject.put("crs", crsJson);
long startTime = System.currentTimeMillis();
// 写入文件
OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
string = JSON.toJSONString(geojsonObject).toString();
return string;
}