我目前正在研究一种方法,该方法应该从wkt文件中提取坐标。 WktFile由9个多面体组成,并具有以下结构:
MULTIPOLYGON(((4.4490734644205 -2.82592405702356 -0.603974130600466,4.61552064787664 -3.29977100457644 -0.600505503171231,4.61981670064913 -3.31128249271675 -0.936085060875472,4.4490734644205 -2.82592405702356 -0.603974130600466)),((4.4490734644205 -2.82592405702356 -0.603974130600466,4.61981670064913 -3.31128249271675 -0.936085060875472,4.44887485891923 -2.83819021528408 -0.942217436432726,4.4490734644205 -2.82592405702356 -0.603974130600466)))
每个Multipolygon有两个多边形,每个多边形包含4个点。 首先,我编写了一个返回三维数组列表的方法(我只需要x和y坐标)。
package org.hs.mainz.praktikum.wktReader;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTFileReader;
import com.vividsolutions.jts.io.WKTReader;
public class PointList3 {
public List<List<List<Point2D>>> getPoints() throws IOException, ParseException {
File file = new File("C:\\\\Users\\\\Maximilian\\\\Desktop\\\\Praktikum\\\\totalMonitors.wkt");
WKTReader r = new WKTReader();
WKTFileReader fr = new WKTFileReader(file, r);
List<Geometry> geometries = fr.read();
List<List<Point2D>> listPolygonPoints = new ArrayList<>();
List<List<List<Point2D>>> listPoints = new ArrayList<>();
for (Geometry g : geometries) {
Coordinate[] c = g.getGeometryN(0).getCoordinates();
CoordinateArraySequence s = new CoordinateArraySequence(c);
List<List<Point2D>> polygonPoints = new ArrayList<>();
for (int i = 0; i < g.getNumGeometries(); i++) {
List<Point2D> points = new ArrayList<>();
for (int j = 0; j < g.getGeometryN(0).getNumPoints(); j++) {
double posX = s.getOrdinate(j, 0);
double posY = s.getOrdinate(j, 1);
Point2D point = new Point2D.Double(posX, posY);
points.add(point);
}
listPolygonPoints.add(points);
}
listPoints.add(polygonPoints);
}
return listPoints;
}
}
控制台显示以下内容:
[[], [], [], [], [], [], [], [], [], []]
,如果没有获取IndexException,我将无法访问内部多边形。 在第二步中,我想使用Path2D绘制所有多边形。
package org.hs.mainz.praktikum.wktReader;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
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 PointList2 p = new PointList2();
public Display() {
setLayout(null);
width = 400;
height = 400;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
Graphics2D g2 = (Graphics2D) g;
g2.translate(height, width);
try {
Path2D path = new Path2D.Double();
for (int multiPolygon = 0; multiPolygon < p.getPoints().size(); multiPolygon++) {
System.out.println(p.getPoints().get(multiPolygon).size());
for (int polygon = 0; polygon < p.getPoints().get(multiPolygon).size(); polygon++) {
path.moveTo(p.getPoints().get(multiPolygon).get(polygon).getX()*100,
p.getPoints().get(multiPolygon).get(0).getY()*100);
for (int line = polygon; line < p.getPoints().get(polygon).size(); line++) {
path.lineTo(p.getPoints().get(multiPolygon).get(line).getX()*100,
p.getPoints().get(multiPolygon).get(line).getY()*100);
}
path.closePath();
}
g2.draw(path);
}
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
此代码相当老,并且已为2DArrayList配置。这是有问题的,因为只绘制了一个多多边形中的两个多边形。 因此,我想一个3DArrayList可以解决这个问题。
任何想法都可以通过遵循上面的给定结构来绘制多面体。
谢谢:)