1)引入依赖
<properties>
<!-- geo地图jts依赖 -->
<vividsolutions-jts.version>1.12</vividsolutions-jts.version>
<!-- geo地图工具 -->
<geotools.version>18.4</geotools.version>
<json-simple.version>1.1</json-simple.version>
</properties>
如果有包拉不下来,可以在pom.xml配置仓库地址
<profiles>
<profile>
<id>osgeo</id>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>osgeo</id>
<name>plug-OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
2)代码
package com.****.utils;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import net.sf.json.JSONObject;
import org.geotools.geojson.geom.GeometryJSON;
import java.io.IOException;
import java.io.StringWriter;
/**
* @author vector
* @version 1.0
*/
public class GeometryUtil {
private GeometryFactory geometryFactory;
private GeometryUtil(GeometryFactory geometryFactory) {
this.geometryFactory = geometryFactory;
}
public static GeometryUtil builder() {
GeometryFactory gf = new GeometryFactory();
return new GeometryUtil(gf);
}
public static GeometryUtil builder(int srid) {
GeometryFactory gf = new GeometryFactory(new PrecisionModel(), srid);
return new GeometryUtil(gf);
}
/**
* Polygon
* Polygon转double[][]
*
* @param polygon
* @return
*/
public double[][] polygon2double(Polygon polygon) {
double[][] path = new double[polygon.getCoordinates().length][];
for (int i = 0; i < polygon.getCoordinates().length; i++) {
double[] t = {polygon.getCoordinates()[i].x, polygon.getCoordinates()[i].y};
path[i] = t;
}
return path;
}
/**
* Polygon
* double[][][]转MultiPolygon
*
* @param pol
* @return
*/
public MultiPolygon double2MultiPolygon(double[][][] pol) {
Polygon[] polygons = new Polygon[pol.length];
for (int i = 0; i < pol.length; i++) {
polygons[i] = double2Polygon(pol[i]);
}
return geometryFactory.createMultiPolygon(polygons);
}
/**
* Polygon
* double[][]转Polygon
*
* @param coo
* @return
*/
public Polygon double2Polygon(double[][] coo) {
Coordinate[] coordinates = new Coordinate[coo.length];
for (int i = 0; i < coo.length; i++) {
coordinates[i] = new Coordinate(coo[i][0], coo[i][1]);
}
LinearRing linear = new GeometryFactory().createLinearRing(coordinates);
return geometryFactory.createPolygon(linear, null);
}
/**
* create a polygon(多边形) by WKT
*
* @return
* @throws ParseException
*/
@SuppressWarnings("unchecked")
public Polygon createPolygonByWKT(String wkt) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
return (Polygon) reader.read(wkt);
}
/**
* create a geom by WKT
*
* @return
* @throws ParseException
*/
@SuppressWarnings("unchecked")
public <T extends Geometry> T createGeomByWKT(String wkt) throws ParseException {
WKTReader reader = new WKTReader(geometryFactory);
return (T) reader.read(wkt);
}
/**
* double转Point
*
* @param pot
* @return
*/
public Point double2point(double[] pot) {
Coordinate coord = new Coordinate(pot[0], pot[1]);
return geometryFactory.createPoint(coord);
}
/**
* Point转double
*
* @param point
* @return
*/
public double[] point2double(Point point) {
return new double[]{point.getX(), point.getY()};
}
/**
* json转geometry对象
*
* @param json
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public <T extends Geometry> T json2geometry(String json) throws IOException {
// 设置保留6位小数,默认4位
GeometryJSON geometryJson = new GeometryJSON(6);
return (T) geometryJson.read(json);
}
/**
* geometry转json对象
*
* @param geometry
* @return
*/
@SuppressWarnings("unchecked")
public <T> T geometry2json(Class<T> clazz, Geometry geometry) throws IOException {
// 设置保留6位小数,默认4位
GeometryJSON geometryJson = new GeometryJSON(6);
try (StringWriter writer = new StringWriter()) {
geometryJson.write(geometry, writer);
JSONObject jsonObject = JSONObject.fromObject(writer.toString());
return (T) JSONObject.toBean(jsonObject, clazz);
}
}
}