geotools的multipolygon数据脱密

脱密思路:以geojson形式获取postgis的数据,并将geojson数据通过geotools转换为multipolygon,遍历multipolygon中的polygon,并将polygon中的外环点位随机偏移1-5米的范围实现multipolygon数据的脱密

1.遍历multipolygon的polygon,将点位数据偏移后重新生成polygon,并最后返回MultiPolygon

   private MultiPolygon multiPolygonDecrypt(MultiPolygon multiPolygon) {
        //获取geometry中的坐标 这里用string的方式保存
        int geoUnm = multiPolygon.getNumGeometries();  // 一个geometry可能含有n个geometry
        Polygon[] polygons = new Polygon[geoUnm];
        for (int j = 0; j < geoUnm; j++) {
            Polygon singleGeo =(Polygon) multiPolygon.getGeometryN(j); //获取其中每一个geometry
            int numGeometries = singleGeo.getNumGeometries();
            LinearRing exteriorRing = (LinearRing)singleGeo.getExteriorRing();
            int numInteriorRing = singleGeo.getNumInteriorRing();
            LinearRing holes[] = new LinearRing[numInteriorRing];
            for (int k = 0; k < numInteriorRing; k++) {
                LinearRing interiorRing = (LinearRing)singleGeo.getInteriorRingN(k);
                holes[k] = interiorRing;
            }
            //int pointCount = singleGeo.getNumPoints();
            Polygon Polygon1 = createPolygon(exteriorRing,holes);
            Polygon1.setSRID(4490);
            //Coordinate[] coords = singleGeo.getCoordinates();
            polygons[j] = Polygon1;
        }

       // MultiPolygon _multiPolygon = new MultiPolygon(polygons);
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
        MultiPolygon _multiPolygon = geometryFactory.createMultiPolygon(polygons);
        return _multiPolygon;
    }

2.shell中的点随机偏移1-5米的范围,并生成新的polygon

    public Polygon createPolygon(LinearRing shell,LinearRing holes[]){
        Polygon polygon = null;
        double random = Math.random();
        double brng=360 * random;
        double dist=5 * random;
        LonLatDecryptUtility llDecryptUt=new LonLatDecryptUtility();
        try{
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );

            Coordinate[] shellCoordinates = shell.getCoordinates();
            int length = shellCoordinates.length;
            int count = 0;
            for (Coordinate coordinate : shellCoordinates) {
                double x = coordinate.getX();
                double y = coordinate.getY();
                String[] lonLat = llDecryptUt.computerThatLonLat(x, y, brng, dist).split(",");
                x = Double.valueOf(lonLat[0]);
                y = Double.valueOf(lonLat[1]);
                if(count == length -1){
                    coordinate.setX(shellCoordinates[0].getX());
                    coordinate.setY(shellCoordinates[0].getY());
                }else{
                    coordinate.setX(x);
                    coordinate.setY(y);
                }

                count++;
            }
};

        LinearRing ring = geometryFactory.createLinearRing( shellCoordinates );
        //LinearRing holes[] = null; // use LinearRing[] to represent holes
        polygon = geometryFactory.createPolygon(ring, holes );


        }
        catch (Exception ex){
            System.out.println(ex);
        }
        return polygon;
    }

3. 坐标(经纬度坐标)随机偏移的方法

package com.appleyk.geotools;

public class LonLatDecryptUtility {

    /*
     * 大地坐标系资料WGS-84 长半径a=6378137 短半径b=6356752.3142 扁率f=1/298.2572236
     */
    /** 长半径a=6378137 */
    private double a = 6378137;
    /** 短半径b=6356752.3142 */
    private double b = 6356752.3142;
    /** 扁率f=1/298.2572236 */
    private double f = 1 / 298.2572236;

    /**
     * 计算另一点经纬度
     *
     * @param lon
     *            经度
     * @param lat
     *            维度
     * @param lon lat
     *            已知点经纬度
     * @param brng
     *            方位角
     * @param dist
     *            距离(米)
     */
    public String computerThatLonLat(double lon, double lat, double brng, double dist) {

    double alpha1 = rad(brng);
    double sinAlpha1 = Math.sin(alpha1);
    double cosAlpha1 = Math.cos(alpha1);

    double tanU1 = (1 - f) * Math.tan(rad(lat));
    double cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1));
    double sinU1 = tanU1 * cosU1;
    double sigma1 = Math.atan2(tanU1, cosAlpha1);
    double sinAlpha = cosU1 * sinAlpha1;
    double cosSqAlpha = 1 - sinAlpha * sinAlpha;
    double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
    double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));

    double cos2SigmaM=0;
    double sinSigma=0;
    double cosSigma=0;
    double sigma = dist / (b * A), sigmaP = 2 * Math.PI;
        while (Math.abs(sigma - sigmaP) > 1e-12) {
        cos2SigmaM = Math.cos(2 * sigma1 + sigma);
        sinSigma = Math.sin(sigma);
        cosSigma = Math.cos(sigma);
        double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)
                - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
        sigmaP = sigma;
        sigma = dist / (b * A) + deltaSigma;
    }

    double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
    double lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
            (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
    double lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
    double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
    double L = lambda - (1 - C) * f * sinAlpha
            * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));

    double revAz = Math.atan2(sinAlpha, -tmp); // final bearing

        //System.out.println(revAz);
        return (lon+deg(L)+","+deg(lat2));
}

    /**
     * 度换成弧度
     *
     * @param d
     *            度
     * @return 弧度
     */
    private double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 弧度换成度
     *
     * @param x
     *            弧度
     * @return 度
     */
    private double deg(double x) {
        return x * 180 / Math.PI;
    }


    public static void main(String[] args) {
        LonLatDecryptUtility test=new LonLatDecryptUtility();
        double lon=121.62486;
        double lat=29.87816;
        double random = Math.random();
        double brng=360 * random;
        double dist=5 * random;
        String lonLat = test.computerThatLonLat(lon, lat, brng, dist);//121.62487488808823,29.878155097075076
        System.out.println(lonLat);
    }

}

你可能感兴趣的:(geotools的multipolygon数据脱密)