Search in sources :

Example 1 with LengthFieldName

use of com.revolsys.record.property.LengthFieldName 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 2 with LengthFieldName

use of com.revolsys.record.property.LengthFieldName in project com.revolsys.open by revolsys.

the class FileGdbRecordStore method getRecordDefinition.

public RecordDefinitionImpl getRecordDefinition(final PathName schemaName, final String path, final String tableDefinition) {
    synchronized (this.apiSync) {
        synchronized (API_SYNC) {
            try {
                final XmlProcessor parser = new EsriGdbXmlParser();
                final DETable deTable = parser.process(tableDefinition);
                final String tableName = deTable.getName();
                final PathName typePath = PathName.newPathName(schemaName.newChild(tableName));
                final RecordStoreSchema schema = getSchema(schemaName);
                final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(schema, typePath);
                recordDefinition.setPolygonRingDirection(ClockDirection.CLOCKWISE);
                String lengthFieldName = null;
                String areaFieldName = null;
                if (deTable instanceof DEFeatureClass) {
                    final DEFeatureClass featureClass = (DEFeatureClass) deTable;
                    lengthFieldName = featureClass.getLengthFieldName();
                    final LengthFieldName lengthFieldNameProperty = new LengthFieldName(lengthFieldName);
                    lengthFieldNameProperty.setRecordDefinition(recordDefinition);
                    areaFieldName = featureClass.getAreaFieldName();
                    final LengthFieldName areaFieldNameProperty = new LengthFieldName(areaFieldName);
                    areaFieldNameProperty.setRecordDefinition(recordDefinition);
                }
                for (final Field field : deTable.getFields()) {
                    final String fieldName = field.getName();
                    AbstractFileGdbFieldDefinition fieldDefinition = null;
                    if (fieldName.equals(lengthFieldName)) {
                        fieldDefinition = new LengthFieldDefinition(field);
                    } else if (fieldName.equals(areaFieldName)) {
                        fieldDefinition = new AreaFieldDefinition(field);
                    } else {
                        final FieldType type = field.getType();
                        final Constructor<? extends AbstractFileGdbFieldDefinition> fieldConstructor = ESRI_FIELD_TYPE_FIELD_DEFINITION_MAP.get(type);
                        if (fieldConstructor != null) {
                            try {
                                fieldDefinition = JavaBeanUtil.invokeConstructor(fieldConstructor, field);
                            } catch (final Throwable e) {
                                Logs.error(this, tableDefinition);
                                throw new RuntimeException("Error creating field for " + typePath + "." + field.getName() + " : " + field.getType(), e);
                            }
                        } else {
                            Logs.error(this, "Unsupported field type " + fieldName + ":" + type);
                        }
                    }
                    if (fieldDefinition != null) {
                        final Domain domain = field.getDomain();
                        if (domain != null) {
                            CodeTable codeTable = getCodeTable(domain.getDomainName() + "_ID");
                            if (codeTable == null) {
                                codeTable = new FileGdbDomainCodeTable(this, domain);
                                addCodeTable(codeTable);
                            }
                            fieldDefinition.setCodeTable(codeTable);
                        }
                        fieldDefinition.setRecordStore(this);
                        recordDefinition.addField(fieldDefinition);
                        if (fieldDefinition instanceof GlobalIdFieldDefinition) {
                            recordDefinition.setIdFieldName(fieldName);
                        }
                    }
                }
                final String oidFieldName = deTable.getOIDFieldName();
                recordDefinition.setProperty(EsriGeodatabaseXmlConstants.ESRI_OBJECT_ID_FIELD_NAME, oidFieldName);
                if (deTable instanceof DEFeatureClass) {
                    final DEFeatureClass featureClass = (DEFeatureClass) deTable;
                    final String shapeFieldName = featureClass.getShapeFieldName();
                    recordDefinition.setGeometryFieldName(shapeFieldName);
                }
                for (final Index index : deTable.getIndexes()) {
                    if (index.getName().endsWith("_PK")) {
                        for (final Field field : index.getFields()) {
                            final String fieldName = field.getName();
                            recordDefinition.setIdFieldName(fieldName);
                        }
                    }
                }
                addRecordDefinitionProperties(recordDefinition);
                if (recordDefinition.getIdFieldIndex() == -1) {
                    recordDefinition.setIdFieldName(deTable.getOIDFieldName());
                }
                this.catalogPathByPath.put(typePath, deTable.getCatalogPath());
                return recordDefinition;
            } catch (final RuntimeException e) {
                Logs.debug(this, tableDefinition);
                throw e;
            }
        }
    }
}
Also used : CodeTable(com.revolsys.record.code.CodeTable) FileGdbDomainCodeTable(com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable) RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) EsriGdbXmlParser(com.revolsys.record.io.format.esri.gdb.xml.model.EsriGdbXmlParser) XmlProcessor(com.revolsys.record.io.format.xml.XmlProcessor) LengthFieldName(com.revolsys.record.property.LengthFieldName) Constructor(java.lang.reflect.Constructor) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) DEFeatureClass(com.revolsys.record.io.format.esri.gdb.xml.model.DEFeatureClass) FileGdbDomainCodeTable(com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable) Index(com.revolsys.record.io.format.esri.gdb.xml.model.Index) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) GlobalIdFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.GlobalIdFieldDefinition) AreaFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AreaFieldDefinition) FieldType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType) Field(com.revolsys.record.io.format.esri.gdb.xml.model.Field) AbstractFileGdbFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition) LengthFieldDefinition(com.revolsys.gis.esri.gdb.file.capi.type.LengthFieldDefinition) PathName(com.revolsys.io.PathName) Domain(com.revolsys.record.io.format.esri.gdb.xml.model.Domain) DETable(com.revolsys.record.io.format.esri.gdb.xml.model.DETable)

Aggregations

LengthFieldName (com.revolsys.record.property.LengthFieldName)2 DataType (com.revolsys.datatype.DataType)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 Lineal (com.revolsys.geometry.model.Lineal)1 Punctual (com.revolsys.geometry.model.Punctual)1 FileGdbDomainCodeTable (com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable)1 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)1 AbstractFileGdbFieldDefinition (com.revolsys.gis.esri.gdb.file.capi.type.AbstractFileGdbFieldDefinition)1 AreaFieldDefinition (com.revolsys.gis.esri.gdb.file.capi.type.AreaFieldDefinition)1 GlobalIdFieldDefinition (com.revolsys.gis.esri.gdb.file.capi.type.GlobalIdFieldDefinition)1 LengthFieldDefinition (com.revolsys.gis.esri.gdb.file.capi.type.LengthFieldDefinition)1 PathName (com.revolsys.io.PathName)1 CodeTable (com.revolsys.record.code.CodeTable)1 DEFeatureClass (com.revolsys.record.io.format.esri.gdb.xml.model.DEFeatureClass)1 DETable (com.revolsys.record.io.format.esri.gdb.xml.model.DETable)1 Domain (com.revolsys.record.io.format.esri.gdb.xml.model.Domain)1 EsriGdbXmlParser (com.revolsys.record.io.format.esri.gdb.xml.model.EsriGdbXmlParser)1 Field (com.revolsys.record.io.format.esri.gdb.xml.model.Field)1 Index (com.revolsys.record.io.format.esri.gdb.xml.model.Index)1 FieldType (com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType)1