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();
Answer
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.
No comments:
Post a Comment