use of org.gdal.ogr.FeatureDefn in project com.revolsys.open by revolsys.
the class OgrRecordStore method newLayerRecordDefinition.
protected RecordDefinitionImpl newLayerRecordDefinition(final RecordStoreSchema schema, final Layer layer) {
final String layerName = layer.GetName();
final PathName typePath = PathName.newPathName(layerName);
/**
* This primes the layer so that the fidColumn is loaded correctly.
*/
layer.GetNextFeature();
final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
String idFieldName = layer.GetFIDColumn();
if (!Property.hasValue(idFieldName)) {
idFieldName = "rowid";
}
this.idFieldNames.put(typePath.getUpperPath(), idFieldName);
final FeatureDefn layerDefinition = layer.GetLayerDefn();
if (SQLITE.equals(this.driverName) || GEO_PAKCAGE.equals(this.driverName)) {
recordDefinition.addField(idFieldName, DataTypes.LONG, true);
recordDefinition.setIdFieldName(idFieldName);
}
for (int fieldIndex = 0; fieldIndex < layerDefinition.GetFieldCount(); fieldIndex++) {
final FieldDefn fieldDefinition = layerDefinition.GetFieldDefn(fieldIndex);
final String fieldName = fieldDefinition.GetName();
final int fieldType = fieldDefinition.GetFieldType();
final int fieldWidth = fieldDefinition.GetWidth();
final int fieldPrecision = fieldDefinition.GetPrecision();
DataType fieldDataType;
switch(fieldType) {
case 0:
fieldDataType = DataTypes.INT;
break;
case 2:
fieldDataType = DataTypes.DOUBLE;
break;
case 4:
case 6:
fieldDataType = DataTypes.STRING;
break;
case 9:
fieldDataType = DataTypes.DATE;
break;
case 11:
fieldDataType = DataTypes.DATE_TIME;
break;
default:
fieldDataType = DataTypes.STRING;
final String fieldTypeName = fieldDefinition.GetFieldTypeName(fieldType);
Logs.error(this, "Unsupported field type " + this.file + " " + fieldName + ": " + fieldTypeName);
break;
}
final FieldDefinition field = new FieldDefinition(fieldName, fieldDataType, fieldWidth, fieldPrecision, false);
recordDefinition.addField(field);
}
for (int fieldIndex = 0; fieldIndex < layerDefinition.GetGeomFieldCount(); fieldIndex++) {
final GeomFieldDefn fieldDefinition = layerDefinition.GetGeomFieldDefn(fieldIndex);
final String fieldName = fieldDefinition.GetName();
final int geometryFieldType = fieldDefinition.GetFieldType();
DataType geometryFieldDataType;
int axisCount = 2;
switch(geometryFieldType) {
case 1:
geometryFieldDataType = DataTypes.POINT;
break;
case 2:
geometryFieldDataType = DataTypes.LINE_STRING;
break;
case 3:
geometryFieldDataType = DataTypes.POLYGON;
break;
case 4:
geometryFieldDataType = DataTypes.MULTI_POINT;
break;
case 5:
geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
break;
case 6:
geometryFieldDataType = DataTypes.MULTI_POLYGON;
break;
case 7:
geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
break;
case 101:
geometryFieldDataType = DataTypes.LINEAR_RING;
break;
case 0x80000000 + 1:
geometryFieldDataType = DataTypes.POINT;
axisCount = 3;
break;
case 0x80000000 + 2:
geometryFieldDataType = DataTypes.LINE_STRING;
axisCount = 3;
break;
case 0x80000000 + 3:
geometryFieldDataType = DataTypes.POLYGON;
axisCount = 3;
break;
case 0x80000000 + 4:
geometryFieldDataType = DataTypes.MULTI_POINT;
axisCount = 3;
break;
case 0x80000000 + 5:
geometryFieldDataType = DataTypes.MULTI_LINE_STRING;
axisCount = 3;
break;
case 0x80000000 + 6:
geometryFieldDataType = DataTypes.MULTI_POLYGON;
axisCount = 3;
break;
case 0x80000000 + 7:
geometryFieldDataType = DataTypes.GEOMETRY_COLLECTION;
axisCount = 3;
break;
default:
geometryFieldDataType = DataTypes.GEOMETRY;
break;
}
final SpatialReference spatialReference = fieldDefinition.GetSpatialRef();
final GeometryFactory geometryFactory = Gdal.getGeometryFactory(spatialReference, axisCount);
final FieldDefinition field = new FieldDefinition(fieldName, geometryFieldDataType, false);
field.setProperty(FieldProperties.GEOMETRY_FACTORY, geometryFactory);
recordDefinition.addField(field);
}
return recordDefinition;
}
use of org.gdal.ogr.FeatureDefn in project com.revolsys.open by revolsys.
the class OgrRecordWriter method insert.
private void insert(final Record record) {
final RecordDefinition sourceRecordDefinition = record.getRecordDefinition();
final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(sourceRecordDefinition);
final String typePath = sourceRecordDefinition.getPath();
final List<FieldDefinition> attributes = recordDefinition.getFields();
final List<String> idFieldNames = recordDefinition.getIdFieldNames();
for (final FieldDefinition attribute : attributes) {
final String name = attribute.getName();
if (!idFieldNames.contains(name)) {
if (attribute.isRequired()) {
final Object value = record.getValue(name);
if (value == null) {
throw new IllegalArgumentException("Atribute " + typePath + "." + name + " is required");
}
}
}
}
try {
final Layer layer = getLayer(sourceRecordDefinition);
final FeatureDefn featureDefinition = layer.GetLayerDefn();
final Feature feature = new Feature(featureDefinition);
try {
setFieldValues(featureDefinition, record, feature);
setGeometries(featureDefinition, record, feature);
layer.CreateFeature(feature);
final String driverName = this.recordStore.getDriverName();
if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
record.setValue(OgrRecordStore.ROWID, feature.GetFieldAsInteger(OgrRecordStore.ROWID));
}
record.setState(RecordState.PERSISTED);
} finally {
feature.delete();
this.recordStore.addStatistic("Insert", record);
}
} catch (final IllegalArgumentException e) {
throw new RuntimeException("Unable to insert row " + e.getMessage() + "\n" + record.toString(), e);
} catch (final RuntimeException e) {
Logs.debug(OgrRecordWriter.class, "Unable to insert row \n:" + record.toString());
throw new RuntimeException("Unable to insert row", e);
}
}
use of org.gdal.ogr.FeatureDefn in project com.revolsys.open by revolsys.
the class OgrRecordWriter method update.
private void update(final Record record) {
final RecordDefinition recordDefinition = record.getRecordDefinition();
final Layer layer = getLayer(recordDefinition);
final String driverName = this.recordStore.getDriverName();
if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
final Integer fid = record.getInteger(OgrRecordStore.ROWID);
if (fid != null) {
final Feature feature = layer.GetFeature(fid);
if (feature != null) {
final FeatureDefn featureDefinition = layer.GetLayerDefn();
setFieldValues(featureDefinition, record, feature);
layer.SetFeature(feature);
}
}
}
}
Aggregations