Search in sources :

Example 1 with CodeDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.

the class SpatiaLiteGeometries method convertToInstanceGeometry.

/**
 * @see eu.esdihumboldt.hale.io.jdbc.GeometryAdvisor#convertToInstanceGeometry(java.lang.Object,
 *      eu.esdihumboldt.hale.common.schema.model.TypeDefinition,
 *      java.lang.Object, java.util.function.Supplier)
 */
@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
    // show error and abort if SpatiaLite is not available
    if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
    // don't throw, will prevent any data being loaded
    // throw new IllegalStateException("SpatiaLite module is not available");
    }
    // decode geometry read from DB
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    Geometry jtsGeom = decodeGeometryValue(geom, columnTypeMetadata, connection);
    // determine CRS
    CRSDefinition crsDef = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equalsIgnoreCase("EPSG")) {
        String epsgCode = authName + ":" + columnTypeMetadata.getSrs();
        crsDef = new CodeDefinition(epsgCode, null);
    } else {
        String wkt = columnTypeMetadata.getSrsText();
        if (wkt != null) {
            crsDef = new WKTDefinition(wkt, null);
        }
    }
    return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Example 2 with CodeDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.

the class CRSHelperFunctionsTest method testFromCode.

@Test
public void testFromCode() {
    String code = "EPSG:4326";
    Map<String, Object> args = new HashMap<>();
    args.put("code", code);
    CRSDefinition crs = CRSHelperFunctions._from(args);
    assertNotNull(crs);
    assertNotNull(crs.getCRS());
    assertTrue(crs instanceof CodeDefinition);
    assertEquals(code, ((CodeDefinition) crs).getCode());
}
Also used : HashMap(java.util.HashMap) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) Test(org.junit.Test)

Example 3 with CodeDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.

the class CRSDefinitionUtil method lookupCrs.

/**
 * Try to lookup the given CRS via Geotools. If the CRS can be resolved, the
 * returned {@link CRSDefinition} will contain a
 * {@link CoordinateReferenceSystem} with additional information like
 * Bursa-Wolf parameters, otherwise the WKT definition of the input CRS will
 * be used as is.
 *
 * @param crs The CRS to look up
 * @return A {@link CodeDefinition} with the resolved CRS or a
 *         {@link WKTDefinition} if the CRS could not be resolved.
 */
public static CRSDefinition lookupCrs(CoordinateReferenceSystem crs) {
    try {
        Integer epsgCode = CRS.lookupEpsgCode(crs, true);
        if (epsgCode != null) {
            // We must use the "EPSG:" prefix here, otherwise Geotools will
            // not honour the longitudeFirst parameter and will always
            // return the lat/lon variant...
            String code = SrsSyntax.EPSG_CODE.getPrefix() + String.valueOf(epsgCode);
            // Check if the input CRS is lon/lat
            boolean lonFirst = (CRS.getAxisOrder(crs) == AxisOrder.EAST_NORTH);
            // Look up the code
            CoordinateReferenceSystem resolved = CRS.decode(code, lonFirst);
            // is still the same (not guaranteed)
            if (CRS.getAxisOrder(crs).equals(CRS.getAxisOrder(resolved))) {
                return new CodeDefinition(code, resolved);
            }
        }
    } catch (FactoryException e) {
    // Ignore
    }
    return new WKTDefinition(crs.toWKT(), crs);
}
Also used : CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Example 4 with CodeDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.

the class MsSqlGeometries method convertToInstanceGeometry.

/**
 * @see eu.esdihumboldt.hale.io.jdbc.GeometryAdvisor#convertToInstanceGeometry(java.lang.Object,
 *      eu.esdihumboldt.hale.common.schema.model.TypeDefinition,
 *      java.lang.Object, java.util.function.Supplier)
 */
@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLServerConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
    Statement stmt = null;
    ResultSet rs = null;
    try {
        // We need Column Data type
        String columnDataType = columnType.getName().getLocalPart();
        String geomAsHex = BaseEncoding.base16().lowerCase().encode((byte[]) geom);
        String sqlGeom = // 
        "SELECT top 1 GeomConvert.geom.STSrid srid, GeomConvert.geom.STAsText() as geomAsText, GeomConvert.geom.STGeometryType() as geomType " + // 
        "FROM " + "(SELECT cast(cast(temp.wkb as varbinary(max)) as " + columnDataType + // 
        ") as geom " + // 
        "FROM " + "( select " + "0x" + geomAsHex + // 
        " as wkb) as temp" + // 
        ") " + // 
        "as GeomConvert";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sqlGeom);
        Geometry jtsGeom = null;
        int srId = 0;
        if (rs.next()) {
            srId = rs.getInt(1);
            String geomAsText = rs.getString(2);
            String geomType = rs.getString(3);
            // WKTReader does not support CircularString, CurvePolygon,
            // CompoundCurve
            WKTReader wktReader = getSpecificWktReader(geomType);
            try {
                // conversion to JTS via WKT
                jtsGeom = wktReader.read(geomAsText);
            } catch (ParseException e) {
                log.error("Could not load geometry from database", e);
            }
        }
        CRSDefinition crsDef = null;
        String authName = SRSUtil.getAuthorityName(srId, connection);
        if (authName != null && authName.equals("EPSG")) {
            // For geography/geometry data type, SQL server assumes lon/lat
            // axis order, if we read using SQL function
            String epsgCode = authName + ":" + SRSUtil.getSRS(srId, connection);
            if (columnDataType.equals("geography"))
                crsDef = new CodeDefinition(epsgCode, true);
            else
                crsDef = new CodeDefinition(epsgCode, null);
        } else {
            String wkt = SRSUtil.getSRSText(srId, connection);
            if (wkt != null) {
                crsDef = new WKTDefinition(wkt, null);
            }
        }
        if (crsDef == null) {
            log.warn("Could not find spatial reference system id " + srId + " in MS sql server");
            crsDef = crsProvider.get();
            if (crsDef == null) {
                log.warn("Could not retrieve default spatial reference for " + srId + " in MS sql server");
            }
            // saving in cache
            if (crsDef != null) {
                String srsName = CRS.toSRS(crsDef.getCRS());
                if (srsName != null) {
                    final int index = srsName.lastIndexOf(':');
                    String authorityName = null;
                    String authorizedId = null;
                    if (index > 0) {
                        authorityName = srsName.substring(0, index);
                        authorizedId = srsName.substring(index + 1).trim();
                    }
                    // we don't need wkt.
                    SRSUtil.addSRSinCache(srId, authorityName, authorizedId, null);
                }
            }
        }
        return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (Exception e) {
            // 
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            // 
            }
    }
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ParseException(com.vividsolutions.jts.io.ParseException) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) WKTReader(com.vividsolutions.jts.io.WKTReader) ParseException(com.vividsolutions.jts.io.ParseException)

Example 5 with CodeDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition in project hale by halestudio.

the class PostGISGeometries method convertToInstanceGeometry.

/**
 * @see eu.esdihumboldt.hale.io.jdbc.GeometryAdvisor#convertToInstanceGeometry(java.lang.Object,
 *      eu.esdihumboldt.hale.common.schema.model.TypeDefinition,
 *      java.lang.Object, java.util.function.Supplier)
 */
@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, PGConnection connection, Supplier<CRSDefinition> crsProvider) throws Exception {
    if (geom instanceof PGgeometry) {
        PGgeometry pgeom = (PGgeometry) geom;
        // conversion to JTS via WKT
        // TODO use better conversion (p4b?)
        WKTReader2 reader = new WKTReader2();
        String value = pgeom.getGeometry().toString();
        if (value.startsWith(PGgeometry.SRIDPREFIX) && value.indexOf(';') >= 0) {
            value = value.substring(value.indexOf(';') + 1);
        }
        Geometry jtsGeom = reader.read(value);
        // determine CRS
        GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
        CRSDefinition crsDef = null;
        String authName = columnTypeMetadata.getAuthName();
        if (authName != null && authName.equals("EPSG")) {
            // PostGIS assumes lon/lat order
            crsDef = new CodeDefinition(authName + ":" + columnTypeMetadata.getSrs(), true);
        } else {
            String wkt = columnTypeMetadata.getSrsText();
            if (wkt != null) {
                crsDef = new WKTDefinition(wkt, null);
            }
        }
        return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
    }
    throw new IllegalArgumentException("Only conversion of PGgeometry supported");
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) WKTReader2(org.geotools.geometry.jts.WKTReader2) PGgeometry(org.postgis.PGgeometry)

Aggregations

CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)10 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)8 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)6 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)6 Geometry (com.vividsolutions.jts.geom.Geometry)5 FactoryException (org.opengis.referencing.FactoryException)3 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)2 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)2 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 Point (com.vividsolutions.jts.geom.Point)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)1 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)1 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)1 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)1