Search in sources :

Example 11 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class EsriGeodatabaseXmlRecordWriter method writeDataElement.

private void writeDataElement(final RecordDefinition recordDefinition, final Geometry geometry) {
    final String dataElementType;
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    boolean hasGeometry = false;
    DataType geometryDataType = null;
    if (geometryField != null) {
        geometryDataType = geometryField.getDataType();
        if (this.fieldTypes.getFieldType(geometryDataType) != null) {
            hasGeometry = true;
            if (geometryDataType.equals(DataTypes.POINT)) {
                this.geometryType = GEOMETRY_TYPE_POINT;
            } else if (geometryDataType.equals(DataTypes.MULTI_POINT)) {
                this.geometryType = GEOMETRY_TYPE_MULTI_POINT;
            } else if (geometryDataType.equals(DataTypes.LINE_STRING)) {
                this.geometryType = GEOMETRY_TYPE_POLYLINE;
            } else if (geometryDataType.equals(DataTypes.MULTI_LINE_STRING)) {
                this.geometryType = GEOMETRY_TYPE_POLYLINE;
            } else if (geometryDataType.equals(DataTypes.POLYGON)) {
                this.geometryType = GEOMETRY_TYPE_POLYGON;
            } else {
                if (geometry instanceof Point) {
                    this.geometryType = GEOMETRY_TYPE_POINT;
                } else if (geometry instanceof Punctual) {
                    this.geometryType = GEOMETRY_TYPE_MULTI_POINT;
                } else if (geometry instanceof Lineal) {
                    this.geometryType = GEOMETRY_TYPE_POLYLINE;
                } else if (geometry instanceof Polygon) {
                    this.geometryType = GEOMETRY_TYPE_POLYGON;
                } else {
                    hasGeometry = false;
                }
            }
        }
    }
    if (hasGeometry) {
        dataElementType = DATA_ELEMENT_FEATURE_CLASS;
        this.datasetType = DATASET_TYPE_FEATURE_CLASS;
    } else {
        dataElementType = DATA_ELEMENT_TABLE;
        this.datasetType = DATASET_TYPE_TABLE;
    }
    this.out.startTag(DATA_ELEMENT);
    this.out.attribute(XsiConstants.TYPE, dataElementType);
    final String path = recordDefinition.getPath();
    final String localName = PathUtil.getName(path);
    this.out.element(CATALOG_PATH, "/FC=" + localName);
    this.out.element(NAME, localName);
    this.out.element(METADATA_RETRIEVED, true);
    this.out.startTag(METADATA);
    this.out.attribute(XsiConstants.TYPE, XML_PROPERTY_SET_TYPE);
    this.out.startTag(XML_DOC);
    this.out.text("<?xml version=\"1.0\"?>");
    this.out.text("<metadata xml:lang=\"en\">");
    this.out.text("<Esri>");
    this.out.text("<MetaID>{");
    this.out.text(UUID.randomUUID().toString().toUpperCase());
    this.out.text("}</MetaID>");
    this.out.text("<CreaDate>");
    final Timestamp date = new Timestamp(System.currentTimeMillis());
    this.out.text(Dates.format("yyyyMMdd", date));
    this.out.text("</CreaDate>");
    this.out.text("<CreaTime>");
    this.out.text(Dates.format("HHmmssSS", date));
    this.out.text("</CreaTime>");
    this.out.text("<SyncOnce>TRUE</SyncOnce>");
    this.out.text("</Esri>");
    this.out.text("</metadata>");
    this.out.endTag(XML_DOC);
    this.out.endTag(METADATA);
    this.out.element(DATASET_TYPE, this.datasetType);
    this.out.element(DSID, this.datasetId++);
    this.out.element(VERSIONED, false);
    this.out.element(CAN_VERSION, false);
    this.out.element(HAS_OID, true);
    this.out.element(OBJECT_ID_FIELD_NAME, "OBJECTID");
    writeFields(recordDefinition);
    this.out.element(CLSID, "{52353152-891A-11D0-BEC6-00805F7C4268}");
    this.out.emptyTag(EXTCLSID);
    this.out.startTag(RELATIONSHIP_CLASS_NAMES);
    this.out.attribute(XsiConstants.TYPE, NAMES_TYPE);
    this.out.endTag(RELATIONSHIP_CLASS_NAMES);
    this.out.element(ALIAS_NAME, localName);
    this.out.emptyTag(MODEL_NAME);
    this.out.element(HAS_GLOBAL_ID, false);
    this.out.emptyTag(GLOBAL_ID_FIELD_NAME);
    this.out.emptyTag(RASTER_FIELD_NAME);
    this.out.startTag(EXTENSION_PROPERTIES);
    this.out.attribute(XsiConstants.TYPE, PROPERTY_SET_TYPE);
    this.out.startTag(PROPERTY_ARRAY);
    this.out.attribute(XsiConstants.TYPE, PROPERTY_ARRAY_TYPE);
    this.out.endTag(PROPERTY_ARRAY);
    this.out.endTag(EXTENSION_PROPERTIES);
    this.out.startTag(CONTROLLER_MEMBERSHIPS);
    this.out.attribute(XsiConstants.TYPE, CONTROLLER_MEMBERSHIPS_TYPE);
    this.out.endTag(CONTROLLER_MEMBERSHIPS);
    if (hasGeometry) {
        this.out.element(FEATURE_TYPE, FEATURE_TYPE_SIMPLE);
        this.out.element(SHAPE_TYPE, this.geometryType);
        this.out.element(SHAPE_FIELD_NAME, geometryField.getName());
        final GeometryFactory geometryFactory = geometryField.getProperty(FieldProperties.GEOMETRY_FACTORY);
        this.out.element(HAS_M, false);
        this.out.element(HAS_Z, geometryFactory.hasZ());
        this.out.element(HAS_SPATIAL_INDEX, false);
        this.out.emptyTag(AREA_FIELD_NAME);
        this.out.emptyTag(LENGTH_FIELD_NAME);
        writeExtent(geometryFactory);
        writeSpatialReference(geometryFactory);
    }
    this.out.endTag(DATA_ELEMENT);
}
Also used : Punctual(com.revolsys.geometry.model.Punctual) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Lineal(com.revolsys.geometry.model.Lineal) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) Point(com.revolsys.geometry.model.Point) Polygon(com.revolsys.geometry.model.Polygon) Timestamp(java.sql.Timestamp)

Example 12 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class EsriXmlRecordDefinitionUtil method newDETable.

public static DETable newDETable(final RecordDefinition recordDefinition, final SpatialReference spatialReference, final boolean createLengthField, final boolean createAreaField) {
    final String typePath = recordDefinition.getPath();
    String schemaPath = PathUtil.getPath(typePath).replaceAll("/", "\\\\");
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    boolean hasGeometry = false;
    DataType geometryDataType = null;
    GeometryType shapeType = null;
    if (geometryField != null) {
        if (spatialReference == null) {
            throw new IllegalArgumentException("A Geometry Factory with a coordinate system must be specified.");
        }
        geometryDataType = geometryField.getDataType();
        if (FIELD_TYPES.getFieldType(geometryDataType) != null) {
            hasGeometry = true;
            // TODO Z,m
            if (geometryDataType.equals(DataTypes.POINT)) {
                shapeType = GeometryType.esriGeometryPoint;
            } else if (geometryDataType.equals(DataTypes.MULTI_POINT)) {
                shapeType = GeometryType.esriGeometryMultipoint;
            } else if (geometryDataType.equals(DataTypes.LINE_STRING)) {
                shapeType = GeometryType.esriGeometryPolyline;
            } else if (geometryDataType.equals(DataTypes.LINEAR_RING)) {
                shapeType = GeometryType.esriGeometryPolyline;
            } else if (geometryDataType.equals(DataTypes.MULTI_LINE_STRING)) {
                shapeType = GeometryType.esriGeometryPolyline;
            } else if (geometryDataType.equals(DataTypes.POLYGON)) {
                shapeType = GeometryType.esriGeometryPolygon;
            } else if (geometryDataType.equals(DataTypes.MULTI_POLYGON)) {
                shapeType = GeometryType.esriGeometryPolygon;
            } else {
                throw new IllegalArgumentException("Unable to detect geometry type");
            }
        }
    }
    final List<FieldDefinition> fieldDefinitions = new ArrayList<>(recordDefinition.getFields());
    final String path = recordDefinition.getPath();
    final String name = PathUtil.getName(path);
    DETable table;
    if (hasGeometry) {
        final DEFeatureClass featureClass = new DEFeatureClass();
        table = featureClass;
        featureClass.setShapeType(shapeType);
        final String geometryFieldName = geometryField.getName();
        featureClass.setShapeFieldName(geometryFieldName);
        final GeometryFactory geometryFactory = spatialReference.getGeometryFactory();
        featureClass.setSpatialReference(spatialReference);
        featureClass.setHasM(geometryFactory.hasM());
        featureClass.setHasZ(geometryFactory.hasZ());
        final EnvelopeN envelope = new EnvelopeN(spatialReference);
        featureClass.setExtent(envelope);
        final Class<?> geometryClass = geometryDataType.getJavaClass();
        if (!Punctual.class.isAssignableFrom(geometryClass)) {
            final LengthFieldName lengthFieldNameProperty = LengthFieldName.getProperty(recordDefinition);
            String lengthFieldName = lengthFieldNameProperty.getFieldName();
            if (createLengthField) {
                if (!Property.hasValue(lengthFieldName)) {
                    lengthFieldName = geometryFieldName + "_Length";
                    lengthFieldNameProperty.setFieldName(lengthFieldName);
                }
                if (!recordDefinition.hasField(lengthFieldName)) {
                    fieldDefinitions.add(new FieldDefinition(lengthFieldName, DataTypes.DOUBLE, true));
                }
            }
            featureClass.setLengthFieldName(lengthFieldName);
            if (!Lineal.class.isAssignableFrom(geometryClass)) {
                final AreaFieldName areaFieldNameProperty = AreaFieldName.getProperty(recordDefinition);
                String areaFieldName = areaFieldNameProperty.getFieldName();
                if (createAreaField) {
                    if (!Property.hasValue(areaFieldName)) {
                        areaFieldName = geometryFieldName + "_Area";
                        areaFieldNameProperty.setFieldName(areaFieldName);
                    }
                    if (!recordDefinition.hasField(areaFieldName)) {
                        fieldDefinitions.add(new FieldDefinition(areaFieldName, DataTypes.DOUBLE, true));
                    }
                }
                featureClass.setAreaFieldName(areaFieldName);
            }
        }
    } else {
        table = new DETable();
        schemaPath = "\\";
    }
    String oidFieldName = recordDefinition.getProperty(EsriGeodatabaseXmlConstants.ESRI_OBJECT_ID_FIELD_NAME);
    if (!Property.hasValue(oidFieldName)) {
        oidFieldName = "OBJECTID";
    }
    final String catalogPath;
    if (schemaPath.equals("\\")) {
        catalogPath = "\\" + name;
    } else {
        catalogPath = schemaPath + "\\" + name;
    }
    table.setCatalogPath(catalogPath);
    table.setName(name);
    table.setHasOID(true);
    table.setOIDFieldName(oidFieldName);
    addObjectIdField(table);
    final FieldDefinition idField = recordDefinition.getIdField();
    for (final FieldDefinition fieldDefinition : fieldDefinitions) {
        if (fieldDefinition == geometryField) {
            addGeometryField(shapeType, table, fieldDefinition);
        } else {
            final String fieldName = fieldDefinition.getName();
            if (!fieldName.equals(oidFieldName)) {
                final Field field = addField(table, fieldDefinition);
                if (idField == fieldDefinition) {
                    table.addIndex(field, true, fieldName + "_PK");
                }
            }
        }
    }
    table.setAliasName(name);
    return table;
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) LengthFieldName(com.revolsys.record.property.LengthFieldName) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) GeometryType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.GeometryType) Punctual(com.revolsys.geometry.model.Punctual) Lineal(com.revolsys.geometry.model.Lineal) DataType(com.revolsys.datatype.DataType) AreaFieldName(com.revolsys.record.property.AreaFieldName)

Example 13 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class EsriXmlRecordDefinitionUtil method addField.

private static void addField(final RecordDefinitionImpl recordDefinition, final DETable deTable, final String tableName, final Field field, final String fieldName) {
    final FieldType fieldType = field.getType();
    final int precision = field.getPrecision();
    final DataType dataType;
    if (fieldType == FieldType.esriFieldTypeGeometry && deTable instanceof DEFeatureClass) {
        final DEFeatureClass featureClass = (DEFeatureClass) deTable;
        final GeometryType shapeType = featureClass.getShapeType();
        switch(shapeType) {
            case esriGeometryPoint:
                dataType = DataTypes.POINT;
                break;
            case esriGeometryMultipoint:
                dataType = DataTypes.MULTI_POINT;
                break;
            case esriGeometryPolyline:
                dataType = DataTypes.MULTI_LINE_STRING;
                break;
            case esriGeometryPolygon:
                dataType = DataTypes.POLYGON;
                break;
            default:
                throw new RuntimeException("Unknown geometry type" + shapeType + " for " + tableName + "." + fieldName);
        }
    } else if (precision > 0 && (fieldType.equals(FieldType.esriFieldTypeSingle) || fieldType.equals(FieldType.esriFieldTypeDouble))) {
        dataType = DataTypes.DECIMAL;
    } else {
        dataType = EsriGeodatabaseXmlFieldTypeRegistry.INSTANCE.getDataType(fieldType);
    }
    final int scale = field.getScale();
    int length = field.getLength();
    if (precision != 0) {
        length = precision;
    }
    final Boolean required = !field.isIsNullable() || Booleans.getBoolean(field.getRequired());
    final FieldDefinition attribute = new FieldDefinition(fieldName, dataType, length, scale, required);
    recordDefinition.addField(attribute);
    if (fieldName.equals(tableName + "_ID")) {
        recordDefinition.setIdFieldName(fieldName);
    }
}
Also used : GeometryType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.GeometryType) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) FieldType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType) EsriGeodatabaseXmlFieldType(com.revolsys.record.io.format.esri.gdb.xml.type.EsriGeodatabaseXmlFieldType)

Example 14 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class CodeTableProperty method loadId.

@Override
protected synchronized Identifier loadId(final List<Object> values, final boolean createId) {
    if (this.loadAll && !this.loadMissingCodes && !isEmpty()) {
        return null;
    }
    Identifier id = null;
    if (createId && this.loadAll && !isLoaded()) {
        loadAll();
        id = getIdentifier(values, false);
    } else {
        final Query query = new Query(this.typePath);
        final And and = new And();
        if (!values.isEmpty()) {
            int i = 0;
            for (final String fieldName : this.valueFieldNames) {
                final Object value = values.get(i);
                if (value == null) {
                    and.and(Q.isNull(fieldName));
                } else {
                    final FieldDefinition fieldDefinition = this.recordDefinition.getField(fieldName);
                    and.and(Q.equal(fieldDefinition, value));
                }
                i++;
            }
        }
        query.setWhereCondition(and);
        final Reader<Record> reader = this.recordStore.getRecords(query);
        try {
            final List<Record> codes = reader.toList();
            if (codes.size() > 0) {
                final CategoryLabelCountMap statistics = this.recordStore.getStatistics();
                if (statistics != null) {
                    statistics.getLabelCountMap("query").addCount(this.typePath, -codes.size());
                }
                addValues(codes);
            }
            id = getIdByValue(values);
            Property.firePropertyChange(this, "valuesChanged", false, true);
        } finally {
            reader.close();
        }
    }
    if (createId && id == null) {
        return newIdentifier(values);
    } else {
        return id;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) SingleIdentifier(com.revolsys.identifier.SingleIdentifier) ListIdentifier(com.revolsys.identifier.ListIdentifier) Query(com.revolsys.record.query.Query) CategoryLabelCountMap(com.revolsys.util.count.CategoryLabelCountMap) And(com.revolsys.record.query.And) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Record(com.revolsys.record.Record)

Example 15 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class RecordDefinitionConvertRecordReader method next.

@Override
public Record next() {
    if (hasNext()) {
        final Record source = this.iterator.next();
        final Record target = new ArrayRecord(this.recordDefinition);
        for (final FieldDefinition attribute : this.recordDefinition.getFields()) {
            final String name = attribute.getName();
            final Object value = source.getValue(name);
            if (value != null) {
                final DataType dataType = this.recordDefinition.getFieldType(name);
                final Object convertedValue = dataType.toObject(value);
                target.setValue(name, convertedValue);
            }
        }
        return target;
    } else {
        throw new NoSuchElementException();
    }
}
Also used : ArrayRecord(com.revolsys.record.ArrayRecord) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) Record(com.revolsys.record.Record) ArrayRecord(com.revolsys.record.ArrayRecord) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

FieldDefinition (com.revolsys.record.schema.FieldDefinition)133 RecordDefinition (com.revolsys.record.schema.RecordDefinition)38 DataType (com.revolsys.datatype.DataType)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)19 PathName (com.revolsys.io.PathName)15 Record (com.revolsys.record.Record)15 ArrayList (java.util.ArrayList)15 Geometry (com.revolsys.geometry.model.Geometry)13 CodeTable (com.revolsys.record.code.CodeTable)9 Query (com.revolsys.record.query.Query)8 LineString (com.revolsys.geometry.model.LineString)7 ArrayRecord (com.revolsys.record.ArrayRecord)7 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)7 SQLException (java.sql.SQLException)7 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 BadLocationException (javax.swing.text.BadLocationException)4