Search in sources :

Example 46 with FieldDefinition

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

the class KmlRecordWriter method write.

@Override
public void write(final Record record) {
    try {
        open();
        this.writer.write("<Placemark>\n");
        final RecordDefinition recordDefinition = record.getRecordDefinition();
        final int geometryIndex = recordDefinition.getGeometryFieldIndex();
        final int idIndex = recordDefinition.getIdFieldIndex();
        final String nameAttribute = getProperty(PLACEMARK_NAME_ATTRIBUTE_PROPERTY);
        String name = null;
        if (nameAttribute != null) {
            name = record.getValue(nameAttribute);
        }
        if (name == null && idIndex != -1) {
            final Object id = record.getValue(idIndex);
            final String typeName = recordDefinition.getName();
            name = typeName + " " + id;
        }
        if (name != null) {
            this.writer.write("<name>");
            XmlWriter.writeElementContent(this.writer, name);
            this.writer.write("</name>\n");
        }
        final String snippet = getProperty(SNIPPET_PROPERTY);
        if (snippet != null) {
            this.writer.write("<Snippet>");
            XmlWriter.writeElementContent(this.writer, snippet);
            this.writer.write("</Snippet>\n");
        }
        String description = getProperty(PLACEMARK_DESCRIPTION_PROPERTY);
        if (description == null) {
            description = getProperty(IoConstants.DESCRIPTION_PROPERTY);
        }
        if (Property.hasValue(description)) {
            this.writer.write("<description>");
            this.writer.write("<![CDATA[");
            this.writer.write(description);
            this.writer.write("]]>");
            this.writer.write("<description>");
        }
        Geometry geometry = null;
        GeometryFactory kmlGeometryFactory = GEOMETRY_FACTORY_2D;
        final List<Integer> geometryFieldIndexes = recordDefinition.getGeometryFieldIndexes();
        if (!geometryFieldIndexes.isEmpty()) {
            if (geometryFieldIndexes.size() == 1) {
                geometry = record.getValue(geometryFieldIndexes.get(0));
                final int axisCount = geometry.getAxisCount();
                if (axisCount > 2) {
                    kmlGeometryFactory = GEOMETRY_FACTORY_2D.convertAxisCount(axisCount);
                }
                geometry = geometry.convertGeometry(kmlGeometryFactory);
            } else {
                final List<Geometry> geometries = new ArrayList<>();
                for (final Integer geometryFieldIndex : geometryFieldIndexes) {
                    Geometry part = record.getValue(geometryFieldIndex);
                    if (part != null) {
                        final int axisCount = part.getAxisCount();
                        if (axisCount > 2) {
                            kmlGeometryFactory = GEOMETRY_FACTORY_2D.convertAxisCount(axisCount);
                        }
                        part = part.convertGeometry(kmlGeometryFactory);
                        if (!part.isEmpty()) {
                            geometries.add(part);
                        }
                    }
                }
                if (!geometries.isEmpty()) {
                    geometry = kmlGeometryFactory.geometry(geometries);
                }
            }
        }
        writeLookAt(record.getGeometry());
        if (Property.hasValue(this.styleUrl)) {
            this.writer.write("<styleUrl>");
            XmlWriter.writeElementContent(this.writer, this.styleUrl);
            this.writer.write("</styleUrl>\n");
        } else if (Property.hasValue(this.defaultStyleUrl)) {
            this.writer.write("<styleUrl>");
            XmlWriter.writeElementContent(this.writer, this.defaultStyleUrl);
            this.writer.write("</styleUrl>\n");
        }
        boolean hasValues = false;
        for (final FieldDefinition field : recordDefinition.getFields()) {
            final int fieldIndex = field.getIndex();
            if (fieldIndex != geometryIndex) {
                final String fieldName = field.getName();
                final Object value;
                if (isWriteCodeValues()) {
                    value = record.getCodeValue(fieldIndex);
                } else {
                    value = record.getValue(fieldIndex);
                }
                if (isValueWritable(value)) {
                    if (!hasValues) {
                        hasValues = true;
                        this.writer.write("<ExtendedData>\n");
                    }
                    this.writer.write("<Data name=\"");
                    XmlWriter.writeAttributeContent(this.writer, fieldName);
                    this.writer.write("\">\n");
                    this.writer.write("<value>");
                    if (Property.hasValue(value)) {
                        XmlWriter.writeElementContent(this.writer, value.toString());
                    }
                    this.writer.write("</value>\n");
                    this.writer.write("</Data>\n");
                }
            }
        }
        if (hasValues) {
            this.writer.write("</ExtendedData>\n");
        }
        if (geometry != null) {
            GeometryFactory geometryFactory = getProperty(IoConstants.GEOMETRY_FACTORY);
            if (geometryFactory == null) {
                geometryFactory = geometry.getGeometryFactory();
            }
            final int axisCount = geometryFactory.getAxisCount();
            KmlWriterUtil.writeGeometry(this.writer, geometry, axisCount);
        }
        this.writer.write("</Placemark>\n");
    } catch (final IOException e) {
        throw Exceptions.wrap(e);
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Point(com.revolsys.geometry.model.Point) RecordDefinition(com.revolsys.record.schema.RecordDefinition) Geometry(com.revolsys.geometry.model.Geometry)

Example 47 with FieldDefinition

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

the class XlsxRecordWriter method write.

@Override
public void write(final Record record) {
    final Row recordRow = smlObjectFactory.createRow();
    this.sheetRows.add(recordRow);
    final List<Cell> cells = recordRow.getC();
    for (final FieldDefinition field : this.recordDefinition.getFields()) {
        final Object value = record.getValue(field);
        final String string = field.toString(value);
        if (value instanceof Number) {
            addCellNumber(cells, string);
        } else {
            addCellInlineString(cells, string);
        }
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) Row(org.xlsx4j.sml.Row) Cell(org.xlsx4j.sml.Cell)

Example 48 with FieldDefinition

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

the class WktRecordReader method initDo.

@Override
protected void initDo() {
    GeometryFactory geometryFactory;
    final FieldDefinition geometryField = getRecordDefinition().getGeometryField();
    if (geometryField == null) {
        geometryFactory = GeometryFactory.DEFAULT_3D;
    } else {
        geometryFactory = geometryField.getProperty(FieldProperties.GEOMETRY_FACTORY);
        if (geometryFactory == null) {
            geometryFactory = getGeometryFactory();
            if (geometryFactory == null) {
                geometryFactory = GeometryFactory.DEFAULT_3D;
            }
            geometryField.setProperty(FieldProperties.GEOMETRY_FACTORY, geometryFactory);
        }
    }
    this.wktParser = new WktParser(geometryFactory);
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Example 49 with FieldDefinition

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

the class XmlRecordWriter method write.

@Override
public void write(final Record record) {
    if (!this.opened) {
        writeHeader();
    }
    QName qualifiedName = this.recordDefinition.getProperty(RecordProperties.QUALIFIED_NAME);
    if (qualifiedName == null) {
        qualifiedName = new QName(this.recordDefinition.getName());
    }
    this.out.startTag(qualifiedName);
    for (final FieldDefinition field : this.recordDefinition.getFields()) {
        final int fieldIndex = field.getIndex();
        final Object value;
        if (isWriteCodeValues()) {
            value = record.getCodeValue(fieldIndex);
        } else {
            value = record.getValue(fieldIndex);
        }
        if (isValueWritable(value)) {
            final String name = field.getName();
            final QName tagName = new QName(name);
            if (value instanceof Map) {
                @SuppressWarnings("unchecked") final Map<String, ?> map = (Map<String, ?>) value;
                this.out.startTag(tagName);
                map(map);
                this.out.endTag();
            } else if (value instanceof List) {
                final List<?> list = (List<?>) value;
                this.out.startTag(tagName);
                list(list);
                this.out.endTag();
            } else {
                final DataType dataType = field.getDataType();
                final String string = dataType.toString(value);
                this.out.nillableElement(tagName, string);
            }
        }
    }
    this.out.endTag();
}
Also used : QName(javax.xml.namespace.QName) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) List(java.util.List) Map(java.util.Map)

Example 50 with FieldDefinition

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

the class OgrVrtWriter method write.

public static void write(final File file, final RecordDefinition recordDefinition, final String dataSource) throws IOException {
    try (XmlWriter writer = new XmlWriter(new FileWriter(file))) {
        writer.setIndent(true);
        writer.startDocument("UTF-8", "1.0");
        writer.startTag("OGRVRTDataSource");
        writer.startTag("OGRVRTLayer");
        final String typeName = recordDefinition.getName();
        writer.attribute("name", typeName);
        writer.startTag("SrcDataSource");
        writer.attribute("relativeToVRT", "1");
        writer.text(dataSource);
        writer.endTag("SrcDataSource");
        writer.element(new QName("SrcLayer"), typeName);
        for (final FieldDefinition attribute : recordDefinition.getFields()) {
            final String fieldName = attribute.getName();
            final DataType fieldType = attribute.getDataType();
            final Class<?> typeClass = attribute.getTypeClass();
            if (Geometry.class.isAssignableFrom(typeClass)) {
                final GeometryFactory geometryFactory = recordDefinition.getGeometryFactory();
                writer.element("GeometryType", "wkb" + fieldType);
                if (geometryFactory != null) {
                    writer.element("LayerSRS", "EPSG:" + geometryFactory.getCoordinateSystemId());
                }
                writer.startTag("GeometryField");
                writer.attribute("encoding", "WKT");
                writer.attribute("field", fieldName);
                writer.attribute("name", fieldName);
                writer.attribute("reportSrcColumn", "FALSE");
                writer.element("GeometryType", "wkb" + fieldType);
                if (geometryFactory != null) {
                    writer.element("SRS", "EPSG:" + geometryFactory.getCoordinateSystemId());
                }
                writer.endTag("GeometryField");
            } else {
                writer.startTag("Field");
                writer.attribute("name", fieldName);
                String type = "String";
                if (Arrays.asList(DataTypes.BYTE, DataTypes.SHORT, DataTypes.INT, DataTypes.LONG, DataTypes.BIG_INTEGER).contains(fieldType)) {
                    type = "Integer";
                } else if (Arrays.asList(DataTypes.FLOAT, DataTypes.DOUBLE, DataTypes.DECIMAL).contains(fieldType)) {
                    type = "Real";
                } else if (DataTypes.DATE.equals(type)) {
                    type = "Date";
                } else if (DataTypes.DATE_TIME.equals(type)) {
                    type = "DateTime";
                } else {
                    type = "String";
                }
                writer.attribute("type", type);
                final int length = attribute.getLength();
                if (length > 0) {
                    writer.attribute("width", length);
                }
                final int scale = attribute.getScale();
                if (scale > 0) {
                    writer.attribute("scale", scale);
                }
                writer.attribute("src", fieldName);
                writer.endTag("Field");
            }
        }
        writer.endTag("OGRVRTLayer");
        writer.endTag("OGRVRTDataSource");
        writer.endDocument();
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) QName(javax.xml.namespace.QName) FileWriter(java.io.FileWriter) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) XmlWriter(com.revolsys.record.io.format.xml.XmlWriter)

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