PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indexer/src/main/scala/output/S2CoveringIndexer.scala

https://gitlab.com/18runt88/twofishes
Scala | 49 lines | 40 code | 8 blank | 1 comment | 3 complexity | a55b67714f1473f786f83ae1d89a20d2 MD5 | raw file
  1. package com.foursquare.twofishes.output
  2. import com.foursquare.twofishes.Indexes
  3. import com.foursquare.twofishes.mongo.{MongoGeocodeDAO, S2CoveringIndex, S2CoveringIndexDAO}
  4. import com.mongodb.Bytes
  5. import com.mongodb.casbah.Imports._
  6. import com.vividsolutions.jts.io.WKBReader
  7. class S2CoveringIndexer(override val basepath: String, override val fidMap: FidMap) extends Indexer {
  8. val index = Indexes.S2CoveringIndex
  9. override val outputs = Seq(index)
  10. def writeIndexImpl() {
  11. val polygonSize = S2CoveringIndexDAO.collection.count()
  12. val usedPolygonSize = MongoGeocodeDAO.count(MongoDBObject("hasPoly" -> true))
  13. val hasPolyCursor =
  14. MongoGeocodeDAO.find(MongoDBObject("hasPoly" -> true))
  15. .sort(orderBy = MongoDBObject("_id" -> 1)) // sort by _id asc
  16. hasPolyCursor.option = Bytes.QUERYOPTION_NOTIMEOUT
  17. val writer = buildMapFileWriter(index)
  18. var numUsedPolygon = 0
  19. val groupSize = 1000
  20. // would be great to unify this with featuresIndex
  21. for {
  22. (g, groupIndex) <- hasPolyCursor.grouped(groupSize).zipWithIndex
  23. group = g.toList
  24. toFindCovers: Map[Long, ObjectId] = group.filter(f => f.hasPoly).map(r => (r._id, r.polyId)).toMap
  25. coverMap: Map[ObjectId, S2CoveringIndex] = S2CoveringIndexDAO.find(MongoDBObject("_id" -> MongoDBObject("$in" -> toFindCovers.values.toList)))
  26. .toList
  27. .groupBy(_._id).map({case (k, v) => (k, v(0))})
  28. (f, coverIndex) <- group.zipWithIndex
  29. covering <- coverMap.get(f.polyId)
  30. } {
  31. if (coverIndex == 0) {
  32. logger.info("S2CoveringIndexer: outputted %d of %d used polys, %d of %d total polys seen".format(
  33. numUsedPolygon, usedPolygonSize, polygonSize, groupIndex*groupSize))
  34. }
  35. numUsedPolygon += 1
  36. writer.append(f.featureId, covering.cellIds)
  37. }
  38. writer.close()
  39. logger.info("done")
  40. }
  41. }