org.locationtech.jts.geom.CoordinateSequence Java Examples

The following examples show how to use org.locationtech.jts.geom.CoordinateSequence. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeometryCoordinateTransform.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Transforms the given sequence of coordinate tuples, producing a new sequence of tuples.
 * This method tries to transform coordinates in batches, in order to reduce the amount of
 * calls to {@link MathTransform#transform(double[], int, double[], int, int)}.
 *
 * @param  sequence   sequence of coordinate tuples to transform.
 * @param  minPoints  minimum number of points to preserve.
 * @return the transformed sequence of coordinate tuples.
 * @throws TransformException if an error occurred while transforming a tuple.
 */
@Override
protected CoordinateSequence transform(final CoordinateSequence sequence, final int minPoints) throws TransformException {
    final int srcDim   = transform.getSourceDimensions();
    final int tgtDim   = transform.getTargetDimensions();
    final int maxDim   = Math.max(srcDim, tgtDim);
    final int count    = sequence.size();
    final int capacity = Math.max(4, Math.min(100, count));
    final CoordinateSequence out = coordinateFactory.create(count, sequence.getDimension());
    if (coordinates == null || coordinates.length / maxDim < capacity) {
        coordinates = new double[capacity * maxDim];
    }
    for (int base=0, n; (n = Math.min(count - base, capacity)) > 0; base += n) {
        int batch = n * srcDim;
        for (int i=0; i<batch; i++) {
            coordinates[i] = sequence.getOrdinate(base + i/srcDim, i % srcDim);
        }
        transform.transform(coordinates, 0, coordinates, 0, n);
        batch = n * tgtDim;
        for (int i=0; i<batch; i++) {
            out.setOrdinate(base + i/tgtDim, i % tgtDim, coordinates[i]);
        }
    }
    return out;
}
 
Example #2
Source File: FilterToElastic.java    From elasticgeo with GNU General Public License v3.0 6 votes vote down vote up
private void visitLiteralGeometry(Literal expression) throws IOException {
    // evaluate the literal and store it for later
    currentGeometry  = (Geometry) evaluateLiteral(expression, Geometry.class);

    if ( currentGeometry instanceof LinearRing ) {
        // convert LinearRing to LineString
        final GeometryFactory factory = currentGeometry.getFactory();
        final LinearRing linearRing = (LinearRing) currentGeometry;
        final CoordinateSequence coordinates;
        coordinates = linearRing.getCoordinateSequence();
        currentGeometry = factory.createLineString(coordinates);
    }

    final String geoJson = new GeometryJSON().toString(currentGeometry);
    currentShapeBuilder = mapReader.readValue(geoJson);
}
 
Example #3
Source File: SpatialiteWKBWriter.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
private void writeCoordinate( CoordinateSequence seq, int index, OutStream os ) throws IOException {
    ByteOrderValues.putDouble(seq.getX(index), buf, byteOrder);
    os.write(buf, 8);
    ByteOrderValues.putDouble(seq.getY(index), buf, byteOrder);
    os.write(buf, 8);

    // only write 3rd dim if caller has requested it for this writer
    if (outputDimension >= 3) {
        // if 3rd dim is requested, only write it if the CoordinateSequence provides it
        double ordVal = Coordinate.NULL_ORDINATE;
        if (seq.getDimension() >= 3)
            ordVal = seq.getOrdinate(index, 2);
        ByteOrderValues.putDouble(ordVal, buf, byteOrder);
        os.write(buf, 8);
    }
}
 
Example #4
Source File: GeophileSpatial.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(CoordinateSequence seq, int i) {
    double x = seq.getCoordinate(i).x;
    double y = seq.getCoordinate(i).y;
    seq.setOrdinate(i, 0, y);
    seq.setOrdinate(i, 1, x);
}
 
Example #5
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private CoordinateSequence readCoordinateSequenceRing( int size ) throws IOException {
    CoordinateSequence seq = readCoordinateSequence(size);
    if (isStrict)
        return seq;
    if (CoordinateSequences.isRing(seq))
        return seq;
    return CoordinateSequences.ensureValidRing(csFactory, seq);
}
 
Example #6
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private CoordinateSequence readCoordinateSequenceLineString( int size ) throws IOException {
    CoordinateSequence seq = readCoordinateSequence(size);
    if (isStrict)
        return seq;
    if (seq.size() == 0 || seq.size() >= 2)
        return seq;
    return CoordinateSequences.extend(csFactory, seq, 2);
}
 
Example #7
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private CoordinateSequence readCoordinateSequence( int size ) throws IOException {
    CoordinateSequence seq = csFactory.create(size, inputDimension);
    int targetDim = seq.getDimension();
    if (targetDim > inputDimension)
        targetDim = inputDimension;
    for( int i = 0; i < size; i++ ) {
        readCoordinate();
        for( int j = 0; j < targetDim; j++ ) {
            seq.setOrdinate(i, j, ordValues[j]);
        }
    }
    return seq;
}
 
Example #8
Source File: GeoJSONEncoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected ArrayNode encodeCoordinates(CoordinateSequence coordinates) {
    ArrayNode list = jsonFactory.arrayNode();
    for (int i = 0; i < coordinates.size(); ++i) {
        list.add(encodeCoordinate(coordinates.getCoordinate(i)));
    }
    return list;
}
 
Example #9
Source File: GeoTiffReaderExample.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public LinearRing createLinearRing(CoordinateSequence cs) {
	if (cs.getCoordinate(0).equals(cs.getCoordinate(cs.size() - 1)))
		return super.createLinearRing(cs);

	// add a new coordinate to close the ring
	CoordinateSequenceFactory csFact = getCoordinateSequenceFactory();
	CoordinateSequence csNew = csFact.create(cs.size() + 1,
			cs.getDimension());
	CoordinateSequences.copy(cs, 0, csNew, 0, cs.size());
	CoordinateSequences.copyCoord(csNew, 0, csNew, csNew.size() - 1);
	return super.createLinearRing(csNew);
}
 
Example #10
Source File: KMLManagement.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public LinearRing createLinearRing(CoordinateSequence cs) {
	if (cs.getCoordinate(0).equals(cs.getCoordinate(cs.size() - 1)))
		return super.createLinearRing(cs);

	// add a new coordinate to close the ring
	CoordinateSequenceFactory csFact = getCoordinateSequenceFactory();
	CoordinateSequence csNew = csFact.create(cs.size() + 1,
			cs.getDimension());
	CoordinateSequences.copy(cs, 0, csNew, 0, cs.size());
	CoordinateSequences.copyCoord(csNew, 0, csNew, csNew.size() - 1);
	return super.createLinearRing(csNew);
}
 
Example #11
Source File: SpatialiteWKBWriter.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private void writeCoordinateSequence( CoordinateSequence seq, boolean writeSize, OutStream os ) throws IOException {
    if (writeSize)
        writeInt(seq.size(), os);

    for( int i = 0; i < seq.size(); i++ ) {
        writeCoordinate(seq, i, os);
    }
}
 
Example #12
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public Point createPoint(CoordinateSequence coordinates) {
    return super.createPoint(convert(coordinates));
}
 
Example #13
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private LinearRing readLinearRing() throws IOException {
    int size = dis.readInt();
    CoordinateSequence pts = readCoordinateSequenceRing(size);
    return factory.createLinearRing(pts);
}
 
Example #14
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private LineString readLineString() throws IOException {
    int size = dis.readInt();
    CoordinateSequence pts = readCoordinateSequenceLineString(size);
    return factory.createLineString(pts);
}
 
Example #15
Source File: SpatialiteWKBReader.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private Point readPoint() throws IOException {
    CoordinateSequence pts = readCoordinateSequence(1);
    return factory.createPoint(pts);
}
 
Example #16
Source File: Spatialite2H2gis.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public void copyData() throws Exception {
    List<TableLevel> tablesList = getTables();
    if (finalDoneOrder != null) {
        tablesList = finalDoneOrder;
    }
    for( TableLevel tableLevel : tablesList ) {
        String tableName = tableLevel.tableName;
        System.out.println("Copy table " + tableName);
        System.out.println("Read data...");
        QueryResult queryResult = spatialite.getTableRecordsMapFromRawSql("select * from " + tableName, -1);
        System.out.println("Done.");

        System.out.println("Insert data...");
        int geometryIndex = queryResult.geometryIndex;

        List<String> names = queryResult.names;
        StringBuilder namesSb = new StringBuilder();
        StringBuilder qmSb = new StringBuilder();
        GeometryColumn gCol = null;
        for( int i = 0; i < names.size(); i++ ) {
            namesSb.append(",").append(names.get(i));

            if (i == geometryIndex) {
                gCol = spatialite.getGeometryColumnsForTable(tableName);
                qmSb.append(",ST_GeomFromText(?, " + gCol.srid + ")");
            } else {
                qmSb.append(",?");
            }
        }
        String namesStr = namesSb.substring(1);
        String qmStr = qmSb.substring(1);
        String prepared = "insert into " + tableName + " (" + namesStr + ") values (" + qmStr + ");";

        GeometryColumn _gCol = gCol;
        h2gis.execOnConnection(connection -> {
            String emptyGeomStr = null;
            try (IHMPreparedStatement stmt = connection.prepareStatement(prepared)) {
                for( Object[] objects : queryResult.data ) {
                    for( int i = 0; i < objects.length; i++ ) {
                        if (i == geometryIndex) {
                            if (objects[i] == null) {

                                if (emptyGeomStr == null) {
                                    EGeometryType gType= _gCol.geometryType;
                                    if (gType.isLine()) {
                                        if (gType.isMulti()) {
                                            emptyGeomStr = gf.createLineString((CoordinateSequence) null).toText();
                                        } else {
                                            emptyGeomStr = gf.createMultiLineString(null).toText();
                                        }
                                    } else if (gType.isPoint()) {
                                        if (gType.isMulti()) {
                                            emptyGeomStr = gf.createMultiPoint((CoordinateSequence) null).toText();
                                        } else {
                                            emptyGeomStr = gf.createPoint((Coordinate) null).toText();
                                        }
                                    } else if (gType.isPolygon()) {
                                        if (gType.isMulti()) {
                                            emptyGeomStr = gf.createMultiPolygon(null).toText();
                                        } else {
                                            emptyGeomStr = gf.createPolygon((CoordinateSequence) null).toText();
                                        }
                                    }
                                }
                                stmt.setString(i + 1, emptyGeomStr);
                            } else {
                                stmt.setString(i + 1, (String) objects[i].toString());
                            }
                        } else if (objects[i] == null) {
                            stmt.setObject(i + 1, null);
                        } else if (objects[i] instanceof Boolean) {
                            stmt.setBoolean(i + 1, (boolean) objects[i]);
                        } else if (objects[i] instanceof byte[]) {
                            stmt.setBytes(i + 1, (byte[]) objects[i]);
                        } else if (objects[i] instanceof Double) {
                            stmt.setDouble(i + 1, (double) objects[i]);
                        } else if (objects[i] instanceof Float) {
                            stmt.setFloat(i + 1, (float) objects[i]);
                        } else if (objects[i] instanceof Integer) {
                            stmt.setInt(i + 1, (int) objects[i]);
                        } else if (objects[i] instanceof Long) {
                            stmt.setLong(i + 1, (long) objects[i]);
                        } else if (objects[i] instanceof Short) {
                            stmt.setShort(i + 1, (short) objects[i]);
                        } else if (objects[i] instanceof String) {
                            stmt.setString(i + 1, (String) objects[i]);
                        } else {
                            stmt.setObject(i + 1, objects[i]);
                        }
                    }
                    stmt.addBatch();
                }
                int[] executeUpdate = stmt.executeBatch();
            }
            System.out.println("Done.");
            return null;
        });

    }

}
 
Example #17
Source File: RoundingFilter.java    From mapbox-vector-tile-java with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(CoordinateSequence seq, int i) {
    seq.setOrdinate(i, 0, Math.round(seq.getOrdinate(i, 0)));
    seq.setOrdinate(i, 1, Math.round(seq.getOrdinate(i, 1)));
}
 
Example #18
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
private CoordinateSequence convert(CoordinateSequence coordinates) {
    return new CoordinateArraySequence(coordinates);
}
 
Example #19
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public MultiPoint createMultiPoint(CoordinateSequence coordinates) {
    return super.createMultiPoint(convert(coordinates));
}
 
Example #20
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public LinearRing createLinearRing(CoordinateSequence coordinates) {
    return super.createLinearRing(convert(coordinates));
}
 
Example #21
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public Polygon createPolygon(CoordinateSequence coordinates) {
    return super.createPolygon(convert(coordinates));
}
 
Example #22
Source File: JTSHelper.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
@Override
public LineString createLineString(CoordinateSequence coordinates) {
    return super.createLineString(convert(coordinates));
}
 
Example #23
Source File: GeoPkgGeomReader.java    From hortonmachine with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a {@link CoordinateSequence} using the provided factory confirming the provided size
 * and dimension are respected.
 *
 * <p>If the requested dimension is larger than the CoordinateSequence implementation can
 * provide, then a sequence of maximum possible dimension should be created. An error should not
 * be thrown.
 *
 * <p>This method is functionally identical to calling csFactory.create(size,dim) - it contains
 * additional logic to work around a limitation on the commonly used
 * CoordinateArraySequenceFactory.
 *
 * @param size the number of coordinates in the sequence
 * @param dimension the dimension of the coordinates in the sequence
 */
public static CoordinateSequence createCS( CoordinateSequenceFactory csFactory, int size, int dimension ) {
    // the coordinates don't have measures
    return createCS(csFactory, size, dimension, 0);
}
 
Example #24
Source File: GeometryTransform.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the given point. Can be invoked directly if the type is known at compile-time,
 * or indirectly through a call to the more generic {@link #transform(Geometry)} method.
 *
 * @param  geom  the point to transform.
 * @return the transformed point.
 * @throws TransformException if an error occurred while transforming the geometry.
 */
public Point transform(final Point geom) throws TransformException {
    final CoordinateSequence coord = geom.getCoordinateSequence();
    return geometryFactory.createPoint(transform(coord, 1));
}
 
Example #25
Source File: GeometryTransform.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the given line string. Can be invoked directly if the type is known at compile-time,
 * or indirectly through a call to the more generic {@link #transform(Geometry)} method.
 *
 * @param  geom  the line string to transform.
 * @return the transformed line string.
 * @throws TransformException if an error occurred while transforming the geometry.
 */
public LineString transform(final LineString geom) throws TransformException {
    final CoordinateSequence seq = transform(geom.getCoordinateSequence(), 2);
    return geometryFactory.createLineString(seq);
}
 
Example #26
Source File: GeometryTransform.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the given linear ring. Can be invoked directly if the type is known at compile-time,
 * or indirectly through a call to the more generic {@link #transform(Geometry)} method.
 *
 * @param  geom  the linear ring to transform.
 * @return the transformed linear ring.
 * @throws TransformException if an error occurred while transforming the geometry.
 */
public LinearRing transform(final LinearRing geom) throws TransformException {
    final CoordinateSequence seq = transform(geom.getCoordinateSequence(), 4);
    return geometryFactory.createLinearRing(seq);
}
 
Example #27
Source File: GeometryTransform.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Transforms the given sequence of coordinate tuples, producing a new sequence of tuples.
 *
 * @param  sequence   sequence of coordinate tuples to transform.
 * @param  minPoints  minimum number of points to preserve.
 * @return the transformed sequence of coordinate tuples.
 * @throws TransformException if an error occurred while transforming a tuple.
 */
protected abstract CoordinateSequence transform(CoordinateSequence sequence, int minPoints) throws TransformException;