Search in sources :

Example 1 with WKTDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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 WKTDefinition

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

the class GeopackageInstanceWriter method toCRSDefinition.

private CRSDefinition toCRSDefinition(SpatialReferenceSystem srs) {
    String org = srs.getOrganization();
    long code = srs.getOrganizationCoordsysId();
    if (org != null && org.equals("EPSG")) {
        // axis order is always x,y (longitude first)
        return new CodeDefinition(org + ":" + code, true);
    }
    return new WKTDefinition(srs.getDefinition(), null);
}
Also used : CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Example 3 with WKTDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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);
            // Look up the code
            CoordinateReferenceSystem resolved = CRS.decode(code);
            // 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 WKTDefinition

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

the class CRSHelperFunctionsTest method testFromWKT.

@Test
public void testFromWKT() {
    String wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";
    CRSDefinition crs = CRSHelperFunctions._fromWKT(wkt);
    assertNotNull(crs);
    assertNotNull(crs.getCRS());
    assertTrue(crs instanceof WKTDefinition);
    assertEquals(wkt, ((WKTDefinition) crs).getWkt());
}
Also used : CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) Test(org.junit.Test)

Example 5 with WKTDefinition

use of eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition 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)

Aggregations

WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)12 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)11 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)8 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)6 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)4 Geometry (com.vividsolutions.jts.geom.Geometry)3 Geometry (org.locationtech.jts.geom.Geometry)3 MultiLineString (org.locationtech.jts.geom.MultiLineString)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 WKTReader2 (org.geotools.geometry.jts.WKTReader2)2 LineString (org.locationtech.jts.geom.LineString)2 FactoryException (org.opengis.referencing.FactoryException)2 PGgeometry (org.postgis.PGgeometry)2 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 SpatialReferenceSystem (mil.nga.geopackage.core.srs.SpatialReferenceSystem)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1