Commit cb5b19ae authored by Matija Obreza's avatar Matija Obreza
Browse files

Generate a circle with specific radius in meters

parent 0347b867
......@@ -87,7 +87,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
// Create a buffer and check for Land
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(ShapefileUtils.getPointBuffer(point, allowedDistanceMargin)));
ff.literal(ShapefileUtils.getPointBuffer(longitude, latitude, allowedDistanceMargin)));
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
return "Coastal";
......@@ -140,7 +140,7 @@ public class LandOrSeaServiceImpl implements LandOrSeaService {
// Create a buffer and check for Land
Filter filterBuffered = ff.intersects(ff.property("the_geom"),
ff.literal(ShapefileUtils.getPointBuffer(point, allowedDistanceFromLand)));
ff.literal(ShapefileUtils.getPointBuffer(longitude, latitude, allowedDistanceFromLand)));
if (sourceLand.getFeatures(filterBuffered).size() > 0) {
return true;
......
package org.genesys.geotools;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
......@@ -8,34 +9,21 @@ import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
public class ShapefileUtils {
private static final MathTransform transformToUtm;
private static final MathTransform transformToGeo;
static {
try {
CoordinateReferenceSystem pointCRS = CRS.decode("EPSG:4326", true);
CoordinateReferenceSystem radiusCRS = CRS.decode("EPSG:32630", true);
transformToUtm = CRS.findMathTransform(pointCRS, radiusCRS);
transformToGeo = CRS.findMathTransform(radiusCRS, pointCRS);
} catch (Throwable e) {
// big problem
throw new RuntimeException(e);
}
}
private static final int RING_POINTS = 12;
private static final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
public static DataStore openShapeFile(String shapeFilePath) throws MalformedURLException, IOException {
......@@ -48,19 +36,39 @@ public class ShapefileUtils {
return dataStore;
}
public static Geometry getPointBuffer(Point point, double distanceMeters) throws MismatchedDimensionException,
TransformException, FactoryException {
Geometry targetGeometry = JTS.transform(point, transformToUtm);
Geometry buffer = targetGeometry.buffer(distanceMeters);
buffer.setSRID(32630);
Geometry bufferGeo = JTS.transform(buffer, transformToGeo);
bufferGeo.setSRID(4326);
/**
* Uses ideas from
* https://geoaware.wordpress.com/2013/10/16/geodetic-buffers-with-geotools/
*
* @param longitude
* @param latitude
* @param distanceMeters
* @return
* @throws MismatchedDimensionException
* @throws TransformException
* @throws FactoryException
*/
public static Geometry getPointBuffer(double longitude, double latitude, double distanceMeters)
throws MismatchedDimensionException, TransformException, FactoryException {
Coordinate[] coords1 = new Coordinate[RING_POINTS + 1];
GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
gc.setStartingGeographicPoint(longitude, latitude);
for (int i = 0; i < RING_POINTS; i++) {
gc.setDirection(i * 360.0 / RING_POINTS, distanceMeters);
Point2D destGeoPoint = gc.getDestinationGeographicPoint();
coords1[i] = new Coordinate(destGeoPoint.getX(), destGeoPoint.getY(), 0);
// System.err.println("a=" + (i * 360.0 / RING_POINTS) + " p=" + p +
// " d=" + gc.getOrthodromicDistance());
}
// Points of LinearRing form a closed linestring
coords1[RING_POINTS] = coords1[0];
// System.err.println(point + " -> " + bufferGeo);
return bufferGeo;
Polygon x = geometryFactory.createPolygon(geometryFactory.createLinearRing(coords1));
// System.err.println(x);
return x;
}
}
package org.genesys.geotools;
import org.junit.Test;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Polygon;
public class PointBufferTest {
@Test
public void test1() throws MismatchedDimensionException, TransformException, FactoryException {
double longitude=151;
double latitude=-35;
double distanceMeters=500;
System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
System.out.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
System.out.println("<Document>");
Polygon polygon = (Polygon) ShapefileUtils.getPointBuffer(longitude, latitude, distanceMeters);
for (Coordinate coord : polygon.getCoordinates()) {
// System.err.println(coord);
System.out.println("<Placemark>");
System.out.print("<name>");
System.out.print("X");
System.out.println("</name>");
System.out.print("<description>");
System.out.print(distanceMeters + " m");
System.out.println("</description>");
System.out.println("<Point><coordinates>");
System.out.print(coord.x);
System.out.print(",");
System.out.print(coord.y);
System.out.println("</coordinates></Point>");
System.out.println("</Placemark>");
}
System.out.println("</Document>");
System.out.println("</kml>");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment