Commit c645cf01 authored by 周健威's avatar 周健威

修改代码

parent 0b8fbd79
...@@ -14,6 +14,23 @@ ...@@ -14,6 +14,23 @@
<artifactId>rs-datacenter-api</artifactId> <artifactId>rs-datacenter-api</artifactId>
<version>2.0-rscp-SNAPSHOT</version> <version>2.0-rscp-SNAPSHOT</version>
<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>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
......
...@@ -14,6 +14,27 @@ ...@@ -14,6 +14,27 @@
<groupId>com.upyuns.platform.rs</groupId> <groupId>com.upyuns.platform.rs</groupId>
<artifactId>rs-datacenter-server</artifactId> <artifactId>rs-datacenter-server</artifactId>
<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>
</repositories>
<properties>
<geotools.version>17.0</geotools.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.github.wxiaoqi</groupId> <groupId>com.github.wxiaoqi</groupId>
...@@ -44,6 +65,43 @@ ...@@ -44,6 +65,43 @@
<optional>true</optional> <optional>true</optional>
<version>2.8.3</version> <version>2.8.3</version>
</dependency> </dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>24.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>24.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>24.3</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>24.3</version>
</dependency>
<!-- 添加geotools-jdbc -->
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>24.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.upyuns.platform.rs.datacenter.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vividsolutions.jts.geom.*;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ShpToGeojson {
/**
* geojson转换为shp文件
*
* @param
* @param shpPath
* @return
*/
public Map geojson2Shape(String jsonPath, String shpPath) {
Map map = new HashMap();
GeometryJSON gjson = new GeometryJSON();
try {
// 读文件到Stringbuffer
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(jsonPath));
String str;
while ((str = br.readLine()) != null) {// 逐行读取
sb.append(str + "\r\n");
}
br.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
JSONObject json = JSONObject.parseObject(sb.toString());
// JSONObject json = JSONObject.fromObject(sb.toString());
JSONArray features = (JSONArray) json.get("features");
JSONObject feature0 = JSONObject.parseObject(features.get(0).toString());
// 获取属性名称
Set properties = JSONObject.parseObject(feature0.getString("properties")).keySet();
String strType = ((JSONObject) feature0.get("geometry")).getString("type").toString();
Class<?> geoType = null;
switch (strType) {
case "Point":
geoType = Point.class;
break;
case "MultiPoint":
geoType = MultiPoint.class;
break;
case "LineString":
geoType = LineString.class;
break;
case "MultiLineString":
geoType = MultiLineString.class;
break;
case "Polygon":
geoType = Polygon.class;
break;
case "MultiPolygon":
geoType = MultiPolygon.class;
break;
}
// 创建shape文件对象
File file = new File(shpPath);
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
// 定义图形信息和属性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
tb.add("the_geom", geoType);// 类型,Point/MultiPoint/LineString/MultiLineString/Polygon/MultiPolygon
Iterator propertiesIter = properties.iterator();
// 设置属性
while (propertiesIter.hasNext()) {
String str = propertiesIter.next().toString();
tb.add(str, String.class);// 此处设置为string,如需修改请自行改写代码
}
ds.createSchema(tb.buildFeatureType());
// 设置编码
Charset charset = Charset.forName("GBK");
ds.setCharset(charset);
// 设置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0],
Transaction.AUTO_COMMIT);
for (int i = 0, len = features.size(); i < len; i++) {
String strFeature = features.get(i).toString();
Reader reader = new StringReader(strFeature);
SimpleFeature feature = writer.next();
switch (strType) {
case "Point":
feature.setAttribute("the_geom", gjson.readPoint(reader));
break;
case "MultiPoint":
feature.setAttribute("the_geom", gjson.readMultiPoint(reader));
break;
case "LineString":
feature.setAttribute("the_geom", gjson.readLine(reader));
break;
case "MultiLineString":
feature.setAttribute("the_geom", gjson.readMultiLine(reader));
break;
case "Polygon":
feature.setAttribute("the_geom", gjson.readPolygon(reader));
break;
case "MultiPolygon":
feature.setAttribute("the_geom", gjson.readMultiPolygon(reader));
break;
}
Iterator propertiesset = properties.iterator();
while (propertiesset.hasNext()) {
String str = propertiesset.next().toString();
JSONObject featurei = JSONObject.parseObject(features.get(i).toString());
feature.setAttribute(str, JSONObject.parseObject(featurei.getString("properties")).get(str));
}
writer.write();
}
writer.close();
ds.dispose();
map.put("status", "success");
map.put("message", shpPath);
} catch (Exception e) {
map.put("status", "failure");
map.put("message", e.getMessage());
e.printStackTrace();
}
return map;
}
/**
* shp文件转换geojson数据
* @param shpPath
* @return
*/
public static Object shp2geojson(String shpPath)
{
Map map = new HashMap();
//新建json对象
FeatureJSON fjson = new FeatureJSON();
JSONObject geojsonObject=new JSONObject();
geojsonObject.put("type","FeatureCollection");
try{
//获取featurecollection
File file = new File(shpPath);
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(file.toURL());
//设置编码
/* Charset charset = Charset.forName("GBK");
shpDataStore.setCharset(charset);*/
String typeName = shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = null;
featureSource = shpDataStore.getFeatureSource (typeName);
SimpleFeatureCollection result = featureSource.getFeatures();
SimpleFeatureIterator itertor = result.features();
JSONArray array = new JSONArray();
//遍历feature转为json对象
while (itertor.hasNext())
{
SimpleFeature feature = itertor.next();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String temp=writer.toString();
Object geometry = JSONObject.parseObject(temp).getString("geometry");
byte[] b=temp.getBytes("iso8859-1");
temp=new String(b,"gbk");
JSONObject json = JSON.parseObject(temp);
array.add(geometry);
}
itertor.close();
return array;
// geojsonObject.put("features",array);
// itertor.close();
// long startTime=System.currentTimeMillis();
//将json字符串使用字符流写入文件
/* File outputfile=new File(jsonPath);
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(outputfile));
bufferedWriter.write(JSON.toJSONString(geojsonObject));
bufferedWriter.flush();
bufferedWriter.close();*/
// File outputfile=new File(jsonPath);
// FileOutputStream fileOutputStream=new FileOutputStream(outputfile);
// OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");
// outputStreamWriter.write(JSON.toJSONString(geojsonObject));
// outputStreamWriter.flush();
// outputStreamWriter.close();
//将json字符串使用字节流写入文件
/* File outputfile=new File(jsonPath);
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outputfile));
byte[] bytes= JSON.toJSONString(geojsonObject).getBytes("utf-8");
bufferedOutputStream.write(bytes);
//fileOutputStream.write(JSON.toJSONString(geojsonObject));
bufferedOutputStream.flush();
bufferedOutputStream.close();*/
// long endTime=System.currentTimeMillis();
// System.out.println("当前程序耗时:"+(endTime-startTime)+"ms");
}
catch(Exception e){
map.put("status", "failure");
map.put("message", e.getMessage());
e.printStackTrace();
}
//
return geojsonObject;
}
/**
* 工具类测试方法
*
* @param args
*/
public static void main(String[] args) {
ShpToGeojson shpToGeojson = new ShpToGeojson();
long start = System.currentTimeMillis();
// shape2Geojson
// String shpPath = "D:/geojsonTest/gaosu.shp";
// String jsonPath = "D:/geojsonTest/gaosu.geojson1";
// Map map = fileFormat.shape2Geojson(shpPath, jsonPath);
// geojson2Shape
String shpPath = "D:/矢量下载/123.shp";
String jsonPath = "D:/123.geojson";
Map map = shpToGeojson.geojson2Shape(jsonPath, shpPath);
System.out.println(shpPath +" "+ jsonPath + ",共耗时" + (System.currentTimeMillis() - start) + "ms");
}
}
...@@ -130,6 +130,14 @@ public class UploadController{ ...@@ -130,6 +130,14 @@ public class UploadController{
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix)); return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
} }
@RequestMapping(value = "/app/unauth/shp/upload", method = RequestMethod.POST)
public JSONObject shpUploads(
@RequestParam("file") MultipartFile file,
@RequestParam(value = "prefix",defaultValue = "shp")String prefix
)throws Exception {
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadShpFile(file,prefix));
}
@IgnoreUserToken @IgnoreUserToken
@RequestMapping(value = "/app/unauth/download", method = RequestMethod.GET) //匹配的是href中的download请求 @RequestMapping(value = "/app/unauth/download", method = RequestMethod.GET) //匹配的是href中的download请求
public ResponseEntity<byte[]> download(@RequestParam("realFileRelPath") String realFileRelPath) throws Exception { public ResponseEntity<byte[]> download(@RequestParam("realFileRelPath") String realFileRelPath) throws Exception {
......
...@@ -42,6 +42,31 @@ public class UploadService { ...@@ -42,6 +42,31 @@ public class UploadService {
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
/**
* 写入上传文件,返回相对路径
* @param file
* @return
*/
public String uploadShpFile(MultipartFile file,String prefix) throws Exception{
//创建本日存放目录
DateTime now = DateTime.now();
String dirPathToday = "/"+prefix+ "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
Long no = redisTemplate.opsForValue().increment(redisNoKey);
if(no.equals(1l)){
redisTemplate.expire(redisNoKey,1, TimeUnit.DAYS);
}
String fileName = file.getOriginalFilename();
String realFileRelPath = dirPathToday + "/" + no + fileName.substring(fileName.lastIndexOf("."));
//文件存放路径
String filePath = baseUploadPath + realFileRelPath;
//将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
redisTemplate.opsForValue().setIfAbsent(realFileRelPath, filePath);
// realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
return realFileRelPath;
}
/** /**
* 写入上传文件,返回相对路径 * 写入上传文件,返回相对路径
* @param file * @param file
...@@ -61,7 +86,7 @@ public class UploadService { ...@@ -61,7 +86,7 @@ public class UploadService {
//文件存放路径 //文件存放路径
String filePath = baseUploadPath + realFileRelPath; String filePath = baseUploadPath + realFileRelPath;
//将文件写入指定位置 //将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath)); FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath; realFileRelPath=xx_url+SystemConfig.XXMP_URL+realFileRelPath;
return realFileRelPath; return realFileRelPath;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment