use of com.mongodb.client.model.geojson.Polygon in project morphia by mongodb.
the class TestGeoQueries method geoIntersects.
@Test
public void geoIntersects() {
// given
City manchester = new City("Manchester", new Point(new Position(53.4722454, -2.2235922)));
getDs().save(manchester);
getDs().save(new City("London", new Point(new Position(51.5286416, -0.1015987))));
City sevilla = getDs().save(new City("Sevilla", new Point(new Position(37.4057731, -5.966287))));
getDs().ensureIndexes();
// when
List<City> matchingCity = getDs().find(City.class).filter(Filters.geoIntersects("location", new Polygon(asList(new Position(37.40759155713022, -5.964911067858338), new Position(37.40341208875179, -5.9643941558897495), new Position(37.40297396667302, -5.970452763140202), new Position(37.40759155713022, -5.964911067858338))))).iterator().toList();
// then
assertThat(matchingCity.size(), is(1));
assertThat(matchingCity.get(0), is(sevilla));
}
use of com.mongodb.client.model.geojson.Polygon in project mongo-java-driver by mongodb.
the class GeometryDecoderHelper method decodePolygon.
private static Polygon decodePolygon(final BsonReader reader) {
String type = null;
PolygonCoordinates 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 = decodePolygonCoordinates(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 Polygon, document contained no type information.");
} else if (!type.equals("Polygon")) {
throw new CodecConfigurationException(format("Invalid Polygon, found type '%s'.", type));
} else if (coordinates == null) {
throw new CodecConfigurationException("Invalid Polygon, missing coordinates.");
}
return crs != null ? new Polygon(crs, coordinates) : new Polygon(coordinates);
}
use of com.mongodb.client.model.geojson.Polygon in project mongo-java-driver by mongodb.
the class GeometryEncoderHelper method encodeGeometry.
@SuppressWarnings("unchecked")
static void encodeGeometry(final BsonWriter writer, final Geometry value, final EncoderContext encoderContext, final CodecRegistry registry) {
writer.writeStartDocument();
writer.writeString("type", value.getType().getTypeName());
if (value instanceof GeometryCollection) {
writer.writeName("geometries");
encodeGeometryCollection(writer, (GeometryCollection) value, encoderContext, registry);
} else {
writer.writeName("coordinates");
if (value instanceof Point) {
encodePoint(writer, (Point) value);
} else if (value instanceof MultiPoint) {
encodeMultiPoint(writer, (MultiPoint) value);
} else if (value instanceof Polygon) {
encodePolygon(writer, (Polygon) value);
} else if (value instanceof MultiPolygon) {
encodeMultiPolygon(writer, (MultiPolygon) value);
} else if (value instanceof LineString) {
encodeLineString(writer, (LineString) value);
} else if (value instanceof MultiLineString) {
encodeMultiLineString(writer, (MultiLineString) value);
} else {
throw new CodecConfigurationException(format("Unsupported Geometry: %s", value));
}
}
encodeCoordinateReferenceSystem(writer, value, encoderContext, registry);
writer.writeEndDocument();
}
use of com.mongodb.client.model.geojson.Polygon in project morphia by mongodb.
the class TestLifecycles method testWithGeoJson.
@Test
public void testWithGeoJson() {
final Polygon polygon = new Polygon(asList(new Position(0d, 0d), new Position(1d, 1d), new Position(2d, 2d), new Position(3d, 3d), new Position(0d, 0d)));
getDs().save(new HoldsPolygon(ObjectId.get(), polygon));
Assert.assertFalse(HoldsPolygon.lifecycle);
Assert.assertNotNull(getDs().find(HoldsPolygon.class).first());
Assert.assertTrue(HoldsPolygon.lifecycle);
}
Aggregations