PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/indexer/src/main/scala/ShapefileWriter.scala

https://gitlab.com/18runt88/twofishes
Scala | 107 lines | 83 code | 20 blank | 4 comment | 8 complexity | f3c16b8d74e09a303282c6b7925c2fd1 MD5 | raw file
  1. package com.foursquare.twofishes
  2. import com.foursquare.twofishes.mongo.MongoGeocodeDAO
  3. import com.mongodb.casbah.Imports._
  4. import com.novus.salat._
  5. import com.novus.salat.annotations._
  6. import com.novus.salat.dao._
  7. import com.novus.salat.global._
  8. import com.vividsolutions.jts.geom.{GeometryFactory, MultiPolygon, Polygon}
  9. import com.vividsolutions.jts.io.WKBReader
  10. import java.io.File
  11. import org.geotools.data.{DataUtilities, DefaultTransaction}
  12. import org.geotools.data.collection.ListFeatureCollection
  13. import org.geotools.data.shapefile.{ShapefileDataStore, ShapefileDataStoreFactory}
  14. import org.geotools.data.simple.{SimpleFeatureCollection, SimpleFeatureStore}
  15. import org.geotools.feature.simple.SimpleFeatureBuilder
  16. import org.geotools.referencing.crs.DefaultGeographicCRS
  17. import org.opengis.feature.simple.SimpleFeatureType
  18. import scalaj.collection.Imports._
  19. object BuildPolygonShapefile {
  20. val TYPE: SimpleFeatureType = DataUtilities.createType("Location",
  21. "location:MultiPolygon:srid=4326," + // <- the geometry attribute: Point type
  22. "id:String" // <- a String attribute
  23. )
  24. def buildAndWriteCollection(filename: String) {
  25. writeCollection(buildCollection(), filename)
  26. }
  27. def writeCollection(collection: SimpleFeatureCollection, filename: String) {
  28. val newFile = new File(filename);
  29. val storeFactory = new ShapefileDataStoreFactory()
  30. val create = Map( "url" -> newFile.toURI.toURL)
  31. val newDataStore = storeFactory.createNewDataStore(create.asJava).asInstanceOf[ShapefileDataStore]
  32. newDataStore.createSchema(TYPE)
  33. /*
  34. * You can comment out this line if you are using the createFeatureType method (at end of
  35. * class file) rather than DataUtilities.createType
  36. */
  37. newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
  38. val transaction = new DefaultTransaction("create");
  39. val typeName = newDataStore.getTypeNames()(0)
  40. val featureSource = newDataStore.getFeatureSource(typeName)
  41. if (featureSource.isInstanceOf[SimpleFeatureStore]) {
  42. val featureStore = featureSource.asInstanceOf[SimpleFeatureStore]
  43. featureStore.setTransaction(transaction);
  44. try {
  45. featureStore.addFeatures(collection);
  46. transaction.commit();
  47. } catch {
  48. case problem: Throwable => {
  49. problem.printStackTrace();
  50. transaction.rollback();
  51. }
  52. } finally {
  53. transaction.close();
  54. }
  55. } else {
  56. System.out.println(typeName + " does not support read/write access");
  57. }
  58. }
  59. def buildCollection(): SimpleFeatureCollection = {
  60. val featureBuilder = new SimpleFeatureBuilder(TYPE)
  61. val collection = new ListFeatureCollection(TYPE)
  62. val total = MongoGeocodeDAO.count(MongoDBObject("hasPoly" -> true))
  63. val records =
  64. MongoGeocodeDAO.find(MongoDBObject("hasPoly" -> true))
  65. val geomFactory = new GeometryFactory()
  66. val wkbReader = new WKBReader()
  67. for {
  68. (record, index) <- records.zipWithIndex
  69. if (record.woeType != YahooWoeType.ADMIN2)
  70. polygon <- record.polygon
  71. } {
  72. val geom = wkbReader.read(polygon)
  73. var multiPolygon = geom
  74. if (geom.isInstanceOf[Polygon]) {
  75. multiPolygon = new MultiPolygon(Array(geom.asInstanceOf[Polygon]), geomFactory)
  76. }
  77. if (index % 10000 == 0) {
  78. println("outputted %d of %d (%.2f%%)".format(index, total, index*100.0/total))
  79. }
  80. featureBuilder.add(multiPolygon)
  81. featureBuilder.add(record.toGeocodeServingFeature.feature.longId + "," + record._woeType.toString)
  82. val feature = featureBuilder.buildFeature(null)
  83. collection.add(feature)
  84. }
  85. collection
  86. }
  87. }