@Override public Object buildLucene() { // decompose linestrings crossing dateline into array of Lines Coordinate[] coordinates = this.coordinates.toArray(new Coordinate[this.coordinates.size()]); if (wrapdateline) { ArrayList<Line> linestrings = decomposeLucene(coordinates, new ArrayList<>()); if (linestrings.size() == 1) { return linestrings.get(0); } else { return linestrings.toArray(new Line[linestrings.size()]); } } return new Line(Arrays.stream(coordinates).mapToDouble(i->normalizeLat(i.y)).toArray(), Arrays.stream(coordinates).mapToDouble(i->normalizeLon(i.x)).toArray()); }
public Object toPolygonLucene() { final org.apache.lucene.geo.Polygon[] holes = new org.apache.lucene.geo.Polygon[this.holes.size()]; for (int i = 0; i < holes.length; ++i) { holes[i] = linearRing(this.holes.get(i).coordinates); } return new org.apache.lucene.geo.Polygon(this.shell.coordinates.stream().mapToDouble(i -> normalizeLat(i.y)).toArray(), this.shell.coordinates.stream().mapToDouble(i -> normalizeLon(i.x)).toArray(), holes); }
protected static org.apache.lucene.geo.Polygon linearRing(List<Coordinate> coordinates) { return new org.apache.lucene.geo.Polygon(coordinates.stream().mapToDouble(i -> normalizeLat(i.y)).toArray(), coordinates.stream().mapToDouble(i -> normalizeLon(i.x)).toArray()); }
static ArrayList<Line> decomposeLucene(Coordinate[] coordinates, ArrayList<Line> lines) { for (Coordinate[] part : decompose(+DATELINE, coordinates)) { for (Coordinate[] line : decompose(-DATELINE, part)) { lines.add(new Line(Arrays.stream(line).mapToDouble(i->normalizeLat(i.y)).toArray(), Arrays.stream(line).mapToDouble(i->normalizeLon(i.x)).toArray())); } } return lines; }
protected static org.apache.lucene.geo.Polygon polygonLucene(Coordinate[][] polygon) { org.apache.lucene.geo.Polygon[] holes; Coordinate[] shell = polygon[0]; if (polygon.length > 1) { holes = new org.apache.lucene.geo.Polygon[polygon.length - 1]; for (int i = 0; i < holes.length; ++i) { Coordinate[] coords = polygon[i+1]; //We do not have holes on the dateline as they get eliminated //when breaking the polygon around it. double[] x = new double[coords.length]; double[] y = new double[coords.length]; for (int c = 0; c < coords.length; ++c) { x[c] = normalizeLon(coords[c].x); y[c] = normalizeLat(coords[c].y); } holes[i] = new org.apache.lucene.geo.Polygon(y, x); } } else { holes = new org.apache.lucene.geo.Polygon[0]; } double[] x = new double[shell.length]; double[] y = new double[shell.length]; for (int i = 0; i < shell.length; ++i) { //Lucene Tessellator treats different +180 and -180 and we should keep the sign. //normalizeLon method excludes -180. x[i] = Math.abs(shell[i].x) > 180 ? normalizeLon(shell[i].x) : shell[i].x; y[i] = normalizeLat(shell[i].y); } return new org.apache.lucene.geo.Polygon(y, x, holes); }