Skip to content

Instantly share code, notes, and snippets.

@ejc123
Created April 9, 2014 19:52
Show Gist options
  • Save ejc123/10307890 to your computer and use it in GitHub Desktop.
Save ejc123/10307890 to your computer and use it in GitHub Desktop.
val valid = geotrellis.data.geojson.GeoJsonReader.parse(geoJson).get.filter(node => node.geom.isValid && node.geom.getGeometryType == "Polygon").map {
g => (Polygon(g.geom,0), g.geom.getCentroid.getCoordinate)
}
val results = months.flatMap {
month => {
val raster = RasterSource(store, s"${month}${year}NDVI_TOA_UTM14")
val mask = RasterSource(store, s"${month}${year}ACCA_State_UTM14")
val masked = raster.localMask(mask, 1, NODATA).cached
val sources = valid.map {
case (poly, coord) =>
masked.zonalMean(poly).map {
result => if (isNoData(result)) (coord, month, "")
else
(coord, month, math.round(result).toString)
}
}
val ds = DataSource.fromSources(sources)
ds.run match {
case Complete (result,_) => result
}
}
}
@lossyrob
Copy link

lossyrob commented Apr 9, 2014

val valid = geotrellis.data.geojson.GeoJsonReader.parse(geoJson).get.filter(node => node.geom.isValid && node.geom.getGeometryType == "Polygon").map {
  g => (Polygon(g.geom,0), g.geom.getCentroid.getCoordinate)
}

val months: Seq[String] = ???

val monthSources: Seq[SeqSource[(Coordinate, String, String)]] = 
  months.map { month =>
    val raster = RasterSource(store, s"${month}${year}NDVI_TOA_UTM14")
    val mask = RasterSource(store, s"${month}${year}ACCA_State_UTM14")
    val masked = raster.localMask(mask, 1, NODATA).cached
    val sources = valid.map {
      case (poly, coord) =>
        masked.zonalMean(poly).map {
          result => if (isNoData(result)) (coord, month, "")
          else
            (coord, month, math.round(result).toString)
        }
    }

    val ds = DataSource.fromSources(sources)
    ds.run match {
      case Complete (result,_) => result
    }
  }

val ds: SeqSource[Seq[(Coordinate, String, String)]] = 
  DataSource.fromSources(monthSources)

ds.run match {
  case Complete(results: Seq[Seq[(Coordinate, String, String)]], _) =>
    results.flatten
  case _ => sys.error("boom")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment