如何在坐标系中显示大坐标?

时间:2018-07-14 09:57:44

标签: java swing coordinate-transformation affinetransform wkt

我想从给定的wkt文件中绘制一些点,但是由于它们的紧密性,我只能显示重叠的椭圆形堆。

这些点仅在小数点后不同:

import numpy as np

L = [[7], [4, 3, 5, 8], [1, 3]]

res = [np.arange(len(sublist)).dot(sublist) for sublist in L]

print(res)

[0, 37, 3]

首先,我试图绘制点,但是正如我已经提到的,所有点似乎都显示在同一位置。

  POINT (3346349.958 5642197.806)
  POINT (3346349.313 5642199.622)
  POINT (3346349.237 5642201.918)
  POINT (3346349.734 5642204.058)
  POINT (3346351.746 5642205.777)
  POINT (3346351.636 5642210.304)
  POINT (3346349.335 5642216.518)
  POINT (3346347.326 5642221.15)
  POINT (3346347.365 5642223.671)
  POINT (3346351.577 5642195.711)
  etc...

但是我认为解决方案可能在于重新缩放坐标系,我试图通过Affinetransform和.scale()进行转换

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import com.vividsolutions.jts.io.ParseException;

public class Display extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int width;
    private int height;
    private WKTGrabsteine p = new WKTGrabsteine();

    public Display() {
        setLayout(null);
        width = 0;
        height = 0;
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);

        Graphics2D g2 = (Graphics2D) g;

        g2.translate(height, width);

        try {
            for (int point = 0; point < p.geoCoordinates().size(); point++) {
                Ellipse2D shape = new Ellipse2D.Double(p.geoCoordinates().get(point).getX() / 1000000 + 400,
                        p.geoCoordinates().get(point).getY() / 1000000 + 100, 5, 5);

                g2.draw(shape);
            }

        } catch (IOException | ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

事实上,我对这个话题还很陌生,不知道如何使所有观点清晰可见。如果有人可以帮助我,那就太好了。 非常感谢

1 个答案:

答案 0 :(得分:0)

结合使用缩放和平移仿射变换。

enter image description here

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class PointPlotter {

    Point2D.Double[] points = {
        new Point2D.Double(3346349.958, 5642197.806),
        new Point2D.Double(3346349.313, 5642199.622),
        new Point2D.Double(3346349.237, 5642201.918),
        new Point2D.Double(3346349.734, 5642204.058),
        new Point2D.Double(3346351.746, 5642205.777),
        new Point2D.Double(3346351.636, 5642210.304),
        new Point2D.Double(3346349.335, 5642216.518),
        new Point2D.Double(3346347.326, 5642221.15),
        new Point2D.Double(3346347.365, 5642223.671),
        new Point2D.Double(3346351.577, 5642195.711)
    };
    public static int SZ = 400;
    BufferedImage image = new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);

    private JComponent ui = null;

    PointPlotter() {
        initUI();
    }

    private void drawImage() {
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, SZ, SZ);

        Area area = new Area();
        double r = 0.3;
        for (Point2D.Double point : points) {
            Ellipse2D.Double e = new Ellipse2D.Double(
                    point.getX() - r, point.getY() - r, 2*r, 2*r);
            area.add(new Area(e));
        }
        Rectangle2D rect = area.getBounds2D();
        double w = rect.getWidth();
        double h = rect.getHeight();
        double max = w>h ? w : h;
        double s = SZ/max;
        AffineTransform scale = AffineTransform.getScaleInstance(s, s);
        double tX = -rect.getMinX();
        double tY = -rect.getMinY();
        AffineTransform translate = AffineTransform.getTranslateInstance(tX, tY);

        AffineTransform transform = scale;
        transform.concatenate(translate);

        area = new Area(transform.createTransformedShape(area));

        g.setColor(Color.RED);
        g.draw(area);

        g.dispose();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }
        drawImage();

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        ui.add(new JLabel(new ImageIcon(image)));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                PointPlotter o = new PointPlotter();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}