View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2015 VoyagerSearch and others
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Apache License, Version 2.0 which
5    * accompanies this distribution and is available at
6    *    http://www.apache.org/licenses/LICENSE-2.0.txt
7    ******************************************************************************/
8   
9   package org.locationtech.spatial4j.io.jackson;
10  
11  import java.io.IOException;
12  import com.fasterxml.jackson.core.JsonGenerator;
13  import com.fasterxml.jackson.core.JsonProcessingException;
14  import com.fasterxml.jackson.databind.JsonSerializer;
15  import com.fasterxml.jackson.databind.SerializerProvider;
16  import org.locationtech.jts.geom.Coordinate;
17  import org.locationtech.jts.geom.CoordinateSequence;
18  import org.locationtech.jts.geom.Geometry;
19  import org.locationtech.jts.geom.GeometryCollection;
20  import org.locationtech.jts.geom.LineString;
21  import org.locationtech.jts.geom.MultiLineString;
22  import org.locationtech.jts.geom.MultiPoint;
23  import org.locationtech.jts.geom.MultiPolygon;
24  import org.locationtech.jts.geom.Point;
25  import org.locationtech.jts.geom.Polygon;
26  
27  public class GeometryAsGeoJSONSerializer extends JsonSerializer<Geometry>
28  {
29    // --------------------------------------------------------------
30    // Write JTS To GeoJSON
31    // --------------------------------------------------------------
32  
33    protected void write(JsonGenerator gen, Coordinate coord) throws IOException {
34      gen.writeStartArray();
35      gen.writeNumber(coord.x);
36      gen.writeNumber(coord.y);
37      gen.writeEndArray();
38    }
39  
40    protected void write(JsonGenerator gen, CoordinateSequence coordseq) throws IOException {
41      gen.writeStartArray();
42      int dim = coordseq.getDimension();
43      for (int i = 0; i < coordseq.size(); i++) {
44        gen.writeStartArray();
45        gen.writeNumber(coordseq.getOrdinate(i, 0));
46        gen.writeNumber(coordseq.getOrdinate(i, 1));
47        if (dim > 2) {
48          double v = coordseq.getOrdinate(i, 2);
49          if (!Double.isNaN(v)) {
50            gen.writeNumber(v);
51          }
52        }
53        gen.writeEndArray();
54      }
55      gen.writeEndArray();
56    }
57  
58    protected void write(JsonGenerator gen, Coordinate[] coord) throws IOException {
59      gen.writeStartArray();
60      for (int i = 0; i < coord.length; i++) {
61        write(gen, coord[i]);
62      }
63      gen.writeEndArray();
64    }
65  
66    protected void write(JsonGenerator gen, Polygon p) throws IOException {
67      gen.writeStartArray();
68      write(gen, p.getExteriorRing().getCoordinateSequence());
69      for (int i = 0; i < p.getNumInteriorRing(); i++) {
70        write(gen, p.getInteriorRingN(i).getCoordinateSequence());
71      }
72      gen.writeEndArray();
73    }
74  
75    @Override
76    public void serialize(Geometry geom, JsonGenerator gen, SerializerProvider serializers) 
77        throws IOException, JsonProcessingException 
78    {
79      gen.writeStartObject();
80      gen.writeFieldName("type");
81      gen.writeString(geom.getClass().getSimpleName());
82      
83      if (geom instanceof Point) {
84        Point v = (Point) geom;
85        gen.writeFieldName("coordinates");
86        write(gen, v.getCoordinate());
87      } else if (geom instanceof Polygon) {
88        gen.writeFieldName("coordinates");
89        write(gen, (Polygon) geom);
90      } else if (geom instanceof LineString) {
91        LineString v = (LineString) geom;
92        gen.writeFieldName("coordinates");
93        write(gen, v.getCoordinateSequence());
94      } else if (geom instanceof MultiPoint) {
95        MultiPoint v = (MultiPoint) geom;
96        gen.writeFieldName("coordinates");
97        write(gen, v.getCoordinates());
98        return;
99      } else if (geom instanceof MultiLineString) {
100       MultiLineString v = (MultiLineString) geom;
101       gen.writeFieldName("coordinates");
102       gen.writeStartArray();
103       for (int i = 0; i < v.getNumGeometries(); i++) {
104         write(gen, v.getGeometryN(i).getCoordinates());
105       }
106       gen.writeEndArray();
107     } else if (geom instanceof MultiPolygon) {
108       MultiPolygon v = (MultiPolygon) geom;
109       gen.writeFieldName("coordinates");
110       gen.writeStartArray();
111       for (int i = 0; i < v.getNumGeometries(); i++) {
112         write(gen, (Polygon) v.getGeometryN(i));
113       }
114       gen.writeEndArray();
115     } else if (geom instanceof GeometryCollection) {
116       GeometryCollection v = (GeometryCollection) geom;
117       gen.writeFieldName("geometries");
118       gen.writeStartArray();
119       for (int i = 0; i < v.getNumGeometries(); i++) {
120         serialize(v.getGeometryN(i), gen, serializers);
121       }
122       gen.writeEndArray();
123     } else {
124       throw new UnsupportedOperationException("unknown: " + geom);
125     }
126     gen.writeEndObject();
127   }
128 }