Search in sources :

Example 21 with Position

use of com.mongodb.client.model.geojson.Position in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeLineString.

private static LineString decodeLineString(final BsonReader reader) {
    String type = null;
    List<Position> coordinates = null;
    CoordinateReferenceSystem crs = null;
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
        } else if (key.equals("coordinates")) {
            coordinates = decodeCoordinates(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid LineString, document contained no type information.");
    } else if (!type.equals("LineString")) {
        throw new CodecConfigurationException(format("Invalid LineString, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid LineString, missing coordinates.");
    }
    return crs != null ? new LineString(crs, coordinates) : new LineString(coordinates);
}
Also used : Position(com.mongodb.client.model.geojson.Position) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)

Example 22 with Position

use of com.mongodb.client.model.geojson.Position in project mongo-java-driver by mongodb.

the class GeometryEncoderHelper method encodeLineString.

private static void encodeLineString(final BsonWriter writer, final LineString value) {
    writer.writeStartArray();
    for (Position position : value.getCoordinates()) {
        encodePosition(writer, position);
    }
    writer.writeEndArray();
}
Also used : Position(com.mongodb.client.model.geojson.Position)

Example 23 with Position

use of com.mongodb.client.model.geojson.Position in project spring-data-mongodb by spring-projects.

the class MongoTemplateMappingTests method writesAndReadsEntityWithOpenNativeMongoGeoJsonTypesCorrectly.

// DATAMONGO-2357
@Test
public void writesAndReadsEntityWithOpenNativeMongoGeoJsonTypesCorrectly() {
    WithOpenMongoGeoJson source = new WithOpenMongoGeoJson();
    source.id = "id-2";
    source.geometry = new MultiPolygon(Arrays.asList(new PolygonCoordinates(Arrays.asList(new Position(0, 0), new Position(0, 1), new Position(1, 1), new Position(1, 0), new Position(0, 0)))));
    template1.save(source);
    assertThat(template1.findOne(query(where("id").is(source.id)), WithOpenMongoGeoJson.class)).isEqualTo(source);
}
Also used : MultiPolygon(com.mongodb.client.model.geojson.MultiPolygon) Position(com.mongodb.client.model.geojson.Position) PolygonCoordinates(com.mongodb.client.model.geojson.PolygonCoordinates) Test(org.junit.Test)

Example 24 with Position

use of com.mongodb.client.model.geojson.Position in project morphia by mongodb.

the class TestGeoQueries method testWithinRadius2.

@Test
public void testWithinRadius2() {
    final Place place1 = new Place("place1", new double[] { 1, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(center("loc", new Point(new Position(0.5, 0.5)), 0.77)).iterator(new FindOptions().limit(1)).next();
    Assert.assertNotNull(found);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 25 with Position

use of com.mongodb.client.model.geojson.Position in project morphia by mongodb.

the class TestGeoQueries method testGeoWithinPolygon2.

@Test
public void testGeoWithinPolygon2() {
    final Place place1 = new Place("place1", new double[] { 10, 1 });
    getDs().save(place1);
    final Place found = getDs().find(Place.class).filter(polygon("loc", new Point(new Position(0, 0)), new Point(new Position(0, 5)), new Point(new Position(2, 3)), new Point(new Position(2, 0)))).iterator(new FindOptions().limit(1)).tryNext();
    Assert.assertNull(found);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Aggregations

Position (com.mongodb.client.model.geojson.Position)35 Point (com.mongodb.client.model.geojson.Point)23 Test (org.testng.annotations.Test)23 FindOptions (dev.morphia.query.FindOptions)16 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)5 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)3 LineString (com.mongodb.client.model.geojson.LineString)3 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)3 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)3 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)3 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)2 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 Datastore (dev.morphia.Datastore)2 GeoCity (dev.morphia.test.models.geo.GeoCity)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 List (java.util.List)1