使用geotools 解析shp文件 不适用java11
jar包在maven仓库是没有的 所以使用jar打包到本地的方式
jar包在我的资源里面有可以下载
pom文件
org.apache.commons
commons-lang3
3.12.0
com.vividsolutions
jts
1.11
com.googlecode.json-simple
json-simple
1.1
org.opengis
geoapi
2.3-M1
org.opengis
geoapi-pending
2.3-M1
org.geotools
gt-api
2.7-M0
org.geotools
gt-geojson
2.7-M0
org.geotools
gt-main
2.7-M0
org.geotools
gt-metadata
2.7-M0
org.geotools
gt-shapefile
2.7-M0
解析的代码
/**
* 导入shp文件解析
* @param files 文件
* @return 解析结果
*/
public AjaxResult importShp(MultipartFile[] files){
long start=System.currentTimeMillis();
String fileId=UUID.randomUUID().toString();
File shapeFile=null;
File shxFile=null;
try{
if(null==files||files.length<2){
return AjaxResult.error("文件不能为空并且必须包含.shp和对应的.shx文件");
}
if(!files[0].getOriginalFilename().contains(".shp")&&!files[0].getOriginalFilename().contains(".shx")){
return AjaxResult.error("文件类型必须是.shp和对应的.shx文件");
}
if(!files[1].getOriginalFilename().contains(".shp")&&!files[1].getOriginalFilename().contains(".shx")){
return AjaxResult.error("文件类型必须是.shp和对应的.shx文件");
}
//转换成file文件 然后进行解析
for (MultipartFile img : files) {
String fileName =img.getOriginalFilename();
if (fileName.contains(".shp")) {
fileName =fileId+".shp";
File file = new File(rsConfig.getThumbnailTempUrl(),fileName);
if (!file.exists()){
img.transferTo(file);
}else{
file.delete();
img.transferTo(file);
}
shapeFile=file;
}else if(fileName.contains(".shx")){
fileName =fileId+".shx";
File file = new File(rsConfig.getThumbnailTempUrl(),fileName);
if (!file.exists()){
img.transferTo(file);
}else{
file.delete();
img.transferTo(file);
}
shxFile=file;
}
}
ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//设置编码
Charset charset = Charset.forName("GBK");
store.setStringCharset(charset);
SimpleFeatureSource sfSource = store.getFeatureSource();
SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
// 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串
List pointList=new ArrayList<>();
while (sfIter.hasNext()) {
SimpleFeature feature = sfIter.next();
// Feature转GeoJSON
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String pointJson = writer.toString();
if(StringUtils.isNotEmpty(pointJson)){
//解析出来的文件有问题 自己处理下
pointJson=pointJson.replace(":}",":{}");
}
log.debug("pointJson===== >>>>{} " ,pointJson);
getPoints(pointJson,pointList);
}
log.info("importShp.delay={}",(System.currentTimeMillis() - start));
//转换格式 返回给前端
return AjaxResult.success("成功",pointList);
}catch (Exception e){
log.error("importShp.error={}",e);
} finally {
//删除临时文件
if(null!=shapeFile&&shapeFile.exists()){
shapeFile.delete();
}
if(null!=shxFile&&shxFile.exists()){
shxFile.delete();
}
}
return AjaxResult.error("解析失败");
}
/**
* 解析组装数据
* @param json 数据
* @param pointList 经纬度信息
*/
public void getPoints(String json,List pointList){
JSONObject jsonObject=JSONObject.parseObject(json);
JSONObject geometry=jsonObject.getJSONObject("geometry");
String type=geometry.getString("type");
JSONArray coordinates=geometry.getJSONArray("coordinates");
if("MultiPolygon".equals(type)) {//多个
for (int k = 0; k < coordinates.size(); k++) {
List points=new ArrayList<>();
JSONArray tempArray = coordinates.getJSONArray(k).getJSONArray(0);
for (int j = 0; j < tempArray.size(); j++) {
JSONArray array = tempArray.getJSONArray(j);
String point=array.getDoubleValue(0)+","+array.getDoubleValue(1);
points.add(point);
}
String pointTem=points.stream().collect(Collectors.joining(";"));
pointList.add(pointTem);
}
}else if("Polygon".equals(type)) {//单个
JSONArray tempArray = coordinates.getJSONArray(0);
List points=new ArrayList<>();
for (int j = 0; j < tempArray.size(); j++) {
JSONArray array = tempArray.getJSONArray(j);
String point=array.getDoubleValue(0)+","+array.getDoubleValue(1);
points.add(point);
}
String pointTem=points.stream().collect(Collectors.joining(";"));
pointList.add(pointTem);
}
}
线上环境java11 所以要变更使用的jar信息
Upgrade — GeoTools 28-SNAPSHOT User Guide
IntelliJ Quickstart — GeoTools 28-SNAPSHOT User Guide
从此处下载所需的对应的版本的jar 包
Nexus Repository Manager
Nexus Repository Manager
org.apache.commons
commons-lang3
3.12.0
com.vividsolutions
jts
1.13
com.googlecode.json-simple
json-simple
1.1.1
org.locationtech.jts
jts-core
1.16.1
org.opengis
geoapi
2.3-M3
org.opengis
geoapi-pending
2.3-M3
org.geotools
gt-geojson
21.5
org.geotools
gt-main
21.5
org.geotools
gt-metadata
21.5
org.geotools
gt-shapefile
21.5
org.geotools
gt-opengis
21.5
org.geotools
gt-referencing
21.5
public static void main(String[] args) throws Exception {
File shapeFile = new File("D:\\","single.shp");
ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//设置编码
Charset charset = Charset.forName("GBK");
store.setCharset(charset);
SimpleFeatureSource sfSource = store.getFeatureSource();
SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
// 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串
List pointList=new ArrayList<>();
while (sfIter.hasNext()) {
SimpleFeature feature = sfIter.next();
// Feature转GeoJSON
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String pointJson = writer.toString();
System.out.println(pointJson);
}
}