use of mil.nga.geopackage.core.srs.SpatialReferenceSystem 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.core.srs.SpatialReferenceSystem in project hale by halestudio.
the class GeopackageInstanceWriter method createEpsgSrs.
private SpatialReferenceSystem createEpsgSrs(GeoPackage geoPackage, CodeDefinition crs, String epsg) throws SQLException {
SpatialReferenceSystemDao srsDao = geoPackage.getSpatialReferenceSystemDao();
SpatialReferenceSystem srs = new SpatialReferenceSystem();
CoordinateReferenceSystem geoCrs = crs.getCRS();
srs.setSrsName(geoCrs.getName().toString());
int codeId = Integer.parseInt(epsg);
String wkt = geoCrs.toWKT();
// XXX how to avoid clashes?
srs.setSrsId(codeId);
srs.setOrganization("EPSG");
srs.setOrganizationCoordsysId(codeId);
// XXX not sure what the difference between the definition types is
srs.setDefinition(wkt);
srs.setDefinition_12_063(wkt);
if (geoCrs.getRemarks() != null) {
srs.setDescription(geoCrs.getRemarks().toString());
}
srsDao.create(srs);
return srs;
}
use of mil.nga.geopackage.core.srs.SpatialReferenceSystem in project hale by halestudio.
the class GeopackageInstanceWriter method findSrs.
private SpatialReferenceSystem findSrs(GeoPackage geoPackage, String org, String code, String wkt, SimpleLog log) {
SpatialReferenceSystemDao srsDao = geoPackage.getSpatialReferenceSystemDao();
SpatialReferenceSystem srs = null;
if (org == null && code != null) {
// try to split auth and code from code
// extract EPSG code
String epsg = CodeDefinition.extractEPSGCode(code);
if (epsg != null) {
org = "EPSG";
code = epsg;
}
}
if (org != null && code != null) {
try {
long codeNum = Long.parseLong(code);
srs = srsDao.queryForOrganizationCoordsysId(org, codeNum);
} catch (Exception e) {
log.warn("Failed to use SRS code for geometry column", e);
}
}
if (srs == null && wkt != null) {
try {
List<SpatialReferenceSystem> candidates = srsDao.queryForEq(SpatialReferenceSystem.COLUMN_DEFINITION, wkt);
if (!candidates.isEmpty()) {
srs = candidates.get(0);
}
} catch (SQLException e) {
log.warn("Failed to retrieve SRS based on WKT definition", e);
}
if (srs == null) {
// TODO create new SRS entry with definition?
}
}
return srs;
}
use of mil.nga.geopackage.core.srs.SpatialReferenceSystem in project hale by halestudio.
the class GeopackageInstanceWriter method determineSrsId.
private long determineSrsId(GeoPackage geoPackage, PropertyDefinition geomProp, Instance instance, SimpleLog log) throws SQLException {
GeometryMetadata geomMetadata = geomProp.getPropertyType().getConstraint(GeometryMetadata.class);
// determine from geometry metadata
SpatialReferenceSystem srs = findSrs(geoPackage, geomMetadata.getAuthName(), geomMetadata.getSrs(), geomMetadata.getSrsText(), log);
if (srs == null && geomMetadata.getSrs() != null && geomMetadata.getAuthName() != null) {
// try to create CRS from geometry metadata
CRSDefinition crs = new CodeDefinition(geomMetadata.getAuthName() + ":" + geomMetadata.getSrs());
srs = findOrCreateSrs(geoPackage, crs, log);
}
if (srs == null) {
// determine from target CRS parameter
CRSDefinition crs = getTargetCRS();
srs = findOrCreateSrs(geoPackage, crs, log);
}
if (srs == null) {
// try to determine from example instance
Object geom = new InstanceAccessor(instance).findChildren(geomProp.getName().getLocalPart()).value();
if (geom instanceof GeometryProperty<?>) {
CRSDefinition crs = ((GeometryProperty<?>) geom).getCRSDefinition();
srs = findOrCreateSrs(geoPackage, crs, log);
}
}
if (srs != null) {
return srs.getSrsId();
}
return 0;
}
use of mil.nga.geopackage.core.srs.SpatialReferenceSystem in project hale by halestudio.
the class GeopackageInstanceWriter method findOrCreateSrs.
private SpatialReferenceSystem findOrCreateSrs(GeoPackage geoPackage, CRSDefinition crs, SimpleLog log) throws SQLException {
SpatialReferenceSystem srs = null;
if (crs instanceof CodeDefinition) {
String code = ((CodeDefinition) crs).getCode();
srs = findSrs(geoPackage, null, code, null, log);
if (srs == null) {
// XXX creating a CRS currently only supported for EPSG codes
String epsg = CodeDefinition.extractEPSGCode(code);
if (epsg != null) {
srs = createEpsgSrs(geoPackage, (CodeDefinition) crs, epsg);
return srs;
}
}
} else if (crs instanceof WKTDefinition) {
String wkt = ((WKTDefinition) crs).getWkt();
srs = findSrs(geoPackage, null, null, wkt, log);
}
return srs;
}
Aggregations