org.apache.lucene.tests.geo.EarthDebugger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lucene-test-framework Show documentation
Show all versions of lucene-test-framework Show documentation
Apache Lucene (module: test-framework)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.tests.geo;
import org.apache.lucene.geo.Polygon;
import org.apache.lucene.geo.Rectangle;
import org.apache.lucene.util.SloppyMath;
/**
* Draws shapes on the earth surface and renders using the very cool http://www.webglearth.org.
*
* Just instantiate this class, add the things you want plotted, and call {@link #finish} to get
* the resulting HTML that you should save and load with a browser.
*/
public class EarthDebugger {
final StringBuilder b = new StringBuilder();
private int nextShape;
private boolean finished;
public EarthDebugger() {
b.append("\n");
b.append("\n");
b.append("
\n");
b.append(" \n");
b.append(" \n");
b.append(" \n");
b.append(" \n");
b.append(" WebGL Earth API: Hello World \n");
b.append(" \n");
b.append(" \n");
b.append(" \n");
b.append(" \n");
b.append("\n");
return b.toString();
}
private static void inverseHaversin(
StringBuilder b, double centerLat, double centerLon, double radiusMeters) {
double angle = 0;
int steps = 100;
newAngle:
while (angle < 360) {
double x = Math.cos(Math.toRadians(angle));
double y = Math.sin(Math.toRadians(angle));
double factor = 2.0;
double step = 1.0;
int last = 0;
double lastDistanceMeters = 0.0;
// System.out.println("angle " + angle + " slope=" + slope);
while (true) {
double lat = wrapLat(centerLat + y * factor);
double lon = wrapLon(centerLon + x * factor);
double distanceMeters = SloppyMath.haversinMeters(centerLat, centerLon, lat, lon);
if (last == 1 && distanceMeters < lastDistanceMeters) {
// For large enough circles, some angles are not possible:
// System.out.println(" done: give up on angle " + angle);
angle += 360. / steps;
continue newAngle;
}
if (last == -1 && distanceMeters > lastDistanceMeters) {
// For large enough circles, some angles are not possible:
// System.out.println(" done: give up on angle " + angle);
angle += 360. / steps;
continue newAngle;
}
lastDistanceMeters = distanceMeters;
// System.out.println(" iter lat=" + lat + " lon=" + lon + " distance=" + distanceMeters +
// " vs " + radiusMeters);
if (Math.abs(distanceMeters - radiusMeters) < 0.1) {
b.append(" [").append(lat).append(", ").append(lon).append("],\n");
break;
}
if (distanceMeters > radiusMeters) {
// too big
// System.out.println(" smaller");
factor -= step;
if (last == 1) {
// System.out.println(" half-step");
step /= 2.0;
}
last = -1;
} else if (distanceMeters < radiusMeters) {
// too small
// System.out.println(" bigger");
factor += step;
if (last == -1) {
// System.out.println(" half-step");
step /= 2.0;
}
last = 1;
}
}
angle += 360. / steps;
}
}
// craziness for plotting stuff :)
private static double wrapLat(double lat) {
// System.out.println("wrapLat " + lat);
if (lat > 90) {
// System.out.println(" " + (180 - lat));
return 180 - lat;
} else if (lat < -90) {
// System.out.println(" " + (-180 - lat));
return -180 - lat;
} else {
// System.out.println(" " + lat);
return lat;
}
}
private static double wrapLon(double lon) {
// System.out.println("wrapLon " + lon);
if (lon > 180) {
// System.out.println(" " + (lon - 360));
return lon - 360;
} else if (lon < -180) {
// System.out.println(" " + (lon + 360));
return lon + 360;
} else {
// System.out.println(" " + lon);
return lon;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy