use of mil.nga.geopackage.geom.GeoPackageGeometryData 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;
}
use of mil.nga.geopackage.geom.GeoPackageGeometryData in project hale by halestudio.
the class GeopackageInstanceWriter method writeInstances.
/**
* Write instances to the GeoPackage.
*
* @param geoPackage the GeoPackage
* @param instances the instances to write
* @param progress the progress indicator
* @param reporter the reporter
* @throws SQLException if an error occurs creating a database table
*/
protected void writeInstances(GeoPackage geoPackage, InstanceCollection instances, ProgressIndicator progress, IOReporter reporter) throws SQLException {
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext() && !progress.isCanceled()) {
Instance instance = it.next();
TypeDefinition type = instance.getDefinition();
// TODO only specific types? (e.g. ignore some kinds of types?)
String tableName = type.getName().getLocalPart();
// determine table for type (and create if necessary)
GeopackageTableType tableType = createTableIfNecessary(geoPackage, tableName, type, instance, reporter);
switch(tableType) {
case ATTRIBUTE:
AttributesDao attributes = geoPackage.getAttributesDao(tableName);
AttributesRow arow = attributes.newRow();
populateRow(arow, instance, reporter);
attributes.insert(arow);
break;
case FEATURE:
FeatureDao features = geoPackage.getFeatureDao(tableName);
FeatureRow frow = features.newRow();
populateRow(frow, instance, reporter);
// set geometry
String geometryColumn = features.getGeometryColumnName();
// XXX instead of using value traverse (GeometryFinder) for
// geometry?
Object geom = new InstanceAccessor(instance).findChildren(geometryColumn).value();
GeoPackageGeometryData geomData = convertGeometry(geom, features.getGeometryColumns(), reporter);
frow.setGeometry(geomData);
features.insert(frow);
break;
}
}
}
}
Aggregations