【问题标题】:CSV to GeoJSON conversionCSV 到 GeoJSON 的转换
【发布时间】:2013-07-01 18:42:17
【问题描述】:

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

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

我想要实现的是在 MongoDB 中拥有这样的文档 { 属性1:名称 属性2:地址 位置:{类型:“点”, 坐标:[纬度,经度] } 属性3:电话号码 } 我如何使用 BasicDBObject 做到这一点?enter code here

【问题讨论】:

    标签: java mongodb geojson mongodb-java


    【解决方案1】:

    最简单的方法是使用 BasicDBObjectBuilder,这是一个构建 DBObject 的实用程序类。你可以这样做:

    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()
    

    【讨论】:

      【解决方案2】:

      我是反着做的

      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])
      

      这也可以按需要工作

      【讨论】:

      • 它也很完美。我倾向于将 BasicDBObjectBuilder 用于复杂的 DBObject。这是构建 DBObjects 的合理 DSL。
      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 2017-08-24
      • 2015-03-28
      • 2014-09-02
      • 2015-05-10
      • 2011-08-02
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多