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;
}
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;
}
}
Aggregations