12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package com.sysu.admin.utils.shape;
- import org.geotools.geojson.geom.GeometryJSON;
- import org.geotools.geometry.jts.WKTWriter2;
- import org.locationtech.jts.geom.*;
- import org.opengis.feature.type.GeometryDescriptor;
- import java.io.IOException;
- import java.io.StringReader;
- public class GeoCastUtil {
- private static WKTWriter2 wktReader2 = new WKTWriter2();
- public static String geomToWkt(Geometry geometry){
- return wktReader2.write(geometry);
- }
- private static GeometryCollection castGeometryCollection2(GeometryJSON fjson, GeometryCollection geometryCollection) throws IOException {
- if (geometryCollection.getGeometryType().equals("GeometryCollection")) {
- return geometryCollection;
- }
- String[] geometriesJsonArray = new String[geometryCollection.getNumGeometries()];
- int length = 0;
- for(int i=0; i< geometryCollection.getNumGeometries(); i++){
- Geometry geometry = geometryCollection.getGeometryN(i);
- geometriesJsonArray[i] = fjson.toString(geometry);
- length += geometriesJsonArray[i].length();
- }
- StringBuffer result = new StringBuffer(length + 100);
- result.append("{\"type\": \"GeometryCollection\",\"geometries\": [");
- result.append(geometriesJsonArray[0]);
- for(int i=1;i< geometriesJsonArray.length;i++){
- result.append(",").append(geometriesJsonArray[i]);
- }
- result.append("]}");
- return fjson.readGeometryCollection(new StringReader(result.toString()));
- }
- public static GeometryCollection castGeometryCollection(GeometryJSON fjson, Geometry geometry) throws IOException {
- if (geometry.getGeometryType().equals("GeometryCollection")) {
- return (GeometryCollection)geometry;
- }
- if (geometry instanceof GeometryCollection) {
- return castGeometryCollection2(fjson, (GeometryCollection)geometry);
- }
- String geometryJson = fjson.toString(geometry);
- StringBuffer result = new StringBuffer(geometryJson.length() + 100);
- result.append("{\"type\": \"GeometryCollection\",\"geometries\": [");
- result.append(geometryJson);
- result.append("]}");
- return fjson.readGeometryCollection(new StringReader(result.toString()));
- }
- public static Geometry readGeometry(Object input, String type, GeometryJSON gjson) throws IOException {
- switch(type){
- case "Point":
- return gjson.readPoint(input);
- case "MultiPoint":
- return gjson.readMultiPoint(input);
- case "LineString":
- return gjson.readLine(input);
- case "MultiLineString":
- return gjson.readMultiLine(input);
- case "Polygon":
- return gjson.readPolygon(input);
- case "MultiPolygon":
- return gjson.readMultiPolygon(input);
- }
- return null;
- }
- }
|