List of usage examples for org.apache.lucene.geo Polygon getPolyLats
public double[] getPolyLats()
From source file:perf.IndexAndSearchOpenStreetMaps.java
License:Apache License
private static List<Polygon[]> readPolygons(String fileName) throws IOException { CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); int BUFFER_SIZE = 1 << 16; // 64K InputStream is = Files.newInputStream(Paths.get(fileName)); if (fileName.endsWith(".gz")) { is = new GZIPInputStream(is); }//from w w w . ja va2 s .c o m BufferedReader reader = new BufferedReader(new InputStreamReader(is, decoder), BUFFER_SIZE); List<Polygon[]> result = new ArrayList<>(); //EarthDebugger earth = new EarthDebugger(51.45677607571096, 0.13580354718348125, 100000.0); int totalVertexCount = 0; while (true) { String line = reader.readLine(); if (line == null) { break; } if (line.startsWith("count=") == false) { throw new AssertionError(); } // Count is the number of polygons the query has: int count = Integer.parseInt(line.substring(6, line.indexOf(' '))); List<Polygon> polys = new ArrayList<>(); for (int i = 0; i < count; i++) { line = reader.readLine(); // How many polygons (if this is > 1, the first poly is the real one, and // all others are hole-polys that are subtracted): if (line.startsWith(" poly count=") == false) { throw new AssertionError(); } int polyCount = Integer.parseInt(line.substring(13)); List<Polygon> polyPlusHoles = new ArrayList<>(); double sumLat = 0.0; double sumLon = 0.0; for (int j = 0; j < polyCount; j++) { line = reader.readLine(); if (line.startsWith(" vertex count=") == false) { throw new AssertionError(); } int vertexCount = Integer.parseInt(line.substring(17)); double[] lats = new double[vertexCount]; double[] lons = new double[vertexCount]; line = reader.readLine(); if (line.startsWith(" lats ") == false) { throw new AssertionError(); } String[] parts = line.substring(11).split(" "); if (parts.length != vertexCount) { throw new AssertionError(); } for (int k = 0; k < vertexCount; k++) { lats[k] = Double.parseDouble(parts[k]); sumLat += lats[k]; minLat = Math.min(minLat, lats[k]); maxLat = Math.max(maxLat, lats[k]); } line = reader.readLine(); if (line.startsWith(" lons ") == false) { throw new AssertionError(); } parts = line.substring(11).split(" "); if (parts.length != vertexCount) { throw new AssertionError(); } for (int k = 0; k < vertexCount; k++) { lons[k] = Double.parseDouble(parts[k]); sumLon += lons[k]; minLon = Math.min(minLon, lons[k]); maxLon = Math.max(maxLon, lons[k]); } //System.out.println("check poly"); /* int dupCount = 0; for(int k=1;k<vertexCount;k++) { if (lats[k] == lats[k-1] && lons[k] == lons[k-1]) { dupCount++; } } if (dupCount != 0) { System.out.println(" dedup to remove " + dupCount + " duplicate points"); double[] newLats = new double[lats.length-dupCount]; double[] newLons = new double[lons.length-dupCount]; newLats[0] = lats[0]; newLons[0] = lons[0]; int upto = 1; for(int k=1;k<lats.length;k++) { if (lats[k] != lats[k-1] || lons[k] != lons[k-1]) { newLats[upto] = lats[k]; newLons[upto] = lons[k]; upto++; } } lats = newLats; lons = newLons; } */ polyPlusHoles.add(new Polygon(lats, lons)); totalVertexCount += vertexCount; } Polygon firstPoly = polyPlusHoles.get(0); Polygon[] holes = polyPlusHoles.subList(1, polyPlusHoles.size()) .toArray(new Polygon[polyPlusHoles.size() - 1]); polys.add(new Polygon(firstPoly.getPolyLats(), firstPoly.getPolyLons(), holes)); } result.add(polys.toArray(new Polygon[polys.size()])); } System.out.println("Total vertex count: " + totalVertexCount); /* try (PrintWriter out = new PrintWriter("/x/tmp/londonpoly.html")) { out.println(earth.finish()); } */ return result; }
From source file:perf.IndexAndSearchOpenStreetMaps.java
License:Apache License
/** One normally need not clone a Polygon (it's a read only holder class), but we do this here just to keep the benchmark honest, by * including Polygon construction cost in measuring search run time. */ private static Polygon[] clonePolygon(Polygon[] polies) { Polygon[] newPolies = new Polygon[polies.length]; for (int i = 0; i < polies.length; i++) { Polygon poly = polies[i]; Polygon[] holes = poly.getHoles(); if (holes.length > 0) { holes = clonePolygon(holes); }/*from ww w .j av a 2 s. c om*/ newPolies[i] = new Polygon(poly.getPolyLats(), poly.getPolyLons(), holes); } return newPolies; }