|
| 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 | +} |
0 commit comments