【測繪程式設計試題集】 試題09 反距離加權插值

staHuri發表於2018-11-12

資料

P01,4302.047,3602.652,10.804
P02,4305.768,3598.683,10.855
P03,4310.610,3595.393,10.998
P04,4313.138,3595.086,12.038
P05,4316.843,3594.703,12.818
P06,4320.164,3593.923,12.784
P07,4324.247,3592.690,12.519
P08,4327.704,3591.004,12.685
P09,4331.911,3589.025,12.324
P10,4335.742,3586.897,11.165
P11,4339.844,3584.716,10.802
P12,4333.566,3627.259,9.648
P13,4329.376,3628.727,9.664
P14,4325.037,3630.602,9.608
P15,4321.569,3632.203,9.556
P16,4316.825,3633.985,9.579
P17,4310.382,3635.810,9.440
P18,4306.162,3637.121,9.729
P19,4302.145,3637.715,10.073
P20,4300.700,3611.324,11.769
P21,4303.182,3614.425,12.123
P22,4306.755,3615.185,12.683
P23,4310.931,3613.805,12.976
P24,4314.595,3612.234,13.308
P25,4318.979,3611.322,13.653
P26,4323.632,3609.654,13.674
P27,4327.618,3608.437,13.921
P28,4330.858,3607.602,14.160
P29,4334.909,3606.218,14.355
P30,4339.637,3605.087,14.325
P31,4342.549,3601.832,14.202
P32,4343.144,3604.749,14.318
P33,4343.057,3608.453,14.486
P34,4343.454,3611.589,14.552
P35,4341.930,3614.516,14.690
P36,4341.758,3615.097,14.585
P37,4338.861,3617.507,14.632
P38,4334.937,3618.789,14.601
P39,4331.481,3619.680,14.240
P40,4327.580,3621.409,13.926
P41,4323.586,3623.381,13.752
P42,4319.758,3625.396,13.448
P43,4315.545,3627.270,13.092
P44,4311.801,3628.831,12.825
P45,4308.221,3630.390,12.251
P46,4305.945,3628.937,12.109

問題

在這裡插入圖片描述
在這裡插入圖片描述

  • 測站點
package com.te.dianli;

/**
 * <p>Title : MyPoint </p>
 * <p>Description : 測站座標點</p>
 *
 * @author huifer
 * @date 2018/11/12
 */
public class MyPoint {

    private String id;
    private double x;
    private double y;
    private double z;
    private double dist;

    public void setDist(double dist) {
        this.dist = dist;
    }

    public String getId() {
        return id;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public double getZ() {
        return z;
    }

    public double getDist() {
        return dist;
    }

    public MyPoint(String id, double x, double y ,double z) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @Override
    public String toString() {
        return "MyPoint{" +
                "id='" + id + '\'' +
                ", x=" + x +
                ", y=" + y +
                ", z=" + z +
                ", dist=" + dist +
                '}';
    }

    public MyPoint() {
        x = y = z = dist = 0;
    }

    public void setZ(double z) {
        this.z = z;
    }
}
  • 讀檔案

package com.te.dianli;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>Title : ReadHelper </p>
 * <p>Description : 讀檔案工具</p>
 *
 * @author huifer
 * @date 2018/11/12
 */
public class ReadHelper {


    private List<MyPoint> points = new ArrayList<>();


    public ReadHelper(String path) {
        List<String> textData = ReadHelper.readFileHelper(path);
        this.points = getPointList(textData);
    }


    public List<MyPoint> getPoints() {
        return points;
    }

    public static void main(String[] args) {

        ReadHelper readHelper = new ReadHelper(
                "E:\\idea_project\\cloud\\src\\main\\java\\com\\te\\dianli\\測站座標.txt");
        List<MyPoint> points = readHelper.getPoints();
        System.out.println(points);
    }

    /**
     * 獲取每一行的 點資訊
     */
    private static List<MyPoint> getPointList(List<String> textData) {
        List<MyPoint> points = new ArrayList<>();
        for (int i = 0; i < textData.size(); i++) {
            String[] s = textData.get(i).split(",");
            MyPoint point = new MyPoint(
                    s[0],
                    Double.parseDouble(s[1]),
                    Double.parseDouble(s[2]),
                    Double.parseDouble(s[3])
            );

            points.add(point);
        }
        return points;
    }


    /***
     * 讀檔案
     * @param path
     * @return
     */
    private static List<String> readFileHelper(String path) {
        InputStreamReader isr;
        try {
            isr = new InputStreamReader(new FileInputStream(path), "utf-8");
            BufferedReader read = new BufferedReader(isr);
            String s = null;
            List<String> list = new ArrayList<String>();
            while ((s = read.readLine()) != null) {
                if (s.trim().length() > 1) {
                    list.add(s.trim());
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
  • 執行
package com.te.dianli;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * <p>Title : RunT </p>
 * <p>Description : 執行工具</p>
 *
 * @author huifer
 * @date 2018/11/12
 */
public class RunT {

    public static void main(String[] args) {
        ReadHelper readHelper = new ReadHelper(
                "E:\\idea_project\\cloud\\src\\main\\java\\com\\te\\dianli\\測站座標.txt");
        List<MyPoint> points = readHelper.getPoints();

        List<MyPoint> paramList = new ArrayList<>();
        MyPoint p = new MyPoint("Q1", 4310, 3600, 0);
        HashMap<String, Object> idw = IDW(points, p, 5);
        System.out.println(idw);
    }

    /**
     * IDW 主要流程
     * @param points
     * @param point
     */
    private static HashMap<String, Object> IDW(List<MyPoint> points,MyPoint point,int N) {

        HashMap<String, Object> res = new HashMap<>();

        for (int i = 0; i < points.size(); i++) {
            double d = distance(points.get(i), point);
            points.get(i).setDist(d);
        }
        // 排序
        points.sort(
                Comparator.comparingDouble(MyPoint::getDist)
        );
        double h = getH(points,N);
        point.setZ(h);
        res.put("引數點", point);
        res.put("參與插值", points.subList(0, N));
        return res;
    }

    /**
     * 計算 距離
     * @param p1 第一個點
     * @param p2 第二個點
     * @return double 距離
     */
    private static double distance(MyPoint p1, MyPoint p2) {
        double dx = p1.getX() - p2.getX();
        double dy = p1.getY() - p2.getY();
        double ds = Math.sqrt(dx * dx + dy * dy);
        return ds;
    }

    /***
     * 計算高
     * @param points 點列表
     * @param N 參與數量
     * @return double h
     */
    private static double getH(List<MyPoint> points,int N) {
        double over = 0;
        double under = 0;
        for (int i = 0; i < N; i++) {
            over += points.get(i).getZ() / points.get(i).getDist();
            under += 1 / points.get(i).getDist();
        }
        return over / under;
    }
}

結果

{參與插值=[MyPoint{id='P02', x=4305.768, y=3598.683, z=10.855, dist=4.432190541932936}, MyPoint{id='P03', x=4310.61, y=3595.393, z=10.998, dist=4.647208732131507}, MyPoint{id='P04', x=4313.138, y=3595.086, z=12.038, dist=5.83047510928583}, MyPoint{id='P01', x=4302.047, y=3602.652, z=10.804, dist=8.383514358549585}, MyPoint{id='P05', x=4316.843, y=3594.703, z=12.818, dist=8.653603757972638}], 引數點=MyPoint{id='Q1', x=4310.0, y=3600.0, z=11.391415937758543, dist=0.0}}

相關文章