Skip to content

Instantly share code, notes, and snippets.

@ianturton
Last active October 27, 2021 11:23
Show Gist options
  • Save ianturton/40c3ed7816859fa3dde1300b94bc2334 to your computer and use it in GitHub Desktop.
Save ianturton/40c3ed7816859fa3dde1300b94bc2334 to your computer and use it in GitHub Desktop.
GeoTools program to calculate a wedge shaped polygon 10 degrees wide given a lat,lon radius and azimuth.
package spike;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.WKTWriter;
public class CellTower {
public static void main(String[] args) {
CellTower cell = new CellTower(50.0, 0, 10, 30);
System.out.println(cell);
}
private static final GeometryFactory GF = new GeometryFactory();
private double azimuth;
private double radius;
private Point point;
private Polygon poly;
private static final GeodeticCalculator CALC = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
/**
* Calculate the arc of something from (lat,lon) centred on bearing clockwise from N and length radius
* @param lat - latitude
* @param lon - longitude
* @param radius - length of arcs in Metres
* @param azimuth - central bearing of the arc
*/
public CellTower(double lat, double lon, double radius, double azimuth) {
this.point = GF.createPoint(new Coordinate(lat, lon));
this.radius = radius;
this.azimuth = azimuth;
createPolygon();
}
private void createPolygon() {
ArrayList<Coordinate> coords = new ArrayList<>();
// start at the tower
coords.add(point.getCoordinate());
// next the edge of the wedge
int nSteps = 10;
// assume width of 10 degrees
double width = 10.0;
double dStep = width/nSteps;
for (int i = -nSteps; i < nSteps; i++) {
CALC.setStartingGeographicPoint(point.getX(), point.getY());
CALC.setDirection((azimuth +(i*dStep)), radius);
Point2D p = CALC.getDestinationGeographicPoint();
coords.add(new Coordinate(p.getX(), p.getY()));
}
// end at the tower
coords.add(point.getCoordinate());
poly = GF.createPolygon(coords.toArray(new Coordinate[] {}));
}
public String toString() {
WKTWriter writer = new WKTWriter();
return writer.write(poly);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment