Search in sources :

Example 1 with SpatialReferenceSystem

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;
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) WKBWriter(org.locationtech.jts.io.WKBWriter) MathTransform(org.opengis.referencing.operation.MathTransform) ByteReader(mil.nga.sf.util.ByteReader) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ConversionException(org.springframework.core.convert.ConversionException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Geometry(org.locationtech.jts.geom.Geometry) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem)

Example 2 with SpatialReferenceSystem

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;
}
Also used : SpatialReferenceSystemDao(mil.nga.geopackage.core.srs.SpatialReferenceSystemDao) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint)

Example 3 with SpatialReferenceSystem

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;
}
Also used : SpatialReferenceSystemDao(mil.nga.geopackage.core.srs.SpatialReferenceSystemDao) SQLException(java.sql.SQLException) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ConversionException(org.springframework.core.convert.ConversionException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 4 with SpatialReferenceSystem

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;
}
Also used : GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)

Example 5 with SpatialReferenceSystem

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;
}
Also used : CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Aggregations

SpatialReferenceSystem (mil.nga.geopackage.core.srs.SpatialReferenceSystem)6 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)2 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)2 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 SpatialReferenceSystemDao (mil.nga.geopackage.core.srs.SpatialReferenceSystemDao)2 Geometry (org.locationtech.jts.geom.Geometry)2 MultiPoint (org.locationtech.jts.geom.MultiPoint)2 Point (org.locationtech.jts.geom.Point)2 ConversionException (org.springframework.core.convert.ConversionException)2 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)1 InstanceAccessor (eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 QName (javax.xml.namespace.QName)1