2

What I want to achieve is to buffer a zip region geometry with a distance in meters and than get the coordinates of the new (expanded) polygon. The original geometry polygon is parsed from a kml file.

This is the code that I wrote so far:

Parse the kml file:

public Geometry getPolygon(String plz) {
  String poly = storage.get(plz);

  if (poly != null) {
    try {
      InputStream stream = new ByteArrayInputStream(poly.getBytes("UTF-8"));
      Parser parser = new Parser(new KMLConfiguration());
      SimpleFeature f = (SimpleFeature) parser.parse(stream);

      return (Geometry) f.getAttribute("GEOMETRY");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  return null;
}

Transform the polygon:

public Geometry getBufferedZipPolygon(Geometry zipPolygon, double distanceMeters) {
  CoordinateReferenceSystem wgsCRS = null;
  CoordinateReferenceSystem distanceMetersCRS = null;
  try {
    wgsCRS = CRS.decode("EPSG:4326");
    distanceMetersCRS = CRS.decode("EPSG:32630");

    Geometry targetGeometry = zipPolygon.transform(distanceMetersCRS);

    Geometry buffer = targetGeometry.getBuffer(distanceMeters);
    buffer.transform(wgsCRS);

    return buffer;
  } catch (FactoryException e) {
    e.printStackTrace();
  } catch (TransformException e) {
    e.printStackTrace(); 
  }

  return null;
}

I have two Questions now. Is this transformation valid and how can I get the new Coordinates from my transformed Geometry Object?

Answer:

With the help from iant I finally figured out this solution.

Parser:

public Polygon getPolygon(String plz) {
  String polyString = storage.get(plz);

  if (polyString != null) {
    try {
      InputStream stream = new ByteArrayInputStream(polyString.getBytes("UTF-8"));
      Parser parser = new Parser(new KMLConfiguration());
      Polygon polygon = (Polygon) parser.parse(stream);
      return polygon;
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  return null;
}

Transformation:

public Geometry getBufferedZipPolygon(Polygon zipPolygon, double distanceMeters) {
  CoordinateReferenceSystem wgsCRS;
  CoordinateReferenceSystem distanceMetersCRS;
  try {
    wgsCRS = CRS.decode("EPSG:4326", true);
    distanceMetersCRS = CRS.decode("EPSG:32630", true);

    MathTransform transform = CRS.findMathTransform(wgsCRS, distanceMetersCRS);
    MathTransform reverseTransform = CRS.findMathTransform(distanceMetersCRS, wgsCRS);

    Geometry transformedZipPolygon = JTS.transform(zipPolygon, transform);
    Geometry bufferZipPolygon = transformedZipPolygon.buffer(distanceMeters);

    return JTS.transform(bufferZipPolygon, reverseTransform);
  } catch (FactoryException e) {
    e.printStackTrace();
  } catch (TransformException e) {
    e.printStackTrace();
  }

  return null;
}

Get the coordinates:

bufferedPlzPolygon = (Polygon) getBufferedZipPolygon(plzPolyStorage.getPolygon(zip), range);
Coordinate[] coords = bufferedPlzPolygon.getCoordinates();
0

You should be able to do that transform (and provided that it doesn't throw an exception it works ok).

To recover the coordinates you will need to do:

Polygon poly  = (Polygon) buffer;
Coordinate[] coords = poly.getCoordinates();

Technically you should do some checks to make sure it is a polygon before you cast it and make sure nothing is null etc but this should get you started.

  • What type of Geometry Object did you use? Because I get the Error can't resolve getDefaultGeometryProperty()... I am using the org.opengis.geometry.Geometry and org.opengis.geometry.coordinate.Polygon – MeiSign Jan 17 '14 at 8:47
  • I would use a JTS Polygon – Ian Turton Jan 17 '14 at 9:21
  • But a JTS Polygon doesnt have the getDefaultGeometryProperty() method neither. Do I need a specific build? – MeiSign Jan 17 '14 at 9:28
  • sorry - I thought you were working from a feature. Just cast your Geometry to a Polygon – Ian Turton Jan 17 '14 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.