Search in sources :

Example 1 with FeatureColumn

use of mil.nga.geopackage.features.user.FeatureColumn in project hale by halestudio.

the class GeopackageInstanceWriter method createTableIfNecessary.

private GeopackageTableType createTableIfNecessary(GeoPackage geoPackage, String tableName, TypeDefinition type, Instance instance, SimpleLog log) throws SQLException {
    if (geoPackage.getFeatureTables().contains(tableName)) {
        // table already exists
        return GeopackageTableType.FEATURE;
    }
    if (geoPackage.getAttributesTables().contains(tableName)) {
        // table already exists
        return GeopackageTableType.ATTRIBUTE;
    }
    // determine if we need a feature or attribute table
    Collection<? extends PropertyDefinition> allProperties = DefinitionUtil.getAllProperties(type).stream().filter(p -> {
        // AugmentedValue
        return p.getPropertyType().getConstraint(HasValueFlag.class).isEnabled() || p.getPropertyType().getConstraint(AugmentedValueFlag.class).isEnabled();
    }).collect(Collectors.toList());
    Optional<? extends PropertyDefinition> geometryProperty = allProperties.stream().filter(property -> {
        return property.getPropertyType().getConstraint(GeometryType.class).isGeometry();
    }).findFirst();
    List<QName> primaryPath = type.getConstraint(PrimaryKey.class).getPrimaryKeyPath();
    String primaryKeyColumn;
    if (primaryPath != null && !primaryPath.isEmpty()) {
        primaryKeyColumn = primaryPath.get(0).getLocalPart();
    } else {
        primaryKeyColumn = null;
    }
    GeopackageTableType tableType;
    if (geometryProperty.isPresent()) {
        // create feature table
        tableType = GeopackageTableType.FEATURE;
        PropertyDefinition geomProp = geometryProperty.get();
        // XXX generally only one geometry column supported?
        GeometryColumns geometryColumns = new GeometryColumns();
        geometryColumns.setId(new TableColumnKey(tableName, geomProp.getName().getLocalPart()));
        geometryColumns.setGeometryType(convertGeometryType(geomProp.getPropertyType().getConstraint(GeometryType.class).getBinding()));
        // not known at that point
        BoundingBox boundingBox = null;
        // determine SRS from GeometryMetadata or sample instance
        long srsId = determineSrsId(geoPackage, geomProp, instance, log);
        List<FeatureColumn> columns = allProperties.stream().map(p -> createFeatureColumn(p, primaryKeyColumn)).collect(Collectors.toList());
        if (primaryKeyColumn == null) {
            // primary key column seems to be always needed (without there
            // were errors reading the data)
            columns.add(FeatureColumn.createPrimaryKeyColumn(findIdName(columns)));
        }
        geoPackage.createFeatureTableWithMetadata(geometryColumns, boundingBox, srsId, columns);
        FeatureDao featureDao = geoPackage.getFeatureDao(tableName);
        String spatialIndexType = getParameter(PARAM_SPATIAL_INDEX_TYPE).as(String.class, DEFAULT_SPATIAL_INDEX_TYPE);
        switch(spatialIndexType.toLowerCase()) {
            case "nga":
                createFeatureTableIndex(geoPackage, featureDao);
                break;
            case "rtree":
                createRTreeIndex(geoPackage, featureDao);
                break;
            case "none":
                // Do nothing
                break;
            default:
                throw new IllegalArgumentException(MessageFormat.format("Unknown or unsupported spatial index type \"{0}\"", spatialIndexType));
        }
    } else {
        // create attributes table
        tableType = GeopackageTableType.ATTRIBUTE;
        List<AttributesColumn> columns = allProperties.stream().map(p -> createAttributeColumn(p, primaryKeyColumn)).collect(Collectors.toList());
        if (primaryKeyColumn == null) {
            // primary key column seems to be always needed (without there
            // were errors reading the data)
            columns.add(AttributesColumn.createPrimaryKeyColumn(findIdName(columns)));
        }
        geoPackage.createAttributesTable(tableName, columns);
    }
    return tableType;
}
Also used : IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) CRS(org.geotools.referencing.CRS) AbstractGeoInstanceWriter(eu.esdihumboldt.hale.common.instance.io.impl.AbstractGeoInstanceWriter) ByteReader(mil.nga.sf.util.ByteReader) Date(java.util.Date) PrimaryKey(eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey) GeoPackageManager(mil.nga.geopackage.manager.GeoPackageManager) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) FeatureTableIndex(mil.nga.geopackage.extension.index.FeatureTableIndex) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) BigInteger(java.math.BigInteger) WKBWriter(org.locationtech.jts.io.WKBWriter) URI(java.net.URI) JTS(org.geotools.geometry.jts.JTS) FeatureIndexType(mil.nga.geopackage.features.index.FeatureIndexType) Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) FeatureIndexManager(mil.nga.geopackage.features.index.FeatureIndexManager) Value(eu.esdihumboldt.hale.common.core.io.Value) GeoPackage(mil.nga.geopackage.GeoPackage) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) MultiLineString(org.locationtech.jts.geom.MultiLineString) TableColumnKey(mil.nga.geopackage.schema.TableColumnKey) FeatureColumn(mil.nga.geopackage.features.user.FeatureColumn) AugmentedValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) Collection(java.util.Collection) GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) Set(java.util.Set) Point(org.locationtech.jts.geom.Point) ConversionException(org.springframework.core.convert.ConversionException) Instant(java.time.Instant) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) Collectors(java.util.stream.Collectors) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) List(java.util.List) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) LocalDate(java.time.LocalDate) Polygon(org.locationtech.jts.geom.Polygon) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) QName(javax.xml.namespace.QName) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) UserColumn(mil.nga.geopackage.user.UserColumn) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) BoundingBox(mil.nga.geopackage.BoundingBox) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) GeoPackageDataType(mil.nga.geopackage.db.GeoPackageDataType) SpatialReferenceSystem(mil.nga.geopackage.core.srs.SpatialReferenceSystem) DefinitionUtil(eu.esdihumboldt.hale.common.schema.model.DefinitionUtil) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) MessageFormat(java.text.MessageFormat) SQLException(java.sql.SQLException) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) SpatialReferenceSystemDao(mil.nga.geopackage.core.srs.SpatialReferenceSystemDao) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) ConversionUtil(eu.esdihumboldt.hale.common.convert.ConversionUtil) IOException(java.io.IOException) UserCoreRow(mil.nga.geopackage.user.UserCoreRow) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) File(java.io.File) AttributesColumn(mil.nga.geopackage.attributes.AttributesColumn) LineString(org.locationtech.jts.geom.LineString) MathTransform(org.opengis.referencing.operation.MathTransform) AttributesRow(mil.nga.geopackage.attributes.AttributesRow) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor) ResourceIterator(eu.esdihumboldt.hale.common.instance.model.ResourceIterator) MultiPoint(org.locationtech.jts.geom.MultiPoint) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) GeometryReader(mil.nga.sf.wkb.GeometryReader) GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) AugmentedValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag) QName(javax.xml.namespace.QName) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) PrimaryKey(eu.esdihumboldt.hale.common.schema.model.constraint.type.PrimaryKey) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) FeatureColumn(mil.nga.geopackage.features.user.FeatureColumn) TableColumnKey(mil.nga.geopackage.schema.TableColumnKey) BoundingBox(mil.nga.geopackage.BoundingBox) AttributesColumn(mil.nga.geopackage.attributes.AttributesColumn)

Example 2 with FeatureColumn

use of mil.nga.geopackage.features.user.FeatureColumn 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)

Aggregations

GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)2 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)2 ConversionUtil (eu.esdihumboldt.hale.common.convert.ConversionUtil)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 ProgressIndicator (eu.esdihumboldt.hale.common.core.io.ProgressIndicator)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 SimpleLog (eu.esdihumboldt.hale.common.core.report.SimpleLog)1 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)1 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)1 InstanceAccessor (eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)1 AbstractGeoInstanceWriter (eu.esdihumboldt.hale.common.instance.io.impl.AbstractGeoInstanceWriter)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 ResourceIterator (eu.esdihumboldt.hale.common.instance.model.ResourceIterator)1 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)1 DefinitionUtil (eu.esdihumboldt.hale.common.schema.model.DefinitionUtil)1