Search in sources :

Example 1 with AttributesDao

use of mil.nga.geopackage.attributes.AttributesDao in project hale by halestudio.

the class GeopackageInstanceWriter method writeInstances.

/**
 * Write instances to the GeoPackage.
 *
 * @param geoPackage the GeoPackage
 * @param instances the instances to write
 * @param progress the progress indicator
 * @param reporter the reporter
 * @throws SQLException if an error occurs creating a database table
 */
protected void writeInstances(GeoPackage geoPackage, InstanceCollection instances, ProgressIndicator progress, IOReporter reporter) throws SQLException {
    try (ResourceIterator<Instance> it = instances.iterator()) {
        while (it.hasNext() && !progress.isCanceled()) {
            Instance instance = it.next();
            TypeDefinition type = instance.getDefinition();
            // TODO only specific types? (e.g. ignore some kinds of types?)
            String tableName = type.getName().getLocalPart();
            // determine table for type (and create if necessary)
            GeopackageTableType tableType = createTableIfNecessary(geoPackage, tableName, type, instance, reporter);
            switch(tableType) {
                case ATTRIBUTE:
                    AttributesDao attributes = geoPackage.getAttributesDao(tableName);
                    AttributesRow arow = attributes.newRow();
                    populateRow(arow, instance, reporter);
                    attributes.insert(arow);
                    break;
                case FEATURE:
                    FeatureDao features = geoPackage.getFeatureDao(tableName);
                    FeatureRow frow = features.newRow();
                    populateRow(frow, instance, reporter);
                    // set geometry
                    String geometryColumn = features.getGeometryColumnName();
                    // XXX instead of using value traverse (GeometryFinder) for
                    // geometry?
                    Object geom = new InstanceAccessor(instance).findChildren(geometryColumn).value();
                    GeoPackageGeometryData geomData = convertGeometry(geom, features.getGeometryColumns(), reporter);
                    frow.setGeometry(geomData);
                    features.insert(frow);
                    break;
            }
        }
    }
}
Also used : GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) InstanceAccessor(eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) AttributesRow(mil.nga.geopackage.attributes.AttributesRow) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 2 with AttributesDao

use of mil.nga.geopackage.attributes.AttributesDao in project hale by halestudio.

the class GeopackageInstanceReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Inspecting GeoPackage", ProgressIndicator.UNKNOWN);
    try {
        Map<String, String> queryFilters = new HashMap<>();
        String queryFiltersParam = getParameter(PARAM_QUERY_FILTER).as(String.class, null);
        if (StringUtils.hasText(queryFiltersParam)) {
            Set<String> queryFiltersByTable = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(queryFiltersParam, "||")));
            for (String queryFilterRaw : queryFiltersByTable) {
                String[] rawSplit = StringUtils.split(queryFilterRaw, "|");
                if (rawSplit.length == 2) {
                    queryFilters.put(rawSplit[0].toUpperCase(), rawSplit[1]);
                }
            }
        }
        URI loc = getSource().getLocation();
        File file;
        try {
            file = new File(loc);
        } catch (Exception e) {
            throw new IllegalArgumentException("Only files are supported as data source", e);
        }
        GeoPackage gpkg = GeoPackageManager.open(file, true);
        Map<TypeDefinition, InstanceCollection> collections = new HashMap<>();
        // try to load each feature table
        for (String table : gpkg.getFeatureTables()) {
            TypeDefinition type = findType(table);
            if (type == null) {
                reporter.warn("For feature table {0} no matching schema type could be identified", table);
            } else {
                FeatureDao features = gpkg.getFeatureDao(table);
                String where = null;
                if (queryFilters.containsKey(table.toUpperCase())) {
                    where = queryFilters.get(table.toUpperCase());
                }
                collections.put(type, new GeopackageFeatureCollection(features, type, where));
            }
        }
        // try to load each attribute table
        for (String table : gpkg.getAttributesTables()) {
            TypeDefinition type = findType(table);
            if (type == null) {
                reporter.warn("For attribute table {0} no matching schema type could be identified", table);
            } else {
                AttributesDao attributes = gpkg.getAttributesDao(table);
                collections.put(type, new GeopackageFeatureCollection(attributes, type));
            }
        }
        collection = new PerTypeInstanceCollection(collections);
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Error configuring database connection", e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : HashMap(java.util.HashMap) GeopackageFeatureCollection(eu.esdihumboldt.hale.io.geopackage.internal.GeopackageFeatureCollection) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) GeoPackage(mil.nga.geopackage.GeoPackage) URI(java.net.URI) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) File(java.io.File) PerTypeInstanceCollection(eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection) HashSet(java.util.HashSet)

Example 3 with AttributesDao

use of mil.nga.geopackage.attributes.AttributesDao in project hale by halestudio.

the class GeopackageSchemaBuilder method buildSchema.

/**
 * Build a schema.
 *
 * @param gpkg the geopackage
 * @param location the schema location or <code>null</code>
 * @return the hale schema
 */
public Schema buildSchema(GeoPackage gpkg, URI location) {
    List<String> tables = gpkg.getFeatureTables();
    DefaultSchema schema = new DefaultSchema(defaultNamespace, location);
    tables.stream().forEach(table -> {
        try {
            FeatureDao features = gpkg.getFeatureDao(table);
            GeometryColumns geomColumns = gpkg.getGeometryColumnsDao().queryForTableName(table);
            schema.addType(buildType(features, geomColumns, schema));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    });
    tables = gpkg.getAttributesTables();
    tables.stream().forEach(table -> {
        AttributesDao attributes = gpkg.getAttributesDao(table);
        schema.addType(buildType(attributes, null, schema));
    });
    return schema;
}
Also used : GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) SQLException(java.sql.SQLException) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString)

Aggregations

AttributesDao (mil.nga.geopackage.attributes.AttributesDao)3 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)2 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 InstanceAccessor (eu.esdihumboldt.hale.common.instance.groovy.InstanceAccessor)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 PerTypeInstanceCollection (eu.esdihumboldt.hale.common.instance.model.ext.impl.PerTypeInstanceCollection)1 DefaultSchema (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema)1 GeopackageFeatureCollection (eu.esdihumboldt.hale.io.geopackage.internal.GeopackageFeatureCollection)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 GeoPackage (mil.nga.geopackage.GeoPackage)1