Search in sources :

Example 6 with WKTDefinition

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

Example 7 with WKTDefinition

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

the class SelectCRSDialog method createDialogArea.

/**
 * @see TitleAreaDialog#createDialogArea(Composite)
 */
@Override
protected Control createDialogArea(Composite parent) {
    Composite page = new Composite(parent, SWT.NONE);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    page.setLayoutData(data);
    page.setLayout(new GridLayout(1, false));
    group = new Group(page, SWT.NONE);
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    group.setLayout(new GridLayout(3, false));
    group.setText(Messages.SelectCRSDialog_GroupText);
    SelectionListener radioListener = new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        // ignore
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            updateSelection();
        }
    };
    boolean lastWasCode = value == null || value instanceof CodeDefinition;
    // CRS string
    radioCRS = new Button(group, SWT.RADIO);
    radioCRS.setSelection(lastWasCode);
    radioCRS.setText(Messages.SelectCRSDialog_RadioCRSText);
    radioCRS.addSelectionListener(radioListener);
    crsField = new CRSFieldEditor();
    crsField.fillIntoGrid(group, 2);
    crsField.setEmptyStringAllowed(false);
    String code;
    if (value instanceof CodeDefinition) {
        code = ((CodeDefinition) value).getCode();
    } else if (value instanceof WKTDefinition) {
        // Don't show possibly contradicting code
        code = "";
    } else {
        code = lastCode;
    }
    crsField.setStringValue(code);
    crsField.setPropertyChangeListener(this);
    crsField.setEnabled(lastWasCode, group);
    // WKT string
    radioWKT = new Button(group, SWT.RADIO);
    radioWKT.setSelection(!lastWasCode);
    radioWKT.setText(Messages.SelectCRSDialog_RadioWKTText);
    radioWKT.addSelectionListener(radioListener);
    wktField = new WKText(group);
    wktField.getTextField().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    String wkt;
    if (value instanceof WKTDefinition) {
        wkt = ((WKTDefinition) value).getWkt();
    } else if (value instanceof CodeDefinition) {
        // Display WKT corresponding to the given code, if possible
        try {
            wkt = ((CodeDefinition) value).getCRS().toWKT();
        } catch (Exception e) {
            // Avoid displaying possibly contradicting WKT
            wkt = "";
        }
    } else {
        wkt = lastWKT;
    }
    wktField.setText(wkt);
    wktField.getTextField().setEnabled(!lastWasCode);
    new Label(group, SWT.NONE);
    wktWarning = new Label(group, SWT.NONE);
    wktWarning.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
    wktWarning.setText("Please be aware that this WKT definition does not contain Bursa-Wolf\n" + "parameters in the DATUM definition. Therefore, hale studio may not be able\n" + "to perform accurate coordinate transformations when a datum shift is involved.\n" + "If you intend to perform coordinate transformations with a datum shift,\n" + "please provide the Bursa-Wolf parameters (TOWGS84) or provide an appropriate\n" + "CRS code instead. If no Bursa-Wolf parameters are required for the datum of\n" + "this CRS (e.g. in case of WGS 84 itself), you can ignore this warning.");
    wktWarning.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));
    return page;
}
Also used : Group(org.eclipse.swt.widgets.Group) Composite(org.eclipse.swt.widgets.Composite) Label(org.eclipse.swt.widgets.Label) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) GridLayout(org.eclipse.swt.layout.GridLayout) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 8 with WKTDefinition

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

the class PostGISGeometries method convertToInstanceGeometry.

@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, PGConnection connection, Supplier<CRSDefinition> crsProvider, SimpleLog log) 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);
            if (_log.isDebugEnabled()) {
                _log.debug("SRID information found in geometry is ignored");
            }
        }
        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(org.locationtech.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(org.locationtech.jts.geom.MultiLineString) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) WKTReader2(org.geotools.geometry.jts.WKTReader2) PGgeometry(org.postgis.PGgeometry)

Example 9 with WKTDefinition

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

the class MsSqlGeometries method convertToInstanceGeometry.

@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLServerConnection connection, Supplier<CRSDefinition> crsProvider, SimpleLog log) 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(org.locationtech.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(org.locationtech.jts.io.ParseException) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) WKTReader(org.locationtech.jts.io.WKTReader) ParseException(org.locationtech.jts.io.ParseException)

Example 10 with WKTDefinition

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

the class SpatiaLiteGeometries method convertToInstanceGeometry.

@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider, SimpleLog log) 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(org.locationtech.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)

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