2013-07-01 65 views
0

我想将CSV行转换为GeoJSON对象。我正在使用CSVReader。因此,nextLine []具有所有分离的标记。 我想创建存储各种属性的BasicDBObject。我按照以下方式进行。CSV到GeoJSON转换

new BasicDBObject("attribute1",nextLine[0]).append("attribute2",nextLine[1]) 

我想实现的是其在MongoDB中 这样的文件{ ATTRIBUTE1:命名 attribute2:地址 位置:{类型: “点”, 坐标:[纬度,经度] } attrribute3:phonenumber } 如何使用BasicDBObject执行此操作? enter code here

回答

0

最简单的方法是使用BasicDBObjectBuilder(一个实用类构建DBObjects)。你可以做这样的事情:

BasicDBObject toInsert = BasicDBObjectBuilder.start() 
    .add("attribute1",nextLine[0]) 
    .add("attribute2",nextLine[1]) 
    .add(" attrribute3",nextLine[2]) 
    .push("location") 
     .add("type", "Point") 
     .add("coordinates", new double[] { nextLine[3], nextLine[4] }) 
    .pop() 
    .get() 
0

我做它的其他方式

double latLong[] = new double[]{10.0, 30.0}; 
BasicDBObject doc = new BasicDBObject("attr1",nextLine[0]) 
     .append("attr2", nextLine[1] 
    .append("location",new BasicDBObject("type","Point") 
.append("coordinates",latLong)) 
    .append("attr3", nextLine[3]) 

这也适用于根据需要

+0

它完美了。我倾向于将BasicDBObjectBuilder用于复杂的DBObject。这是构建DBObjects的合理DSL。 –