Search in sources :

Example 26 with LineStringEditor

use of com.revolsys.geometry.model.editor.LineStringEditor in project com.revolsys.open by revolsys.

the class WktParser method parsePointParts.

private List<Point> parsePointParts(final GeometryFactory geometryFactory, final PushbackReader reader, final int axisCount) throws IOException {
    skipWhitespace(reader);
    final List<Point> parts = new ArrayList<>();
    int character = reader.read();
    switch(character) {
        case '(':
            do {
                final LineStringEditor lineBuilder = parseCoordinatesLineString(geometryFactory, reader, axisCount);
                parts.add(geometryFactory.point(lineBuilder));
                skipWhitespace(reader);
                character = reader.read();
            } while (character == ',');
            if (character != ')') {
                throw new IllegalArgumentException("Expecting ) not" + FileUtil.getString(reader, 50));
            }
            break;
        case ')':
            character = reader.read();
            if (character == ')' || character == ',') {
            } else {
                throw new IllegalArgumentException("Expecting ' or ) not" + FileUtil.getString(reader, 50));
            }
            break;
        default:
            throw new IllegalArgumentException("Expecting ( not" + FileUtil.getString(reader, 50));
    }
    return parts;
}
Also used : ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) LineStringEditor(com.revolsys.geometry.model.editor.LineStringEditor) Point(com.revolsys.geometry.model.Point)

Example 27 with LineStringEditor

use of com.revolsys.geometry.model.editor.LineStringEditor in project com.revolsys.open by revolsys.

the class WktParser method parseCoordinatesLineString.

private LineStringEditor parseCoordinatesLineString(final GeometryFactory geometryFactory, final PushbackReader reader, final int axisCount) throws IOException {
    final LineStringEditor line = geometryFactory.newLineStringBuilder();
    skipWhitespace(reader);
    int character = reader.read();
    if (character == '(') {
        int axisIndex = 0;
        int vertexIndex = 0;
        while (true) {
            final double number;
            try {
                number = parseDouble(reader);
            } catch (final IllegalArgumentException e) {
                character = reader.read();
                if (character == ')') {
                    return line;
                } else {
                    reader.unread(character);
                    throw new IllegalArgumentException("Invalid WKT geometry. Expecting end of coordinates ')' not " + FileUtil.getString(reader, 50));
                }
            }
            character = reader.read();
            if (character == ',' || character == ')') {
                if (character == ',') {
                    skipWhitespace(reader);
                }
                if (axisIndex < axisCount) {
                    line.setCoordinate(vertexIndex, axisIndex, number);
                    axisIndex = 0;
                    vertexIndex++;
                }
                if (character == ')') {
                    return line;
                }
            } else if (character == ' ' || Character.isWhitespace(character)) {
                skipWhitespace(reader);
                if (axisIndex == 0) {
                    line.appendVertex(number, Double.NaN);
                    axisIndex++;
                } else if (axisIndex < axisCount) {
                    line.setCoordinate(vertexIndex, axisIndex, number);
                    axisIndex++;
                }
            } else {
                throw new IllegalArgumentException("Invalid WKT geometry. Expecting a space between coordinates not: " + FileUtil.getString(reader, 50));
            }
        }
    } else {
        reader.unread(character);
        throw new IllegalArgumentException("Invalid WKT geometry. Expecting start of coordinates '(' not: " + FileUtil.getString(reader, 50));
    }
}
Also used : LineStringEditor(com.revolsys.geometry.model.editor.LineStringEditor) Point(com.revolsys.geometry.model.Point)

Example 28 with LineStringEditor

use of com.revolsys.geometry.model.editor.LineStringEditor in project com.revolsys.open by revolsys.

the class ArcConverter method read.

@Override
public Object read(final OsnIterator iterator) {
    final Map<String, Object> values = new TreeMap<>();
    values.put(SaifConstants.TYPE, this.geometryType);
    String field = iterator.nextFieldName();
    LineString geometry = null;
    while (field != null) {
        if (field.equals("LineString")) {
            int axisCount = 2;
            final LineStringEditor line = new LineStringEditor(this.geometryFactory);
            while (iterator.next() != OsnIterator.END_LIST) {
                final String pointName = iterator.nextObjectName();
                if (!pointName.equals("/Point")) {
                    iterator.throwParseError("Expecting Point object");
                }
                final String coordsName = iterator.nextFieldName();
                if (!coordsName.equals("coords")) {
                    iterator.throwParseError("Expecting coords attribute");
                }
                final String coordTypeName = iterator.nextObjectName();
                if (coordTypeName.equals("/Coord3D")) {
                    final double x = iterator.nextDoubleAttribute("c1");
                    final double y = iterator.nextDoubleAttribute("c2");
                    final double z = iterator.nextDoubleAttribute("c3");
                    axisCount = 3;
                    line.appendVertex(x, y, z);
                } else if (coordTypeName.equals("/Coord2D")) {
                    final double x = iterator.nextDoubleAttribute("c1");
                    final double y = iterator.nextDoubleAttribute("c2");
                    line.appendVertex(x, y);
                } else {
                    iterator.throwParseError("Expecting Coord2D or Coord3D");
                }
                iterator.nextEndObject();
                iterator.nextEndObject();
            }
            final int axisCount1 = axisCount;
            final GeometryFactory geometryFactory1 = this.geometryFactory.convertAxisCount(axisCount1);
            geometry = newLineString(geometryFactory1, line);
        } else {
            readAttribute(iterator, field, values);
        }
        field = iterator.nextFieldName();
    }
    Property.set(geometry, values);
    return geometry;
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) ArcLineString(com.revolsys.record.io.format.saif.geometry.ArcLineString) LineString(com.revolsys.geometry.model.LineString) ArcLineString(com.revolsys.record.io.format.saif.geometry.ArcLineString) LineString(com.revolsys.geometry.model.LineString) TreeMap(java.util.TreeMap) LineStringEditor(com.revolsys.geometry.model.editor.LineStringEditor)

Example 29 with LineStringEditor

use of com.revolsys.geometry.model.editor.LineStringEditor in project com.revolsys.open by revolsys.

the class WktParser method parseLineString.

private Geometry parseLineString(GeometryFactory geometryFactory, final boolean useAxisCountFromGeometryFactory, final PushbackReader reader) throws IOException {
    final int axisCount = getAxisCount(reader);
    if (!useAxisCountFromGeometryFactory) {
        if (axisCount != geometryFactory.getAxisCount()) {
            final int srid = geometryFactory.getCoordinateSystemId();
            final double[] scales = geometryFactory.newScales(axisCount);
            geometryFactory = GeometryFactory.fixed(srid, axisCount, scales);
        }
    }
    if (isEmpty(reader)) {
        return geometryFactory.lineString();
    } else {
        final LineStringEditor points = parseCoordinatesLineString(geometryFactory, reader, axisCount);
        if (points.getVertexCount() == 1) {
            return geometryFactory.point(points);
        } else {
            return points.newLineString();
        }
    }
}
Also used : LineStringEditor(com.revolsys.geometry.model.editor.LineStringEditor) Point(com.revolsys.geometry.model.Point)

Example 30 with LineStringEditor

use of com.revolsys.geometry.model.editor.LineStringEditor in project com.revolsys.open by revolsys.

the class SnapTransformer method snapVertices.

/**
 * Snap source vertices to vertices in the target.
 *
 * @param newCoordinates the points to snap
 * @param snapPoints the points to snap to
 */
private LineString snapVertices(final LineString line) {
    LineStringEditor newLine = null;
    final int vertexCount = line.getVertexCount();
    final boolean closed = line.isClosed();
    // if src is a ring then don't snap final vertex
    final int end = closed ? vertexCount - 1 : vertexCount;
    for (int i = 0; i < end; i++) {
        final double x = line.getX(i);
        final double y = line.getY(i);
        final Point snapVert = findSnapForVertex(x, y);
        if (snapVert != null) {
            if (newLine == null) {
                newLine = LineStringEditor.newLineStringEditor(line);
                if (i == 0 && closed) {
                    // keep final closing point in synch (rings only)
                    newLine.setVertex(vertexCount - 1, snapVert);
                }
            }
            newLine.setVertex(i, snapVert);
        }
    }
    if (newLine == null) {
        return line;
    } else {
        return newLine;
    }
}
Also used : Point(com.revolsys.geometry.model.Point) LineStringEditor(com.revolsys.geometry.model.editor.LineStringEditor) Point(com.revolsys.geometry.model.Point)

Aggregations

LineStringEditor (com.revolsys.geometry.model.editor.LineStringEditor)35 LineString (com.revolsys.geometry.model.LineString)13 Test (org.junit.Test)13 Point (com.revolsys.geometry.model.Point)11 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)5 ArrayList (java.util.ArrayList)3 TopologyException (com.revolsys.geometry.model.TopologyException)1 AbstractPoint (com.revolsys.geometry.model.impl.AbstractPoint)1 PreparedLineString (com.revolsys.geometry.model.prep.PreparedLineString)1 ArcLineString (com.revolsys.record.io.format.saif.geometry.ArcLineString)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1