Search in sources :

Example 21 with Geometry

use of com.revolsys.geometry.model.Geometry in project com.revolsys.open by revolsys.

the class CsvRecordWriter method write.

@Override
public void write(final Record record) {
    Writer out = this.out;
    if (this.paused) {
        this.paused = false;
        final Resource resource = getResource();
        out = this.out = resource.newWriterAppend();
    }
    if (out != null) {
        try {
            final RecordDefinition recordDefinition = this.recordDefinition;
            final char fieldSeparator = this.fieldSeparator;
            boolean first = true;
            for (final FieldDefinition field : recordDefinition.getFields()) {
                if (first) {
                    first = false;
                } else {
                    out.write(fieldSeparator);
                }
                final String fieldName = field.getName();
                final Object value;
                if (isWriteCodeValues()) {
                    value = record.getCodeValue(fieldName);
                } else {
                    value = record.getValue(fieldName);
                }
                if (value instanceof Geometry) {
                    final Geometry geometry = (Geometry) value;
                    if (this.useQuotes) {
                        out.write('"');
                        EWktWriter.write(out, geometry, this.ewkt);
                        out.write('"');
                    } else {
                        EWktWriter.write(out, geometry, this.ewkt);
                    }
                } else if (value != null) {
                    final DataType dataType = field.getDataType();
                    final String stringValue = dataType.toString(value);
                    if (dataType.isRequiresQuotes()) {
                        string(stringValue);
                    } else {
                        out.write(stringValue, 0, stringValue.length());
                    }
                }
            }
            out.write('\n');
        } catch (final IOException e) {
            throw Exceptions.wrap(e);
        }
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Resource(com.revolsys.spring.resource.Resource) DataType(com.revolsys.datatype.DataType) IOException(java.io.IOException) AbstractRecordWriter(com.revolsys.io.AbstractRecordWriter) EWktWriter(com.revolsys.record.io.format.wkt.EWktWriter) Writer(java.io.Writer) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 22 with Geometry

use of com.revolsys.geometry.model.Geometry in project com.revolsys.open by revolsys.

the class DirectoryRecordWriter method getWriter.

private Writer<Record> getWriter(final Record record) {
    final RecordDefinition recordDefinition = record.getRecordDefinition();
    final String path = recordDefinition.getPath();
    Writer<Record> writer = this.writers.get(path);
    if (writer == null) {
        final File directory = getDirectory(recordDefinition);
        directory.mkdirs();
        final String fileName = getFileName(recordDefinition);
        final File file = new File(directory, fileName + this.nameSuffix + "." + this.fileExtension);
        final PathResource resource = new PathResource(file);
        writer = RecordWriter.newRecordWriter(recordDefinition, resource);
        if (writer == null) {
            throw new IllegalArgumentException("Unable to create writer for " + resource);
        } else {
            final Map<String, Object> properties = getProperties();
            writer.setProperties(properties);
            final Geometry geometry = record.getGeometry();
            if (geometry != null) {
                final GeometryFactory geometryFactory = geometry.getGeometryFactory();
                setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
            }
            this.writers.put(path, writer);
            RecordDefinition writerRecordDefinition = recordDefinition;
            if (writer instanceof AbstractRecordWriter) {
                final AbstractRecordWriter recordWriter = (AbstractRecordWriter) writer;
                writerRecordDefinition = recordWriter.getRecordDefinition();
                if (writerRecordDefinition == null) {
                    writerRecordDefinition = recordDefinition;
                }
            }
            this.recordDefinitionMap.put(path, writerRecordDefinition);
        }
    }
    return writer;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) AbstractRecordWriter(com.revolsys.io.AbstractRecordWriter) PathResource(com.revolsys.spring.resource.PathResource) Record(com.revolsys.record.Record) File(java.io.File) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 23 with Geometry

use of com.revolsys.geometry.model.Geometry in project com.revolsys.open by revolsys.

the class ShapefileGeometryUtil method writePolygon.

private List<LineString> writePolygon(final EndianOutput out, final Geometry geometry, final int shapeType, final int headerOverhead, final int wordsPerPoint) throws IOException {
    int vertexCount = 0;
    final List<LineString> rings = new ArrayList<>();
    for (int i = 0; i < geometry.getGeometryCount(); i++) {
        final Geometry part = geometry.getGeometry(i);
        if (part instanceof Polygon) {
            final Polygon polygon = (Polygon) part;
            LineString shell = polygon.getShell();
            shell = shell.toClockwise();
            rings.add(shell);
            vertexCount += shell.getVertexCount();
            final int numHoles = polygon.getHoleCount();
            for (int j = 0; j < numHoles; j++) {
                LineString hole = polygon.getHole(j);
                hole = hole.toCounterClockwise();
                rings.add(hole);
                vertexCount += hole.getVertexCount();
            }
        } else {
            throw new IllegalArgumentException("Expecting " + Polygon.class + " geometry got " + part.getClass());
        }
    }
    final int numParts = rings.size();
    if (this.writeLength) {
        final int recordLength = 22 + headerOverhead + 2 * numParts + wordsPerPoint * vertexCount;
        out.writeInt(recordLength);
    }
    out.writeLEInt(shapeType);
    final BoundingBox envelope = geometry.getBoundingBox();
    writeEnvelope(out, envelope);
    out.writeLEInt(numParts);
    out.writeLEInt(vertexCount);
    int partIndex = 0;
    for (final LineString ring : rings) {
        out.writeLEInt(partIndex);
        partIndex += ring.getVertexCount();
    }
    for (final LineString ring : rings) {
        writeXYCoordinates(out, ring);
    }
    return rings;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) BoundingBox(com.revolsys.geometry.model.BoundingBox) ArrayList(java.util.ArrayList) Polygon(com.revolsys.geometry.model.Polygon) Point(com.revolsys.geometry.model.Point)

Example 24 with Geometry

use of com.revolsys.geometry.model.Geometry in project com.revolsys.open by revolsys.

the class ShapefileDirectoryWriter method getWriter.

private Writer<Record> getWriter(final Record object) {
    final RecordDefinition recordDefinition = object.getRecordDefinition();
    final String path = recordDefinition.getPath();
    Writer<Record> writer = this.writers.get(path);
    if (writer == null) {
        final File directory = getDirectory(recordDefinition);
        directory.mkdirs();
        final File file = new File(directory, getFileName(recordDefinition) + this.nameSuffix + ".shp");
        writer = RecordWriter.newRecordWriter(recordDefinition, new PathResource(file));
        ((XbaseRecordWriter) writer).setUseZeroForNull(this.useZeroForNull);
        final Geometry geometry = object.getGeometry();
        if (geometry != null) {
            setProperty(IoConstants.GEOMETRY_FACTORY, geometry.getGeometryFactory());
        }
        this.writers.put(path, writer);
        this.recordDefinitionMap.put(path, ((ShapefileRecordWriter) writer).getRecordDefinition());
    }
    return writer;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) XbaseRecordWriter(com.revolsys.record.io.format.xbase.XbaseRecordWriter) PathResource(com.revolsys.spring.resource.PathResource) Record(com.revolsys.record.Record) File(java.io.File) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 25 with Geometry

use of com.revolsys.geometry.model.Geometry in project com.revolsys.open by revolsys.

the class TextOnCurveConverter method read.

@Override
public Object read(final OsnIterator iterator) {
    final List<Point> points = new ArrayList<>();
    String fieldName = iterator.nextFieldName();
    while (fieldName != null) {
        if (fieldName.equals("characters")) {
            while (iterator.next() != OsnIterator.END_LIST) {
                final String objectName = iterator.nextObjectName();
                final OsnConverter osnConverter = this.converters.getConverter(objectName);
                if (osnConverter == null) {
                    iterator.throwParseError("No Geometry Converter for " + objectName);
                }
                points.add((TextLinePoint) osnConverter.read(iterator));
            }
        }
        fieldName = iterator.nextFieldName();
    }
    final Geometry geometry = new TextOnCurve(this.geometryFactory, points);
    return geometry;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) ArrayList(java.util.ArrayList) TextOnCurve(com.revolsys.record.io.format.saif.geometry.TextOnCurve) TextLinePoint(com.revolsys.record.io.format.saif.geometry.TextLinePoint) Point(com.revolsys.geometry.model.Point)

Aggregations

Geometry (com.revolsys.geometry.model.Geometry)488 Point (com.revolsys.geometry.model.Point)140 LineString (com.revolsys.geometry.model.LineString)87 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)75 ArrayList (java.util.ArrayList)70 BoundingBox (com.revolsys.geometry.model.BoundingBox)39 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)39 Polygon (com.revolsys.geometry.model.Polygon)34 List (java.util.List)33 Record (com.revolsys.record.Record)32 Iterator (java.util.Iterator)20 RecordDefinition (com.revolsys.record.schema.RecordDefinition)18 LinearRing (com.revolsys.geometry.model.LinearRing)16 Vertex (com.revolsys.geometry.model.vertex.Vertex)16 LayerRecord (com.revolsys.swing.map.layer.record.LayerRecord)14 LineSegment (com.revolsys.geometry.model.segment.LineSegment)13 GeometricShapeFactory (com.revolsys.geometry.util.GeometricShapeFactory)13 FieldDefinition (com.revolsys.record.schema.FieldDefinition)12 DataType (com.revolsys.datatype.DataType)10 IOException (java.io.IOException)10