use of mil.nga.geopackage.schema.TableColumnKey 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;
}
Aggregations