Geometry坐标转换工具

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值