Search in sources :

Example 26 with DefaultTypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.

the class GeopackageSchemaBuilder method getOrCreatePropertyType.

private TypeDefinition getOrCreatePropertyType(UserColumn column, GeometryColumns geomColumns, DefaultSchema schema) {
    String localName;
    if (column instanceof FeatureColumn && ((FeatureColumn) column).isGeometry()) {
        localName = ((FeatureColumn) column).getGeometryType().getName();
    } else {
        localName = column.getDataType().name();
    }
    QName typeName = new QName(defaultNamespace, localName);
    TypeDefinition type = schema.getType(typeName);
    if (type != null) {
        // use existing type
        return type;
    } else {
        DefaultTypeDefinition typeDef = new DefaultTypeDefinition(typeName);
        typeDef.setConstraint(MappingRelevantFlag.DISABLED);
        typeDef.setConstraint(MappableFlag.DISABLED);
        // simple type
        typeDef.setConstraint(HasValueFlag.ENABLED);
        if (column instanceof FeatureColumn && ((FeatureColumn) column).isGeometry()) {
            mil.nga.sf.GeometryType geomType = ((FeatureColumn) column).getGeometryType();
            typeDef.setConstraint(Binding.get(GeometryProperty.class));
            Class<? extends Geometry> geomClass = Geometry.class;
            switch(geomType) {
                case LINESTRING:
                    geomClass = LineString.class;
                    break;
                case MULTIPOINT:
                    geomClass = MultiPoint.class;
                    break;
                case MULTILINESTRING:
                    geomClass = MultiLineString.class;
                    break;
                case MULTIPOLYGON:
                    geomClass = MultiPolygon.class;
                    break;
                case POINT:
                    geomClass = Point.class;
                    break;
                case POLYGON:
                    geomClass = Polygon.class;
                    break;
                default:
                    break;
            }
            typeDef.setConstraint(GeometryType.get(geomClass));
            SpatialReferenceSystem srs = geomColumns.getSrs();
            if (srs != null) {
                String code = String.valueOf(srs.getOrganizationCoordsysId());
                int dimension = GeometryMetadata.UNKNOWN_DIMENSION;
                String srsText = srs.getDefinition();
                String auth_name = srs.getOrganization();
                typeDef.setConstraint(new GeometryMetadata(code, dimension, srsText, auth_name));
            }
        } else {
            GeoPackageDataType dataType = column.getDataType();
            Class<?> binding;
            switch(dataType) {
                case DATETIME:
                    binding = Instant.class;
                    break;
                case DATE:
                    binding = LocalDate.class;
                    break;
                default:
                    binding = dataType.getClassType();
            }
            typeDef.setConstraint(Binding.get(binding));
        }
        return typeDef;
    }
}
Also used : GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) QName(javax.xml.namespace.QName) GeoPackageDataType(mil.nga.geopackage.db.GeoPackageDataType) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) Geometry(org.locationtech.jts.geom.Geometry) FeatureColumn(mil.nga.geopackage.features.user.FeatureColumn) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem)

Example 27 with DefaultTypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.

the class CSVSchemaReader method loadFromSource.

@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // $NON-NLS-1$
    progress.begin("Load CSV schema", ProgressIndicator.UNKNOWN);
    String namespace = CSVFileIO.CSVFILE_NS;
    DefaultSchema schema = new DefaultSchema(namespace, getSource().getLocation());
    CSVReader reader = CSVUtil.readFirst(this);
    try {
        // initializes the first line of the table (names of the columns)
        firstLine = reader.readNext();
        // create type definition
        String typename = getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class);
        if (typename == null || typename.isEmpty()) {
            reporter.setSuccess(false);
            reporter.error(new IOMessageImpl("No Typename was set", null));
            return null;
        }
        DefaultTypeDefinition type = new DefaultTypeDefinition(new QName(typename));
        // constraints on main type
        type.setConstraint(MappingRelevantFlag.ENABLED);
        type.setConstraint(MappableFlag.ENABLED);
        type.setConstraint(HasValueFlag.DISABLED);
        type.setConstraint(AbstractFlag.DISABLED);
        // set metadata for main type
        type.setLocation(getSource().getLocation());
        StringBuffer defaultPropertyTypeBuffer = new StringBuffer();
        String[] comboSelections;
        if (getParameter(PARAM_PROPERTYTYPE).isEmpty()) {
            for (int i = 0; i < firstLine.length; i++) {
                defaultPropertyTypeBuffer.append("java.lang.String");
                defaultPropertyTypeBuffer.append(",");
            }
            defaultPropertyTypeBuffer.deleteCharAt(defaultPropertyTypeBuffer.lastIndexOf(","));
            String combs = defaultPropertyTypeBuffer.toString();
            comboSelections = combs.split(",");
        } else {
            comboSelections = getParameter(PARAM_PROPERTYTYPE).as(String.class).split(",");
        }
        String[] properties;
        if (getParameter(PARAM_PROPERTY).isEmpty()) {
            properties = firstLine;
        } else {
            properties = getParameter(PARAM_PROPERTY).as(String.class).split(",");
        }
        // than the entries in the first line
        if ((firstLine.length != properties.length && properties.length != 0) || (firstLine.length != comboSelections.length && comboSelections.length != 0)) {
            fail("Not the same number of entries for property names, property types and words in the first line of the file");
        }
        for (int i = 0; i < comboSelections.length; i++) {
            PropertyType propertyType;
            propertyType = PropertyTypeExtension.getInstance().getFactory(comboSelections[i]).createExtensionObject();
            DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(properties[i]), type, propertyType.getTypeDefinition());
            // set constraints on property
            // property.setConstraint(NillableFlag.DISABLED); // nillable
            // nillable FIXME
            property.setConstraint(NillableFlag.ENABLED);
            // should be
            // configurable
            // per field
            // (see also
            // CSVInstanceReader)
            // cardinality
            property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
            // set metadata for property
            property.setLocation(getSource().getLocation());
        }
        boolean skip = Arrays.equals(properties, firstLine);
        type.setConstraint(new CSVConfiguration(CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skip ? 1 : 0));
        schema.addType(type);
    } catch (Exception ex) {
        reporter.error(new IOMessageImpl("Cannot load csv schema", ex));
        reporter.setSuccess(false);
        return null;
    }
    reporter.setSuccess(true);
    return schema;
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) CSVReader(au.com.bytecode.opencsv.CSVReader) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyType(eu.esdihumboldt.hale.io.csv.PropertyType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)

Example 28 with DefaultTypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.

the class GeopackageSchemaBuilder method buildType.

private TypeDefinition buildType(UserDao<?, ?, ?, ?> dao, GeometryColumns geomColumns, DefaultSchema schema) {
    QName name = new QName(defaultNamespace, dao.getTableName());
    DefaultTypeDefinition type = new DefaultTypeDefinition(name);
    type.setConstraint(MappableFlag.ENABLED);
    type.setConstraint(MappingRelevantFlag.ENABLED);
    type.setConstraint(HasValueFlag.DISABLED);
    type.setConstraint(AbstractFlag.DISABLED);
    type.setConstraint(Binding.get(Instance.class));
    UserTable<? extends UserColumn> table = dao.getTable();
    // set primary key constraint
    UserColumn pkColumn = table.getPkColumn();
    if (pkColumn != null) {
        type.setConstraint(new PrimaryKey(Collections.singletonList(new QName(pkColumn.getName()))));
    }
    for (String columnName : table.getColumnNames()) {
        UserColumn column = table.getColumn(columnName);
        QName propertyName = new QName(columnName);
        // determine property type
        TypeDefinition propertyType = getOrCreatePropertyType(column, geomColumns, schema);
        DefaultPropertyDefinition property = new DefaultPropertyDefinition(propertyName, type, propertyType);
        property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
        if (column.isNotNull()) {
            property.setConstraint(NillableFlag.DISABLED);
        } else {
            property.setConstraint(NillableFlag.ENABLED);
        }
    }
    return type;
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) UserColumn(mil.nga.geopackage.user.UserColumn) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) PrimaryKey(eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)28 QName (javax.xml.namespace.QName)23 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)18 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)15 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)5 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)4 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)4 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)4 DefaultGroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultGroupPropertyDefinition)4 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)3 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)2 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)2