use of mil.nga.geopackage.GeoPackage in project hale by halestudio.
the class GeopackageApiTest method testLoadFile.
/**
* Test using the Geopackage API to load a file.
*/
@Test
public void testLoadFile() {
withTestFile("testdata/util_wastewater_discharge.gpkg", file -> {
GeoPackage gp = GeoPackageManager.open(file);
List<String> tables = gp.getFeatureTables();
assertEquals(1, tables.size());
String tableName = tables.get(0);
FeatureDao features = gp.getFeatureDao(tableName);
// FeatureTable table = features.getTable();
assertEquals(3193, features.count());
});
}
use of mil.nga.geopackage.GeoPackage 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;
}
use of mil.nga.geopackage.GeoPackage in project hale by halestudio.
the class GeopackageInstanceWriter method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Generating Geopackage", ProgressIndicator.UNKNOWN);
InstanceCollection instances = getInstances();
GeoPackage geoPackage = null;
// Connection connection = null;
try {
URI loc = getTarget().getLocation();
File file;
try {
file = new File(loc);
} catch (Exception e) {
throw new IllegalArgumentException("Only files are supported as data source", e);
}
if (file.exists() && file.length() == 0L) {
// convenience for overwriting empty existing file
file.delete();
}
if (!file.exists()) {
GeoPackageManager.create(file);
}
// open existing file
geoPackage = GeoPackageManager.open(file, true);
if (true) /* isWriteUnordered() */
{
// write instances as they come in
writeInstances(geoPackage, instances, progress, reporter);
} else {
// write instances based on type order needed for insert
// (to avoid violating constraints)
/*
* Set<TypeDefinition> sortedSet = getSortedSchemas(
* getTargetSchema().getMappingRelevantTypes());
*
* for (TypeDefinition td : sortedSet) {
* writeInstances(geoPackage, instances.select(new
* TypeFilter(td)), progress, reporter); }
*/
}
// connection.commit();
reporter.setSuccess(true);
} catch (Exception e) {
reporter.error(new IOMessageImpl(e.getLocalizedMessage(), e));
reporter.setSuccess(false);
reporter.setSummary("Saving instances to GeoPackage failed.");
// if (connection != null) {
// try {
// connection.rollback();
// } catch (SQLException e1) {
// reporter.error("Error rolling back transaction", e1);
// }
// }
} finally {
if (geoPackage != null) {
geoPackage.close();
}
progress.end();
}
return reporter;
}
use of mil.nga.geopackage.GeoPackage in project hale by halestudio.
the class GeopackageSchemaReader method loadFromSource.
@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Read Geopackage schema", ProgressIndicator.UNKNOWN);
try {
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);
Schema schema = new GeopackageSchemaBuilder().buildSchema(gpkg, loc);
reporter.setSuccess(true);
return schema;
} catch (Exception e) {
reporter.setSuccess(false);
reporter.error("Error loading schema", e);
return null;
} finally {
progress.end();
}
}
use of mil.nga.geopackage.GeoPackage 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;
}
Aggregations