1

With geotools would it be possible that generating a polygon from a line (coords[]) in a defined distance? E.g. (100,100), (101,100), (102,100) with distance 1km. So from each point it generates a circle and becomes sth. like:

-------------- before

(========) after

Or I have to firstly convert GPS coordinates into orthogonal coordinates in km, and then generating a polygon using Trigonometric functions, and finally, convert it back into GPS?

Holm
  • 2,987
  • 3
  • 27
  • 48

2 Answers2

2

At it's simplest you simply need to call the buffer(distance) method on the underlying JTS geometry. The tricky bit is handling the reprojection to and from a projection which is in meters (so you can specify your buffer in meters or kilometers).

public SimpleFeature bufferFeature(SimpleFeature feature,
        Measure<Double, Length> distance) {
    // extract the geometry
    GeometryAttribute gProp = feature.getDefaultGeometryProperty();
    CoordinateReferenceSystem origCRS = gProp.getDescriptor()
        .getCoordinateReferenceSystem();

    Geometry geom = (Geometry) feature.getDefaultGeometry();
    Geometry pGeom = geom;
    MathTransform toTransform,fromTransform = null;
    // reproject the geometry to a local projection
    if (!(origCRS instanceof ProjectedCRS)) {

        Point c = geom.getCentroid();
        double x = c.getCoordinate().x;
        double y = c.getCoordinate().y;

        String code = "AUTO:42001," + x + "," + y;
        // System.out.println(code);
        CoordinateReferenceSystem auto;
        try {
        auto = CRS.decode(code);
         toTransform = CRS.findMathTransform(
            DefaultGeographicCRS.WGS84, auto);
         fromTransform = CRS.findMathTransform(auto,
            DefaultGeographicCRS.WGS84);
        pGeom = JTS.transform(geom, toTransform);
        } catch (MismatchedDimensionException | TransformException
            | FactoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

    }

    // buffer
    Geometry out = buffer(pGeom, distance.doubleValue(SI.METER));
    Geometry retGeom = out;
    // reproject the geometry to the original projection
    if (!(origCRS instanceof ProjectedCRS)) {
        try {
        retGeom = JTS.transform(out, fromTransform);
        } catch (MismatchedDimensionException | TransformException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
    // return a new feature containing the geom
    SimpleFeatureType schema = feature.getFeatureType();
    SimpleFeatureTypeBuilder ftBuilder = new SimpleFeatureTypeBuilder();
    ftBuilder.setCRS(origCRS);
    //ftBuilder.setDefaultGeometry("buffer");
    ftBuilder.addAll(schema.getAttributeDescriptors());
    ftBuilder.setName(schema.getName());

    SimpleFeatureType nSchema = ftBuilder.buildFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(nSchema);
     List<Object> atts = feature.getAttributes();
     for(int i=0;i<atts.size();i++) {
         if(atts.get(i) instanceof Geometry) {
         atts.set(i, retGeom);
         }
     }
    SimpleFeature nFeature = builder.buildFeature(null, atts.toArray() );
    return nFeature;
    }

The full code is at https://gist.github.com/ianturton/9a7cfee378e7072ec3cd which shows how to handle the whole thing.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
0

Yes, this would be definitely possible to do so with out converting coordinates. But if you are looking certain properties, such as rectilinear, then coordinate conversion might be necessary.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36