use of mil.nga.sf.util.ByteReader in project hale by halestudio.
the class GeopackageInstanceWriter method convertGeometry.
private GeoPackageGeometryData convertGeometry(Object someGeom, GeometryColumns geomColumns, SimpleLog log) {
Geometry geom = null;
CRSDefinition sourceCrs = null;
if (someGeom instanceof GeometryProperty<?>) {
GeometryProperty<?> prop = (GeometryProperty<?>) someGeom;
geom = prop.getGeometry();
sourceCrs = prop.getCRSDefinition();
} else if (someGeom instanceof Geometry) {
geom = (Geometry) someGeom;
}
GeoPackageGeometryData geometryData = new GeoPackageGeometryData(geomColumns.getSrsId());
if (geom != null) {
SpatialReferenceSystem targetSrs = geomColumns.getSrs();
CRSDefinition targetCrs = toCRSDefinition(targetSrs);
// do conversion to target CRS (if possible)
Geometry targetGeometry = geom;
try {
if (sourceCrs != null && targetCrs != null) {
MathTransform transform = CRS.findMathTransform(sourceCrs.getCRS(), targetCrs.getCRS());
targetGeometry = JTS.transform(geom, transform);
}
} catch (Exception e) {
log.error("Failed to convert geometry to target SRS " + targetSrs.getSrsName());
}
// XXX also an option to only use a SrsId or use a SrsId that
// differs from the column SrsId?
byte[] wkb = new WKBWriter().write(targetGeometry);
mil.nga.sf.Geometry geometry = GeometryReader.readGeometry(new ByteReader(wkb));
geometryData.setGeometry(geometry);
}
return geometryData;
}
Aggregations