import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.geojson.LineString;
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Polygon;
import com.mongodb.client.model.geojson.Position;
import com.tusvn.ccinfra.api.data.storage.MongoUtilsForV2x;
import dgtn.sren.cmm.config.bus.MongoCfg;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml"})
@WebAppConfiguration
@Slf4j
public class MongoGisTest {
@Test
public void mongoPolygonTest()throws Exception{
MongoCollection<Document> collection = null;//todo
// 创建Polygon对象
List<Position> positions = new ArrayList<>();
positions.add(new Position(40.7128, -74.0060));
positions.add(new Position(40.7128, -73.9352));
positions.add(new Position(40.6828, -73.9352));
positions.add(new Position(40.6828, -74.0060));
positions.add(new Position(40.7128, -74.0060));
Polygon polygon = new Polygon(positions);
// 创建文档并插入Polygon对象
Document document = new Document("name", "Polygon")
.append("area", polygon);
collection.insertOne(document);
// 打印插入的文档ID
ObjectId id = (ObjectId) document.get("_id");
System.out.println("Inserted document with ID: " + id);
mongoPointTest();
}
@Test
public void mongoPointTest(){
MongoCollection<Document> collection = null;//todo
// 创建Point对象
Position position = new Position(40.7128, -74.0060);
Point point = new Point(position);
// 创建文档并插入Point对象
Document document = new Document("name", "New York")
.append("location", point);
collection.insertOne(document);
// 打印插入的文档ID
ObjectId id = (ObjectId) document.get("_id");
System.out.println("Inserted document with ID: " + id);
}
@Test
public void mongoInsertLine()throws Exception{
MongoCollection<Document> collection = null;//todo
// 创建LineString对象
List<Position> positions = new ArrayList<>();
positions.add(new Position(40.7128, -74.0060));
positions.add(new Position(40.6828, -73.9352));
positions.add(new Position(40.6828, -74.0060));
LineString lineString = new LineString(positions);
// 创建文档并插入LineString对象
Document document = new Document("name", "Line")
.append("path", lineString);
collection.insertOne(document);
// 打印插入的文档ID
ObjectId id = (ObjectId) document.get("_id");
System.out.println("Inserted document with ID: " + id);
}
}