Skip to content

Commit fdc0800

Browse files
committed
v1.7.0 release
1 parent 559674f commit fdc0800

File tree

15 files changed

+732
-143
lines changed

15 files changed

+732
-143
lines changed

.gitignore

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22
target
33

44
logs
5+
*.log
56

6-
# windows #
77
Thumbs.db
88

9-
# Mac #
109
.DS_Store
1110

12-
# eclipse #
1311
.settings
1412
.project
1513
.classpath
1614
.log
1715
*.class
1816

19-
# idea #
2017
.idea
2118
*.iml
2219

23-
# Package Files #
2420
*.war
2521
*.ear
2622
/target
2723
/public
2824

2925
jmhResult.json
26+
/D_tmp1rocksrtree/
27+
/arthas-output/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* * Copyright (c) 2022- "giscat (https://github.com/codingmiao/giscat)"
4+
* *
5+
* * 本项目采用自定义版权协议,在不同行业使用时有不同约束,详情参阅:
6+
* *
7+
* * https://github.com/codingmiao/giscat/blob/main/LICENSE
8+
*
9+
*/
10+
11+
package org.wowtools.giscat.vector.pojo;
12+
13+
import org.locationtech.jts.geom.GeometryFactory;
14+
15+
/**
16+
* 常量
17+
* @author liuyu
18+
* @date 2023/4/6
19+
*/
20+
public class PojoConstant {
21+
22+
/**
23+
* 通用的geometryFactory
24+
*/
25+
public static final GeometryFactory geometryFactory = new GeometryFactory();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
*
3+
* * Copyright (c) 2022- "giscat (https://github.com/codingmiao/giscat)"
4+
* *
5+
* * 本项目采用自定义版权协议,在不同行业使用时有不同约束,详情参阅:
6+
* *
7+
* * https://github.com/codingmiao/giscat/blob/main/LICENSE
8+
*
9+
*/
10+
11+
package org.wowtools.giscat.vector.pojo.converter;
12+
13+
14+
import org.locationtech.jts.geom.Coordinate;
15+
import org.locationtech.jts.geom.LineString;
16+
import org.locationtech.jts.geom.Point;
17+
import org.locationtech.jts.geom.Polygon;
18+
19+
import java.util.Iterator;
20+
import java.util.List;
21+
22+
import static org.wowtools.giscat.vector.pojo.PojoConstant.geometryFactory;
23+
24+
/**
25+
* 傻瓜式的对象转换工具,例如把一个坐标list转为一条线
26+
*
27+
* @author liuyu
28+
* @date 2023/4/6
29+
*/
30+
public class FoolStyleFeatureConverter {
31+
32+
/**
33+
* 将 x y转为point对象
34+
*
35+
* @param x x
36+
* @param y y
37+
* @return Point
38+
*/
39+
public static Point xy2Point(double x, double y) {
40+
return geometryFactory.createPoint(new Coordinate(x, y));
41+
}
42+
43+
/**
44+
* 将list转为point
45+
*
46+
* @param list list
47+
* @return Point
48+
*/
49+
public static Point list2Point(List<Double> list) {
50+
return geometryFactory.createPoint(new Coordinate(list.get(0), list.get(1)));
51+
}
52+
53+
/**
54+
* 将array转为point
55+
*
56+
* @param array array
57+
* @return Point
58+
*/
59+
public static Point array2Point(double[] array) {
60+
return geometryFactory.createPoint(new Coordinate(array[0], array[1]));
61+
}
62+
63+
/**
64+
* 将string转为point,例如str2Point("10,2",",")转为POINT(10 2)
65+
*
66+
* @param str str
67+
* @param regex 分隔符
68+
* @return Point
69+
*/
70+
public static Point str2Point(String str, String regex) {
71+
String[] strs = str.trim().split(regex);
72+
return geometryFactory.createPoint(new Coordinate(Double.parseDouble(strs[0]), Double.parseDouble(strs[1])));
73+
}
74+
75+
/**
76+
* 将list转为线
77+
*
78+
* @param list list 例如[1,2,3,4]转为LINESTRING(1 2,3 4)
79+
* @return LineString
80+
*/
81+
public static LineString list2Line(List<Double> list) {
82+
Coordinate[] coords = new Coordinate[list.size() / 2];
83+
int i = 0;
84+
Iterator<Double> iterator = list.iterator();
85+
while (iterator.hasNext()) {
86+
double x = iterator.next();
87+
double y = iterator.next();
88+
coords[i] = new Coordinate(x, y);
89+
i++;
90+
}
91+
return geometryFactory.createLineString(coords);
92+
}
93+
94+
/**
95+
* 将array转为线
96+
*
97+
* @param array list 例如[1,2,3,4]转为LINESTRING(1 2,3 4)
98+
* @return LineString
99+
*/
100+
public static LineString array2Line(double[] array) {
101+
Coordinate[] coords = new Coordinate[array.length / 2];
102+
for (int i = 0; i < array.length; i += 2) {
103+
double x = array[i];
104+
double y = array[i + 1];
105+
coords[i / 2] = new Coordinate(x, y);
106+
}
107+
return geometryFactory.createLineString(coords);
108+
}
109+
110+
/**
111+
* 将list转为线
112+
*
113+
* @param list list 例如[[1,2],[3,4]]转为LINESTRING(1 2,3 4)
114+
* @return LineString
115+
*/
116+
public static LineString lists2Line(List<double[]> list) {
117+
Coordinate[] coords = new Coordinate[list.size()];
118+
int i = 0;
119+
for (double[] doubles : list) {
120+
double[] ds = doubles;
121+
coords[i] = new Coordinate(ds[0], ds[1]);
122+
i++;
123+
}
124+
return geometryFactory.createLineString(coords);
125+
}
126+
127+
/**
128+
* 将array转为线
129+
*
130+
* @param array array 例如[[1,2],[3,4]]转为LINESTRING(1 2,3 4)
131+
* @return LineString
132+
*/
133+
public static LineString arrays2Line(double[][] array) {
134+
Coordinate[] coords = new Coordinate[array.length];
135+
for (int i = 0; i < array.length; i++) {
136+
double[] ds = array[i];
137+
coords[i] = new Coordinate(ds[0], ds[1]);
138+
}
139+
return geometryFactory.createLineString(coords);
140+
}
141+
142+
/**
143+
* 将string转为线,例如str2Line("10,2;15,3",",",";")转为LINESTRING(10 2,15 3)
144+
*
145+
* @param str str
146+
* @param xyRegex xy坐标间的分隔符
147+
* @param pointRegex 点之间的分隔符
148+
* @return LineString
149+
*/
150+
public static LineString str2Line(String str, String xyRegex, String pointRegex) {
151+
String[] pointArray = str.trim().split(pointRegex);
152+
Coordinate[] coords = new Coordinate[pointArray.length];
153+
for (int i = 0; i < pointArray.length; i++) {
154+
String[] xy = pointArray[i].trim().split(xyRegex);
155+
coords[i] = new Coordinate(Double.parseDouble(xy[0]), Double.parseDouble(xy[1]));
156+
}
157+
return geometryFactory.createLineString(coords);
158+
}
159+
160+
161+
/**
162+
* 将list转为面
163+
*
164+
* @param list list 例如[1,2,3,4,6,6]转为POLYGON ((1 2, 3 4, 6 6, 1 2))
165+
* @return Polygon
166+
*/
167+
public static Polygon list2Polygon(List<Double> list) {
168+
double[] array = new double[list.size()];
169+
int i = 0;
170+
for (Double v : list) {
171+
array[i] = v;
172+
i++;
173+
}
174+
return array2Polygon(array);
175+
}
176+
177+
/**
178+
* 将array转为面
179+
*
180+
* @param array array 例如[1,2,3,4,6,6]转为POLYGON ((1 2, 3 4, 6 6, 1 2))
181+
* @return Polygon
182+
*/
183+
public static Polygon array2Polygon(double[] array) {
184+
boolean endSame = array[array.length - 1] == array[1] && array[array.length - 2] == array[0];
185+
186+
Coordinate[] coords = new Coordinate[endSame ? array.length / 2 : array.length / 2 + 1];
187+
for (int i = 0; i < array.length; i += 2) {
188+
double x = array[i];
189+
double y = array[i + 1];
190+
coords[i / 2] = new Coordinate(x, y);
191+
}
192+
if (!endSame) {
193+
coords[coords.length - 1] = coords[0].copy();
194+
}
195+
return geometryFactory.createPolygon(coords);
196+
}
197+
198+
/**
199+
* 将list转为面
200+
*
201+
* @param list list 例如[[1,2],[3,4],[6,6]]转为POLYGON ((1 2, 3 4, 6 6, 1 2))
202+
* @return Polygon
203+
*/
204+
public static Polygon lists2Polygon(List<double[]> list) {
205+
double[][] array = new double[list.size()][];
206+
int i = 0;
207+
for (double[] v : list) {
208+
array[i] = v;
209+
i++;
210+
}
211+
return arrays2Polygon(array);
212+
}
213+
214+
/**
215+
* 将array转为面
216+
*
217+
* @param array array 例如[[1,2],[3,4],[6,6]]转为POLYGON ((1 2, 3 4, 6 6, 1 2))
218+
* @return Polygon
219+
*/
220+
public static Polygon arrays2Polygon(double[][] array) {
221+
boolean endSame = array[0][0] == array[array.length - 1][0] && array[0][1] == array[array.length - 1][1];
222+
Coordinate[] coords = new Coordinate[endSame ? array.length : array.length + 1];
223+
for (int i = 0; i < array.length; i ++) {
224+
double[] xy = array[i];
225+
coords[i] = new Coordinate(xy[0], xy[1]);
226+
}
227+
if (!endSame) {
228+
coords[coords.length - 1] = coords[0].copy();
229+
}
230+
return geometryFactory.createPolygon(coords);
231+
}
232+
233+
/**
234+
* 将string转为线,例如str2Line("1,2;3,4;6,6",",",";")转为POLYGON ((1 2, 3 4, 6 6, 1 2))
235+
*
236+
* @param str str
237+
* @param xyRegex xy坐标间的分隔符
238+
* @param pointRegex 点之间的分隔符
239+
* @return Polygon
240+
*/
241+
public static Polygon str2Polygon(String str, String xyRegex, String pointRegex){
242+
String[] points = str.trim().split(pointRegex);
243+
double[][] array = new double[points.length][];
244+
int i = 0;
245+
for (String point: points) {
246+
String[] xy = point.trim().split(xyRegex);
247+
array[i] = new double[]{Double.parseDouble(xy[0]),Double.parseDouble(xy[1])};
248+
i++;
249+
}
250+
return arrays2Polygon(array);
251+
}
252+
253+
254+
255+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.wowtools.giscat.vector.pojo.converter;
2+
3+
import junit.framework.TestCase;
4+
import org.junit.Assert;
5+
import org.locationtech.jts.geom.LineString;
6+
import org.locationtech.jts.geom.Point;
7+
import org.locationtech.jts.geom.Polygon;
8+
9+
import java.util.List;
10+
11+
public class FoolStyleFeatureConverterTest extends TestCase {
12+
13+
public void testXy2Point() {
14+
Point point = FoolStyleFeatureConverter.xy2Point(20, 30);
15+
Assert.assertEquals("POINT (20 30)", point.toText());
16+
}
17+
18+
public void testList2Point() {
19+
Point point = FoolStyleFeatureConverter.list2Point(List.of(20d, 30d));
20+
Assert.assertEquals("POINT (20 30)", point.toText());
21+
}
22+
23+
public void testArray2Point() {
24+
Point point = FoolStyleFeatureConverter.array2Point(new double[]{20, 30});
25+
Assert.assertEquals("POINT (20 30)", point.toText());
26+
}
27+
28+
public void testStr2Point() {
29+
Point point = FoolStyleFeatureConverter.str2Point("20 30", " ");
30+
Assert.assertEquals("POINT (20 30)", point.toText());
31+
}
32+
33+
public void testList2Line() {
34+
LineString line = FoolStyleFeatureConverter.list2Line(List.of(1d, 2d, 3d, 4d));
35+
Assert.assertEquals("LINESTRING (1 2, 3 4)", line.toText());
36+
}
37+
38+
public void testArray2Line() {
39+
LineString line = FoolStyleFeatureConverter.array2Line(new double[]{1, 2, 3, 4});
40+
Assert.assertEquals("LINESTRING (1 2, 3 4)", line.toText());
41+
}
42+
43+
public void testLists2Line() {
44+
LineString line = FoolStyleFeatureConverter.lists2Line(List.of(new double[]{1, 2}, new double[]{3, 4}));
45+
Assert.assertEquals("LINESTRING (1 2, 3 4)", line.toText());
46+
}
47+
48+
public void testArrays2Line() {
49+
LineString line = FoolStyleFeatureConverter.arrays2Line(new double[][]{new double[]{1, 2}, new double[]{3, 4}});
50+
Assert.assertEquals("LINESTRING (1 2, 3 4)", line.toText());
51+
}
52+
53+
public void testStr2Line() {
54+
LineString line = FoolStyleFeatureConverter.str2Line("1,2;3,4",",",";");
55+
Assert.assertEquals("LINESTRING (1 2, 3 4)", line.toText());
56+
}
57+
58+
public void testList2Polygon() {
59+
Polygon polygon = FoolStyleFeatureConverter.list2Polygon(List.of(1d, 2d, 3d, 4d, 6d, 6d));
60+
Assert.assertEquals("POLYGON ((1 2, 3 4, 6 6, 1 2))", polygon.toText());
61+
}
62+
63+
public void testArray2Polygon() {
64+
Polygon polygon = FoolStyleFeatureConverter.array2Polygon(new double[]{1d, 2d, 3d, 4d, 6d, 6d});
65+
Assert.assertEquals("POLYGON ((1 2, 3 4, 6 6, 1 2))", polygon.toText());
66+
}
67+
68+
public void testLists2Polygon() {
69+
Polygon polygon = FoolStyleFeatureConverter.lists2Polygon(List.of(new double[]{1,2},new double[]{3,4},new double[]{6,6}));
70+
Assert.assertEquals("POLYGON ((1 2, 3 4, 6 6, 1 2))", polygon.toText());
71+
}
72+
73+
public void testArrays2Polygon() {
74+
Polygon polygon = FoolStyleFeatureConverter.arrays2Polygon(new double[][]{new double[]{1,2},new double[]{3,4},new double[]{6,6}});
75+
Assert.assertEquals("POLYGON ((1 2, 3 4, 6 6, 1 2))", polygon.toText());
76+
}
77+
78+
public void testStr2Polygon() {
79+
Polygon polygon = FoolStyleFeatureConverter.str2Polygon("1 2,3 4,6 6, 1 2"," ",",");
80+
Assert.assertEquals("POLYGON ((1 2, 3 4, 6 6, 1 2))", polygon.toText());
81+
}
82+
}

0 commit comments

Comments
 (0)