庞家镇
This commit is contained in:
@ -129,7 +129,42 @@
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.locationtech.jts</groupId>
|
||||
<artifactId>jts-core</artifactId>
|
||||
<version>1.16.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>20.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-geojson</artifactId>
|
||||
<version>20.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<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>
|
||||
|
||||
</project>
|
||||
|
@ -76,6 +76,14 @@ public class DateUtil {
|
||||
return cal.getTime();
|
||||
}
|
||||
|
||||
//获取七天前的开始时间
|
||||
public static Date getBeginDayOf7day() {
|
||||
Calendar cal = new GregorianCalendar();
|
||||
cal.setTime(getDayBegin());
|
||||
cal.add(Calendar.DAY_OF_MONTH, -7);
|
||||
return cal.getTime();
|
||||
}
|
||||
|
||||
// 获取本周的开始时间
|
||||
@SuppressWarnings("unused")
|
||||
public static Date getBeginDayOfWeek() {
|
||||
@ -233,6 +241,27 @@ public class DateUtil {
|
||||
long date2ms = endDate.getTime();
|
||||
return date2ms - date1ms;
|
||||
}
|
||||
//两个日期相差多少小时
|
||||
public static String dateDiffHours(Date beginDate,Date endDate){
|
||||
long nd = 1000 * 24 * 60 * 60;
|
||||
float nh = 1000 * 60 * 60;
|
||||
long nm = 1000 * 60;
|
||||
// long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
float diff = endDate.getTime() - beginDate.getTime();
|
||||
// 计算差多少天
|
||||
// long day = diff / nd;
|
||||
// 计算差多少小时
|
||||
// float hour = diff % nd / nh;
|
||||
float hour = diff / nh;
|
||||
String fhour = String.format("%.2f", hour);
|
||||
// 计算差多少分钟
|
||||
// long min = diff % nd % nh / nm;
|
||||
// 计算差多少秒//输出结果
|
||||
// long sec = diff % nd % nh % nm / ns;
|
||||
// return day + "天" + hour + "小时" + min + "分钟";
|
||||
return fhour;
|
||||
}
|
||||
|
||||
// 获取两个日期中的最大日期
|
||||
public static Date max(Date beginDate, Date endDate) {
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
public class MyPoint {
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(double x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(double y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public MyPoint() {
|
||||
}
|
||||
|
||||
public MyPoint(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MyPoint{" +
|
||||
"x=" + x +
|
||||
", y=" + y +
|
||||
'}';
|
||||
}
|
||||
}
|
414
ruoyi-common/src/main/java/com/ruoyi/common/utils/ShapeUtil.java
Normal file
414
ruoyi-common/src/main/java/com/ruoyi/common/utils/ShapeUtil.java
Normal file
@ -0,0 +1,414 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.geotools.data.DefaultTransaction;
|
||||
import org.geotools.data.FileDataStore;
|
||||
import org.geotools.data.FileDataStoreFinder;
|
||||
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.data.simple.SimpleFeatureStore;
|
||||
import org.geotools.feature.DefaultFeatureCollection;
|
||||
import org.geotools.feature.simple.SimpleFeatureBuilder;
|
||||
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
|
||||
import org.geotools.geometry.jts.JTS;
|
||||
import org.geotools.geometry.jts.JTSFactoryFinder;
|
||||
import org.geotools.referencing.CRS;
|
||||
import org.locationtech.jts.geom.*;
|
||||
import org.locationtech.jts.io.ParseException;
|
||||
import org.locationtech.jts.io.WKTReader;
|
||||
import org.opengis.feature.Property;
|
||||
import org.opengis.feature.simple.SimpleFeature;
|
||||
import org.opengis.feature.simple.SimpleFeatureType;
|
||||
import org.opengis.referencing.FactoryException;
|
||||
import org.opengis.referencing.crs.CoordinateReferenceSystem;
|
||||
import org.opengis.referencing.operation.MathTransform;
|
||||
import org.opengis.referencing.operation.TransformException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ShapeUtil {
|
||||
private static final Logger logger = LoggerFactory.getLogger("ShapeUtil.class");
|
||||
static class polygonPoint {
|
||||
public double x;
|
||||
public double y;
|
||||
|
||||
public polygonPoint(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成shp文件
|
||||
* @param wkt wkt格式 经纬度必须首尾相连
|
||||
* @throws IOException
|
||||
* @throws FactoryException
|
||||
*/
|
||||
public static String writeShape(String wkt,String geoType,String targetPath,List userdata) throws Exception {
|
||||
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
|
||||
builder.setName("Feature");
|
||||
/**
|
||||
* //方式一
|
||||
* CRS.decode("EPSG:4326");
|
||||
* //方式二
|
||||
* DefaultGeographicCRS.WGS84;
|
||||
*
|
||||
* EPSG:4490为大地坐标系_国家2000大地坐标系CGCS2000
|
||||
* EPSG:4326支持一种默认的定义方式, 坐标系WGS84
|
||||
*/
|
||||
// builder.setCRS(DefaultGeographicCRS.WGS84);
|
||||
builder.setCRS(CRS.decode("EPSG:4326"));
|
||||
//农机编号
|
||||
builder.add("vehicleno", String.class);
|
||||
//作业地块
|
||||
builder.add("workplace", String.class);
|
||||
//作业类型
|
||||
builder.add("jobtype", String.class);
|
||||
|
||||
if ("Polygon".equals(geoType) || "polygon".equals(geoType)) {
|
||||
builder.add("the_geom", Polygon.class);
|
||||
} else if ("MultiPolygon".equals(geoType)) {
|
||||
builder.add("the_geom", MultiPolygon.class);
|
||||
} else if ("Point".equals(geoType) || "point".equals(geoType)) {
|
||||
builder.add("the_geom", Point.class);
|
||||
} else if ("MultiPoint".equals(geoType)) {
|
||||
builder.add("the_geom", MultiPoint.class);
|
||||
} else if ("LineString".equals(geoType)) {
|
||||
builder.add("the_geom", LineString.class);
|
||||
} else if ("MultiLineString".equals(geoType) || "Polyline".equals(geoType) || "polyline".equals(geoType)) {
|
||||
builder.add("the_geom", MultiLineString.class);
|
||||
} else {
|
||||
throw new Exception("Geometry中没有该类型:" + geoType);
|
||||
}
|
||||
SimpleFeatureType featureType = builder.buildFeatureType();
|
||||
// DefaultFeatureCollection collection = new DefaultFeatureCollection();
|
||||
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
|
||||
|
||||
// SimpleFeature feature = (SimpleFeature) toFeature(wkt, featureType, geometryFactory,geoType);
|
||||
DefaultFeatureCollection collection = toFeature(wkt, featureType, geometryFactory, geoType,userdata);
|
||||
// collection.add(feature);
|
||||
|
||||
|
||||
// collection.forEach(name -> System.out.println(name));
|
||||
// File shapeFile = new File(new File("").getAbsolutePath() +
|
||||
// "\\ruoyi-admin\\src\\main\\resources\\shp\\shapefile.shp");
|
||||
|
||||
File shapeFile = new File(targetPath);
|
||||
// String fileName = RuoYiConfig.getUploadPath() + "/shpFile/" + LocalDate.now() + "shapefile.shp";
|
||||
// File shapeFile = new File(fileName);
|
||||
Map<String, Serializable> params = new HashMap<>();
|
||||
params.put("url", shapeFile.toURI().toURL());
|
||||
params.put("create spatial index", Boolean.TRUE);
|
||||
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
||||
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
||||
|
||||
// dataStore.setCharset(StandardCharsets.UTF_8);
|
||||
|
||||
dataStore.createSchema(featureType);
|
||||
Transaction transaction = new DefaultTransaction("create");
|
||||
String typeName = dataStore.getTypeNames()[0];
|
||||
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
|
||||
|
||||
if (featureSource instanceof SimpleFeatureStore) {
|
||||
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
|
||||
featureStore.setTransaction(transaction);
|
||||
try {
|
||||
featureStore.addFeatures(collection);
|
||||
transaction.commit();
|
||||
zipShapeFile(shapeFile.getPath());
|
||||
} catch (Exception problem) {
|
||||
transaction.rollback();
|
||||
} finally {
|
||||
transaction.close();
|
||||
}
|
||||
}
|
||||
logger.info("生成shp文件成功:"+shapeFile.getAbsolutePath());
|
||||
return shapeFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建要素
|
||||
*
|
||||
* @param wkt
|
||||
* @param featureType
|
||||
* @param geometryFactory
|
||||
* @return
|
||||
*/
|
||||
public static DefaultFeatureCollection toFeature(String wkt, SimpleFeatureType featureType,
|
||||
GeometryFactory geometryFactory, String geoType,List userdata) throws Exception {
|
||||
//通过坐标对象Coordinate创建
|
||||
// Coordinate[] coords = new Coordinate[locations.size()];
|
||||
// int i = 0;
|
||||
// for (MyPoint location : locations) {
|
||||
// Coordinate coord = new Coordinate(location.getX(), location.getY(), 0);
|
||||
// coords[i] = (coord);
|
||||
// i++;
|
||||
// }
|
||||
// Polygon polygon = geometryFactory.createPolygon(coords);
|
||||
|
||||
//通过WKT创建
|
||||
WKTReader reader = new WKTReader(geometryFactory);
|
||||
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
|
||||
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
|
||||
if ("Polygon".equals(geoType) || "polygon".equals(geoType)) {
|
||||
Polygon polygon = (Polygon) reader.read(wkt);
|
||||
featureBuilder.add(polygon);
|
||||
} else if ("MultiPolygon".equals(geoType)) {
|
||||
MultiPolygon multiPolygon = (MultiPolygon) reader.read(wkt);
|
||||
int index = -1;
|
||||
for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
|
||||
Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
|
||||
featureBuilder.add(polygon);
|
||||
SimpleFeature feature = featureBuilder.buildFeature(null);
|
||||
featureCollection.add(feature);
|
||||
}
|
||||
return featureCollection;
|
||||
} else if ("Point".equals(geoType) || "point".equals(geoType)) {
|
||||
Point point = (Point) reader.read(wkt);
|
||||
featureBuilder.add(point);
|
||||
} else if ("MultiPoint".equals(geoType)) {
|
||||
MultiPoint multiPoint = (MultiPoint) reader.read(wkt);
|
||||
featureBuilder.add(multiPoint);
|
||||
} else if ("LineString".equals(geoType)) {
|
||||
LineString lineString = (LineString) reader.read(wkt);
|
||||
featureBuilder.add(lineString);
|
||||
} else if ("MultiLineString".equals(geoType) || "Polyline".equals(geoType) || "polyline".equals(geoType)) {
|
||||
MultiLineString multiLineString = (MultiLineString) reader.read(wkt);
|
||||
int index = -1;
|
||||
for (int i = 0; i < multiLineString.getNumGeometries(); i++) {
|
||||
LineString lineString = (LineString) multiLineString.getGeometryN(i);
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
featureBuilder.add(userdata.get(++index));
|
||||
|
||||
featureBuilder.add(lineString);
|
||||
SimpleFeature feature = featureBuilder.buildFeature(null);
|
||||
featureCollection.add(feature);
|
||||
}
|
||||
return featureCollection;
|
||||
} else {
|
||||
throw new Exception("Geometry中没有该类型:" + geoType);
|
||||
}
|
||||
featureCollection.add(featureBuilder.buildFeature(null));
|
||||
return featureCollection;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取shp文件
|
||||
* @param shpPath
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static List<Map<String, Object>> readShp(String shpPath) throws IOException {
|
||||
//加载文件
|
||||
File file = new File(shpPath);
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
//map记录shapefile key-value数据
|
||||
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
||||
//通过store获取featurecollection
|
||||
FileDataStore store = FileDataStoreFinder.getDataStore(file);
|
||||
SimpleFeatureSource featureSource = store.getFeatureSource();
|
||||
SimpleFeatureCollection simpleFeatureCollection = featureSource.getFeatures();
|
||||
SimpleFeatureIterator itertor = simpleFeatureCollection.features();
|
||||
//遍历featurecollection
|
||||
while (itertor.hasNext()) {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
SimpleFeature feature = itertor.next();
|
||||
Collection<Property> p = feature.getProperties();
|
||||
Iterator<Property> it = p.iterator();
|
||||
//遍历feature的properties
|
||||
while (it.hasNext()) {
|
||||
Property pro = it.next();
|
||||
String field = pro.getName().toString();
|
||||
String value = pro.getValue().toString();
|
||||
field = field.equals("the_geom") ? "wkt" : field;
|
||||
byte[] bytes = value.getBytes("iso8859-1");
|
||||
value = new String(bytes, "gbk");
|
||||
data.put(field, value);
|
||||
}
|
||||
list.add(data);
|
||||
}
|
||||
itertor.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据wkt计算多边形面积
|
||||
* @param wkt 待计算wkt
|
||||
* @return 面积数据
|
||||
*/
|
||||
public static double getAreaByWkt(String wkt){
|
||||
Geometry geometry ;
|
||||
WKTReader reader = new WKTReader();
|
||||
try {
|
||||
geometry = reader.read(wkt);
|
||||
} catch (ParseException e) {
|
||||
geometry = null;
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getArea(geometry);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据多边形类型计算出多边形面积,单位(平方米)
|
||||
* @param geometry 多边形对象
|
||||
* @return 面积
|
||||
*/
|
||||
public static double getArea(Geometry geometry){
|
||||
CoordinateReferenceSystem source = null;
|
||||
try {
|
||||
// WGS84(一般项目中常用的是CSR:84和EPSG:4326)
|
||||
source = CRS.decode("EPSG:4326");
|
||||
} catch (FactoryException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
CoordinateReferenceSystem target = null;
|
||||
try {
|
||||
target = CRS.decode("EPSG:3857");
|
||||
// target = CRS.decode("EPSG:3005");
|
||||
} catch (FactoryException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
MathTransform transform = null;
|
||||
try {
|
||||
transform = CRS.findMathTransform(source, target, true);
|
||||
} catch (FactoryException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Geometry transform1 = null;
|
||||
try {
|
||||
transform1 = JTS.transform(geometry, transform);
|
||||
} catch (TransformException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
double area = transform1.getArea();
|
||||
//周长
|
||||
double length = transform1.getLength();
|
||||
return area;
|
||||
}
|
||||
|
||||
/**
|
||||
* 压缩shape文件
|
||||
*
|
||||
* @param shpPath shape文件路径(包含shape文件名称)
|
||||
*/
|
||||
public static void zipShapeFile(String shpPath) {
|
||||
try {
|
||||
File shpFile = new File(shpPath);
|
||||
String shpRoot = shpFile.getParentFile().getPath();
|
||||
String shpName = shpFile.getName().substring(0, shpFile.getName().lastIndexOf("."));
|
||||
|
||||
String zipPath = shpRoot + File.separator + shpName + ".zip";
|
||||
File zipFile = new File(zipPath);
|
||||
InputStream input = null;
|
||||
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
|
||||
// zip的名称为
|
||||
zipOut.setComment(shpName);
|
||||
String[] shpFiles = new String[]{
|
||||
shpRoot + File.separator + shpName + ".dbf",
|
||||
shpRoot + File.separator + shpName + ".prj",
|
||||
shpRoot + File.separator + shpName + ".shp",
|
||||
shpRoot + File.separator + shpName + ".shx",
|
||||
shpRoot + File.separator + shpName + ".fix"
|
||||
};
|
||||
|
||||
for (int i = 0; i < shpFiles.length; i++) {
|
||||
File file = new File(shpFiles[i]);
|
||||
input = new FileInputStream(file);
|
||||
zipOut.putNextEntry(new ZipEntry(file.getName()));
|
||||
int temp = 0;
|
||||
while ((temp = input.read()) != -1) {
|
||||
zipOut.write(temp);
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
zipOut.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*文件解压缩
|
||||
* @param zipfile 要解压缩的zip文件
|
||||
* @param destpath 解压后文件所放的目录,需要"D:\\"或"D:\\test\"格式
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String unzip(String zipfile, String destpath) throws IOException {
|
||||
try {
|
||||
File zipFile = new File(zipfile);
|
||||
if (!zipFile.exists()) {
|
||||
throw new IOException("要解压的压缩文件不存在");
|
||||
}
|
||||
File pathFile = new File(destpath);
|
||||
if (!pathFile.exists()) {
|
||||
pathFile.mkdirs();
|
||||
}
|
||||
FileInputStream fis = new FileInputStream(zipfile);
|
||||
ZipInputStream zis = new ZipInputStream(fis);
|
||||
ZipEntry z1 = null;
|
||||
while ((z1 = zis.getNextEntry()) != null) {
|
||||
if (z1.isDirectory()) {
|
||||
File f = new File("D:\\" + z1.getName());
|
||||
f.mkdirs();
|
||||
} else {
|
||||
String fileName = z1.getName();
|
||||
FileOutputStream fos = new FileOutputStream(destpath + fileName);
|
||||
int tmp = -1;
|
||||
while ((tmp = zis.read()) != -1) {
|
||||
/*写入到目标文件中*/
|
||||
fos.write(tmp);
|
||||
}
|
||||
zis.closeEntry();
|
||||
fos.close();
|
||||
}
|
||||
}
|
||||
zis.close();
|
||||
} catch (Exception e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
return "解压成功!文件路径:"+destpath;
|
||||
}
|
||||
|
||||
//将经纬度数组转换为wkt格式
|
||||
public static String point2WktPolygon(List<MyPoint> points) {
|
||||
StringBuilder sb = new StringBuilder("POLYGON((");
|
||||
for (MyPoint point : points) {
|
||||
sb.append(point.getX()).append(" ").append(point.getY()).append(",");
|
||||
}
|
||||
sb.append(points.get(0).getX()).append(" ").append(points.get(0).getY()).append("))");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String point2WktLine(List<MyPoint> points) {
|
||||
StringBuilder sb = new StringBuilder("LINESTRING(");
|
||||
for (MyPoint point : points) {
|
||||
sb.append(point.getX()).append(" ").append(point.getY()).append(", ");
|
||||
}
|
||||
sb.append(points.get(0).getX()).append(" ").append(points.get(0).getY()).append(")");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user