View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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    // Write JTS To GeoJSON
47    // --------------------------------------------------------------
48  
49    protected void write(Encoder output, CoordinateSequence coordseq) throws IOException {
50      int dim = coordseq.getDimension();
51  //    if(dim>2) {
52  //      throw new IllegalArgumentException("only supports 2d geometry now ("+dim+")");
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 }