1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.locationtech.spatial4j.io.jts;
19
20 import java.io.IOException;
21
22 import org.locationtech.spatial4j.context.SpatialContextFactory;
23 import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
24 import org.locationtech.spatial4j.io.PolyshapeWriter;
25 import org.locationtech.spatial4j.shape.Shape;
26 import org.locationtech.spatial4j.shape.jts.JtsGeometry;
27 import org.locationtech.jts.geom.Coordinate;
28 import org.locationtech.jts.geom.CoordinateSequence;
29 import org.locationtech.jts.geom.Geometry;
30 import org.locationtech.jts.geom.GeometryCollection;
31 import org.locationtech.jts.geom.LineString;
32 import org.locationtech.jts.geom.MultiPoint;
33 import org.locationtech.jts.geom.Point;
34 import org.locationtech.jts.geom.Polygon;
35
36 public class JtsPolyshapeWriter extends PolyshapeWriter {
37
38 protected final JtsSpatialContext ctx;
39
40 public JtsPolyshapeWriter(JtsSpatialContext ctx, SpatialContextFactory factory) {
41 super(ctx, factory);
42 this.ctx = ctx;
43 }
44
45
46
47
48
49 protected void write(Encoder output, CoordinateSequence coordseq) throws IOException {
50 int dim = coordseq.getDimension();
51
52
53
54 for (int i = 0; i < coordseq.size(); i++) {
55 output.write(coordseq.getOrdinate(i, 0),
56 coordseq.getOrdinate(i, 1));
57 }
58 }
59
60 protected void write(Encoder output, Coordinate[] coord) throws IOException {
61 for (int i = 0; i < coord.length; i++) {
62 output.write(coord[i].x, coord[i].y);
63 }
64 }
65
66 protected void write(Encoder output, Polygon p) throws IOException {
67 output.write(PolyshapeWriter.KEY_POLYGON);
68 write(output, p.getExteriorRing().getCoordinateSequence());
69 for (int i = 0; i < p.getNumInteriorRing(); i++) {
70 output.startRing();
71 write(output, p.getInteriorRingN(i).getCoordinateSequence());
72 }
73 }
74
75 public void write(Encoder output, Geometry geom) throws IOException {
76 if (geom instanceof Point) {
77 Point v = (Point) geom;
78 output.write(PolyshapeWriter.KEY_POINT);
79 write(output, v.getCoordinateSequence());
80 return;
81 } else if (geom instanceof Polygon) {
82 write(output, (Polygon) geom);
83 return;
84 } else if (geom instanceof LineString) {
85 LineString v = (LineString) geom;
86 output.write(PolyshapeWriter.KEY_LINE);
87 write(output, v.getCoordinateSequence());
88 return;
89 } else if (geom instanceof MultiPoint) {
90 MultiPoint v = (MultiPoint) geom;
91 output.write(PolyshapeWriter.KEY_MULTIPOINT);
92 write(output, v.getCoordinates());
93 return;
94 } else if (geom instanceof GeometryCollection) {
95 GeometryCollection v = (GeometryCollection) geom;
96 for (int i = 0; i < v.getNumGeometries(); i++) {
97 if (i > 0) {
98 output.seperator();
99 }
100 write(output, v.getGeometryN(i));
101 }
102 } else {
103 throw new UnsupportedOperationException("unknown: " + geom);
104 }
105 }
106
107 @Override
108 public void write(Encoder enc, Shape shape) throws IOException {
109 if (shape == null) {
110 throw new NullPointerException("Shape can not be null");
111 }
112 if (shape instanceof JtsGeometry) {
113 write(enc, ((JtsGeometry) shape).getGeom());
114 return;
115 }
116 super.write(enc, shape);
117 }
118 }