Geotools: bounding box for a buffer in wgs84

I am need a Java function that will generate a bounding box (rectangle) around a buffer. The buffer is defined by the center point (WGS84 coordinate) and the radius (in meters).

Getting a bounding box for a buffer in JTS seems to be quite simple:

Point center = ....
Geometry boundingBox = center.buffer(...).getEnvelope();

This however is pure planar geometry. Is there a way to do this using a coordinate reference system with the distance given in meters?

Optimally with Geotools but other Java solutions will also work...

Although you have approached it in another way, I have another solution for that. The results will be way more precise than with your proposed solution.

GeometryFactory GEOMETRY_FACTORY = JTSFactoryFinder.getGeometryFactory();

// Remember, order is (longitude, latitude)
Coordinate center = Coordinate(2.29443, 48.85816);
Point point = GEOMETRY_FACTORY.createPoint(center);

// Buffer 50KM around the point, then get the envelope
Envelope envelopeInternal = buffer(point, 50000).getEnvelopeInternal();

// Then you can play with the envelope, e.g.,
double minX = envelopeInternal.getMinX();
double maxX = envelopeInternal.getMaxX();

// The buffer using distanceInMeters
private Geometry buffer(Geometry geometry, double distanceInMeters) throws FactoryException, TransformException {
     String code = "AUTO:42001," + geometry.getCentroid().getCoordinate().x + "," + geometry.getCentroid().getCoordinate().y;
     CoordinateReferenceSystem auto = CRS.decode(code);

     MathTransform toTransform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
     MathTransform fromTransform = CRS.findMathTransform(auto, DefaultGeographicCRS.WGS84);

     Geometry pGeom = JTS.transform(geometry, toTransform);
     Geometry pBufferedGeom = pGeom.buffer(distanceInMeters);
     return JTS.transform(pBufferedGeom, fromTransform);
}

And here is the map with the result, buffer in red, envelope in black.

java - Geotools: bounding box for a buffer in wgs84, Although you have approached it in another way, I have another solution for that. The results will be way more precise than with your proposed solution. The main difference is that a bounding box is 2 dimensional while an envelope has 2 or more dimensions. A dimension is a "direction" so for a bounding box there are 2 (North and East or X and Y) while for an envelope there can be lots like North, East, Up, or X, Y and time, or X,Y,Z,T.

I ended up using a GeodeticCalculator to manually find the corners of the box. Frankly the results aren't very precise, but that's the best solution I found till now:

 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
 CoordinateReferenceSystem wgs84 = DefaultGeographicCRS.WGS84;
 GeodeticCalculator geodeticCalculator = new GeodeticCalculator(wgs84);
 geodeticCalculator.setStartingGeographicPoint(center.getX(), center.getY());
 Coordinate[] coordinates = new Coordinate[5];
 for (int i = 0; i < 4; i++) {
    geodeticCalculator.setDirection(-180 + i * 90 + 45, bufferRadiusMeters * Math.sqrt(2));
    Point2D point2D = geodeticCalculator.getDestinationGeographicPoint();
    coordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
 }
 coordinates[4] = coordinates[0];
 Polygon box = geometryFactory.createPolygon(coordinates);

Filter, Translating that to a BoundingBox , and then into a query is a little involved. WGS84; // usually "THE_GEOM" for shapefiles String geometryPropertyName = schema. Another common approach is to call geometry.buffer(distance) and then  get bounding box for a polygon; convert circle to a polygon; create a polygon from a point cloud; translate a wgs84 coordinate by x & y meters along the latitude and longitude; GeoHashUtils class with methods that allow you to: encode and decode geo hashes; check containment of a point in a geohash; find out the boundingbox of a geohash

Here is a simple solution that I used to generate bounding box coordinates that I use with GeoNames citieJSON API to get nearby big cities from a gps decimal coordinate.

This is a Java method from my GitHub repository: FusionTableModifyJava

I had a decimal GPS location and I needed to find the biggest city/state "near" that location. I needed a relatively accurate bounding box to pass to the citiesJSON GeoNames webservice to get back the biggest city in that bounding box. I pass the location and the "radius" I am interested in (in km) and it gives back the north, south, east, west decimal coordinates needed to pass to citiesJSON.

(I found these resources useful in doing my research:

Calculate distance, bearing and more between Latitude/Longitude points.

Longitude - Wikipedia)

It is not super accurate but accurate enough for what I was using it for:

    // Compute bounding Box coordinates for use with Geonames API.
    class BoundingBox
    {
        public double north, south, east, west;
        public BoundingBox(String location, float km)
        {
             //System.out.println(location + " : "+ km);
            String[] parts = location.replaceAll("\\s","").split(","); //remove spaces and split on ,

            double lat = Double.parseDouble(parts[0]);
            double lng = Double.parseDouble(parts[1]);

            double adjust = .008983112; // 1km in degrees at equator.
            //adjust = 0.008983152770714983; // 1km in degrees at equator.

            //System.out.println("deg: "+(1.0/40075.017)*360.0);


            north = lat + ( km * adjust);
            south = lat - ( km * adjust);

            double lngRatio = 1/Math.cos(Math.toRadians(lat)); //ratio for lng size
            //System.out.println("lngRatio: "+lngRatio);

            east = lng + (km * adjust) * lngRatio;
            west = lng - (km * adjust) * lngRatio;
        }

    }

Frequently Asked Questions, The referencing module does not do very much out of the box - it needs between the spheroid being used by your shapefile and the normal WGS84 spheroid. This module only draws into a Graphics2D context (so printer or image buffer). GridCoverage with a BoundingBox crossing the Date Line: the final image is not  My goal is simple, I am using RStudio and I want to create a bounding box from the above two lats and longs and then generate Polygon and I want to add random points into the square polygon. I have NELat/Lng and SW-Lat/Lng in a csv file that I could import into a data.frame.

BoundingBox (Geotools modules 24-SNAPSHOT API), @Extension public interface BoundingBox extends Envelope Example: if box is a bounding box using a geographic CRS with WGS84 datum, then one can  Geotools. Geotools is a PHP geo-related library, built atop Geocoder and React libraries.. Features. Batch geocode & reverse geocoding request(s) in series / in parallel against one or a set of providers.

Frequently Asked Questions, The referencing module does not do very much out of the box - it needs someone the spheroid being used by your shapefile and the normal WGS84 spheroid. provide hints before use; may revisit data several times (for bounding box and This module only draws into a Graphics2D context (so printer or image buffer). This means that the bounding box filter and the * Query filter will be combined, also including maxFeatures from Query * < li >At least that the layer's definition query explicitly says to retrieve some attribute,

CRS (Geotools modules 24-SNAPSHOT API), CRS objects created by previous calls to this method are cached in a buffer using weak references. If none are found, then the geographic bounding boxes are used as a fallback WGS84 (formally defined by CRSAuthorityFactory); ogc:uri:. 3D Geometry and Rendering ideas. This page is supposed to hold ideas and goals for 3D rendering in GeoTools/GeoWidgets. This is a call for opinions and ideas. So please answer. Goals. Allow threedimensional display of 3D geographical data. Look at Google Earth, WorldWind, Grass, ArcScene and you know what I mean. Ease of use.

Comments
  • This is great! I just couldn't get my buffer radius projected as a perfect circle until I read this - it was an ellipse.